Node.js连接Mysql

1.安装

npm install mysql

注意要复制node_modules到当前工程的文件夹中

2.基本连接

/**
* Created by Administrator on 13-12-25.
*/
var mysql = require('mysql');
var connection = mysql.createConnection({
host:'192.168.41.104',
user:'email',
password:'123456',
insecureAuth: true
}); connection.connect(); connection.query('select 1+1 as solution', function(err, rows, fields) {
if (err) throw err;
console.log('This solution is: ', rows[0].solution);
}); connection.end();

其中 insecureAuth: true 字段不是必须的,但是在某些比较老的Mysql数据库中必须加入该字段,否则会报如下错误:

MySQL server is requesting the old and insecure pre-4.1 auth mechanism.Upgrade the user password or use the {insecureAuth: true} option.

正确输出如下:

This solution is:  2
上一篇:50.Node.js 连接 MySQL


下一篇:Node.js 连接 MySQL数据库