Mariadb-docker: "No database selected" when trying to restore from a dump file

Created on 14 Jan 2021  路  3Comments  路  Source: MariaDB/mariadb-docker

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.

question

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

All 3 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amon-ra picture amon-ra  路  3Comments

andrerom picture andrerom  路  5Comments

ramonmoraes8080 picture ramonmoraes8080  路  3Comments

dbjpanda picture dbjpanda  路  5Comments

iBobik picture iBobik  路  7Comments