最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nodejs node-mysql module not connecting to database - Stack Overflow

programmeradmin1浏览0评论

I am running this script:

var mysql      = require('mysql');

var connection = mysql.createConnection("mysql://localhost/");

connection.connect();
console.log(connection)

But getting errors...

{ config: 
   { host: 'localhost',
     port: 3306,
     socketPath: undefined,
     user: undefined,
     password: undefined,
     database: '',
     insecureAuth: false,
     supportBigNumbers: false,
     debug: undefined,
     timezone: 'local',
     flags: '',
     queryFormat: undefined,
     pool: undefined,
     typeCast: true,
     maxPacketSize: 0,
     charsetNumber: 33,
     clientFlags: 193487 },
  _socket: 
   { _handle: 
      { writeQueueSize: 0,
        owner: [Circular],
        onread: [Function: onread] },
     _pendingWriteReqs: 0,
     _flags: 0,
     _connectQueueSize: 0,
     destroyed: false,
     errorEmitted: false,
     bytesRead: 0,
     _bytesDispatched: 0,
     allowHalfOpen: undefined,
     _connecting: true,
     writable: true,
     _events: 
      { data: [Function: ondata],
        end: [Object],
        close: [Object],
        error: [Object],
        drain: [Function: ondrain] } },
  _protocol: 
   { readable: true,
     writable: true,
     _config: 
      { host: 'localhost',
        port: 3306,
        socketPath: undefined,
        user: undefined,
        password: undefined,
        database: '',
        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: 
      { _callback: undefined,
        _ended: false,
        _callSite: '    at Handshake.Sequence (/usr/local/lib/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:21)\n    at new Handshake (/usr/local/lib/node_modules/mysql/lib/protocol/sequences/Handshake.js:9:12)\n    at Protocol.handshake (/usr/local/lib/node_modules/mysql/lib/protocol/Protocol.js:41:50)\n    at Connection.connect (/usr/local/lib/node_modules/mysql/lib/Connection.js:63:18)\n    at Object.<anonymous> (/Users/itaccess/Desktop/test.js:5:12)\n    at Module._pile (module.js:449:26)\n    at Object.Module._extensions..js (module.js:467:10)\n    at Module.load (module.js:356:32)\n    at Function.Module._load (module.js:312:12)\n    at Module.runMain (module.js:492:10)',
        _config: [Object],
        _handshakeInitializationPacket: null,
        _events: [Object] },
     _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 },
     _events: 
      { drain: [Object],
        error: [Object],
        end: [Object],
        close: [Object],
        data: [Function: ondata],
        unhandledError: [Function] } },
  _connectCalled: true }
{ [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  fatal: true }

And this error in the console...

/Users/itaccess/Desktop/test.js:10
  if (err) throw err;
                 ^
Error: connect ECONNREFUSED
    at errnoException (net.js:769:11)
    at Object.afterConnect [as onplete] (net.js:760:19)
    --------------------
    at Handshake.Sequence (/usr/local/lib/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:21)
    at new Handshake (/usr/local/lib/node_modules/mysql/lib/protocol/sequences/Handshake.js:9:12)
    at Protocol.handshake (/usr/local/lib/node_modules/mysql/lib/protocol/Protocol.js:41:50)
    at Connection.connect (/usr/local/lib/node_modules/mysql/lib/Connection.js:63:18)
    at Object.<anonymous> (/Users/itaccess/Desktop/test.js:5:12)
    at Module._pile (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)

I am certain the username/password is not required, and I have tried with various passwords I am sure of too. I have tried using object to set settings, and query string. I have tried adding the port explicitly, which is the default port anyway.

Why is it not connecting?

I am running this script:

var mysql      = require('mysql');

var connection = mysql.createConnection("mysql://localhost/");

connection.connect();
console.log(connection)

But getting errors...

{ config: 
   { host: 'localhost',
     port: 3306,
     socketPath: undefined,
     user: undefined,
     password: undefined,
     database: '',
     insecureAuth: false,
     supportBigNumbers: false,
     debug: undefined,
     timezone: 'local',
     flags: '',
     queryFormat: undefined,
     pool: undefined,
     typeCast: true,
     maxPacketSize: 0,
     charsetNumber: 33,
     clientFlags: 193487 },
  _socket: 
   { _handle: 
      { writeQueueSize: 0,
        owner: [Circular],
        onread: [Function: onread] },
     _pendingWriteReqs: 0,
     _flags: 0,
     _connectQueueSize: 0,
     destroyed: false,
     errorEmitted: false,
     bytesRead: 0,
     _bytesDispatched: 0,
     allowHalfOpen: undefined,
     _connecting: true,
     writable: true,
     _events: 
      { data: [Function: ondata],
        end: [Object],
        close: [Object],
        error: [Object],
        drain: [Function: ondrain] } },
  _protocol: 
   { readable: true,
     writable: true,
     _config: 
      { host: 'localhost',
        port: 3306,
        socketPath: undefined,
        user: undefined,
        password: undefined,
        database: '',
        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: 
      { _callback: undefined,
        _ended: false,
        _callSite: '    at Handshake.Sequence (/usr/local/lib/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:21)\n    at new Handshake (/usr/local/lib/node_modules/mysql/lib/protocol/sequences/Handshake.js:9:12)\n    at Protocol.handshake (/usr/local/lib/node_modules/mysql/lib/protocol/Protocol.js:41:50)\n    at Connection.connect (/usr/local/lib/node_modules/mysql/lib/Connection.js:63:18)\n    at Object.<anonymous> (/Users/itaccess/Desktop/test.js:5:12)\n    at Module._pile (module.js:449:26)\n    at Object.Module._extensions..js (module.js:467:10)\n    at Module.load (module.js:356:32)\n    at Function.Module._load (module.js:312:12)\n    at Module.runMain (module.js:492:10)',
        _config: [Object],
        _handshakeInitializationPacket: null,
        _events: [Object] },
     _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 },
     _events: 
      { drain: [Object],
        error: [Object],
        end: [Object],
        close: [Object],
        data: [Function: ondata],
        unhandledError: [Function] } },
  _connectCalled: true }
{ [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  fatal: true }

And this error in the console...

/Users/itaccess/Desktop/test.js:10
  if (err) throw err;
                 ^
Error: connect ECONNREFUSED
    at errnoException (net.js:769:11)
    at Object.afterConnect [as onplete] (net.js:760:19)
    --------------------
    at Handshake.Sequence (/usr/local/lib/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:21)
    at new Handshake (/usr/local/lib/node_modules/mysql/lib/protocol/sequences/Handshake.js:9:12)
    at Protocol.handshake (/usr/local/lib/node_modules/mysql/lib/protocol/Protocol.js:41:50)
    at Connection.connect (/usr/local/lib/node_modules/mysql/lib/Connection.js:63:18)
    at Object.<anonymous> (/Users/itaccess/Desktop/test.js:5:12)
    at Module._pile (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)

I am certain the username/password is not required, and I have tried with various passwords I am sure of too. I have tried using object to set settings, and query string. I have tried adding the port explicitly, which is the default port anyway.

Why is it not connecting?

Share Improve this question edited Feb 27, 2013 at 16:39 Billy Moon asked Feb 27, 2013 at 16:19 Billy MoonBilly Moon 58.6k27 gold badges148 silver badges244 bronze badges 3
  • 2 can you connect from the console with the mysql mand line client? mysql -h localhost ⏎ – istepaniuk Commented Feb 27, 2013 at 16:27
  • can you tell us what is the error it is printing in console? – kobe Commented Feb 27, 2013 at 16:33
  • @kobe it was at the bottom of the JSON output, I have separated it to make it easy to see – Billy Moon Commented Feb 27, 2013 at 16:40
Add a ment  | 

2 Answers 2

Reset to default 6

You need to set the socket path.

var mysql      = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'password',
    socketPath  : '/var/run/mysqld/mysqld.sock',
});

try something like this , I think you need login and password for sure

some sample code

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

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].solution);
});

connection.end();
发布评论

评论列表(0)

  1. 暂无评论