Nodejs app can't connect to local MySQL on FreeBSD 8.2-STABLE - mysql

guys.
I've installed MySQL module for node.js using npm install mysql and bumped into connection error.
Here's my js code.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'user',
password : 'userpassword',
database : 'node'
});
connection.connect();
connection.query('SELECT * FROM table', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ' + fields);
});
connection.end();
Here's the output I get:
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'node'#'146.66.*.*' (using password: YES)
at Handshake.Sequence._packetToError (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:48:14)
at Handshake.ErrorPacket (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/sequences/Handshake.js:101:18)
at Protocol._parsePacket (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/Protocol.js:270:23)
at Parser.write (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/Parser.js:77:12)
at Protocol.write (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/usr/local/www/apache22/data/node/node_modules/mysql/lib/Connection.js:82:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
--------------------
at Protocol._enqueue (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/usr/local/www/apache22/data/node/node_modules/mysql/lib/Connection.js:108:18)
at Object.<anonymous> (/usr/local/www/apache22/data/node/local_db.js:9: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)
The thing is that instead of connecting to MySQL via localhost (as I specified in connection options) it goes through externalIPaddress (for no obvious reason) and firewall drops connection in accordance with it's rules.
I've searched for solutions in other topics, but either error output or suitable solutions differ a lot.
I've also tried popular solutions for related problems from other questions and this is the list of options, that didn't work for me:
1. Flushing MySQL privileges table after new user creation and granting necessary privileges.
2. Modifying /etc/hosts to get proper answer from nslookup localhost and nslookup localhost.
3. Manually pointing path to MySQL socket also hasn't change anything.
4. I do not use host and server options at the same time.
5. I've tried 3306 port specifying.
After all, I still get the same error.
Thank's for your help in advance.

By default mysql binds and listens to the "everything" address at 0.0.0.0. Perhaps your node and your node.js module are doing the same thing. This would make requests and responses come over your external interface and trigger your firewall.
From your output I assume node and MySQL are on the same machine and that you are using Apache as a reverse proxy. If this is the case only httpd should be listening to ports on external addresses: node, mysql, etc. should be talking to each other over local interfaces or via sockets.
Check your configuration and make sure node and mysql-server are binding/listening to ports on the lo interface, localhost or 127.0.0.1. This could resolve your problem.
HTH, G'Cito

Related

AppEngine and Cloud SQL connection error

I'm using App Engine and Cloud SQL from Google Cloud.
They are both in the same project, which should be enough to be able to make connections to the SQL.
If your App Engine application is in the same project as your Cloud
SQL instance, you can skip this section and go to Setting up your
local environment. Otherwise, proceed to the next step.
If I spin up the Node.js on my Mac, locally - It can easily connect to the Cloud SQL, because my IP address is whitelisted.
As soon as I deploy it to App Engine, it's not working, because it can't connect to the DB. I'm using the Public IP Address for the Cloud SQL.
I have been reading a lot of tutorials, and I have also read the documentation provided by Google Cloud. And I can see there is something about the socketPath.
But I don't want to use Knex. I want to use the standard mysql.
I have been trying with the following:
var db_config = {
host: 'PUBLIC IP',
user: 'root',
password: 'PASSWORD!',
database: 'DB_NAME',
socketPath: '/cloudsql/CONNECTION_NAME'
};
Error:
error when connecting to db: { Error: connect ENOENT /cloudsql/CONNECTION_NAME-209415:europe-west1:DB_NAME
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1174:14)
--------------------
at Protocol._enqueue (/Users/root/API/Node-API/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/Users/root/API/Node-API/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/Users/root/API/Node-API/node_modules/mysql/lib/Connection.js:130:18)
at handleDisconnect (/Users/root/API/Node-API/src/App.ts:44:14)
at Object.<anonymous> (/Users/root/API/Node-API/src/App.ts:60:1)
at Module._compile (internal/modules/cjs/loader.js:654:30)
at Module.m._compile (/Users/root/API/Node-API/node_modules/ts-node/src/index.ts:435:23)
at Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/root/API/Node-API/node_modules/ts-node/src/index.ts:438:12)
at Module.load (internal/modules/cjs/loader.js:566:32)
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'connect',
address: '/cloudsql/CONNECTION_NAME-209415:europe-west1:DB_NAME',
fatal: true }
Any suggestions, how to fix this? If I remove the socketPath from the db_config, then it works locally - But not when it's deployed.
Okay, it seems like the error is caused, when localhost AND socketPath are applied. Everything works fine without Knex, with the following db_config for mysql.
db_config = {
user: 'root',
password: 'PASSWORD!',
database: 'DB_NAME',
socketPath: '/cloudsql/CONNECTION_NAME'
};
Finally, remember to add cloud_sql_instance to app.yaml
beta_settings:
cloud_sql_instances: CONNECTION_NAME

