SoFunction
Updated on 2025-03-09

How to execute mysql scripts in mysql container created by docker

Execute mysql script in mysql container created by docker

You can go to the MySQL container and execute it by following the steps below.sqlscript:

1. Enter the MySQL container

Find your container ID or name

Then run the following command to enter the container terminal:

docker exec -it <container_name_or_id> /bin/bash

2. Run .sql scripts

After entering the container terminal, connect to the MySQL service and execute the script.

Assumptions.sqlThe file is already inside the container, or you can mount it into the container.

Connect to MySQL:

mysql -u root -p

3. Execute SQL scripts

After logging in to MySQL

Run the following command to execute.sqlFile (assuming the file path is/path/to/):

source /path/to/;

if.sqlFile on the host

You can also execute it directly through the following command:

docker exec -i <container_name_or_id> mysql -u root -p < database_name < /path/on/host/

This eliminates the step of entering the container and executes scripts directly on the host.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.