node/mysql/nginx - 502 bad gateway - mysql

I'm running a node app on ubuntu server 14.04 with nginx. I have a mysql database, which was initially running on a different server. Everything worked fine, but in an effort to tune performance I moved it onto the same server and I'm getting an error trying to connect through localhost. I'm using node-mysql with the following configuration:
host: 'localhost',
user: 'root',
password: 'xxx',
database: 'xxx'
I haven't really changed anything with mysql after the initial installation, simply created a dump file and ran it to create the database.
I get the following error whenever my app hits the database:
502 (Bad Gateway)
I've searched around but everything I find on this is PHP related.
Update:
Checked the pm2 logs (using pm2 to run node) and it's giving the following error:
ECONNREFUSED 127.0.0.1:3306
Is there something I need to do to open this up?

After being helped in the right direction via the comments and finding the real error, I was able to find the solution here. Needed to supply the mysql socket path in the node-mysql configuration.
host: 'localhost',
user: 'root',
password: 'xxx',
database: 'xxx',
socketPath: '/var/run/mysqld/mysqld.sock'

Related

How do I give access to my partner to my local MySQL database?

My partner and I are using MySQL, Express, and Node while working with a database that is setup on my local machine using MySQL workbench. Below is the code I use to connect to the database.
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: '****',
database: 'events',
port: '3306'
})
We tried for a little bit to fix the issue but didn't figure it out before we agreed to meet tomorrow to try and get it to work.
Below is what I have come up with so far to see tomorrow if it fixes the issue:
Added 'bind-address = 0.0.0.0' to the my.ini file directly underneath the [mysql] section
Created a root2 user for my partner to use in place of root user in his connection code with host as % instead of localhost with command GRANT ALL PRIVILEGES ON events.* TO 'root2'#'%'; on the mysql command line
Adjusted firewall settings to account for port 3306
Would the last step be to change my partner's host: 'localhost' to the ip address of my machine? Would it be easier/necessary for my partner to connect using MySQL workbench or does he need to download anything else for it to work?
Check the error log for your MySQL server.
Select the host user from mysql.user.
Check your firewalld(linux), then shutdown. Check my.ini(windows) bind-address.

MySQL lost connection with system error: 10060

I am studying react and node js, to build a full stack login authentication page.
Which I found tutorial from this github : "https://github.com/bezkoder/node-js-jwt-auth".
I found this tutorial extremely confusing due to mysql connection errors, the only edit I made is changing the running port from 8080 to 3306, which is a fast fix for having another port running on 8080.
When I run "node server.js" this error pops up few seconds
also, in order to view mysql databases, I am running MySQL Workbench localhost:3306, but the server is stopped, refresh wouldn't work, nor restarting the mysql through services.
The Message shows: Lost connection to MySQL server at 'waiting for initial communication packet'. system error 10060.
Please help, I have no idea what I am doing.
you didn't learn how node.js or react work, so start with much simpler tutorials
port 8080 is the port when you type it in the browser localhost:8080 a webpage appears.
So don't change it, as long as you don't have another page running.
You start by editng
node-js-jwt-auth/app/config/db.config.js
There you find
module.exports = {
HOST: "localhost",
USER: "root",
PASSWORD: "123456",
DB: "testdb",
dialect: "mysql",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
};
Here you enter the same data as you enter in workbench.
Of course the mysql server has to be installed and running.

A hook (`orm`) failed to load! sailsjs

I have been trying since yesterday and somehow it's not working.
i dont know where am I going wrong with this.
here's the code in connections.js
module.exports.connections = {
mysql: {
adapter: 'sails-mysql',
host: 'localhost',
user: 'root', //optional
password: 'root', //optional
database: 'mysql' //optional
}
}
and here's the code in models.js
module.exports.models = {
connection:'mysql',
migrate:'alter'
}
could you guys tell me what's wrong here? and what's the reason for this error?
Here's the detailed error
error: A hook (`orm`) failed to load!
error: Error (E_UNKNOWN) :: Encountered an unexpected error
: Could not connect to MySQL:
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'#'localhost' (using password: YES)
at afterwards (/Users/faraz/code-playground/sailsapi/node_modules/sails-mysql/lib/connections/spawn.js:72:13)
at /Users/faraz/code-playground/sailsapi/node_modules/sails-mysql/lib/connections/spawn.js:40:7
at Handshake.onConnect [as _callback] (/Users/faraz/code-playground/sailsapi/node_modules/sails-mysql/node_modules/mysql/lib/Pool.js:54:9)
just wanted to show you the main page when I start mamp.
i think there's something wrong there
because it actually opens on http://localhost:8888/MAMP/?language=English but config shows port as 3306
could that be the problem??
here's a screenshot
I see that your configuration in sails is OK, so the reason why you cannot connect to the MySQL server is elsewhere. Here are some steps you may want to try:
Use phpMyAdmin to make sure you can connect to your server, but my guess is you will not be able to do that. If you do however, create a user for your DB there and grant all privileges for the DB to that user, and for host try to use "any host". Then adjust your connection according to that user.
In order to check your connection for the new user, the most convenient way is to use the MySQL Workbench tool from here: https://dev.mysql.com/downloads/workbench/
If all of that doesn't help, check this answer: Can't connect to MySQL server on 'localhost' (10061)

