skip to Main Content

My docker compose does not create mysql user,database even though i have passed them as environmental variable.here is my docker-compose.yml.

version: '3.3'
services:
web:
  image: zadiki1/posshop-webapp
  ports:
    - "3000:3000"
  depends_on:
    - db
    - phpmyadmin
  environment:
    HOST: db
    MYSQL_DATABASE: 'posshop'
    USER_NAME: 'zadik'
    PASSWORD: 'zadik'
db:
  image: mysql:8
  command: --default-authentication-plugin=mysql_native_password
  restart: always
  ports:
    - "3306:3306"
  environment:
    MYSQL_ROOT_PASSWORD: 'root'
    MYSQL_USER: 'zadik'
    MYSQL_PASSWORD: 'zadik'
    MYSQL_DATABASE: 'posshop'
    MYSQL_ROOT_HOST: '0.0.0.0'
  expose:
        - '3306'
phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin-dot
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: 'root'
      PMA_HOST: db
    depends_on:
    - db
    ports:
      - 8000:80

when i go to the mysql server both the database and the user are not created
I have searched online but cant find a solution

2

Answers


  1. The environment variables are used to configure access to existing resources within the database. If you need to have a database posshop and a user zadik in the db (mysql:8) instance, you will need to create these against the database server before you can access them, e.g. from web.

    It is slightly inconvenient to do this with Docker Compose but, in my experience, a successful solution is to use e.g. a docker volume to persist the database server configuration, create the database and user while the server’s configured to use this for persistence. Then, reference it subsequently in a configuration (as yours) that depends upon it.

    It’s possible but more challenging to have another container added to your Docker Compose that creates these assets and blocks e.g. web from starting until this is done.

    Login or Signup to reply.
  2. Your docker-compose.yml file is correct (at least for the db service). It will create everything you configured – root password, user, and database.

    See below:

    Using the exact same yaml as you for the db service:

    User-MacBook-Pro:testmysql myuser$ cat docker-compose.yml
    version: '3.3'
    services:
      db:
        image: mysql:8
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        ports:
          - "3306:3306"
        environment:
          MYSQL_ROOT_PASSWORD: 'root'
          MYSQL_USER: 'zadik'
          MYSQL_PASSWORD: 'zadik'
          MYSQL_DATABASE: 'posshop'
          MYSQL_ROOT_HOST: '0.0.0.0'
        expose:
          - '3306'
    

    Start containers:

    User-MacBook-Pro:testmysql myuser$ docker-compose up -d
    Creating network "testmysql_default" with the default driver
    Creating testmysql_db_1 ... done
    

    Exec into mysql container:

    User-MacBook-Pro:testmysql myuser$ docker-compose exec db bash
    

    Connect with root and list databases (which shows posshop):

    root@aeba05c5cc08:/# mysql -u root -proot
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 8
    Server version: 8.0.17 MySQL Community Server - GPL
    
    Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    mysql>
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | posshop            |
    | sys                |
    +--------------------+
    5 rows in set (0.01 sec)
    
    mysql> show tables;
    Empty set (0.00 sec)
    
    mysql> exit
    Bye
    

    Connect with user (zadik/zadik):

    root@aeba05c5cc08:/# mysql -u zadik -pzadik
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 9
    Server version: 8.0.17 MySQL Community Server - GPL
    
    Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | posshop            |
    +--------------------+
    2 rows in set (0.01 sec)
    
    mysql> use posshop;
    Database changed
    mysql>
    

    Additional note: As stated in the other answer you should also create a volume to persist data (although that wasn’t the question).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search