nodejs连接mysql  var&n…

本文介绍了在Node.js环境中安装mysql模块的过程,并详细记录了解决因版本更新导致的createClient方法不再可用的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Node.js中安装了mysql:

?
1
2
3
4
5
6
7
8
9
10
11
12

之后,去测试mysql。

相关代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
var mysql = require( 'mysql' ); 
   
var TEST_DATABASE = 'nodejs_db'
var TEST_TABLE = 'test'
   
//创建连接 
var client = mysql.createClient({ 
   user: 'root'
   password: 'rainbow'
});
   
......

结果出错:

结果出错:

?
1
2
3
4
5
6
7
8
9
10
11
D:\tmp\tmp_dev_root\node.js\mysql\mysql_test.js:7
var client = mysql.createClient({
                    ^
TypeError: Object #has no method 'createClient' lient'
     at Object. (D:\tmp\tmp_dev_root\node.js\mysql\mysql_test.js:7:20)
     at Module._compile (module.js:449:26)
     at Object.Module._extensions..js (module.js:467:10)
     at Module.load (module.js:356:32)
     at Function.Module._load (module.js:312:12)
     at Module.runMain (module.js:492:10)
     at process.startup.processNextTick.process._tickCallback (node.js:244:9)

 

【解决过程】

1.找了半天,终于找到了个有用的:

Not working with latest version of node-mysql

然后参考其解释:

v2.0.0-alpha (2012-05-15)

This release is a rewrite. You should carefully test your application after

upgrading to avoid problems.

The first thing you will run into is that the old Client class is gone and

has been replaced with a less ambitious Connection class. So instead of

mysql.createClient(), you now have to:

var mysql = require(‘mysql’);

var connection = mysql.createConnection({

host : ‘localhost’,

user : ‘me’,

password : ‘secret’,

});

把源码改为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var mysql = require( 'mysql' );
 
var TEST_DATABASE = 'nodejs_db' ;
var TEST_TABLE = 'test' ;
 
// //创建连接
// var client = mysql.createClient({
   // user: 'root',
   // password: 'rainbow',
// });
var connection = mysql.createConnection({
     host : 'localhost' ,
     user : 'root' ,
     password : 'xxxxxx' ,
});
 
//创建数据库
connection.query( 'CREATE DATABASE ' +TEST_DATABASE, function (err) {
   if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
     throw err;
   }
});
 
//不指定回调函数,如果出错,则体现为客户端错误
connection.query( 'USE ' +TEST_DATABASE);
 
//创建表格,插入数据
connection.query(
   'CREATE TABLE ' +TEST_TABLE+
   '(id INT(11) AUTO_INCREMENT, ' +
   'name VARCHAR(255), ' +
   'PRIMARY KEY (id))'
);
 
connection.query(
   'INSERT INTO ' +TEST_TABLE+ ' ' +
   'SET name = ?' ,
   [ 'nodejs1' ]
);
 
var query = connection.query(
   'INSERT INTO ' +TEST_TABLE+ ' ' +
   'SET name = ?' ,
   [ 'nodejs2' ]
);
 
//查询,并设置回调函数
connection.query(
   'SELECT * FROM ' +TEST_TABLE,
   function selectCb(err, results, fields) {
     if (err) {
       throw err;
     }
 
     console.log(results);
     console.log(fields);
     connection.end();
   }
);

然后就可以成功运行了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
D:\tmp\tmp_dev_root\node.js\mysql>node mysql_test.js
[ id id: 1, nam 'nodejs1' s1' }, id id: 2, nam 'nodejs2' s2' } ]
[ { catalo 'def' ef',
     d 'nodejs_db' db',
     tabl 'test' st',
     orgTabl 'test' st',
     nam 'id' id',
     orgNam 'id' id',
     filler1: ,
     charsetNr: 63,
     length: undefi type
     type: 3,
     flags: 16899,
     decimals: 0,
     filler2: ,
     default: undefined, false zeroFill: false,
     fieldLength: 11 } 'def' catalog: 'nodejs_db' db: 'nodejs_d 'test'  table: 'test', 'test' rgTable: 'te 'name'    name: 'name' 'name' orgName: 'name',
     filler1: ,
     charsetNr: 33,
     le type : undefined,
     type: 253,
     flags: 0,
     decimals: 0,
     filler2: ,
     default false efined,
     zeroFill: false,
     fieldLength: 765 } ]

然后也可以通过MySQL的命令行(通过开始菜单找到MySQL -> MySQL 5.5 Command Line Client),验证出结果的确新增了对应的nodejs_db:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.18 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| joomla             |
| mysql              |
| performance_schema | test est               |
| wordpress          |
+--------------------+
6 ro in set set (0.08 sec)
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| joomla             |
| mysql              |
| nodejs_db          |
| performance_schem test | test               |
| wordpress          |
+--------------------+
7 in o set in set (0.02 sec)
 
mysql>

 

【总结】

运行mysql.createClient出错的原因在于,新版的Node.js中的mysql,已经去掉了createClient,而改为了mysql.createConnection。

所以换为mysql.createConnection,就可以正常运行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值