Ubuntu 16.04 Node.JS app can't connect to MySQL database - Error: connect ECONNREFUSED

I'm trying to launch my Node.JS application on an Ubuntu virtual machine. The application uses MySQL. When I run the app, I get the following error regarding what I'd assume is MySQL.
events.js:182
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED myIP:3306
at Object._errnoException (util.js:1041:11)
at _exceptionWithHostPort (util.js:1064:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1153:14)
--------------------
at Protocol._enqueue (/root/pub code test/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/root/pub code test/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/root/pub code test/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/root/pub code test/runTest.js:17:12)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
Here's my code for connecting to MySQL:
var connection = mysql.createConnection ({
host: "myIP",
user: "root",
password: "password",
database: "dbname"
});
connection.connect()
I've replaced the password, database, and hostname for security purposes, but I have indeed double checked all of them, along with the username
Node.JS version: 8.4.0
NPM version: 5.3.0
MySQL version: 5.4.2
Ubuntu: 16.04
I'm not sure what I did wrong to get this error but if anyone knows why, please do let me know. Thanks so much!
Got it! My simple fix was to change the IP to localhost. I was previously using just a standard public IP.

How to connect to MySQL using NodeJS server in OpenShift?

I'm trying to connect to MySQL database in OpenShift using Node JS server.
And I have an error:
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1057:14)
--------------------
at Protocol._enqueue (D:\nodejs\node_modules\mysql\lib\protocol\Protocol.js:141:48)
at Protocol.handshake (D:\nodejs\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (D:\nodejs\node_modules\mysql\lib\Connection.js:123:18)
at Object.<anonymous> (D:\nodejs\server.js:17:12)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
That's my code:
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
});
connection.connect(function(err){
if (err) {throw err;}
});
I read a lot about this error but I didn't find a solution.
I can connect to MySQL using ssh, and I also can check these environmental variables.
HOST is 127.9.156.2 and NODEJS_IP is 127.9.156.1. PORT is 3306.
PASS and USER are also ok.
Where is there a problem?
This is most likely caused from two different issues. One of them is that the port is wrong or the MySQL instance is trying to connect to a port that is already busy. You need to change this, if this is what is causing it.
The other issue and is more likely is by default MySQL only allows 127.0.0.1 connections. You'll have to go into the config file for MySQL and set it up to allow for other host connections, here is a link to their docs on how to do this. http://dev.mysql.com/doc/refman/5.0/en/server-options.html#option_mysqld_bind-address .

Mysql and NodeJS can't connect remotely to deployed app

