Use Node to connect to Mysql database and nodemysql Database
Using Node for Web page development basically connects to non-relational database mongodb. Here I try to connect to the mysql database first, Because mongodb is too unfamiliar with mysql, if you want to quickly get out of the page, select mysql that you are familiar.
1. Install mysql
Download MySQL: MySQL Downloads and install it. After the installation, you will be guided to configure the database, set the root password, and create a common user and password.
2. Install Node-mysql
Install the mysql software package through npm and use it to quickly call functions to connect to the mysql database. Go to the project folder and run npm install mysql -- save.
After installation, the mysql directory is generated under the node_modules directory in the project folder.
3. View readme documents
Go to the mysql directory and check the README document. This step is very important. Do not use Baidu or Google Search everywhere. Because of the different versions, the answer may not allow you to connect to the database successfully. After all, Node develops so fast.
If you carefully read the README document, you do not need to read the subsequent steps to avoid misleading you due to version inconsistency.
4. Connect to the mysql database
Go to the project document, create a 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();
Basic connection Parameters
- Host name. localhost indicates the local host.
- User Mysql user
- Password
- Database connected database
Client. connect () connect to the database
Client. query () Execute SQL statements
Client. end () to close the connection.
Then run the program through node TestMysql. js to ensure that you have started the Mysql service before execution.
5. add, delete, modify, and query
Adding, deleting, modifying, and querying databases may help you in the following example.
Var mysql = require ('mysql'); var connection = mysql. createConnection ({host: 'localhost', user: 'me', password: 'secret', database: 'My _ db'}); connection. connect (); // Add a client record. query ('insert into test (username, password) values ("lupeng", "123456") '); // Delete the client. query ('delete from test where username = "lupeng" '); // modify the client. query ('Update test set username = "pengloo53" where username = "lupeng" '); // query the client. query ("select * from test", function selectTable (err, rows, fields) {if (err) {throw err;} if (rows) {for (var I = 0; I <rows. length; I ++) {console. log ("% d \ t % s", rows [I]. id, rows [I]. username, rows [I]. password) ;}}); connection. end ();
At this point, the initial connection to the Mysql database has come to an end, and you can start to use it in the Node project.
I hope you will continue to pay attention to it.
Articles you may be interested in:
- Use nodejs to Access ActiveX objects.
- Node. js Development Guide-Node. js connects to MySQL and performs database operations
- Node. js instance for mongoDB operations
- Example of operating mysql database in nodejs
- Interaction between mongodb databases with amazing node. js Reading Notes
- Node. js development-access Redis database tutorial
- A Simple Method for reading and writing Redis databases in Node. js applications
- Node Connection database (express + mysql)