I'm trying to link up my application with my database using the mysql library. Here's my code:
//Require Libraries
const mysql = require('mysql');
//Set up MySql connection
const connection = mysql.createConnection({
host : "255.255.255.255", //that ain't actually the ip :P
port : "3306",
user : "root",
password : "password :P",
database : "db_name"
});
connection.connect(); //Connect to the database
The error the code gives is:
events.js:182
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED (MY IP IS IN THIS BIT):3306
at Object.exports._errnoException (util.js:1024:11)
at exports._exceptionWithHostPort (util.js:1047:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1150:14)
--------------------
at Protocol._enqueue (C:\file\location:145:48)
at Protocol.handshake (C:\file\location\Protocol.js:52:23)
at Connection.connect (C:\file\location\Connection.js:130:18)
at Object.<anonymous> (C:\file\location\index.js:25:12)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
If you are wondering, the database is most certainly running, I can connect to it using mySQL workbench. Also just a quick note that the database is being hosted remotely just in case that may change anything.
Please check in the MySQL workbench->Server->Users_and_Privileges if your user connecting to the remote database is allowed to do that from your "host". See also this documentation for more information.
Related
I'm a complete newbie to Node.js, but I do have some experience with mysql and some other languages.
I am trying to establish a database connection between Node.js and MySQL, however, my terminal keeps on giving me errors. It failed, so I tried to debug it by using "error.stack" as stated from the documentation, that failed also so I'm totally lost now. Can someone please help me to figure out the bug.
Also, can anyone also tell me how to tell Node.js to print out the reason of why it failed to connect to the database like the function "mysql_error()" from PHP.
My original Node.js code:
var MySQL = require('mysql');
var DatabaseConnection = MySQL.createConnection({
host: '10.0.0.25',
user: 'Admin',
password: 'Password'
});
DatabaseConnection.connect(function(error) {
if(!error) {
console.log("Database is connected");
return;
} else {
console.log("Database is not connected: ");
console.log(error.stack);
}
});
My result:
Database is not connected:
Error: connect ECONNREFUSED 10.0.0.25:3306
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
--------------------
at Protocol._enqueue (/Users/marc/Documents/JSON_MySQL_Database_Test/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/Users/marc/Documents/JSON_MySQL_Database_Test/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/Users/marc/Documents/JSON_MySQL_Database_Test/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/Users/marc/Documents/JSON_MySQL_Database_Test/Database_Test.js:8:20)
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)
I can't figure out why a script to is failing to connect to the MySQL instance.
I can connect to the database from the mysql client on the command line, with the exact same credentials.
I can connect to the database from a remote client, using the same credentials.
The database connect script works fine if I set the parameters to a remote database.
Neither host or socket parameters works through the script.
I make dumb mistakes at times but if this is another one, I can't see it.
An outside observer would help me here.
==================== DB Connect code
var db = require('./config');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: db.db_parms.host,
port: db.db_parms.port,
user: db.db_parms.user,
password: db.db_parms.password,
database: db.db_parms.context,
debug:true
});
connection.connect(function(err) {
if(err){
console.log("SQL CONNECT ERROR: " + err);
} else {
console.log("SQL CONNECT SUCCESSFUL.");
}
});
connection.query("SELECT now()", function(error,results,fields) {
console.log("Results:" + results);
console.log("Fields:" + fields);
});
exports.connection = connection;
=== Error Stack returned:
SQL CONNECT ERROR: Error: connect ECONNREFUSED 127.0.0.1:3307
at Object.exports._errnoException (util.js:1036:11)
at exports._exceptionWithHostPort (util.js:1059:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
--------------------
at Protocol._enqueue (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/kingram/node/apps/fss_register/db/db_connect.js:19:12)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
========= config.js with host = 'localhost':
var db_parms = {
host: 'localhost',
port: 3307,
user:'foo',
password:'bar',
database:'thisone'
}
exports.db_parms = db_parms;
========= config.js with host = socket:
host: '/var/run/mysqld/mysqld.sock',
=== Error Stack returned:
SQL CONNECT ERROR: Error: getaddrinfo ENOTFOUND /var/run/mysqld/mysqld.sock /var/run/mysqld/mysqld.sock:3307
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
--------------------
at Protocol._enqueue (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/kingram/node/apps/fss_register/db/db_connect.js:19:12)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
I'm not sure if the error from the socket is a permissions issue or not and if that is relevant.
As it turns out, a check of the my.conf shows that the bind-addresss was set to the IP of the server and not to localhost.
Setting host: '<ip-address>' fixed the issue.
I'm trying to create a link between node.js and mysql. I get an error as connect ECONNREFUSED. how to solve ths?
CODE:
var mysql = require('mysql');
var conn = mysql.createConnection({
host : "192.164.0.114",
user : "supriya",
password : "root123#",
database: "student"
}
);
var queryString = "SELECT * FROM studata";
conn.query("someurlhere",queryString, function(error, results)
{
if(error)
{
throw error;
}
else {
console.log(results);
}
})
conn.end();
Error is:
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
--------------------
at Protocol._enqueue (/home/supriya/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/home/supriya/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/supriya/node_modules/mysql/lib/Connection.js:109:18)
at Connection._implyConnect (/home/supriya/node_modules/mysql/lib/Connection.js:387:10)
at Connection.query (/home/supriya/node_modules/mysql/lib/Connection.js:174:8)
at Object.<anonymous> (/home/supriya/node_js/example2.js:19:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
I had the same problem. I invoked node server.js, which says
It's Started on PORT 3000 Problem with MySQLError: connect
ECONNREFUSED
By first line, I assumed, intially, it starts mysql server. However, that is not the case. it starts node server, which tries to connect to mysql server and node server says ECONNREFUSED. Clearly, mysql server was not running. I found this, when I logged in, as root, in mysql workbench and tried to execute simple query. There in the corner it said, "..at port 3306, it is not connected" or sth like that.
So, conclusion is starting server.js on some port starts node server and not mysql server.
At the minute I'm just trying to connect to the database but I am getting a connection refused error.The database is from a wordpress site that was set up and I am trying to connect to that through Node using the node mysql driver - https://www.npmjs.org/package/mysql
The way the user's have looked at the database before was through phpmyadmin and im not sure what to use as the hostname for a database like this. i have tried using the server name given to me - cust-mysql-123-14 but that just gives me a address not found error obviously. The only other information i have about the server is this address;
linweb14.atlas.pipex.net
When i connect with that it then throws up this error;
error connecting: Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
--------------------
at Protocol._enqueue (/Users/tp/Desktop/angular-seed/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/Users/tp/Desktop/angular-seed/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/Users/tp/Desktop/angular-seed/node_modules/mysql/lib/Connection.js:108:18)
at Object.<anonymous> (/Users/tp/Desktop/angular-seed/app/server.js:29:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
Heres the database bit of code in my .js file (replaced connection details);
var mysql = require('mysql');
var connection = mysql.createConnection(
{
host : 'hidden',
user : 'hidden',
password : 'hiddden',
database : 'hidden',
port : 3000
}
);
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
Any ideas or advice on where I'm going wrong?
I have a nodejs (with express) openshift app that has a MySQL database (because that is what I am most familiar with), and I cannot figure out how to connect to it on the local environment. My last project I used a php framework with mamp to connect to the local database. What is the equivalent for node? This is the code I have to connect:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '127.0.0.1',
port : '3307',
database : 'test'
});
connection.connect( function(err){
if (err){
throw err;
}
else {
console.log('Connected');
}
});
Here is the error when trying to connect locally:
Error: connect ETIMEDOUT
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
--------------------
at Protocol._enqueue (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:110:26)
at Protocol.handshake (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/Connection.js:98:18)
at Object.<anonymous> (/Users/brandonmoffitt/stembudsnode/server.js:16:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
I am guessing that because I am trying to locally connect to the MySQL db set up by openshift, that I am receiving errors. I just dont know what else to try here. I have tried it without the password as well.
If I switch to localhost for the host, this is the error I receive:
Error: ER_DBACCESS_DENIED_ERROR: Access denied for user ''#'localhost' to database 'stembuds'
at Handshake.Sequence._packetToError (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/sequences/Sequence.js:30:14)
at Handshake.ErrorPacket (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/sequences/Handshake.js:91:18)
at Protocol._parsePacket (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:202:24)
at Parser.write (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.<anonymous> (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/Connection.js:72:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:736:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
--------------------
at Protocol._enqueue (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:110:26)
at Protocol.handshake (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/Connection.js:98:18)
at Object.<anonymous> (/Users/brandonmoffitt/stembudsnode/server.js:16:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
Sorry for the length of this post, I just have no idea why it isnt working.
EDIT:
I ssh'ed into the openshift shell to look at the mysql settings. When I do the 'show grants\g' command, this is what I receive:
Grants for adminMYXaSuf#127.11.28.130
GRANT ALL PRIVILEGES ON *.* TO 'adminMYXaSuf'#'127.11.28.130' IDENTIFIED BY PASSWORD '*D0CE5FA2AAD801A33C9190D0B615CC92F364BE6B' WITH GRANT OPTION
This password is different than the one openshift provided.
did you read their doc?.
if you have load balancer, your mysql will install on another gear.
you need to use the env variable when you install the cartridge
port
OPENSHIFT_MYSQL_DB_PORT=44331
host
OPENSHIFT_MYSQL_DB_HOST=12345600a4382ec97a10000f7-somehost.rhcloud.com
OPENSHIFT_MYSQL_DB_PASSWORD=somerandompassword
username
OPENSHIFT_MYSQL_DB_USERNAME=yourusername
pass the variable when you start your app as args.
look at the example for node
https://github.com/openshift-quickstart/nodejs-example/blob/master/server.js
just use the env variable like process.env.OPENSHIFT_MYSQL_DB_HOST to replace the string in the database connection.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : process.env.OPENSHIFT_MYSQL_DB_HOST,
user : process.env.OPENSHIFT_MYSQL_DB_USERNAME,
password : process.env.OPENSHIFT_MYSQL_DB_PASSWORD,
port : process.env.OPENSHIFT_MYSQL_DB_PORT,
database : process.env.OPENSHIFT_APP_NAME
});
It looks like you are running this on your local computer? If you want to connect to your mysql database on OpenShift from your local computer, you need to use the rhc port-forward command, and look at the port it gives you for mysql, so then you would set the ip to 127.0.0.1 and the port to something like 3307 or whatever it gives you locally. Keep the port-forward command running while you test the connection. You could also try pushing your code up to openshift and run it with the values that you have (or use the mysql environment variables that you can get with env | grep MYSQL on your gear) and then see if it works.
127.x.x.x are not publicly available ip addresses, they are private to that server, that is why you can't connect to it from your machine, and need to use the rhc port-forward command
This works for me:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '127.0.0.1',
port : '3306',
user : 'admind4DxYD5',
password : 'some_pass',
database : 'developercorey'
});
connection.connect( function(err){
if (err){
throw err;
}
else {
console.log('Connected');
}
});