-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automation script for MySQL 8.0 data migration
- Loading branch information
Showing
7 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
DB_ROOT_PASSWORD='secret' | ||
DB_USER='testuser' | ||
DB_PASSWORD='password' | ||
DB_DATABASE='testdb' | ||
MYSQL_name='mysql-container' | ||
MARIADB_DUMP_name='mariadb-container-dump' | ||
MARIADB_MIGRATED_name='mariadb-migrated-mysql8.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
version: "3" | ||
|
||
services: | ||
mysql: | ||
environment: | ||
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD} | ||
MYSQL_USER: ${DB_USER} | ||
MYSQL_PASSWORD: ${DB_PASSWORD} | ||
MYSQL_DATABASE: ${DB_DATABASE} | ||
container_name: ${MYSQL_name} | ||
image: mysql:8.3.0 | ||
healthcheck: | ||
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"] | ||
interval: 20s | ||
timeout: 20s | ||
retries: 2 | ||
start_period: 0s | ||
volumes: | ||
# Preload files for MySQL data | ||
- ./mysql:/docker-entrypoint-initdb.d:z | ||
# We have to save MySQL volume that will be used in upgrade | ||
- dbdata:/var/lib/mysql | ||
networks: | ||
- backend | ||
|
||
# Sidecar for dumping files | ||
mariadb-dump: | ||
environment: | ||
MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD} | ||
MARIADB_USER: ${DB_USER} | ||
MARIADB_PASSWORD: ${DB_PASSWORD} | ||
container_name: ${MARIADB_DUMP_name} | ||
image: mariadb:lts | ||
depends_on: | ||
mysql: | ||
condition: service_healthy | ||
healthcheck: | ||
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"] | ||
start_period: 10s | ||
interval: 20s | ||
timeout: 20s | ||
retries: 3 | ||
# command: > | ||
# bash -c " | ||
# echo 'MariaDB service started. Dump MySQL data ...' | ||
# mariadb-dump -h mysql-container -uroot -psecret testdb > /etc/dump/mysql-dump-data.sql" | ||
# user: ${UID}:${EUID} | ||
volumes: | ||
- mysqldump:/etc/dump/ | ||
# This will not exit container, but command: will | ||
- ./dump-mysql.sh:/docker-entrypoint-initdb.d/dump-mysql.sh | ||
networks: | ||
- backend | ||
# entrypoint: ["/docker-entrypoint-initdb.d/dump-mysql.sh"] | ||
|
||
# We cannot share the same dump directory, we need to stop mariadb-dump | ||
stopper: | ||
image: docker:20.10 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
an3l
Author
Contributor
|
||
depends_on: | ||
mariadb-dump: | ||
condition: service_healthy | ||
volumes: | ||
- /var/run/docker.sock:/var/run/docker.sock | ||
command: ["sh", "-c", "docker stop mariadb-container-dump"] | ||
|
||
# Sidecar for insert dump file | ||
mariadb-migrated-from-mysql8: | ||
environment: | ||
MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD} | ||
MARIADB_USER: ${DB_USER} | ||
MARIADB_PASSWORD: ${DB_PASSWORD} | ||
container_name: ${MARIADB_MIGRATED_name} | ||
image: mariadb:lts | ||
depends_on: | ||
mariadb-dump: | ||
condition: service_completed_successfully | ||
volumes: | ||
- mysqldump:/etc/dump/ | ||
- ./migrate-mariadb.sh:/docker-entrypoint-initdb.d/migrate-mariadb.sh | ||
This comment has been minimized.
Sorry, something went wrong.
grooverdan
Member
|
||
networks: | ||
- backend | ||
|
||
volumes: | ||
dbdata: {} | ||
# sudo chown -R 999:999 ${PWD}/dump-data # host | ||
mysqldump: | ||
driver: local | ||
driver_opts: | ||
type: none | ||
device: "${PWD}/dump-data" | ||
o: bind | ||
|
||
networks: | ||
backend: |
50 changes: 50 additions & 0 deletions
50
examples/migration-8.0/dump-data/mysql-dump-data-utf8mb4_unicode_ci.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
-- MariaDB dump 10.19 Distrib 10.11.6-MariaDB, for debian-linux-gnu (x86_64) | ||
-- | ||
This comment has been minimized.
Sorry, something went wrong. |
||
-- Host: mysql-container Database: testdb | ||
-- ------------------------------------------------------ | ||
-- Server version 8.3.0 | ||
|
||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | ||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | ||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | ||
/*!40101 SET NAMES utf8mb4 */; | ||
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; | ||
/*!40103 SET TIME_ZONE='+00:00' */; | ||
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; | ||
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; | ||
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; | ||
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; | ||
|
||
-- | ||
-- Table structure for table `countries` | ||
-- | ||
|
||
DROP TABLE IF EXISTS `countries`; | ||
/*!40101 SET @saved_cs_client = @@character_set_client */; | ||
/*!40101 SET character_set_client = utf8 */; | ||
CREATE TABLE `countries` ( | ||
`name` char(20) DEFAULT NULL | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; | ||
/*!40101 SET character_set_client = @saved_cs_client */; | ||
|
||
-- | ||
-- Dumping data for table `countries` | ||
-- | ||
|
||
LOCK TABLES `countries` WRITE; | ||
/*!40000 ALTER TABLE `countries` DISABLE KEYS */; | ||
INSERT INTO `countries` VALUES | ||
('Bosnia & Herzegovina'); | ||
/*!40000 ALTER TABLE `countries` ENABLE KEYS */; | ||
UNLOCK TABLES; | ||
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; | ||
|
||
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; | ||
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; | ||
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; | ||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | ||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | ||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; | ||
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; | ||
|
||
-- Dump completed on 2024-02-12 13:48:45 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
-- MariaDB dump 10.19 Distrib 10.11.6-MariaDB, for debian-linux-gnu (x86_64) | ||
-- | ||
-- Host: mysql-container Database: testdb | ||
This comment has been minimized.
Sorry, something went wrong. |
||
-- ------------------------------------------------------ | ||
-- Server version 8.3.0 | ||
|
||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | ||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | ||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | ||
/*!40101 SET NAMES utf8mb4 */; | ||
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; | ||
/*!40103 SET TIME_ZONE='+00:00' */; | ||
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; | ||
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; | ||
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; | ||
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; | ||
|
||
-- | ||
-- Table structure for table `countries` | ||
-- | ||
|
||
DROP TABLE IF EXISTS `countries`; | ||
/*!40101 SET @saved_cs_client = @@character_set_client */; | ||
/*!40101 SET character_set_client = utf8 */; | ||
CREATE TABLE `countries` ( | ||
`name` char(20) DEFAULT NULL | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; | ||
/*!40101 SET character_set_client = @saved_cs_client */; | ||
|
||
-- | ||
-- Dumping data for table `countries` | ||
-- | ||
|
||
LOCK TABLES `countries` WRITE; | ||
/*!40000 ALTER TABLE `countries` DISABLE KEYS */; | ||
INSERT INTO `countries` VALUES | ||
('Bosnia & Herzegovina'); | ||
/*!40000 ALTER TABLE `countries` ENABLE KEYS */; | ||
UNLOCK TABLES; | ||
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; | ||
|
||
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; | ||
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; | ||
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; | ||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | ||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | ||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; | ||
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; | ||
|
||
-- Dump completed on 2024-02-12 13:48:45 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
|
||
echo 'MariaDB service started. Dump MySQL data ...' | ||
# Run your commands and exit container | ||
whoami # mysql" | ||
# sh -c "chown -R mysql:mysql /etc/dump" # Operation permitted | ||
# sh -c "ls -la /etc/dump" | ||
sh -c "mariadb-dump -h mysql-container -uroot -psecret testdb > /etc/dump/mysql-dump-data.sql" | ||
sh -c "ls -la /etc/dump/" | ||
echo "List before" | ||
sh -c "cp /etc/dump/mysql-dump-data.sql /etc/dump/mysql-dump-data-utf8mb4_unicode_ci.sql" | ||
sh -c "ls -la /etc/dump/" | ||
echo "List after" | ||
sh -c "sed -i 's/utf8mb4_0900_ai_ci/utf8mb4_unicode_ci/g' /etc/dump/mysql-dump-data-utf8mb4_unicode_ci.sql" | ||
This comment has been minimized.
Sorry, something went wrong.
grooverdan
Member
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
echo "Show data in MariaDB" | ||
mariadb -uroot -psecret -e "create database testdb;" | ||
mariadb -uroot -psecret testdb < /etc/dump/mysql-dump-data-utf8mb4_unicode_ci.sql | ||
mariadb -uroot -psecret -e "show databases; select * from countries;" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DROP TABLE IF EXISTS countries; | ||
CREATE TABLE countries(name char(20)); | ||
INSERT INTO countries values ("Bosnia & Herzegovina"); |
If
mariadb-dump
can be make to be acommand: mariadb-dump
, then thestopper
can be acondition: service_completed_successfully
(ref) withmysql
container withcommand: mysql -h ${MYSQL_name} -u root -p$.. -e 'shutdown'