create account

NodeJS Tutorials #3 - MySQL Part 1 by lonelywolf

View this thread on: hive.blogpeakd.comecency.com
· @lonelywolf · (edited)
$0.73
NodeJS Tutorials #3 - MySQL Part 1
![](https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Node.js_logo.svg/1200px-Node.js_logo.svg.png)

#### What Will I Learn?

- Download XAMPP for apache & MySQL server with PHPMyAdmin
- installing MySQL package on nodejs
- setup MySQL server on nodejs server

#### Requirements

- NodeJS [Download](https://nodejs.org/en/)
- XAMPP [Download](https://www.apachefriends.org/download.html)

#### Difficulty

- Intermediate

#### Tutorial Contents
In this tutorial, you will learn how to install XAMPP, Install MySQL package and setup MySQL in NodeJS.

#### Curriculum

- [NodeJS Tutorials #1](https://utopian.io/utopian-io/@lonelywolf/nodejs-tutorials-1-creating-a-static-site-node-js)


#### Setup MySQL

### Step 1 - Setup Your NodeJS server

```
var app = require('http').createServer(handler);
var fs = require('fs');
app.listen(80);
```

`app` - creating the server.

`fs` - import file system package

`app.listen` - listening to the server.

```
function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
  if (err) {
  res.writeHead(500);
  return res.end('Error loading index.html');
  }

  res.writeHead(200);
  res.end(data);
  });
}
```

this function will open the server and read the `index.html` file if you have one.

if you want better explanation go to [here](https://utopian.io/utopian-io/@lonelywolf/nodejs-tutorials-1-creating-a-static-site-node-js)
### Step 2 - Install XAMPP
we need XAMPP to use MySQL server and we need apache to run phpmyadmin for easy usage with MySQL.

download the installer [here](https://www.apachefriends.org/download.html)

![image.png](https://cdn.utopian.io/posts/95ce86a1d879401e6829f091963fc210d3f2image.png)

![image.png](https://cdn.utopian.io/posts/0cd10b4e0960a26417261c734459534ad0a9image.png)

![image.png](https://cdn.utopian.io/posts/0d36cef666bd6375dac0845bf9f43f1b75c3image.png)

![image.png](https://cdn.utopian.io/posts/0ab1882916005889d39945a1b24b26abca3eimage.png)

After all of these steps, press `Next` and start the installation process.

when you have done you ready to run MySQL & Apache, we need apache to use phpmyadmin.

Go to your xampp directory and open `apache_start` and `mysql_start`

![image.png](https://cdn.utopian.io/posts/04f72b3950ea71aae2706fc297f95fd4dbdcimage.png)

![image.png](https://cdn.utopian.io/posts/78d48264cbb246b5aa771f3fd7d2060fedbfimage.png)


if all done correctly go to `http://localhost/phpmyadmin`

![image.png](https://cdn.utopian.io/posts/fdbe8c725fc8dd480a3da765757e25dd11a1image.png)

### Step 3 - Install MySQL package

use the command

```
npm install mysql --save
```

this command installs the MySQL package to NodeJS so you can use it on your server and in your script.

![image.png](https://cdn.utopian.io/posts/add4c7f85cf9f74e2162c3cc27c05dabf969image.png)

![image.png](https://cdn.utopian.io/posts/a82cd9e56e7c38845aa7f27496b018e53b05image.png)

import MySQL package to your server,

```
const mysql = require('mysql');
```

### Step 4 - Setup MySQL on NodeJS

setup config json variable with `hostname`, `user`, `password`, `database`

`hostname` - the hostname of the mysql server, for example, `127.0.0.1`.

`user` - the username of the mysql server.

`password` - the password of the mysql server.

`database` - the database name.

xampp defaults: `hostname: localhost`, `user: root`, `password: no password`.

create a database with phpmyadmin, for example `mysql_example`

```
const config = {
  "host": "localhost",
  "user": "root",
  "password": "",
  "base": "mysql_example"
};
```

now we need to create the connection

```
var db = mysql.createConnection({
  host: config.host,
  user: config.user,
  password: config.password,
  database: config.base
});
```

connection made with `hostname`, `user`, `password`, `database`.

`mysql.createConnection` - this function creates the connection and put it to the `db` variable.

now connect to the database

```
db.connect(function (error) {
  if (!!error)
  throw error;

  console.log('mysql connected to ' + config.host + ", user " + config.user + ", database " + config.base);
});
```

`db.connect` - connect to the database after you made the connection.

if all done correctly you will see this reult

![image.png](https://cdn.utopian.io/posts/01a317e3fbc91272bf05c27a9c4f4985598cimage.png)

##### part 2 - use MySQL to insert, import, update and delete data from the database

Part 2 "What will I learn?"

- Insert data into the database
- Extract data from the database
- Update data in the database
- Delete data from the database

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@lonelywolf/nodejs-tutorials-3-mysql-part-1">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
๐Ÿ‘  , , , , , , , , , , , , , , ,
properties (23)
authorlonelywolf
permlinknodejs-tutorials-3-mysql-part-1
categoryutopian-io
json_metadata"{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":27193779,"name":"node","full_name":"nodejs/node","html_url":"https://github.com/nodejs/node","fork":false,"owner":{"login":"nodejs"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","tutorial","mysql","nodejs","database"],"links":["https://nodejs.org/en/","https://www.apachefriends.org/download.html","https://utopian.io/utopian-io/@lonelywolf/nodejs-tutorials-1-creating-a-static-site-node-js","https://utopian.io/utopian-io/@lonelywolf/nodejs-tutorials-3-mysql-part-1"],"image":["https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Node.js_logo.svg/1200px-Node.js_logo.svg.png","https://cdn.utopian.io/posts/95ce86a1d879401e6829f091963fc210d3f2image.png","https://cdn.utopian.io/posts/0cd10b4e0960a26417261c734459534ad0a9image.png","https://cdn.utopian.io/posts/0d36cef666bd6375dac0845bf9f43f1b75c3image.png","https://cdn.utopian.io/posts/0ab1882916005889d39945a1b24b26abca3eimage.png","https://cdn.utopian.io/posts/04f72b3950ea71aae2706fc297f95fd4dbdcimage.png","https://cdn.utopian.io/posts/78d48264cbb246b5aa771f3fd7d2060fedbfimage.png","https://cdn.utopian.io/posts/fdbe8c725fc8dd480a3da765757e25dd11a1image.png","https://cdn.utopian.io/posts/add4c7f85cf9f74e2162c3cc27c05dabf969image.png","https://cdn.utopian.io/posts/a82cd9e56e7c38845aa7f27496b018e53b05image.png","https://cdn.utopian.io/posts/01a317e3fbc91272bf05c27a9c4f4985598cimage.png"],"moderator":{"account":"scipio","time":"2018-04-12T09:53:06.415Z","pending":false,"reviewed":false,"flagged":true},"questions":null,"score":null,"total_influence":null,"staff_pick":null,"config":{"questions":[{"question":"Does the tutorial address a minimum of 3 substantial concepts and no more than 5?","question_id":"tut-1","answers":[{"answer":"3-5 substantial concepts covered in the tutorial.","answer_id":1,"value":10},{"answer":"Less than 3 or more than 5 substantial concepts covered in the tutorial.","answer_id":2,"value":5},{"answer":"No substantial or recognisable concepts.","answer_id":3,"value":0}]},{"question":"Concepts covered in the tutorial are indicated in the post text with a short description of each concept and when appropriate, images?","question_id":"tut-2","answers":[{"answer":"Thorough text and images for concepts covered.","answer_id":1,"value":10},{"answer":"Minimal text and images.","answer_id":2,"value":5},{"answer":"No or very little text and images.","answer_id":3,"value":0}]},{"question":"Does the contributor provide supplementary resources, such as code and sample files in the contribution post or a GitHub repository?","question_id":"tut-3","answers":[{"answer":"Yes","answer_id":1,"value":10},{"answer":"No","answer_id":2,"value":0}]},{"question":"Is the tutorial part of a series?","question_id":"tut-4","answers":[{"answer":"Yes.","answer_id":1,"value":10},{"answer":"Yes, but first entry in the series.","answer_id":2,"value":5},{"answer":"No.","answer_id":3,"value":0}]},{"question":"Is there an outline for the tutorial content at the beginning of the post?","question_id":"tut-5","answers":[{"answer":"Yes.","answer_id":1,"value":10},{"answer":"Yes, but not detailed enough or does not cover all sections.","answer_id":2,"value":5},{"answer":"No.","answer_id":3,"value":0}]},{"question":"Does the writing style meet the Utopian standard considering formalness, informativeness and clarity of the content?","question_id":"c-1","answers":[{"answer":"It is formal, informative and well written with clear content.","answer_id":1,"value":10},{"answer":"It is informative with clear content but not formal enough.","answer_id":2,"value":5},{"answer":"The contribution could be more informative or contains unrelated information, formality and clarity of the content are good enough.","answer_id":3,"value":4},{"answer":"Not all sections were clear enough but overall holds value for the project.","answer_id":4,"value":2},{"answer":"Not at all.","answer_id":5,"value":0}]},{"question":"Was the provided category template for the editor followed?","question_id":"c-2","answers":[{"answer":"All points of the template were included with additional points as well.","answer_id":1,"value":5},{"answer":"The template was followed without additions.","answer_id":2,"value":4},{"answer":"The template was edited but the points were covered in different way.","answer_id":3,"value":3},{"answer":"Not all points of the template were covered in the contribution but the structure is clear enough.","answer_id":4,"value":3},{"answer":"The template was not followed but the structure is clear enough.","answer_id":5,"value":2},{"answer":"The contents are not clearly structured at all.","answer_id":6,"value":0}]},{"question":"Did the contributor tag other users?","question_id":"c-3","answers":[{"answer":"No other users were tagged by the contributor.","answer_id":1,"value":5},{"answer":"Used tags are reasonable and all tagged people are connected to the project and/or the contribution.","answer_id":2,"value":5},{"answer":"The contribution contains mentions of other users that are not directly related to the contribution but related in other ways.","answer_id":3,"value":2},{"answer":"The contributor misuses tagging of other users.","answer_id":4,"value":0}]},{"question":"Did the contributor ask for upvotes, resteems, follows or witness vote?","question_id":"c-4","answers":[{"answer":"No","answer_id":1,"value":5},{"answer":"Yes, but not in a way that disturbs readability. ","answer_id":2,"value":5},{"answer":"Yes.","answer_id":3,"value":0}]},{"question":"Was a graphical content like images, charts, videos or screenshots included?","question_id":"c-5","answers":[{"answer":"Yes, the graphical content is included and adds more value to the contribution.","answer_id":1,"value":5},{"answer":"No but the contribution works well without graphical content well.","answer_id":2,"value":4},{"answer":"Yes, but most of the graphical contentโ€™s purpose is just for presentational matters.","answer_id":3,"value":3},{"answer":"No relevant or useful graphical content is included in the contribution.","answer_id":4,"value":0}]},{"question":"How would you rate the overall added value?","question_id":"c-6","answers":[{"answer":"Extraordinary value to both the project and the open source community overall.","answer_id":1,"value":20},{"answer":"Significant value to the project or open source community.","answer_id":2,"value":15},{"answer":"Some value to the project or open source community.","answer_id":3,"value":10},{"answer":"Little value to the project or open source community.","answer_id":4,"value":5},{"answer":"No obvious value to project or open source community.","answer_id":5,"value":0}]}]}}"
created2018-04-12 02:15:45
last_update2018-04-12 09:53:15
depth0
children4
last_payout2018-04-19 02:15:45
cashout_time1969-12-31 23:59:59
total_payout_value0.604 HBD
curator_payout_value0.129 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,734
author_reputation25,295,791,457,391
root_title"NodeJS Tutorials #3 - MySQL Part 1"
beneficiaries
0.
accountutopian.pay
weight1,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id49,592,274
net_rshares184,928,641,990
author_curate_reward""
vote details (16)
@a-0-0 ·
Read my profile if you want me to resteem your blog post to my 33,200+ followers. https://steemit.com/@a-0-0
๐Ÿ‘Ž  
properties (23)
authora-0-0
permlinkre-lonelywolf-nodejs-tutorials-3-mysql-part-1-20180412t021600306z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://steemit.com/@a-0-0"],"app":"steemit/0.1"}
created2018-04-12 02:16:03
last_update2018-04-12 02:16:03
depth1
children0
last_payout2018-04-19 02:16:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length108
author_reputation-4,863,186,238,920
root_title"NodeJS Tutorials #3 - MySQL Part 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id49,592,308
net_rshares-4,495,170,782
author_curate_reward""
vote details (1)
@scipio ·
$0.32
Hi, this contribution of yours **cannot be approved** because:
- it only covers a fairly basic setting-things-up explanation (part 1, which you by itself explained fine);
- yet when it seemed to get interesting (part 2) the tutorial ended;
- to get a Utopian upvote, I want you to always do your utmost best, be as complete and in-depth as possible, and to produce really valuable tutorials. It's better to explain too much than too little;
- please don't delete this either, but just continue with your remaining mySQL tutorials, and cover some more topics that are not too trivial in there.

Good luck!
@scipio

----------------------------------------------------------------------
Need help? Write a ticket on https://support.utopian.io.
Chat with us on [Discord](https://discord.gg/uTyJkNm).

**[[utopian-moderator]](https://utopian.io/moderators)**
๐Ÿ‘  ,
properties (23)
authorscipio
permlinkre-lonelywolf-nodejs-tutorials-3-mysql-part-1-20180412t095637104z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-04-12 09:56:39
last_update2018-04-12 09:56:39
depth1
children2
last_payout2018-04-19 09:56:39
cashout_time1969-12-31 23:59:59
total_payout_value0.321 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length854
author_reputation34,229,826,851,736
root_title"NodeJS Tutorials #3 - MySQL Part 1"
beneficiaries
0.
accountutopian.pay
weight1,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id49,646,991
net_rshares109,921,829,568
author_curate_reward""
vote details (2)
@lonelywolf ·
`It's better to explain too much than too little`, maybe your'e true haha :)
I didn't want to make this post too much detailed so I thought 2 parts will be better.
thanks!
properties (22)
authorlonelywolf
permlinkre-scipio-re-lonelywolf-nodejs-tutorials-3-mysql-part-1-20180412t095915740z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-04-12 09:59:21
last_update2018-04-12 09:59:21
depth2
children0
last_payout2018-04-19 09:59:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length171
author_reputation25,295,791,457,391
root_title"NodeJS Tutorials #3 - MySQL Part 1"
beneficiaries
0.
accountutopian.pay
weight1,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id49,647,323
net_rshares0
@lonelywolf ·
You can check my part 2 I think I did a great job there hh :)
properties (22)
authorlonelywolf
permlinkre-scipio-re-lonelywolf-nodejs-tutorials-3-mysql-part-1-20180413t051037222z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-04-13 05:10:42
last_update2018-04-13 05:10:42
depth2
children0
last_payout2018-04-20 05:10:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length61
author_reputation25,295,791,457,391
root_title"NodeJS Tutorials #3 - MySQL Part 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id49,794,660
net_rshares0