user12341234
user12341234

Reputation: 1497

How to start mysqld and then run query immediately from command line?

I'm trying to run a "one off" mysql query on a mysql docker container which is not running.

docker-compose \
  run --rm db \
  mysqld & mysql my-db-name -e "SELECT *"

this doesn't really work because it's not waiting for mysqld to be started.

Upvotes: 1

Views: 151

Answers (1)

David Maze
David Maze

Reputation: 159875

There's not really any shortcuts here: you have to start the database, wait for it to be ready, make your query, and then shut down the database.

However, you don't have to make the query specifically from inside the database container. It looks like you already have a block in the docker-compose.yml file to start the database, so you can

# Start the database
docker-compose up -d db

# Try to connect to the database
mysql -h 127.0.0.1 -e 'SELECT 1;'
# Repeat until successful, usually 30-60 seconds

# Make your actual query
mysql -h 127.0.0.1 my-db-name -e "SELECT *"

# Stop the database
docker-compose down db

This is exactly what you'd need to do if the database wasn't in Docker as well (replace docker-compose commands with /etc/init.d/mysql commands). Docker doesn't make this easier or harder, just different.

Upvotes: 2

Related Questions