6

I'm using a docker container for MySQL with docker-compose that works just fine.

The only problem is that I get the error unknown database "database_name" the first time I run it every day (after Windows startup)

After that, if I stop it and re-run it I get no errors and everything works fine.

yaml configuration:

version: "2.0"
services:
  mysql:
    container_name: mysql
    restart: always
    image: mysql:5.7
    command: --max_allowed_packet=32505856
    ports:
      - "3306:3306"
    volumes:
      - 'C:\data\mysql_db:/var/lib/mysql/'
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
    networks:
      - shared
networks:
  shared: 
    external:
      name: shared

EDIT: here is a pastebin of the logs of a startup: https://pastebin.com/aJiKJ4aE

8
  • 1
    Maybe adding MYSQL_DATABASE: database_name in environment would help ? Commented May 28, 2019 at 8:37
  • I don't think so, I'm working with multiple databases and none of them is recognized Commented May 28, 2019 at 8:45
  • Do you get the error from startup? Not from connecting and trying to do some queries? Commented May 28, 2019 at 16:15
  • Yes, from startup (and also if I try to connect consequently) Commented May 28, 2019 at 16:43
  • 1
    What is in 'docker logs mysql'? Commented May 29, 2019 at 1:10

2 Answers 2

3
+50

I believe you're experiencing this problem. There's a couple possible solutions there, but I haven't tried them myself as I don't have Docker on Windows:

  1. Solution 1 by shayne
    Remove restart:always from your container. Instead run this command once, it'll create a container that will start your container when the mount is ready:
docker run --name holdup
    --restart always
    -v 'C:\data\mysql_db:/var/lib/mysql/'
    -v //var/run/docker.sock:/var/run/docker.sock
    shaynesweeney/holdup

This will however have an effect of starting all your stopped containers on reboot.

  1. Solution 2 by evolart
    Create the following Powershell script (adjust your location to where your docker-compose.yml is):
Do {
    $dockerps = (docker ps)
    Start-Sleep -S 5
} While (! $dockerps -contains "mysql")
Set-Location D:\Docker\MySQL
docker-compose restart

Then:

Add a Task Scheduler Task with the action Start a program to run the script.
Program/script: powershell.exe
Add arguments: -windowstyle hidden -file D:\Docker\MySQL-Restart.ps1

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

1 Comment

I'm facing similar issue almost once a month. What I'm doing for now is to run DB backup script from host docker exec -i mysql_test mysql -uroot -ptest </home/some_user/db_backups/backup_2019-06-21.sql
0

The problem is that there is no such a schema in your database container instance. What you can do is:

  • run the app
  • open your mysql workbench and connect to your db container instance using the binding port ( you can also create the schema using the command line)
  • create the schema "database_name"
  • create table and insert date(optional)
  • finally restart your docker app

You should be able to access the container db now.

check this video if you don't know how to connect to the database container using the workbench

Comments

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.