Published By - Rajendra Nagar

How to Import a MySQL Dump Database Using a Docker Container

Working with MySQL databases inside Docker containers can streamline development workflows. If you have a MySQL dump file (.sql) and need to import it into a MySQL database running in a Docker container, follow this step-by-step guide.

Rajendra Nagar

Prerequisites

Before starting, ensure you have:

  1. A running MySQL container.

  2. The MySQL dump file you wish to import (dump.sql).

  3. Docker installed on your system.


Step-by-Step Guide

Step 1: Identify Your MySQL Container

Start by identifying the name or ID of your running MySQL container. Run the following command:

docker ps

This will list all running containers. Look for the container running MySQL and note its name or ID.

Step 2: Prepare the SQL Dump File

Ensure your .sql dump file is accessible on your local machine. For example, the file could be located at /path/to/dump.sql.

Step 3: Copy the Dump File Into the Container (Optional)

If you prefer to copy the dump file into the container before importing it, use the docker cp command:

docker cp /path/to/dump.sql <container_name>:/dump.sql

Replace <container_name> with the name or ID of your container.

Step 4: Import the Dump File

You can import the database dump using the mysql command inside the container. There are two approaches:

Option 1: Without Copying the File

Stream the dump file directly into the MySQL container:

cat /path/to/dump.sql | docker exec -i <container_name> mysql -u <username> -p<password> <database_name>

Replace:

  • <container_name>: Name or ID of the MySQL container.

  • <username>: MySQL username (e.g., root).

  • <password>: MySQL password (omit -p if there is no password).

  • <database_name>: Target database name.

Option 2: After Copying the File

If you previously copied the file into the container, execute the following command:

docker exec -i <container_name> mysql -u <username> -p<password> <database_name> < /dump.sql

Step 5: Verify the Import

Once the import process is complete, you can verify the database content:

  1. Access the MySQL container:

    docker exec -it <container_name> mysql -u <username> -p<password>

  2. Inside the MySQL shell, run:

    SHOW DATABASES;
    USE <database_name>;
    SHOW TABLES;

This will confirm that the database and its tables have been successfully imported.


Quick Import Example

If you are confident about the paths and credentials, you can use a single command to import the dump without copying it:

cat dump.sql | docker exec -i my_mysql_container mysql -u root -proot_password my_database


Troubleshooting

  • Error: Access Denied Ensure the username, password, and database name are correct. Verify that the database exists before importing.

  • File Not Found Double-check the path to the dump file on your host machine or within the container.

  • Container Not Found Ensure the MySQL container is running and the name/ID is correct.


Following this guide, you can efficiently import a MySQL dump database into a Dockerized MySQL container, streamlining your development and deployment workflows.