我正在使用docker-compose,尝试在控制台中运行yarn sequelize db:migrate
时遇到以下错误:
“已加载配置文件“ src / config / database.js”。 错误:getaddrinfo ENOTFOUND db“
一切正常,但我无法运行迁移。
Dockerfile
FROM node:alpine
WORKDIR /usr/app
COPY package.json yarn.lock ./
RUN yarn
COPY . .
EXPOSE 3000
CMD ["yarn", "dev"]
Docker-compose.yml
version: '3'
services:
api:
container_name: api
build: .
ports:
- '3000:3000'
command: yarn dev
volumes:
- .:/usr/app
links:
- db
db:
container_name: db
image: mysql:5.7
volumes:
- ./data:/mysql/db
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: be_db
MYSQL_USER: root
MYSQL_PASSWORD: password
.env
# Database Variables
DB_HOST=db
DB_USER=root
DB_PASS=password
DB_NAME=be_db
DB_PORT:3306
/src/config/database.js
require('dotenv').config();
module.exports = {
dialect: 'mysql',
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
define: {
timestamps: false,
underscored: true,
underscoredAll: true,
},
};
OBS::如果我将DB_HOST=db
切换为DB_HOST=localhost
,我可以运行迁移,但是它们会出现以下错误:
code: 'ECONNREFUSED',
api | syscall: 'connect',
api | address: '127.0.0.1',
api | port: 3306,
api | fatal: true
答案 0 :(得分:0)
好的,我只需要将端口和主机分开到各个字段中即可
"development": {
"username": "postgres",
"password": "postgres",
"database": "postgres",
"host": "localhost",
"port": "5434",
"dialect": "postgres"
},
我对docker-compose
和sequelize-cli
有相同的问题
node:
build:
context: ./node
target: dev
volumes:
- ./node/src:/app/src
- ./node/private:/app/private
ports:
- 3111:3000
command: npm run dev
environment:
- NODE_ENV=development
env_file:
- ./node/.env
depends_on:
- db
db:
image: postgres:12-alpine
ports:
- 5434:5432
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
"development": {
"username": "postgres",
"password": "postgres",
"database": "postgres",
"host": "localhost:5434",
"dialect": "postgres"
}
当我运行❯ npx sequelize-cli db:migrate
我收到以下错误 :
Sequelize CLI [Node: 15.2.1, CLI: 6.2.0, ORM: 6.3.5]
Loaded configuration file "config/config.json".
Using environment "development".
ERROR: getaddrinfo ENOTFOUND localhost:5434
,它可与以下命令配合使用,更多是一种解决方法
❯ npx sequelize-cli db:migrate --url 'postgres://postgres:postgres@localhost:5434/postgres'
Sequelize CLI [Node: 15.2.1, CLI: 6.2.0, ORM: 6.3.5]
Parsed url postgres://postgres:*****@localhost:5434/postgres
No migrations were executed, database schema was already up to date.