The command:
docker exec -i mariadb_container_name sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' database_name < /path/to/dump.sql
Since the dump file doesn't do a DROP/CREATE or USE the database, I have created the database (indicated as database_name) prior to running the command. The error I get is this:
ERROR 1046 (3D000) at line 27: No database selected
The dump file is way too large to open and edit, so I thought of asking for help here.
Instead of < /path/to/dump.sql, you could do something like { echo 'CREATE DATABASE database_name IF NOT EXISTS; USE database_name;'; cat /path/to/dump.sql; } | mysql ... (or set MYSQL_DATABASE so that the image will pre-create your desired database automatically).
Thanks to your command suggestion and a bit of searching around, I found something that works for me:
cat <(echo 'USE database_name;') /path/to/dump.sql | docker exec -i mariadb_container_name sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"'
I just wonder why the first command didn't work...
In looking at this with fresh eyes, I think I just realized why your first command didn't work, and it all comes down to quoting: :sweat_smile:
Before: docker exec -i mariadb_container_name sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' database_name < /path/to/dump.sql
After: docker exec -i mariadb_container_name sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" database_name' < /path/to/dump.sql
(In other words, database_name was being passed as an argument to sh, _not_ to mysql :smile:)
Most helpful comment
In looking at this with fresh eyes, I think I just realized why your first command didn't work, and it all comes down to quoting: :sweat_smile:
Before:
docker exec -i mariadb_container_name sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' database_name < /path/to/dump.sqlAfter:
docker exec -i mariadb_container_name sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" database_name' < /path/to/dump.sql(In other words,
database_namewas being passed as an argument tosh, _not_ tomysql:smile:)