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

 

posted on 2013-12-25 15:10  SZlibraco  阅读(965)  评论(0编辑  收藏  举报