errorError: getaddrinfo ENOTFOUND - mysql

I'm running a server on c9.io using Node.js and trying to connect to Mysql
I guess I get this error:
Error: getaddrinfo ENOTFOUND
because the connection to the db is wrong.
I'm using this:
var connection = mysql.createConnection({
host: "REMOTE_ADDR",
user: "MYUSERNAME", // this is replaced by my username
database: "c9",
port: 3306
});
Any idea what's wrong?
Thanks!
After one hour, and I don't know why, I found the issue.
In my case I replaced
host: 'localhost'
by
host: '127.0.0.1'
I know it's been a while since this was asked but I spent the better part of today trying to fix this. What to check for:
Within your nodejs source:
Try it without the 'port' option unless on your db VM, MySQL is
listening on a port besides 3306. I had this option specified but had
connection problems until I removed it. And 3306 is already the
default value for the option anyway.
Try it with the actual host IP in the host 'option'. I was using an actual domain name and, more often than not, node-mysql would throw
the same exact "errorError: getaddrinfo ENOTFOUND" When I used the IP
address, no more errors. Also, I did not get this error while on
Ubuntu instances in AWS. As soon as I switched over to Ubuntu VMs in
Azure, I got the problem.
Set the "debug" connection option to true which will give you a verbose trace of what's happening during the connection
On your db VM/Box/Instance:
Ensure that MySQL is listening on the right port
If you're seeing the error when trying to make concurrent connections using a pool, check that your max_connections and max_user_connections haven't been changed from the default settings in my.conf
Monitor "SHOW PROCESSLIST;" in MySQL to see if you see any issues there
This code works for me.
var mysql = require('mysql');
var con = mysql.createConnection({
host: '127.0.0.1',
port: '3306',
user: 'yourUsername',
password: '**********'
});
con.connect(function(err){
if(err) throw err;
console.log('connected!');
});
the host and port name find them in your mysql server.
in-place of '127.0.0.1' you can use 'localhost'
I was facing this issue. I did fix it simply you have to change your configuration of a connection string like if you are running on local machine try
host:'localhost' or host:'127.0.0.1'
...and set your user name and if you want this to publish your code on server then give the host according to server if in docker container give it name host:'db'.
I almost spend two hours to understand what is the problem with the mysql database. I am using Windows cmd.
I tried multiple ways e.g
npm install mysql2
Changing localhost to 127.0.0.1
Refreshing node
so in order to resolve this issue
[Error: getaddrinfo ENOTFOUND 127.0.0.1:3306]
I open XAMPP and start the MySQL database then manually check the credentials and works fine for me.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'test',
password: 'test123',
});

node-mysql error: connect ECONNREFUSED

I'm trying to setup a remote connection between my database server and a client node app using node-mysql.
When I try to connect to the remote db, I get this error:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: connect ECONNREFUSED
at errnoException (net.js:646:11)
at Object.afterConnect [as oncomplete] (net.js:637:18)
Connecting to a local db works ok (with the socketPort parameter).
I can connect to this remote db with PHP from my computer localhost as well as another server I own so I don't think there's something wrong with mysql conf.
For info, nodejs is running with nginx and I've setup a proxy to make node work on port 80, maybe this is the issue?
How can I check that?
Thanks.
EDIT
Here's my code, just in case:
var express = require('express');
var mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
debug: false,
host: '12.34.56.67',
user: 'user',
password: 'pass'
});
Try using mysql socket:
var connection = mysql.createConnection({
user: 'user',
password: 'pass',
socketPath: 'mysql-socket-path', /*example: /Applications/MAMP/tmp/mysql/mysql.sock*/
database: 'dbname'
});
Should one still look for an answer:
Check your MySql server's configuration.
In my case running netstat -ln | grep mysql(as per Troubleshooting Problems Connecting to MySQL) revealed that my server is listening on the socket /tmp/mysql.sock so in createConnection's options I used socketPath: '/tmp/mysql.sock' instead of host: and port:
This error has to do with the MySQL client not being able to connect to the host:port given. As defined in errno.h, ECONNREFUSED is an error that is thrown when a connection is refused - this is possibly caused by:
a firewall such as iptables blocking the port
no such process running on the port
the process is running on a different host
the host or port were incorrect
In my case, my MySQL server was not correctly bound to 0.0.0.0 for external connections, instead being bound to 127.0.0.1, as it is by default; however you can change this.
The error does not originate from the node-mysql package, as doubly shown by the stacktrace, which only shows errors from net.js.
I added port:3306, that solved the issue .
var connection = mysql.createConnection
({
user: 'root',
password: 'root',
server: 'localhost',
port:3306,
database: 'abcd',
insecureAuth: true,
dialect: 'mysql',
multipleStatements: true,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
});
connection.connect();
Ok so I checked with node-mysql dev and it seems that it's not a node-mysql bug, but it was hard to investigate more.
Anyway, I found this lib which works, so I'll go with it.
https://github.com/mariano/node-db-mysql
i had the same problem.
solved it, by set the right port in the createConnection function options.
get your port by using following command:
netstat -tlnp
search for program name mysqld or just named.
Use the command: yarn run start, and DO NOT use: cd /folderX && yarn run start