Skip to content Skip to sidebar Skip to footer

Nodejs Node-mysql Module Not Connecting To Database

I am running this script: var mysql = require('mysql'); var connection = mysql.createConnection('mysql://localhost/'); connection.connect(); console.log(connection) But get

Solution 1:

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',
});

Solution 2:

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

Post a Comment for "Nodejs Node-mysql Module Not Connecting To Database"