1

I have a VM Instance on Google Cloud. When I run my Node app from the command line my website shows the database information but when I start the same app using pm2 I can't see the information and I am not getting any errors to indicate why this is happening. How do I get my app to connect to my database while it is being ran by pm2.

Here is a code snippet:

app.get('/gregs-garments/clothing-data', async (req,res) => {
const poolClothes = mariadb.createPool({
    host: 'localhost',
    user: 'myUser',
    port: 3306,
    password: process.env.DB_PWD,
    database: 'gregs_garments'
});
let sqlquery = 'SELECT * FROM clothes;';
let conn = await poolClothes.getConnection();
let rows = await conn.query(sqlquery);
res.json(rows);

UPDATE: This is the pm2 error log:

0|mysite   |  From event:
0|mysite   |     at _registerHandshakeCmd (/mysite/node_modules/mariadb/lib/connection.js:735:11)
0|mysite   |     at /mysite/node_modules/mariadb/lib/connection.js:57:11
0|mysite   |     at new Promise (<anonymous>)
0|mysite   |     at Connection.connect (/mysite/node_modules/mariadb/lib/connection.js:56:16)
0|mysite   |     at createConnectionPoolPromise (/mysite/node_modules/mariadb/lib/pool-promise.js:31:8)
0|mysite   |     at creationTryout (/mysite/node_modules/mariadb/lib/pool-base.js:352:9)
0|mysite   |     at /mysite/node_modules/mariadb/lib/pool-base.js:371:7
0|mysite   |     at new Promise (<anonymous>)
0|mysite   |     at connectionCreationLoop (/mysite/node_modules/mariadb/lib/pool-base.js:336:12)
0|mysite   |     at process.nextTick (/mysite/node_modules/mariadb/lib/pool-base.js:443:11)
0|mysite   |   fatal: true,
0|mysite   |   errno: 1045,
0|mysite   |   sqlState: '28000',
0|mysite   |   code: 'ER_ACCESS_DENIED_ERROR' }
  • Based on your investigation, it only started after the shutdown of the instance. It is possible that the IP address of the VM Instance has been changed. To fix that you just need to [reserve a new IP address](https://cloud.google.com/compute/docs/ip-addresses/reserve-static-external-ip-address) and fix the address configuration so it won't happen again. Otherwise, check the logs what is causing the error and provide the exact error message on your post. – Alex G Apr 20 '21 at 14:56
  • @AlexG I have updated my original post to show the error message. – DeveloperGregory Apr 22 '21 at 11:49
  • Does this answer your question? [NodeJS/mySQL - ER\_ACCESS\_DENIED\_ERROR Access denied for user 'root'@'localhost' (using password: YES)](https://stackoverflow.com/questions/40477625/nodejs-mysql-er-access-denied-error-access-denied-for-user-rootlocalhost) – Alex G Apr 22 '21 at 14:30
  • Thanks for all your help Alex G. Unfortunately, that did not help. But I pulled the script out of PM2 and then saved it to synchronized the processes and then add my nodejs script as a new process and now it works. – DeveloperGregory Apr 22 '21 at 19:27

0 Answers0