0

so I have a basic Node.js/Express/Mysql app I'm working on that just takes addresses and logs tipping history on them, as a tool for delivery workers to use. ( https://shittip.com )

This is set up on a Digital Ocean droplet (ubuntu 16.04 - nginx - SSL enabled), and I cloned my github repo to bring the files in. https://github.com/Yintii/sh-tT-ps

The port and credentials for the live site and live site's DB are correct, the user and pass and the same as what I used for my set up locally, and the port being anything other than 3306 creates a 502 error with Nginx as I've defined that as the proxy in the config file for it already as well.

The error from the pm2 logs is showing that the connection gets closed, and it's pretty much the first thing that happens -> https://pastebin.com/3XbjkTx7

My connection code is pretty basic:

connectDB.js

var config = require('./exports');
var mysql = require('mysql');
var db = mysql.createConnection({
host: config.host,
user: config.user,
password: config.password,
database: config.database,
port: config.port
});

db.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});

module.exports = {
db
};

The exports file is also pretty clean and basic

exports.js

module.exports = {
host: "localhost",
port: '3306',
user: "root",
password: ---------,
database: "addresses"
}

This is not an issue or anything I originally had to troubleshoot while developing the app locally, this is the first time I've had this problem.

This is how it's supposed to respond, and how it currently responds when I run the app on my LOCAL machine:

https://gyazo.com/075ba6013647ec1a4825b18b1846b05c


This is how the app is responding on my droplet:

https://gyazo.com/60af2ff0f65823ef653721c45ed766ef

The only files that are different are the ones that define the exports and the database connection. Any insight is greatly appreciated.

edit

Oh and I also changed the server variable in my main.js to reflect my domain name and not localhost, so that's not it, that causes a different larger error

Yintii
  • 43
  • 11
  • `user: "root",` eww. btw *reflect my domain name and not localhost* where is this mysql server in? is it on remote place (different instance/vm)? can you try to connect to the db through the instance/vm where your app is? – Bagus Tesa Apr 25 '18 at 01:29
  • Yeah, I can ssh into the server, then log into Mysql there, I made a database there with some dummy entries so thats there and Im not trying to query my local database on my machine or anything way dumb like that. In my main.js file the server variable is localhost:80, where on my live server it needs to reflect the server it's on so it's then changed to the domain with http in front (as it will pull the server IP from the domains A record) – Yintii Apr 25 '18 at 01:31
  • ah, pardon me, then the main.js change is not related to the db. btw, i found [similar issue with an answer](https://stackoverflow.com/a/20211143/4648586). it seems you will need to add a handle for the connection closed condition as we are unable to reuse the connection it seems. – Bagus Tesa Apr 25 '18 at 01:36
  • Do you mean I should only open the connection to the database when it's queried and then close it immediately after? The answer I saw there seemed to be as if it WAS working at some point, and closed because it was idle. As soon as It tries to connect it closes in my case, so opening it again would just create a infinite loop. – Yintii Apr 25 '18 at 01:57
  • Either way I did try said fix and it doesn't change anything in my app functionality. – Yintii Apr 25 '18 at 02:07

1 Answers1

0

Not 100% sure how I did this, but I got it to work. So the issue for me was, I needed to DECLARE MY SOCKET PATH on the live server. I found the answer here :

node/mysql/nginx - 502 bad gateway

My original question above shows a 504 error, though I was getting a 502 the morning I woke up to work on this again. I'm not sure what happened to flip the error, but as soon as I declared my Mysql socket path in the connection and exports file it connected and worked. Happy coding all!

Yintii
  • 43
  • 11