1. 安装
官方文档:https://www.npmjs.com/package/mysql
npm install --save mysql
2.代码演示
var mysql = require('mysql');// 1.创建连接
var connection = mysql.createConnection({host : 'localhost',user : 'root',password : 'root',database : 'my_db'
});// 2.连接数据库
connection.connect();// 3.执行数据操作
/* connection.query('SELECT * FROM `users`', function (error, results, fields) {if (error) throw error;console.log('The solution is: ', results);
}); */connection.query('INSERT INTO users VALUES(NULL, "admin", "123456789")', function (error, results, fields) {if (error) throw error;console.log('The solution is: ', results);
});// 4. 关闭连接
connection.end();