Initial use of node to connect MySQL database _node.js

Source: Internet
Author: User
Tags mongodb install node

Use node to do Web page development, is basically connected to the MongoDB database, and here I would like to try to connect the MySQL database, because relative to MySQL MongoDB too unfamiliar, think quickly out of the page, so choose relatively familiar with some of the MySQL.

1. Install MySQL

Download the Mysql:mysql Downloads and install it. After installation, it will guide you to configure the database, set the root password, and create a common user and password.

2. Install Node-mysql

Using NPM to install the MySQL software package, it makes it easy to quickly call functions to connect to the MySQL database. Go to the project folder and perform NPM install MySQL--save on the line.

After installation, the MySQL directory is generated in the Node_modules directory of the project folder.

3. View Readme Document

Into the MySQL directory, view the Readme document, this step is very important, do not go everywhere Baidu Google search how to use, because the version of the different, perhaps you get the answer does not allow you to successfully connect to the database. After all, node develops so quickly.

If you read the Readme document carefully, the next steps don't need to be looked at to avoid misleading you because of inconsistent versions.

4. Connect MySQL Database

Enter the project document, create a new testmysql.js example, and write the following code:

var mysql   = require (' mysql ');
var connection = Mysql.createconnection ({
 host   : ' localhost ',
 user   : ' Me ',
 password: ' Secret ',
 database: ' my_db '
});

Connection.connect ();

Connection.query (' SELECT 1 + 1 as solution ', function (err, rows, fields) {
 if (err) throw err;

 Console.log (' The solution is: ', rows[0].solution);
});

Connection.end ();

Connection Basic Parameters

    • Host hostname, localhost on behalf of local
    • User MySQL Users
    • Password Password
    • Database-connected databases

Client.connect () Connection database

Client.query () Execute SQL statement
Client.end () closes the connection.
then execute the program through node testmysql.js to ensure that you have started the MySQL service before executing.

5. Change and check

Use the database without any additions or deletions, the following example may help you a little.

var mysql   = require (' mysql ');
var connection = Mysql.createconnection ({
 host   : ' localhost ',
 user   : ' Me ',
 password: ' Secret ',
 database: ' my_db '
});

Connection.connect ();

Add record
client.query (' INSERT into test (username, password) VALUES ("Lupeng", "123456") ");

Deletes the record
client.query (' Delete from test where username = ' Lupeng ');

Modify the record
client.query (' update test set username = "Pengloo53" where username = "Lupeng");

Query record
client.query ("SELECT * from Test", function selecttable (Err, rows, fields) {
 if (err) {
  throw err;< c21/>}
 if (rows) {
  for (var i = 0; i < rows.length i++) {
   console.log ("%d\t%s\t%s", rows[i].id,rows[i). Username,rows[i].password);
  }
 }
;

Connection.end ();

So the initial connection to the MySQL database is over, and the next step is to do it in the node project.

I hope you will continue to pay attention.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.