我创建了一个使用mysql数据库的应用程序,但当我将我的服务Dockerize时,即使我创建了相同的网络并将我的服务名称放入数据库配置中,我也无法从彼此访问我的容器。
version: "3.8"
services:
db:
container_name: mydb
image: mysql:latest
restart: unless-stopped
cap_add:
- SYS_NICE
environment:
- MYSQL_ROOT_PASSWORD=12345678
- MYSQL_DATABASE=something
- MYSQL_USER=something
- MYSQL_PASSWORD=12345678
volumes:
- db:/var/lib/mysql
network_mode: "host"
nodeserver:
depends_on:
- db
build:
context: ./app
network_mode: "host"
volumes:
db:
# pull the Node.js Docker image
FROM node:alpine
# create the directory inside the container
WORKDIR /usr/src/app
# copy the package.json files from local machine to the workdir in container
COPY package*.json ./
# run npm install in our local machine
RUN npm install
# copy the generated modules and all other files to the container
COPY . .
# our app is running on port 5000 within the container, so need to expose it
EXPOSE 5000
# the command that starts our app
CMD ["node", "server.js"]
erver_1 | ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:3306
nodeserver_1 | at ConnectionManager.connect (/usr/src/app/node_modules/sequelize/dist/lib/dialects/mysql/connection-manager.js:92:17)
nodeserver_1 | at processTicksAndRejections (node:internal/process/task_queues:96:5)
nodeserver_1 | at async ConnectionManager._connect (/usr/src/app/node_modules/sequelize/dist/lib/dialects/abstract/connection-manager.js:216:24)
nodeserver_1 | at async /usr/src/app/node_modules/sequelize/dist/lib/dialects/abstract/connection-manager.js:174:32
nodeserver_1 | at async ConnectionManager.getConnection (/usr/src/app/node_modules/sequelize/dist/lib/dialects/abstract/connection-manager.js:197:7)
nodeserver_1 | at async /usr/src/app/node_modules/sequelize/dist/lib/sequelize.js:303:26
nodeserver_1 | at async MySQLQueryInterface.createTable (/usr/src/app/node_modules/sequelize/dist/lib/dialects/abstract/query-interface.js:94:12)
nodeserver_1 | at async Function.sync (/usr/src/app/node_modules/sequelize/dist/lib/model.js:913:5)
nodeserver_1 | at async Sequelize.sync (/usr/src/app/node_modules/sequelize/dist/lib/sequelize.js:377:9)
nodeserver_1 | at async initialize (/usr/src/app/_helpers/db.js:36:5) {
nodeserver_1 | parent: Error: connect ECONNREFUSED 127.0.0.1:3306
nodeserver_1 | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
nodeserver_1 | errno: -111,
nodeserver_1 | code: 'ECONNREFUSED',
nodeserver_1 | syscall: 'connect',
nodeserver_1 | address: '127.0.0.1',
nodeserver_1 | port: 3306,
nodeserver_1 | fatal: true
nodeserver_1 | },
nodeserver_1 | original: Error: connect ECONNREFUSED 127.0.0.1:3306
nodeserver_1 | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
nodeserver_1 | errno: -111,
nodeserver_1 | code: 'ECONNREFUSED',
nodeserver_1 | syscall: 'connect',
nodeserver_1 | address: '127.0.0.1',
nodeserver_1 | port: 3306,
nodeserver_1 | fatal: true
nodeserver_1 | }
在运行我的项目与docker-compose了我得到这个错误
2条答案
按热度按时间hjzp0vay1#
该错误是由于未在Sequelize选项中提供主机名而导致的,如果您不提供,则默认为localhost。您需要执行以下操作:
参考(见选项3):https://sequelize.org/master/manual/getting-started.html#connecting-to-a-database
bsxbgnwa2#