Initial use of Node to connect to Mysql database

  • 2021-01-19 22:00:07
  • OfStack

Use Node to do Web page development, basically is to connect to the non-relational database mongodb, and here I still try to connect 1 mysql database, because mongodb is too unfamiliar to mysql, want to quickly out of the page, so choose relatively familiar with some mysql.

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 mysql packages via npm to facilitate quick calls to functions connected to mysql databases. Go to the project folder and execute npm install mysql --save.

After installation, the mysql directory will be generated under the node_modules directory in the project folder.

3. Check the readme documentation

Go to the mysql directory and look at the README documentation. It is very important that you do not search for Google as you may not be able to connect to the database successfully due to the different versions. After all, the ES43en is evolving so fast.

If you have read the README documentation carefully, the following steps should not be read to avoid mislead you because the version is not the same.

4. Connect to mysql database

Enter the project document, create a new example TestMysql. js, 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 hostname, localhost stands for local
user Mysql user
password password
database connection to the database

client.connect () connects to the database

client.query () executes the ES75en statement
client.end () Closes the connection.
Then run the program through node TestMysql.js to make sure you have started the Mysql service before you execute.

5. Add, delete, change and check

Using a database is nothing more than adding, deleting, changing and searching. The following example may help you.


var mysql   = require('mysql');
var connection = mysql.createConnection({
 host   : 'localhost',
 user   : 'me',
 password : 'secret',
 database : 'my_db'
});

connection.connect();

//  Add records 
client.query('insert into test (username ,password) values ("lupeng" , "123456")');

//  Delete records 
client.query('delete from test where username = "lupeng"');

//  Modify the record 
client.query('update test set username = "pengloo53" where username = "lupeng"');

//  Query log 
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\t%s", rows[i].id,rows[i].username,rows[i].password);
  }
 }
});

connection.end();

At this point, the initial connection to the Mysql database is over, and you are ready to work on your own in the Node project.

I hope you will continue to pay attention.


Related articles: