安装mysql模块的命令:
npm install mysql
然后建立mysqlTest.js:
var mysql = require('mysql'); console.log("over-------------------------"); var TEST_DATABASE = 'nodejs_mysql_test'; var TEST_TABLE = 'test'; var client = mysql.createClient({ user: 'root', password: 'root', }); client.query('CREATE DATABASE '+TEST_DATABASE, function(err) { if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) { throw err; } }); // If no callback is provided, any errors will be emitted as `'error'` // events by the client client.query('USE '+TEST_DATABASE); client.query( 'CREATE TEMPORARY TABLE '+TEST_TABLE+ '(id INT(11) AUTO_INCREMENT, '+ 'title VARCHAR(255), '+ 'text TEXT, '+ 'created DATETIME, '+ 'PRIMARY KEY (id))' ); client.query( 'INSERT INTO '+TEST_TABLE+' '+ 'SET title = ?, text = ?, created = ?', ['super cool', 'this is a nice text', '2010-08-16 10:00:23'] ); var query = client.query( 'INSERT INTO '+TEST_TABLE+' '+ 'SET title = ?, text = ?, created = ?', ['another entry', 'because 2 entries make a better test', '2010-08-16 12:42:15'] ); client.query( 'SELECT * FROM '+TEST_TABLE, function selectCb(err, results, fields) { if (err) { throw err; } console.log(results); console.log(fields); client.end(); } );
运行命令:
$ node /example/mysql/mysqltest.js
如果你运行代码错误,那么注意一下是否是一下错误(这是笔者在测试时遇到的错误):
Administrator@WIN-23C1Q4GKQ4G ~ $ node /example/mysql/mysqltest.js over------------------------- /example/mysql/mysqltest.js:12 throw err; ^ Error: ENOTFOUND, Domain name not found at IOWatcher.callback (dns.js:74:15)
解决方法:
笔者看了一下client.js(mysql模块里面的代码)里面的代码,我们来分析一下:
function Client() { if (!(this instanceof Client) || arguments.length) { throw new Error('deprecated: use mysql.createClient() instead'); } EventEmitter.call(this); this.host = 'localhost'; this.port = 3306; this.user = 'root'; this.password = null; this.database = ''; this.typeCast = true; this.flags = Client.defaultFlags; this.maxPacketSize = 0x01000000; this.charsetNumber = constants.UTF8_UNICODE_CI; this.debug = false; this.ending = false; this.connected = false; this._greeting = null; this._queue = []; this._socket = null; this._parser = null; };
嘿嘿,我们配置了user,password,database,table而host和port呢?
因此在client对象后面加上代码:
client.host = '127.0.0.1'; client.port = 3306;
这里不能把host写成localhost,不然一样会抛出err错误。
嘿嘿,问题解决了,是不是该给笔者一个好评,谢谢
运行结果:
$ node /example/mysql/mysqltest.js
over-------------------------
shenyuc629-----null
[ { id: 1,
title: 'super cool',
text: 'this is a nice text',
created: Mon, 16 Aug 2010 10:00:23 GMT },
{ id: 2,
title: 'another entry',
text: 'because 2 entries make a better test',
created: Mon, 16 Aug 2010 12:42:15 GMT } ]
{ id:
{ length: 51,
received: 51,
number: 2,
type: 4,
catalog: 'def',
db: 'nodejs_mysql_test',
table: 'test',
originalTable: 'test',
name: 'id',
originalName: 'id',
charsetNumber: 63,
fieldLength: 11,
fieldType: 3,
flags: 16899,
decimals: 0 },
title:
{ length: 57,
received: 57,
number: 3,
type: 4,
catalog: 'def',
db: 'nodejs_mysql_test',
table: 'test',
originalTable: 'test',
name: 'title',
originalName: 'title',
charsetNumber: 192,
fieldLength: 765,
fieldType: 253,
flags: 0,
decimals: 0 },
text:
{ length: 55,
received: 55,
number: 4,
type: 4,
catalog: 'def',
db: 'nodejs_mysql_test',
table: 'test',
originalTable: 'test',
name: 'text',
originalName: 'text',
charsetNumber: 192,
fieldLength: 196605,
fieldType: 252,
flags: 16,
decimals: 0 },
created:
{ length: 61,
received: 61,
number: 5,
type: 4,
catalog: 'def',
db: 'nodejs_mysql_test',
table: 'test',
originalTable: 'test',
name: 'created',
originalName: 'created',
charsetNumber: 63,
fieldLength: 19,
fieldType: 12,
flags: 128,
decimals: 0 } }
1 楼
witcheryne
前天
建议不要折腾node.js + 关系型数据库这块. 有点浪费时间.
2 楼
shenyuc629
昨天
一楼,我是知道什么数据库,首先测试什么数据库