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.