无法使用 docker compose 从我的节点应用程序连接到 mysql 数据库

编程入门 行业动态 更新时间:2024-10-04 15:31:21

无法使用 docker compose 从我的<a href=https://www.elefans.com/category/jswz/34/1771452.html style=节点应用程序连接到 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 数据库

本文发布于:2024-05-31 00:21:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771082.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   连接到   应用程序   数据库   docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!