2024-08-30 19:58:12 +02:00
---
title: Migrate database
layout: default
parent: How Tos
2024-09-30 00:40:35 +02:00
nav_order: 10
2024-08-30 19:58:12 +02:00
---
2025-01-13 14:56:08 +01:00
# Migrate Database
2024-08-30 19:58:12 +02:00
2025-01-13 14:56:08 +01:00
To migrate a MySQL database from a source to a target database, you can use the `migrate` command. This feature simplifies the process by combining the backup and restore operations into a single step.
2024-08-30 19:58:12 +02:00
{: .note }
2025-01-13 14:56:08 +01:00
The `migrate` command eliminates the need for separate backup and restore operations. It directly transfers data from the source database to the target database.
2024-08-30 19:58:12 +02:00
2024-09-03 06:49:26 +02:00
{: .warning }
2025-01-13 14:56:08 +01:00
The `migrate` operation is **irreversible** . Always back up your target database before performing this action.
---
## Configuration Steps
1. **Source Database** : Provide connection details for the source database.
2. **Target Database** : Provide connection details for the target database.
3. **Run the Migration** : Use the `migrate` command to initiate the migration.
2024-08-30 19:58:12 +02:00
2025-01-13 14:56:08 +01:00
---
## Example: Docker Compose Configuration
Below is an example `docker-compose.yml` configuration for migrating a database:
```yaml
2024-08-30 19:58:12 +02:00
services:
mysql-bkup:
2025-01-13 14:56:08 +01:00
# In production, lock your image tag to a specific release version
# instead of using `latest` . Check https://github.com/jkaninda/mysqlbkup/releases
# for available releases.
2024-08-30 19:58:12 +02:00
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command: migrate
volumes:
- ./backup:/backup
environment:
2025-01-13 14:56:08 +01:00
## Source Database
2024-08-30 19:58:12 +02:00
- DB_PORT=3306
- DB_HOST=mysql
- DB_NAME=database
- DB_USERNAME=username
- DB_PASSWORD=password
2025-01-13 14:56:08 +01:00
## Target Database
- TARGET_DB_HOST=target-postgres
2024-09-03 06:49:26 +02:00
- TARGET_DB_PORT=3306
- TARGET_DB_NAME=dbname
- TARGET_DB_USERNAME=username
- TARGET_DB_PASSWORD=password
2025-01-13 14:56:08 +01:00
# Ensure the mysql-bkup container is connected to the same network as your database
2024-08-30 19:58:12 +02:00
networks:
- web
2025-01-13 14:56:08 +01:00
2024-08-30 19:58:12 +02:00
networks:
web:
```
2025-01-13 14:56:08 +01:00
---
2024-09-03 06:49:26 +02:00
2025-01-13 14:56:08 +01:00
## Migrate Database Using Docker CLI
2024-08-30 19:58:12 +02:00
2025-01-13 14:56:08 +01:00
You can also run the migration directly using the Docker CLI. Below is an example:
2024-08-30 19:58:12 +02:00
2025-01-13 14:56:08 +01:00
### Environment Variables
Save your source and target database connection details in an environment file (e.g., `your-env` ):
```bash
## Source Database
DB_HOST=postgres
2024-09-03 06:49:26 +02:00
DB_PORT=3306
DB_NAME=dbname
DB_USERNAME=username
2024-08-30 19:58:12 +02:00
DB_PASSWORD=password
2025-01-13 14:56:08 +01:00
## Target Database
TARGET_DB_HOST=target-postgres
2024-09-03 06:49:26 +02:00
TARGET_DB_PORT=3306
TARGET_DB_NAME=dbname
TARGET_DB_USERNAME=username
TARGET_DB_PASSWORD=password
2024-08-30 19:58:12 +02:00
```
2025-01-13 14:56:08 +01:00
### Run the Migration
```bash
docker run --rm --network your_network_name \
--env-file your-env \
-v $PWD/backup:/backup/ \
jkaninda/pg-bkup migrate
2024-08-30 19:58:12 +02:00
```
2025-01-13 14:56:08 +01:00
---
## Key Notes
- **Irreversible Operation**: The `migrate` command directly transfers data from the source to the target database. Ensure you have a backup of the target database before proceeding.
- **Network Configuration**: Ensure the `mysql-bkup` container is connected to the same network as your source and target databases.