1

I have managed to create a MySQL and PHP container and my scripts execute and all my tables are there.

However I have a database that I call "myDb" and a user that is called "someuser" and when the database is created for some reason the name of the database is "somedatabase"

my docker-compose.yaml file:

services:
  mysql:
    image: mysql:latest
    ports:
      - 3307:3306
    environment:
      MYSQL_DATABASE: myDb
      MYSQL_ROOT_PASSWORD: SomeRootPassword1!
      MYSQL_USER: someuser
      MYSQL_PASSWORD: Password1!
    volumes:
      - ./dbScript/winit_Script2.sql:/docker-entrypoint-initdb.d/winit_Script2.sql
      - db_data:/var/lib/mysql
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: dev_pma
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3307
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8183:80

volumes:
  db_data:

phpAdmin:

enter image description here

Mysqlworkbench:

enter image description here

What have I done wrong here?

A little edit after the comments:

It would seem that when having a volumes section you create volumes in docker and when you create a volume on a specific port once then it gets reused every time you do docker-compose up. This was the case for me.

More details in accepted answer.

4
  • Can you please share the log of the docker compose up command? Especially the mysql container logs. Commented Aug 27, 2020 at 13:44
  • @NeoAnderson please view edits Commented Aug 27, 2020 at 13:46
  • My guess is that you are composing up the same DB, which has the old somedatabase from a previous run. Can you check for a line telling something like "init.sh executed" or conversely "the volume already contains data...skipping DB initialization"? I'll try to reproduce meanwhile Commented Aug 27, 2020 at 13:49
  • I think it will only create the database named in MYSQL_DATABASE if the volume is empty. So if you can afford to lose this data, just drop the Docker volume, and it will recreate the database (you will obviously have to run your migrations and repopulate the tables). Commented Aug 27, 2020 at 13:51

1 Answer 1

3

The mysql image does not initialize the database if the volume is not clean.

When you are stopping and starting the database from the same compose file, the volume is always the same, hence you want the data to be persisted even after an app restart.

To force the re-initialization of the data, you can delete that docker volume(only if you no longer need that database! this cannot be undone):

First, stop and delete the containers.

Then list and delete the volume that persists the database:

docker volume ls
DRIVER              VOLUME NAME
local               <your-deployment-name>_db_data


docker volume rm <your-deployment-name>_db_data

Then run again the docker-compose up command and you'll be able to find the myDb in phpMyAdmin instead of somedb


Edit:
Unless you change yourself the entrypoint and rebuild the image to force it initialize your DB according to the ENV you're passing, even if the volume is not clean, the only option that comes to my mind is to create the new DB manually. Here is the conditional that skips the re-initialization of the DB and here is the script that is invoked if the volume is clean.

Sign up to request clarification or add additional context in comments.

4 Comments

Ill give this a try.
This will wipe your current DB! Do it only if you no longer need that DB
Omg...why did you not tell me this earlier...my poor db...lols. This did the trick thanks alot.
@CodingLittle, I usually don't use bold, but here I felt I had to do it in the post :). The risk of having someone blindly copy-pasting is real :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.