Exporting a MySQL Database from a Docker Container
When running MySQL inside a Docker container, you can easily export a database using mysqldump. This is useful for backups, migrations, or moving data between environments.
Step 1: Identify the container and database
First, list your running containers to confirm the MySQL container name:
docker ps
For example, assume your container is named bedrock_db and the database you want to export is ghost_db.
Step 2: Export the database
Run the following command to export the database to a SQL file on your host machine:
docker exec bedrock_db mysqldump -u root -p'YOUR_PASSWORD' ghost_db > ghost_db_backup.sql
- Replace
YOUR_PASSWORDwith the MySQL root password. - Replace
ghost_dbwith the name of your database. ghost_db_backup.sqlwill be created in your current directory.
Step 3: Verify the export
Check that the file was created and contains SQL statements:
ls -lh ghost_db_backup.sql
head -n 20 ghost_db_backup.sql
The SQL dump can now be imported into another MySQL instance or stored as a backup.