Execute mysql script in mysql container created by docker
You can go to the MySQL container and execute it by following the steps below.sql
script:
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.sql
The 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.sql
File (assuming the file path is/path/to/
):
source /path/to/;
if.sql
File 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.