我正在通过 docker-compose 启动两个 docker 容器。
文件内容docker-compose.yml
:
version: "3.7"
services:
app:
image: my-app:latest
ports:
- 8000:8000
working_dir: /usr/app/MyApp
environment:
MYSQL_ROOT_PASSWORD: myawesomepassword
MYSQL_DATABASE: myawesomedb
MYSQL_USER: root
MYSQL_PASSWORD: myawesomepassword
mysql:
image: mysql:5.6
volumes:
- db-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: myawesomepassword
MYSQL_DATABASE: myawesomedb
volumes:
db-data: {}
该应用程序像这样连接到数据库:
import mysql from 'mysql2/promise.js';
var config = {
app: {
host: 'http://localhost',
port: '8000'
},
database: {
host: 'host.docker.internal',
port: 3306,
database: process.env['MYSQL_DATABASE'],
user: process.env['MYSQL_USER'],
password: process.env['MYSQL_PASSWORD']
}
}
const {host, port, user, password, database } = config.database;
// Error occurs here
const connection = await mysql.createConnection({ host, port, user, password });
当应用程序服务尝试连接到 mysql 时,我收到此错误:
Error: Access denied for user 'root'@'localhost' (using password: YES)
欢迎任何建议。提前谢谢您