-
Notifications
You must be signed in to change notification settings - Fork 443
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
6 changed files
with
118 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 @@ | ||
dump-data/* |
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,81 @@ | ||
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} | ||
MARIADB_DB: ${DB_DATABASE} | ||
MYSQL_CONT_NAME: ${MYSQL_name} | ||
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 | ||
volumes: | ||
- mysqldump:/etc/dump/ | ||
- ./dump-mysql.sh:/docker-entrypoint-initdb.d/dump-mysql.sh | ||
networks: | ||
- backend | ||
|
||
# Sidecar for insert dump file | ||
mariadb-migrated-from-mysql8: | ||
environment: | ||
MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD} | ||
MARIADB_USER: ${DB_USER} | ||
MARIADB_PASSWORD: ${DB_PASSWORD} | ||
MARIADB_DB: ${DB_DATABASE} | ||
container_name: ${MARIADB_MIGRATED_name} | ||
image: mariadb:lts | ||
depends_on: | ||
mariadb-dump: | ||
condition: service_healthy | ||
# restart: true | ||
volumes: | ||
- mysqldump:/etc/dump/ | ||
- ./migrate-mariadb.sh:/docker-entrypoint-initdb.d/migrate-mariadb.sh | ||
networks: | ||
- backend | ||
|
||
volumes: | ||
dbdata: {} | ||
# sudo chown -R 999:999 ${PWD}/dump-data # on host before running this file | ||
mysqldump: | ||
driver: local | ||
driver_opts: | ||
type: none | ||
device: "${PWD}/dump-data" | ||
o: bind | ||
|
||
networks: | ||
backend: |
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,13 @@ | ||
#!/bin/bash | ||
|
||
echo 'MariaDB service started.' | ||
# Run your commands and exit container | ||
whoami # mysql | ||
# sh -c "chown -R mysql:mysql /etc/dump" # Operation permitted | ||
echo 'Dump and compress MySQL data with changed collation ...' | ||
fileName="mysql-dump-data.sql.zst" | ||
if [ -f "$fileName" ]; then | ||
echo "File ${fileName} exists. Remove it ... " | ||
rm "$fileName" | ||
fi | ||
sh -c "mariadb-dump -h${MYSQL_CONT_NAME} -uroot -p${MARIADB_ROOT_PASSWORD} ${MARIADB_DB} | sed 's/utf8mb4_0900/uca1400/g' | zstd > /etc/dump/${fileName}" |
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,13 @@ | ||
#!/bin/bash | ||
echo "Extract file" | ||
oldFile="/etc/dump/mysql-dump.sql" | ||
if [ -f "$oldFile" ]; then | ||
echo "Old file ${oldFile} exists. Remove it ... " | ||
rm "$oldFile" | ||
echo "Extracting ..." | ||
fi | ||
sh -c "zstd -d /etc/dump/mysql-dump-data.sql.zst -o /etc/dump/mysql-dump.sql" | ||
echo "Show data in MariaDB" | ||
mariadb -uroot -p"${MARIADB_ROOT_PASSWORD}" -e "create database testdb;" | ||
mariadb -uroot -p"${MARIADB_ROOT_PASSWORD}" "${MARIADB_DB}" < /etc/dump/mysql-dump.sql | ||
mariadb -uroot -p"${MARIADB_ROOT_PASSWORD}" -e "show databases; select * from testdb.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"); |