mysql icon indicating copy to clipboard operation
mysql copied to clipboard

Creating Multiple Databases without init script

Open scrummyin opened this issue 9 years ago • 8 comments

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.

scrummyin avatar Oct 26 '17 17:10 scrummyin

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).

tianon avatar Nov 15 '17 00:11 tianon

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:

tianon avatar Apr 19 '18 20:04 tianon

  • [x] Multiple database support.

Awesome! This is exactly what I want.

seekcx avatar May 03 '18 09:05 seekcx

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

dmitrym0 avatar May 10 '18 17:05 dmitrym0

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 avatar May 10 '18 17:05 tianon

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

xandriaw avatar May 15 '18 20:05 xandriaw

That should be plenty, yep!

tianon avatar May 15 '18 20:05 tianon

Bobby Tables

:D

glensc avatar Oct 02 '18 21:10 glensc