Skip to content

Commit

Permalink
Automation script for MySQL 8.0 data migration
Browse files Browse the repository at this point in the history
  • Loading branch information
an3l committed Feb 12, 2024
1 parent 7cf67e4 commit 2fa4337
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/migration-8.0/.env
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'
94 changes: 94 additions & 0 deletions examples/migration-8.0/compose-mysql8.0.yml
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.

Copy link
@grooverdan

grooverdan Feb 13, 2024

Member

If mariadb-dump can be make to be a command: mariadb-dump, then the stopper can be a condition: service_completed_successfully (ref) with mysql container with command: mysql -h ${MYSQL_name} -u root -p$.. -e 'shutdown'

This comment has been minimized.

Copy link
@an3l

an3l Feb 14, 2024

Author Contributor
  • I have tried that (and lost previously written comment with log) - it doesn't work with shutdown - it just stops temporary server process and re-init again, so service doesn't get completed and condition service_completed_successfully is not true.
  • Alternatively I decided to remove stopper and use service_healthy only - it worked.
  • Optionally I have added restart option (ref) of depends_on that restart current service (mariadb-migrated-from-mysql8) after it updates the dependency service (mariadb-dump). Works now, please see new branch anel-mysql8-migration-v2
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.

Copy link
@grooverdan

grooverdan Feb 13, 2024

Member

does mysqldump:/docker-entrypoint.initdb.d work (as its just a .sql file?).

This comment has been minimized.

Copy link
@an3l

an3l Feb 14, 2024

Author Contributor

If you mean to move migrate-mariadb.sh to dump-data where dump files are, I'm more biased to have separate locations for script and dumps.

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:
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.

Copy link
@an3l

an3l Feb 12, 2024

Author Contributor

This will be removed

-- 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
50 changes: 50 additions & 0 deletions examples/migration-8.0/dump-data/mysql-dump-data.sql
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.

Copy link
@an3l

an3l Feb 12, 2024

Author Contributor

This file will be removed.

-- ------------------------------------------------------
-- 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
14 changes: 14 additions & 0 deletions examples/migration-8.0/dump-mysql.sh
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.

Copy link
@grooverdan

grooverdan Feb 13, 2024

Member

s/utf8mb4_0900/uca1400/g which takes same _a[is]_c[is] suffix. Can this be just piped sed expression that gets piped from mariadb-dump? And | zstd > /etc/dump/mysql-dump.sql.zst

This comment has been minimized.

Copy link
@an3l

an3l Feb 14, 2024

Author Contributor

Applied

5 changes: 5 additions & 0 deletions examples/migration-8.0/migrate-mariadb.sh
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;"
3 changes: 3 additions & 0 deletions examples/migration-8.0/mysql/mysql-data.sql
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");

0 comments on commit 2fa4337

Please sign in to comment.