Reputation: 149
We are currently trying to find a more efficient way to deal with MySQL docker start up process and currently have the following:
FROM mysql
ENV MYSQL_DATABASE marsdb
COPY ./src/main/resources/ /docker-entrypoint-initdb.d/
and
echo "Building database image..."
docker build -t marsdb -f Dockerfile.marsdb .
echo "Starting database and removing old database container."
docker rm -f marssql
docker run -d -p 3306:3306 --name marssql -e MYSQL_ROOT_PASSWORD=pass marsdb
The issue with this setup is the following: Once the container is started someone would currently have to manually go into the container and select the database, in this case marsdb
like the following:
docker exec -it marssql bin/bash
mysql -u root -p
pass
use marsdb;
Is there a way to auto select the database after the container launches?
Upvotes: 0
Views: 426
Reputation: 4220
I think you should consider not doing this when starting the container. You can specify the database that you want to work on, in the connection string when connecting to the database from whatever app is using it.
Try reading the following answer: How do I set the default schema for a user in MySQL
Upvotes: 0
Reputation: 59936
What about just passing the DB name to command, so you will not need to switch
mysql -u root -ppass marsdb
#or
docker exec -it marssql bin/bash -c "mysql -u root -ppass marsdb"
Or if you are not sure about DB, then you can try to run the container with the default DB environment variable. in this MySQL will create automatically create DB for and will run entrypoint script against this DB.
docker run -it --rm --name marssql -e DEFAULT_DB=marsedb -e MYSQL_ROOT_PASSWORD=pass -e MYSQL_DATABASE=marsedb mysql
Then you can connect like
docker exec -it marssql bin/bash -c "mysql -u root -ppass $DEFAULT_DB"
MYSQL_DATABASE
This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access (corresponding to GRANT ALL) to this database. MySQL docker Environment Variables
Upvotes: 2