How to connect to mysql server from nodejs app using sequelize. But, earlier i used docker and ran a mysql container. Now am unable to connect locally

huangapple go评论66阅读模式
英文:

How to connect to mysql server from nodejs app using sequelize. But, earlier i used docker and ran a mysql container. Now am unable to connect locally

问题

每当我尝试将我的Express.js应用程序连接到Node服务器时,都会出现错误(请参见下面的控制台消息)。
在将我的应用程序Docker化之前,以前一切都运行得很顺利,但在创建了我的mysql-image-container(Docker)并通过Docker运行mysql后,一切都进行得很顺利。但现在,当我尝试连接我的应用程序与SQL服务器而不是通过Docker容器时,我会收到以下错误消息:

(node:22136) UnhandledPromiseRejectionWarning: SequelizeAccessDeniedError: 用户'root'@'localhost'被拒绝访问(使用密码:是)
在ConnectionManager.connect (C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\mysql\connection-manager.js:94:17)
在processTicksAndRejections (internal/process/task_queues.js:95:5)
在async ConnectionManager._connect (C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:220:24)
在async C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:174:32
在async ConnectionManager.getConnection (C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:197:7)
在async C:\Users\A\ARISE1\node_modules\sequelize\lib\sequelize.js:304:26
在async MySQLQueryInterface.tableExists (C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\abstract\query-interface.js:102:17)
在async Function.sync (C:\Users\A\ARISE1\node_modules\sequelize\lib\model.js:939:21)
在async Sequelize.sync (C:\Users\A\ARISE1\node_modules\sequelize\lib\sequelize.js:376:9)
(使用node --trace-warnings ...来显示警告的创建位置)
(node:22136) UnhandledPromiseRejectionWarning: 未处理的promise拒绝。此错误要么是由于在没有catch块的异步函数内抛出,要么是由于拒绝未使用.catch()处理的promise引起的。要在未处理的promise拒绝时终止节点进程,请使用CLI标志--unhandled-rejections=strict(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。(拒绝ID:1)
(node:22136) [DEP0018] DeprecationWarning: 未处理的promise拒绝已弃用。将来,未处理的promise拒绝将以非零退出码终止Node.js进程。

我尝试删除了Docker文件、mysql-image-container(来自Docker),但仍然无法将我的应用程序连接到本地mysql服务器。现在,即使我尝试通过SQL命令行连接到我的数据库,也无法这样做,即使使用正确的凭据。尽管我能够通过MySQL Workbench连接到数据库。
用于构建映像/运行容器的Docker命令如下:

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

在此之前,我曾右键单击Docker文件并选择构建映像选项。
我没有完全理解Dockerfile的内容,因此我从在线源中混合了一些代码。
任何帮助将不胜感激。

英文:

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.

答案1

得分: 0

const sequelize = new Sequelize(数据库, 用户名, 密码, {
host: 主机,
dialect: 方言
});

英文:
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
    });

答案2

得分: 0

请分享您的代码。
还请查看下面的代码,以了解如何建立连接。

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 address
    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);
}
英文:

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);
}

huangapple
  • 本文由 发表于 2023年1月4日 17:27:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75003417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定