node.js mysql客户端未定义

我正在尝试创build一个mysql数据库到node.js服务器。 我已经通过命令提示符安装了mysql模块:

 npm install mysql 

然后我执行下面的代码:

 var Client = require('mysql').Client; console.log(Client); 

控制台显示undefined 。 也就是说, Clientundefined 。 请告诉我为什么它是undefined

我正在学习本教程http://utahjs.com/2010/09/22/nodejs-and-mysql-introduction/

也许这个教程有点老了。 只需使用node-mysql 文档中的指令即可:

 var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'me', password : 'secret' }); connection.connect(); 

你应该可以连接到你的MySQL数据库。

节点js API在过去一直在变化很多,所以你所遵循的教程很可能会根据你所使用的版本而过时。 你可以按照这里我正在更新的代码示例,或者你可以参考其他的东西,唯一重要的是它应该以最低的成本工作。

  var mysql = require('mysql'); app.use( connection(mysql, { host: 'myhost', user: 'user_name', password: 'password', port: 3306, //port mysql database: 'database_name', multipleStatements: 'true' //false by default }, 'pool')); req.getConnection(function(err, connection) { connection.query("SELECT * FROM `table_name`;",function (error,row){ if(!error){ //do something..... } else console.log("Error : "+err); }); //do something else... }); 

谢谢…!