Home > Software engineering >  Access denied for user 'root' (using password: NO)
Access denied for user 'root' (using password: NO)

Time:08-04

This is my docker-compose file, everytime I run the docker-compose up command I get the error specified in the title. I have tried running docker-compose config and everything matches.

 version: "3.8"                                                                          
    services:                                                                               
      phedon-service:                                                                       
        build: .                                                                            
        restart: always                                                                     
        ports:                                                                              
          - "8080:8080"                                                                     
        networks:                                                                           
          - phedon                                                                          
        depends_on:                                                                         
          - phedon_db                                                                       
        env_file:                                                                           
          - .env                                                                            
      phedon_db:                                                                            
        image: "mariadb:10.6"                                                               
        container_name: mariadb                                                             
        restart: always                                                                     
        healthcheck:                                                                        
          test: [ "CMD", "mariadb-admin", "--protocol", "tcp" ,"ping" ]                     
          timeout: 3m                                                                       
          interval: 10s                                                                     
          retries: 10                                                                       
        ports:                                                                              
          - "3307:3306"                                                                     
        networks:                                                                           
          - phedon                                                                          
        environment:                                                                        
          -MYSQL_DATABASE: "phedondb"                                                       
          -MYSQL_USER: "root"                                                               
          -MYSQL_PASSWORD: "12345"                                                          
          -MYSQL_ROOT_PASSWORD: "12345"                                                                                      
        env_file:                                                                           
        - .env                                                                              
    networks:                                                                               
       phedon:        

                                                                  

CodePudding user response:

  1. your services.db.environment must be a mapping, not a list.

  2. we can omit the root user password with MARIADB_ALLOW_EMPTY_ROOT_PASSWORD

    db:
      image: "mariadb:10.6"                                                               
      container_name: mariadb                                                             
      restart: always                                                                     
      healthcheck:                                                                        
        test: [ "CMD", "mariadb-admin", "--protocol", "tcp" ,"ping" ]                     
        timeout: 3m                                                                       
        interval: 10s                                                                     
        retries: 10                                                                       
      ports:                                                                              
        - "3333:3306"
      environment:                                                                        
        MYSQL_DATABASE: "phedondb"                                                       
        MYSQL_USER: "phedon"                                                               
        MYSQL_PASSWORD: "12345"
        MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: true
    
  3. If you like to use the root password For the admin ping command, we've to provide the password for the DB server. This can be done in different ways.

    • Using the MYSQL_PWD environment var.
    • Using the -p flag in the docker CLI
    • Creating some credentials file
    services:
      db:
      image: "mariadb:10.6"                                                               
      container_name: mariadb                                                             
      restart: always                                                                     
      healthcheck:                                                                        
        test: [ "CMD-SHELL", "mysqladmin ping" ]                     
        timeout: 3m                                                                       
        interval: 10s                                                                     
        retries: 10                                                                       
      ports:                                                                              
        - "3333:3306"
      environment:                                                                        
        MYSQL_DATABASE: "phedondb"                                                       
        MYSQL_USER: "phedon"                                                               
        MYSQL_PASSWORD: "12345"
        MYSQL_ROOT_PASSWORD: "12345"
        MYSQL_PWD: "12345"
    
  • Related