Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL Docker container gives "unknown database" error

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

like image 411
Nicolò Gasparini Avatar asked Nov 22 '18 08:11

Nicolò Gasparini


1 Answers

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

like image 112
Rafał G. Avatar answered Sep 18 '22 17:09

Rafał G.