Mysql: Creating Multiple Databases without init script

Created on 26 Oct 2017  路  8Comments  路  Source: docker-library/mysql

I found a bug/feature. I was able to create multiple databases without the need for my own dockerfile. I kinda abused the docker-entrypoint.sh. Below is a terminal session that shows how I did it

$ cat elist
MYSQL_DATABASE=one`; CREATE DATABASE IF NOT EXISTS two; CREATE DATABASE IF NOT EXISTS `three
MYSQL_ROOT_PASSWORD=pass
$ docker run -p 127.0.0.1:3306:3306 --env-file elist mysql

I'm not sure want to do with this information, but it works as a quick hack to get multiple databases.

All 8 comments

Heh, this is a cute hack -- I'm not sure what to do with it either. Either leave it as an easter egg or try to do some form of mysql_real_escape_string in Bash? (yuck)

I'm inclined to simply leave it as an undocumented thing that happens to work as a consequence of how our script works and leave it alone (since we'd likely have to add more dependencies and thus make the image bigger to attempt to "fix" it properly and having backtick in a database name should be pretty rare and those folks should have to think long and hard about what the heck they're doing).

Since this isn't something we've deemed worth fixing, I'm going to close -- the issue will hopefully serve as a good enough reference for folks looking to take advantage of this accidental "feature" (which is really a bug, to be explicitly clear). :+1:

  • [x] Multiple database support.

Awesome!
This is exactly what I want.

This is cute, but surely there's a cleaner way to create multiple database on init? What about permission grants?

The cleaner way is to use a proper SQL file (or shell script) in /docker-entrypoint-initdb.d that performs the steps necessary to set up the database in the way that you need.

@tianon can I just have two .sql scripts, each with 'create database' commands in them or is there something more necessary?

That should be plenty, yep!

Bobby Tables

:D

Was this page helpful?
0 / 5 - 0 ratings