Issue with connecting my express server on heroku to the remote mySQL

406 Views Asked by At

I'm trying to deploy my express server on Heroku which needs to connect to the remote MySQL database.

I used 'heroku config:add DATABASE_URL=mysql://dbusername:dbpassword@databasehostIP:databaseserverport/databasename with the correct information but still it tries to connect through wrong address.

I also used 'heroku config:add EXTERNAL_DATABASE_URL=mysql://dbusername:dbpassword@databasehostIP:databaseserverport/databasename with the correct information but still it tries to connect through wrong address.

In my Heroku app panel under 'setting' in 'Config Vars' section I see that DATABASE_URL and EXTERNAL_DATABASE_URL appeared with correct information. but in heroku log I still see the wrong information

This is my sequelize variable on the express server:

const sequelize = new Sequelize('dbName', 'USER', 'Password', {
host:"hostAddress",
dialect: 'mysql'

}

But I see the following on Heroku log:

2019-02-16T18:31:42.231390+00:00 app[web.1]: Unhandled rejection 
SequelizeAccessDeniedError: Access denied for user 
'USER'@'ec2-54-162-8-141.compute-1.amazonaws.com' (using 
password: YES)

How can I change 'ec2-54-162-8-141.compute-1.amazonaws.com' to the remote MySQL host address?

1

There are 1 best solutions below

8
davejagoda On

Try setting your variable with something like this:

if (process.env.DATABASE_URL) {
  const sequelize = new Sequelize(process.env.DATABASE_URL, {
    define: {
      freezeTableName: true, // don't make plural table names
      underscored: true // don't use camel case
    },
    dialect: 'mysql',
    dialectOptions: {
      ssl: true
    },
    logging: true,
    protocol: 'mysql',
    quoteIdentifiers: false // set case-insensitive
  });
} else {
  console.log('Fatal error: DATABASE_URL not set');
  process.exit(1);
}