Nodejs远程连接MySQL数据库

客户机系统环境

  • win7 64bit
  • Nodejs:V0.10.5
  • npm:1.4.28

安装Nodejs的MySQL驱动(这里我使用felixge/node-mysql

felixge/node-mysql是一个纯nodejs的用javascript实现的一个MySQL客户端程序。felixge/node-mysql封装了Nodejs对MySQL的基本操作,100% MIT公共许可证。
项目地址:https://github.com/felixge/node-mysql

安装命令:

> npm install [email protected]

安装成功会看到:

[email protected] node_modules\mysql
├── [email protected]
└── [email protected]

现在我们就来测试一下(新建mysqlTest.js测试文件)

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : '192.168.3.110',//远程MySQL数据库的ip地址
  user     : 'me',
  password : 'secret',
});

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();

运行node:

C:\Users\***\Desktop\node>node mysqlTest.js
The solution is:  2

连接成功

你可能感兴趣的:(Nodejs远程连接MySQL数据库)