第一次自己假设网站第一次用node.js第一次用mysql。。
环境: Ubuntu 12.04 LTS node.js 版本:v0.6.12 mysql 版本:2.0.0-alpha7
我的代码: var mysql = require( ‘mysql’ ); var connection = mysql.createConnection( { host:’127.0.0.1’, user:’userme’, password:’sqllogin’ });
console.log( connection );
var databaseName = 'usersdata’;
connection.connect( function(err) { if( err ) { console.log( err ); } else { console.log( ‘mysql connected’ ); } });
connection.end();
最后的错误信息(包括connection输出)是:
{ config: { host: '127.0.0.1’, port: 3306, socketPath: undefined, user: 'nbook’, password: 'sqllogin’, database: undefined, insecureAuth: false, supportBigNumbers: false, debug: undefined, timezone: 'local’, flags: '’, queryFormat: undefined, pool: undefined, typeCast: true, maxPacketSize: 0, charsetNumber: 33, clientFlags: 193487 }, _socket: undefined, _protocol: { readable: true, writable: true, _config: { host: '127.0.0.1’, port: 3306, socketPath: undefined, user: 'nbook’, password: 'sqllogin’, database: undefined, insecureAuth: false, supportBigNumbers: false, debug: undefined, timezone: 'local’, flags: '’, queryFormat: undefined, pool: undefined, typeCast: true, maxPacketSize: 0, charsetNumber: 33, clientFlags: 193487 }, _connection: [Circular], _callback: null, _fatalError: null, _quitSequence: null, _handshakeSequence: null, _destroyed: false, _queue: [], _handshakeInitializationPacket: null, _parser: { _supportBigNumbers: false, _buffer: <Buffer >, _longPacketBuffers: [], _offset: 0, _packetEnd: null, _packetHeader: null, _onPacket: [Function], _nextPacketNumber: 0, _encoding: 'utf-8’, _paused: false } }, _connectCalled: false } Express is listening. { [Error: connect ECONNREFUSED] code: 'ECONNREFUSED’, errno: 'ECONNREFUSED’, syscall: 'connect’, fatal: true }
诚心向各位请教!
在网上查来查去,说是什么port localhost之类的问题,还有说socket问题,但是我电脑里/var/run并没有socket。。是要安装什么吗?
你添加一个默认端口试试,将代码修改为如下!
var mysql = require( 'mysql' );
var connection = mysql.createConnection( {
host:'127.0.0.1',
user:'userme',
password:'sqllogin',
port:3306
});