skip to Main Content

I try to run my server in NodeJS and a MySQL database using Docker , when I run sudo docker-compose up I get this error in nodejs server:

Access denied for user 'Pedro'@'172.19.0.4' (using password: YES)
backend_container |     at Packet.asError (/app/node_modules/mysql2/lib/packets/packet.js:728:17)
backend_container |     at ClientHandshake.execute (/app/node_modules/mysql2/lib/commands/command.js:29:26)
backend_container |     at Connection.handlePacket (/app/node_modules/mysql2/lib/connection.js:478:34)
backend_container |     at PacketParser.onPacket (/app/node_modules/mysql2/lib/connection.js:97:12)
backend_container |     at PacketParser.executeStart (/app/node_modules/mysql2/lib/packet_parser.js:75:16)
backend_container |     at Socket.<anonymous> (/app/node_modules/mysql2/lib/connection.js:104:25)
backend_container |     at Socket.emit (node:events:517:28)
backend_container |     at addChunk (node:internal/streams/readable:335:12)
backend_container |     at readableAddChunk (node:internal/streams/readable:308:9)
backend_container |     at Readable.push (node:internal/streams/readable:245:10) {
backend_container |   code: 'ER_ACCESS_DENIED_ERROR',
backend_container |   errno: 1045,
backend_container |   sqlState: '28000',
backend_container |   sqlMessage: "Access denied for user 'Pedro'@'172.19.0.4' (using password: YES)",
backend_container |   sql: undefined
backend_container | }

this is how I connect to the database in Nodejs:

const db = mysql.createConnection({
    host: process.env.DB_HOST, 
    user: process.env.DB_USER, 
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE, 
    timezone: 'Z'
});


db.connect(err => {
    if (err) {
        console.error("Error al conectar a la base de datos:", err);
    } else {
        console.log("Conexión exitosa a la base de datos!");
    }
});

this is my NodeJS dockerfile:


FROM node:18

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "index.js"]

Here’s my docker-compose.yml:

version: '3.2'

services:
    database:
      image: "mysql"
      environment:
        MYSQL_ROOT_PASSWORD: dbpasswd
        MYSQL_DATABASE: Monitoreo
      ports:
        - "3306:3306"
      volumes:
        - ./BD/Monitoreo_BD.sql:/docker-entrypoint-initdb.d/Monitoreo_BD.sql
        - mysql-data:/var/lib/mysql

    backend:
        image: 'pedrocastro2001/backend-so1'
        container_name: "backend_container"
        environment:
          DB_HOST: database
          DB_USER: Pedro
          DB_PASSWORD: Pedro22
          DB_DATABASE: Monitoreo
        ports:
          - "3000:3000"
        depends_on:
          - database

    frontend:
        image: 'pedrocastro2001/frontend-so1'
        container_name: "frontend_container"
        ports:
          - "5173:5173"

    

volumes:
  mysql-data:

Is there something wrong with my docker-compose file? how can I solve it? thank you.

2

Answers


  1. I think that it’s because you did not config the user Pedro in your mysql container.
    Try adding those to the environment config of mysql container in your docker compose file

    MYSQL_USER: Pedro
    MYSQL_PASSWORD: Pedro22
    
    Login or Signup to reply.
  2. Use this environment for mysql container.

    environment:
          MYSQL_USER: Pedro
          MYSQL_PASSWORD: Pedro22
          MYSQL_ROOT_PASSWORD: dbpasswd
          MYSQL_DATABASE: Monitoreo
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search