我正在尝试进行简单的测试查询以使节点mysql模块正常工作,但它没有连接到数据库。 这是我在app.js中的代码:
var http = require('http'),
io = require('socket.io'),
sys = require('sys'),
express = require('express'),
mysql = require('mysql'),
conn = mysql.createConnection({
host:'localhost',
port:'3000',
user:'root',
password:'pass',
database:'some_database'
});
var port = 3000;
var app = express();
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.get('/', function(req,res) {
res.render('./index.html',{test:'Hello'});
});
app.get('/home', function(req,res) {
res.render('./index.html');
});
var chat_log = new Array();
var socket = io.listen(http.createServer(app).listen(port));
if(conn.connect()){console.log('yay');}else{console.log('whyyyy');};
console.log(conn);
conn.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
conn.end();
console.log(conn)返回以下内容:
{ domain: null,
_events: {},
_maxListeners: 10,
config:
{ host: 'localhost',
port: '3000',
socketPath: undefined,
user: 'root',
password: 'snareline1',
database: 'test',
insecureAuth: false,
supportBigNumbers: false,
bigNumberStrings: false,
debug: undefined,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: undefined,
multipleStatements: false,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 193487 },
_socket:
{ _connecting: true,
_handle:
{ fd: -1,
writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread] },
_readableState:
{ highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: false,
endEmitted: false,
reading: true,
calledRead: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
objectMode: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events:
{ end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
data: [Object],
readable: [Function],
connect: [Object],
close: [Object],
error: [Object],
drain: [Function: ondrain] },
_maxListeners: 10,
_writableState:
{ highWaterMark: 16384,
objectMode: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
sync: true,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
buffer: [] },
writable: true,
allowHalfOpen: false,
onend: null,
destroyed: false,
errorEmitted: false,
bytesRead: 0,
_bytesDispatched: 0,
_pendingData: null,
_pendingEncoding: '',
pipe: [Function],
addListener: [Function],
on: [Function],
pause: [Function],
resume: [Function],
read: [Function],
_consuming: true },
_protocol:
{ domain: null,
_events:
{ drain: [Object],
error: [Object],
close: [Object],
data: [Function: ondata],
end: [Object],
handshake: [Function],
unhandledError: [Function] },
_maxListeners: 10,
readable: true,
writable: true,
_config:
{ host: 'localhost',
port: '3000',
socketPath: undefined,
user: 'root',
password: 'snareline1',
database: 'test',
insecureAuth: false,
supportBigNumbers: false,
bigNumberStrings: false,
debug: undefined,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: undefined,
multipleStatements: false,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 193487 },
_connection: [Circular],
_callback: null,
_fatalError: null,
_quitSequence: null,
_handshakeSequence:
{ domain: null,
_events: [Object],
_maxListeners: 10,
_callback: undefined,
_ended: false,
_callSite: [Error],
_config: [Object],
_handshakeInitializationPacket: null },
_handshaked: false,
_destroyed: false,
_queue: [ [Object] ],
_handshakeInitializationPacket: null,
_parser:
{ _supportBigNumbers: false,
_buffer: <Buffer >,
_longPacketBuffers: [],
_offset: 0,
_packetEnd: null,
_packetHeader: null,
_onPacket: [Function],
_nextPacketNumber: 0,
_encoding: 'utf-8',
_paused: false } },
_connectCalled: true,
state: 'disconnected' }
感谢您的帮助。
答案 0 :(得分:1)
尝试:
conn = mysql.createConnection({
host:'localhost',
port:'3306',
user:'root',
password:'pass',
database:'some_database'
});