I am trying to connect from a NodeJS app to a MySQL database (already deployed, not in a localhost), currently it throws an error saying:
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'#'187.189.130.120' (using password: YES)
at Handshake.Sequence._packetToError (C:\wamp\Respaldo Linux\Documentos\WebDev\misGastosApp\node_modules\mysql\lib\protocol\sequences\Sequence.js:48:14)
at Handshake.ErrorPacket (C:\wamp\Respaldo Linux\Documentos\WebDev\misGastosApp\node_modules\mysql\lib\protocol\sequences\Handshake.js:101:18)
at Protocol._parsePacket (C:\wamp\Respaldo Linux\Documentos\WebDev\misGastosApp\node_modules\mysql\lib\protocol\Protocol.js:271:23)
at Parser.write (C:\wamp\Respaldo Linux\Documentos\WebDev\misGastosApp\node_modules\mysql\lib\protocol\Parser.js:77:12)
at Protocol.write (C:\wamp\Respaldo Linux\Documentos\WebDev\misGastosApp\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\wamp\Respaldo Linux\Documentos\WebDev\misGastosApp\node_modules\mysql\lib\Connection.js:82:28)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:748:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:410:10)
--------------------
at Protocol._enqueue (C:\wamp\Respaldo Linux\Documentos\WebDev\misGastosApp\node_modules\mysql\lib\protocol\Protocol.js:135:48)
at Protocol.handshake (C:\wamp\Respaldo Linux\Documentos\WebDev\misGastosApp\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (C:\wamp\Respaldo Linux\Documentos\WebDev\misGastosApp\node_modules\mysql\lib\Connection.js:109:18)
at Object.<anonymous> (C:\wamp\Respaldo Linux\Documentos\WebDev\misGastosApp\app.js:87: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)
If I log into my cpanel account and then add my public IP to MySQL remote connections, I am able to connect the app with the MySQL database, but I want to know how could I make it work for when the app is in production. I already read that many users grant all privileges for a user, which already did and still the same (can't connect).
I also read that I could grant remote access to an IP with a wildcard but if I do this, I would need to grant access for all IPs in Mexico? I been reading many things trying to accomplish this connection. My main goal is within production, being able to download the app to my phone (or anyone's phone), then access the app and connect to MySQL. This is because I need constantly check if the data is new and then sync to MySQL. Also I have some doubts if allowing remote MySQL connections would be a security issue?
Note: This is my first app with sync and NodeJS-MySQL, so any advice is really appreciated.
This is not a NodeJS issue. You have not granted your "user" access to the database from all hosts.
http://dev.mysql.com/doc/refman/5.1/en/grant.html
Something like this in mysql shell:
GRANT SELECT ON db.* TO 'user'#'%'
% represents ALL hosts not just a single one

Cannot Connect nodejs to mysql

I am running Node.js 0.10.31 on windows and my mySQL 5.5.35 on virtual-box ubuntu running on 192.168.2.150. Now to connect node js to mysql i have used following connector :
https://github.com/felixge/node-mysql
Code :
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.2.150',
user : 'root',
password : '*****',
port : 3306
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
connection.query('SELECT 1', function(err, rows) {
// connected! (unless `err` is set)
console.log(err, rows);
});
In my.cnf file bind address is 0.0.0.0. Moreover, in the user table i have given permission to all database to root#192.168.2.150. But, still i am not able to connect from node to mysql and it gives the following error.
I have even tried to the Node js 0.8.2 version but still it doesn't work. can anyone point out the error?
Error:
error connecting: Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'#'
192.168.2.99' (using password: YES)
at Handshake.Sequence._packetToError (G:\Coding_Zone\NodeJs_Work\Output\node
_modules\mysql\lib\protocol\sequences\Sequence.js:48:14)
at Handshake.ErrorPacket (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mys
ql\lib\protocol\sequences\Handshake.js:101:18)
at Protocol._parsePacket (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mys
ql\lib\protocol\Protocol.js:271:23)
at Parser.write (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\lib\pr
otocol\Parser.js:77:12)
at Protocol.write (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\lib\
protocol\Protocol.js:39:16)
at Socket.Connection.connect (G:\Coding_Zone\NodeJs_Work\Output\node_modules
\mysql\lib\Connection.js:82:28)
at Socket.EventEmitter.emit (events.js:88:17)
at TCP.onread (net.js:397:14)
--------------------
at Protocol._enqueue (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\l
ib\protocol\Protocol.js:135:48)
at Protocol.handshake (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\
lib\protocol\Protocol.js:52:41)
at Connection.connect (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\
lib\Connection.js:109:18)
at Object.<anonymous> (G:\Coding_Zone\NodeJs_Work\Output\mySqlTest.js:9:12)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
{ [Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'#'192.168.2.99' (
using password: YES)]
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlState: '28000',
fatal: true } undefined
Update :
I did try with other users also and granted them full permission in the database. But still wasn't able to resolve the error.
But the strange thing is that i am able to connect with the mysql Client
As #Paul stated correctly it's not the problem of NodeJS. If you look carefully You are giving permissions to user root#192.168.2.150 (which says "user root connecting from IP 192.168.2.150"), but in error message is stated:
Access denied for user 'root'#'192.168.2.99'
Try to give your root permission for IP where your NodeJS instance is running (192.168.2.99) and it should work.
I had the same problem and I tried the other solutions listed above.
Following the recommendation of jackleekr at https://github.com/mysqljs/mysql/issues/250#issuecomment-7153315
I was able to connect, using a connection string:
var conn = mysql.createConnection('mysql://root:*******#localhost:3306/company?debug=true');
Have you tried connecting using the same information in the MySQL client?
Are you sure the root user isn't disabled (as it often is by default)?
In short, this doesn't look like a Node problem to me, but an issue getting the right credentials and configuration.
Adding my inputs as it might help others. I faced the same issue and below steps resolved it.
Open WorkBench
Open the connection to your database belongs.
Left hand side, under management, click Users and Privilages.
Click Add Account button, just below User Accounts list.
Enter Login Name, 'host matching' to localhost or your host name,
enter password.
Goto Schema Privileges tab, click Add Entry Button and click Ok. Optionally you can specify the single database the user can access.
Select the desired rights for the user, like 'Select', 'Insert', etc
Click Apply and use the new created user to connect from Nodejs.
The command prompt is running with limited privilegies. Uncheck the respective box in the Command Prompt shortcut and it may work properly. I had this issue yesterday.
Try by changing port number to 3307
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.2.150',
user : 'root',
password : '*****',
port : 3307
});
connection.connect( function(error) {
if(error) throw error;
console.log("Connected to database");
});
It worked for me
I too faced the same situation and the other answers didn't help me.The problem is the number of connections to the database - by default it is zero. You should change it.
Steps:
Open WorkBench
Open the connection to your database belongs.
Left hand side, under management, click Users and Privilages.
Go to Account Limits.
Change Concurrent connections to your feasibility.