How to connect mysql with nodejs - mysql

I want to connect my database with nodejs but it's not connecting and giving an Error: connect ETIMEDOUT.
My code is...
var con = mysql.createConnection({
host: hostname,
user: user,
password: pass,
database: database_name,
});
con.connect(function (error) {
if (error) console.log("Not Connected: " + error);
else console.log("connected");
});

const mysql = require('mysql');
const dbPool = mysql.createPool({
connectionLimit : 10,
port : 'port of your Db',
host : 'host of your Db,
user : 'user of your Db,
password : 'password of your Db,
database : 'database name,
dateStrings : true,
debug : false
});

Related

Release Connection When Query Is Complete

I have a question, Before I change my code, I got error [error: connection lost: the server closed the connection.], possibly because its idle for sometime.
This is my old code.
const dbConn = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'test'
});
dbConn.connect(function(err) {
if(err) throw err;
console.log("Database Connected!");
})
module.exports = dbConn;
After searching for a while, most of it recommend using createPool intead of createConnection, so I change my code to this
const dbConn = mysql.createPool({
host : 'localhost',
user : 'root',
password : '',
database : 'test'
});
module.exports = dbConn;
Then my question is, do I have to release the connection everytime we complete a query? This is how I do query.
dbConn.query("SELECT * FROM spesialisasi s ORDER BY s.nama_spesialisasi ASC ",
function(err, res) {
if(err) {
console.log("error: ", err);
result(err, null);
} else {
result(null, res);
}
}
)
From my knowledge, pool.query() will automatically release the connection when the query completes. Here's the section on connection pooling from the MySQL NPM docs

Cloud Function not connecting with Cloud SQL

Here is my node js code
var mysql = require('mysql');
exports.helloGET = (req, res) => {
console.log(req.body.message);
var con = mysql.createConnection({
//socketPath: '/cloudsql/' + 'project-id:region-name:instance',
host: 'xxx.xxx.xxx.xxx',
user: 'user',
password: 'root',
database: 'test'
});
con.connect(function(err) {
console.log("Connection Error - " + err)
if (err) throw err;
console.log("Connected to Cloud SQL");
});
con.query(`SELECT now()`, null, function (err, result) {
//made reply here
console.log("Query Error - " + err)
if (err) throw err;
console.log("Result: " + result);
});
con.end();
res.status(200).send('Acknowledgement : ' + req.body.message);
};
Error: connect ETIMEDOUT xxx.xxx.xxx.xxx:3306
I tried using socketPath instead of host -
var con = mysql.createConnection({
socketPath: '/cloudsql/' + 'project-id:region-name:instance',
//host: 'xxx.xxx.xxx.xxx',
user: 'user',
password: 'root',
database: 'test'
});
For this I am getting - Error: connect ECONNREFUSED
That feature is currently not supported. However there is a feature request open that you can follow in the issue tracker #36388165.
Like is mentioned in this comment from Jason Polites you can fill this early access form for access Cloud SQL from a Cloud Function.

nodejs to mysql connection error

I'm getting error :
error:Error: Handshake inactivity timeout
I connected mysql to node by like this.
Install mysql
npm install mysql
var mysql = require('mysql');
let connection = mysql.createConnection({
host: 'localhost',
user: 'root',
port: '8888', /* port on which phpmyadmin run */
password: 'root',
database: 'dbname',
socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock' //for mac and linux
});
connection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
You can use node-mysql, It very easy to use. I have used once, Here is the example :-
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});
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();
Use Connection Pool:
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'example.org',
user : 'bob',
password : 'secret'
});
pool.getConnection(function(err, connection) {
// Use the connection
connection.query( 'SELECT something FROM sometable', function(err, rows) {
// And done with the connection.
connection.release();
// Don't use the connection here, it has been returned to the pool.
});
});

Cannot connect to mysql using Node js

I am trying to connect to mysql but I am getting the following error :
"Error: ER_ACCESS_DENIED_ERROR: Access denied for user ' root'#'localhost' (using password: YES)"
But when I connect using mysql command line client I am able to connect to mysql.
Here is my config.js file where I have defined the connectivity settings :
//config.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
port : '3306',
user : ' root',
password : 'root',
database : 'world'
});
module.exports = connection;
I am trying to connect to the db in this file :
// userController.js
var jwt = require('jwt-simple');
var connection = require("../config.js");
var auth = {
getAll: function(req, res) {
console.log("in user comtroller");
console.log(connection);
connection.connect(function(err) {
// connected! (unless `err` is set)
if(err){
console.log("Failed to connect to mysql. Error : "+err);
}
else{
console.log("connection Successful..!!");
}
});
res.send("going good");
}
}
module.exports = auth;

Change <username>#<computername> to <username>#localhost in node js application

I'm new to node js, I was trying a sample of connecting MySql with node. When I run the code I get
Error while performing Query.Error: ER_ACCESS_DENIED_ERROR:
Access denied for user <username>#<computername> (using password: YES)
The problem is when I connect to Mysql through workbench with localhost it succeeds but when I use < computername > it gives me the same error.
I added "mysql": "^2.5.4" dependency in package.json file.
So can anyone help me on how to change the default option from < computername > to localhost
index.js file
var app = express();
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'myusername',
password : 'mypassword',
database : 'mydb'
});
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.get('/', function(request, response) {
connection.connect();
connection.query('SELECT * from projects', function(err, rows, fields) {
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.' + err);
});
connection.end();
response.send('Hello World !');
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
Try using the IP for localhost instead of the string 'localhost' for the host. The default is 127.0.0.1
var connection = mysql.createConnection({
host : '127.0.0.1',
user : 'myusername',
password : 'mypassword',
database : 'mydb'
});
EDIT:
It seems that you actually are able to connect to MYSQL, but that your user is not allowed to use the chosen database. Ensure that the user you connect with can access the database, in your example 'mydb'.