Home > Mobile >  How to connect to mysql server from nodejs app using sequelize. But, earlier i used docker and ran a
How to connect to mysql server from nodejs app using sequelize. But, earlier i used docker and ran a

Time:01-05

Whenever i try to connect my express.js app to the node server. I get an error(see below console message). Earlier, before dockerizing my app everything used to run smoothly but after creating my mysql-image-container(docker) and running mysql through docker everything went well. But now, when i try to connect my app with SQL server and not through the docker-container i get the following error

(node:22136) UnhandledPromiseRejectionWarning: SequelizeAccessDeniedError: Access denied for user 'root'@'localhost' (using password: YES)
at ConnectionManager.connect (C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\mysql\connection-manager.js:94:17)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async ConnectionManager._connect (C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:220:24)      
at async C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:174:32
at async ConnectionManager.getConnection (C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:197:7)  
at async C:\Users\A\ARISE1\node_modules\sequelize\lib\sequelize.js:304:26
at async MySQLQueryInterface.tableExists (C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\abstract\query-interface.js:102:17)    
at async Function.sync (C:\Users\A\ARISE1\node_modules\sequelize\lib\model.js:939:21)
at async Sequelize.sync (C:\Users\A\ARISE1\node_modules\sequelize\lib\sequelize.js:376:9)

(Use node --trace-warnings ... to show where the warning was created) (node:22136) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:22136) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I have tried deleting docker file, mysql-image-container(from docker) but am still unable to connect my app with local mysql server. And now if i even try to connect to my database through SQL-command line, i am unable to do so, even with correct credentials. Though I'm able to connect to the database through MySQL Workbench. The docker command used to build-image/run the container is:

docker run -p 3306:3306 --name nodejs-mysql -e MYSQL_ROOT_PASSWORD=pass -e MYSQL_DATABASE=database -d mysql:5.7

before this, i used to right-click on the dockerfile and choose build image option. I didn't completely understood the contents of Dockerfile and so i mixed some code from online sources.

Any help would be well appreciated.

CodePudding user response:

const sequelize = new Sequelize( username, password, database, {
    host: host,
    dialect: dialect
});

Instead of above, it should be this

const sequelize = new Sequelize(  database, username, password, {
        host: host,
        dialect: dialect
    });

CodePudding user response:

Please share your code. Also see the the code below on how you establish a connection

const { Sequelize,DataTypes } = require('sequelize');
const config={
    HOST:"localhost",// you can replace this with the url of where your server is hosted or an ip adress
    USER:"root",
    PASSWORD:"yourpassword",
    DB:"yourDb",
    dialect:"mysql",//sqlite, mariadb
    pool:{
        max:5,
        min:0,
        acquire:30000,
        idle:10000
    }

const sequelize= new Sequelize(
config.DB,
config.USER,
config.PASSWORD,
{

    host:config.HOST,
    dialect:config.dialect,
    operatorAliases:false,
    pool:{
        max:config.pool.max,
        min:config.pool.min,
        acquire:config.pool.acquire,
        idle:config.pool.idle
    }
}

)
try {
  await sequelize.authenticate();
  console.log('Connection has been established successfully.');
} catch (error) {
  console.error('Unable to connect to the database:', error);
}

  • Related