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

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'.

Related

Can't connect to mysql (express) with Node.js

Hello Stacksoverflowers,
I have a problem to connect my server.js file to Mysql via Express.
I've already insatlled mysql and express via npm.
when i run my server.js
The only thing it says in my console (MacOS default terminal) is:
"node server running now - using http://localhost:3000"
(I got no errors)
and my webpage also showing correct - "Node Server running - startpage" in the browser.
The problem is that they say nothing like "Connected to the MySQL server" or "Timeout: error messages" ?? (look at #3)
// Dan k
//#1
// My codes from server.js
const express = require('express');
const mysql = require('mysql');
const hostname = "localhost:";
const port = 3000;
//#2
// create connection to DB
const connection = mysql.createConnection({
host: "localhost",
user: 'root',
password: 'root',
port: 3000
});
//#3
// Connect to DB
connection.connect(function(err) {
if (!err) {
console.log('Connected to the MySQL server.');
}
else if (err)
{
return console.error('Timeout: ' + err.message);
}
});
// Routing with Express
const app = express();
//#4
// Startpage (root)
app.get('/', function(request, response){
response.send('Node Server running - startpage...');
response.end();
})
connection.end();
//#5
app.listen(port, (err) => {
if(err) throw err;
console.log("node server running now - using http://"+(hostname)+ .
(port)");
});
I got what is causing the issue. You are using the same port number 3000 to connect to your mysqlDB and at the same time using the same port to run your nodejs application.
This does throw an error but it takes a really long time to throw the error.
So the simple answer here is that the port that you are mentioning in your createConnection() method is incorrect and needs to be changed. This port should be port number on which your mysql DB is running. From the error it is clear that your mysql DB is not running on port 3306. You can check this post to figure out which port is your mysql DB running on. https://serverfault.com/questions/116100/how-to-check-what-port-mysql-is-running-on. Below is a reference for you. Please see the comments.
// create connection to DB
const connection = mysql.createConnection({
host: "localhost",
user: 'root',
password: 'root',
port: 3000 // You need to change this and put in the correct port number.
});

why do i get an error connection in nodejs - mysql (trying remote connection)

I recently started learning Nodejs to connect to MySQL and i'm having connection issues. I tried using putty to connect and i'm able to do so without any issues
this is my example.route.js file
const express = require('express'),
router = express.Router(),
mysql = require('mysql');
var connection = mysql.createConnection({
host: <ipaddress>,
port: <default Port>,
user: 'root',
password: <given password>,
database: <db name>,
connectionTimeout: 30000
});
connection.connect((error) => {
if (error) {
console.log('error connecting: ' + error);
} else {
console.log('Connected to server');
}
});
connection.query('SELECT * FROM test', function(err, results, fields) {
console.log('results in table: ' + results);
console.log('fields in table: ' + fields); // -1
connection.release();
if (err) throw error;
});
connection.end();
module.exports = router;
i get the following error => Error: connect ECONNREFUSED (Will update with complete error in a few hours)
As i mentioned before i used PUTTY in order to make sure there was an issue when connecting but i was able to connect to the given database name, host with the same user and password.
Not sure if this helps is an ubuntu server with MySQL
Anyone has an idea of why i'm getting the connection error? I would appreciate it the help

node js mysql connection

i have a nodejs application and want to connect the application to mysql remote host. here is my query
mysql -u user -p password -h http://xxx.xxx.com.
but getting error ERROR 2005 (HY000): Unknown MySQL server host 'http://xxx.xxx.com' (0).
and here is nodejs code both are not working
var connection = mysql.createConnection({
host : 'http://xxx.xxx.com',
hostname : 'xxx.xxx.com',
user : 'user',
password : 'password'
});
connection.connect(function(err){
});
connection.query('SELECT * from test', function(err, rows, fields) {
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.');
});
connection.end();
thanks in advance

Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433

I'm trying to connect to my local MySql server from node.js/express application using mssql with following config:
// Database connection
var sql = require('mssql');
var config = {
user: 'db_user',
password: 'db_pass',
server: 'localhost',
database: 'project_db'
};
sql.connect(config, function (err) {
if (err) {
console.log(err);
}
});
I get this error:
{ [ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433]
name: 'ConnectionError',
message: 'Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433',
code: 'ESOCKET' }
I know I have to enable TCP/IP connections for my local MySql server, but I can not find how to do it on OSX El Capitan. There is nothing like control panel with settings. That's all available at System Preferences > MySql:
Any ideas how to fix this, guys? Thanks for trying to help.
mssql and mysql are two very different things... You should use the node-mysql or some other mysql client library if you are going to be running mysql on your dev machine
https://github.com/felixge/node-mysql
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();

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;