node/mysql/nginx - 502 bad gateway

2019-09-08 16:04发布

问题:

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?

回答1:

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'