节点应用程序连接到 mysql 数据库"/>
无法使用 docker compose 从我的节点应用程序连接到 mysql 数据库
每次我启动我的服务器时,我都会收到以下错误
Unable to initialize database: { Error: Access denied for user
和以下消息
留言: '拒绝用户'kasper'@'172.18.0.1'的访问(使用密码:YES)', 代码:'ER_ACCESS_DENIED_ERROR', 错误号:1045, sqlState: '28000' } 在 http://localhost:5000 上监听
我正在使用 docker compose 创建数据库,文件如下所示:
version: '5.7'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_DATABASE: mydb
MYSQL_USER: kasper
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
ports:
- 3306:3306
volumes:
- ./data:/var/lib/mysql
我在名为 user.js 的文件中连接到数据库,下面包含带有相关数据库代码的文件示例:
const sequelize = new Sequelize('mydb', 'kasper', 'test', {
host: 'localhost',
dialect: 'mysql'
});
// Define a User model with fields for email and hashed password
const User = sequelize.define('User', {
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
teamId: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'Team',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
},
});
// Define a function to initialize the database
async function initDatabase() {
try {
// Connect to the database
const connection = await mysql.createConnection({
host: 'localhost',
user: 'kasper',
password: 'test'
});
// Create the database if it doesn't exist
await connection.query('CREATE DATABASE IF NOT EXISTS teamplay');
// Close the connection to the default database
await connection.end();
// Sync the model with the database to create the User table
await sequelize.sync();
console.log('Database initialized');
} catch (error) {
console.error('Unable to initialize database:', error);
}
}
任何人都知道如何修复它所以我不会被拒绝
回答如下:更多推荐
无法使用 docker compose 从我的节点应用程序连接到 mysql 数据库
发布评论