Nicolò Gasparini
Nicolò Gasparini

Reputation: 2396

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

Upvotes: 6

Views: 10382

Answers (2)

Glory Pierrette
Glory Pierrette

Reputation: 3

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

Upvotes: 0

Rafał G.
Rafał G.

Reputation: 1692

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

Upvotes: 3

Related Questions