1、首先需要安装nodejs 的mysql包
npm install mysql
2、编写nodejs与mysql交互的代码
var mysql = require('mysql'); var TEST_DATABASE = 'ceshi';
var TEST_TABLE = 'user'; //创建连接
var client = mysql.createConnection({
user: 'root',
password: '',
}); client.connect();
client.query("use " + TEST_DATABASE); client.query(
'SELECT * FROM '+TEST_TABLE,
function selectCb(err, results, fields) {
if (err) {
throw err;
} if(results)
{
for(var i = 0; i < results.length; i++)
{
console.log("%d\t%s\t%s", results[i].id, results[i].name, results[i].age);
}
}
client.end();
}
);
3、运行结果
D:\nodejs\mysql>node mysql.js
1 u1 23
2 u2 24
3 u3 25