Example of a simple method for accessing and operating the mysql database using nodejs, nodejsmysql
This document describes how to use nodejs to access and operate a mysql database. We will share this with you for your reference. The details are as follows:
Var mysql = require ('mysql'); // call the mysql module of the MySQL module to install $ npm install mysql // create a connectionvar connection = mysql. createConnection ({host: '2017. 0.0.1 ', // host user: 'root', // MySQL authentication Username password: '', // MySQL authentication username port: '123', // port number database: ''// database name}); // creates a connectionconnection. connect (function (err) {if (err) {console. log ('[query]-:' + err); return;} console. log ('[connection connect] succeed! ');}); // Execute the SQL statement connection. query ('select 1 + 1 AS solution', function (err, rows, fields) {if (err) {console. log ('[query]-:' + err); return;} console. log ('the solution is: ', rows [0]. solution) ;}); // close connectionconnection. end (function (err) {if (err) {return;} console. log ('[connection end] succeed! ');});
Note: When operating the database, nodejs does not need to set the database encoding format set names utf8.
I hope this article will help you design nodejs programs.