Mysql: Initialization from database dumps does not work correctly

Created on 15 Feb 2016  路  2Comments  路  Source: docker-library/mysql

See the documentation here.

If I mount in one or more .sql files into this directory then mysql errors on initialization saying that no database is selected: ERROR 1046 (3D000) at line 22: No database selected. This is because I didn't provide a database name environment variable, because I expected the names of the databases to be derived from the .sql files.

When I add the database env variable MYSQL_DATABASE then each of the .sql files are imported into that same database, potentially overwriting tables.

So for each .sql file in the directory it should take the name of the file without the extension and that would be used for importing the database. The only way for me to work around this was to rename my two dumps to not have an extension, then add a shell script that manually does the operations that I'd expect to happen. If this as expected, then this behavior should be added to the docs.

Most helpful comment

I added the param: MYSQL_DATABASE in my configuration as such:

mysql:
image: mysql:5.5.53
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: watcher
ports:

  • 3307:3306
    volumes:
  • ./db-dump:/docker-entrypoint-initdb.d

All 2 comments

IMO, this should be solved by adding USE xyz-databse; to the top of your .sql dump files.

The --databases/-B flag to mysqldump will do this automatically:

CREATE DATABASE and USE statements are included in the output before each new database.

I added the param: MYSQL_DATABASE in my configuration as such:

mysql:
image: mysql:5.5.53
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: watcher
ports:

  • 3307:3306
    volumes:
  • ./db-dump:/docker-entrypoint-initdb.d
Was this page helpful?
0 / 5 - 0 ratings