简体   繁体   中英

node.js won't connect to mysql

Code:

var mysql = require('mysql');
var db_config = require('./db-config.json');

var connection = mysql.createConnection({
  host: db_config.host,
  user: db_config.user,
  port: db_config.port,
  password: db_config.password,
  database: db_config.database
});

connection.connect(function(err){
  if (!err) {
    console.log("Database is connected... \n\n");
  } else {
    console.log("Error connecting database \n\n" + err);
  }
});

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();

and my ./db-config.json is

{
"host": "**.*.**.***",
"user": "***",
"port": "13306",
"password": "****",
"database": "***"
}

and error is Error connecting database

Error: Connection lost: The server closed the connection.
C:\project\Nodejs\my-app\routes\db\dbtest.js:21
if(err) throw err;
^

Error: Connection lost: The server closed the connection.
at Protocol.end            (C:\project\Nodejs\node_modules\mysql\lib\protocol\Protocol.js:113:13)
at Socket.<anonymous>    (C:\project\Nodejs\node_modules\mysql\lib\Connection.js:109:28)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1055:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
--------------------
at Protocol._enqueue    (C:\project\Nodejs\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.handshake    (C:\project\Nodejs\node_modules\mysql\lib\protocol\Protocol.js:52:23)
at Connection.connect (C:\project\Nodejs\node_modules\mysql\lib\Connection.js:130:18)
at Object.<anonymous> (C:\project\Nodejs\my-app\routes\db\dbtest.js:12:12)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)

This is very strange because this database is already used in another program and its works just fine. I think the code itself is the problem because my local mysql has no problem.

I just start learning nodejs & express and at stuck at this very basic stage. Any help is appreciated.

You are ending the connection to the DB before the callbacks do their job.

var mysql = require('mysql');
var db_config = require('./db-config.json');

var connection = mysql.createConnection({
  host: db_config.host,
  user: db_config.user,
  port: db_config.port,
  password: db_config.password,
  database: db_config.database
});

connection.connect(function(err){
  if (err) {
    console.log("Error connecting database \n\n" + err);
    throw err
  } 
  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();
    });
});

Callbacks don't execute immediately, they are asynchronous. Your code is getting to connection.end() before it has done all the async operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM