From 1b2833d38b6b2e6ce72b89471def34fe99f65fe6 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Thu, 4 Feb 2021 09:10:38 -0500 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82=20?= =?UTF-8?q?=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9=D0=BA=D0=B8=20=D0=B3?= =?UTF-8?q?=D1=80=D1=83=D0=BF=D0=BF=D0=BE=D0=B2=D0=BE=D0=B9=20=D1=80=D0=B5?= =?UTF-8?q?=D0=BF=D0=BB=D0=B8=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=BE=D1=81=D0=BD=D0=BE=D0=B2=D0=B5=20=D0=B4=D0=B5=D1=84=D0=BE?= =?UTF-8?q?=D0=BB=D1=82=D0=BD=D1=8B=D1=85=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=20mysql=205.7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cicd/docker-compose.yml | 47 +++++++++++++++++++------- cicd/init.sh | 59 +++++++++++++++++++++++++++++++++ cicd/mysql/master_init/init.sql | 3 ++ cicd/mysql/mysql_master.conf | 9 +++++ cicd/mysql/mysql_slave.conf | 9 +++++ cicd/mysql/slave_init/init.sql | 2 ++ 6 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 cicd/init.sh create mode 100644 cicd/mysql/master_init/init.sql create mode 100644 cicd/mysql/mysql_master.conf create mode 100644 cicd/mysql/mysql_slave.conf create mode 100644 cicd/mysql/slave_init/init.sql diff --git a/cicd/docker-compose.yml b/cicd/docker-compose.yml index a153b1e..b63f49c 100644 --- a/cicd/docker-compose.yml +++ b/cicd/docker-compose.yml @@ -1,22 +1,45 @@ version: '3' services: - mysql: - image: mysql:5.6 - hostname: "mysql" - container_name: mysql - command: --default-authentication-plugin=mysql_native_password + + mysql_master: + image: mysql:5.7 + hostname: "mysql_master" + container_name: mysql_master restart: always environment: MYSQL_ROOT_PASSWORD: root - MYSQL_DATABASE: app MYSQL_USER: app MYSQL_PASSWORD: app - volumes: - - /opt/mysql:/var/lib/mysql - - ./dbinit:/docker-entrypoint-initdb.d + MYSQL_DATABASE: app + MYSQL_PORT: 3306 + MYSQL_LOWER_CASE_TABLE_NAMES: 0 ports: - - "3306:3306" + - 13306:3306 + volumes: + - ./mysql/mysql_master.conf:/etc/mysql/conf.d/mysql.conf.cnf + - /opt/mysql_master:/var/lib/mysql + - ./mysql/master_init:/docker-entrypoint-initdb.d + mysql_slave: + image: mysql:5.7 + hostname: "mysql_slave" + container_name: mysql_slave + restart: always + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_USER: app + MYSQL_PASSWORD: app + MYSQL_DATABASE: app + MYSQL_PORT: 3306 + MYSQL_LOWER_CASE_TABLE_NAMES: 0 + ports: + - 23306:3306 + depends_on: + - mysql_master + volumes: + - ./mysql/mysql_slave.conf:/etc/mysql/conf.d/mysql.conf.cnf + - /opt/mysql_slave:/var/lib/mysql + - ./mysql/slave_init:/docker-entrypoint-initdb.d app: build: @@ -26,8 +49,8 @@ services: environment: APP_SERVER_ADDRESS: 0.0.0.0 APP_SERVER_PORT: 8080 - APP_DSN_HOST: mysql - APP_DSN_PORT: 3306 + APP_DSN_HOST: mysql_master + APP_DSN_PORT: 13306 APP_DSN_USER: app APP_DSN_PASS: app APP_DSN_BASE: app diff --git a/cicd/init.sh b/cicd/init.sh new file mode 100644 index 0000000..7da191f --- /dev/null +++ b/cicd/init.sh @@ -0,0 +1,59 @@ +#!/bin/bash + + mysql: + image: mysql:5.6 + hostname: "mysql" + container_name: mysql + command: --default-authentication-plugin=mysql_native_password + restart: always + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: app + MYSQL_USER: app + MYSQL_PASSWORD: app + volumes: + - /opt/mysql:/var/lib/mysql + - ./dbinit:/docker-entrypoint-initdb.d + ports: + - "3306:3306" + + + + + +docker-compose down +rm -rf ./master/data/* +rm -rf ./slave/data/* +docker-compose build +docker-compose up -d + +until docker exec mysql_master sh -c 'export MYSQL_PWD=111; mysql -u root -e ";"' +do + echo "Waiting for mysql_master database connection..." + sleep 4 +done + +priv_stmt='GRANT REPLICATION SLAVE ON *.* TO "mydb_slave_user"@"%" IDENTIFIED BY "mydb_slave_pwd"; FLUSH PRIVILEGES;' +docker exec mysql_master sh -c "export MYSQL_PWD=111; mysql -u root -e '$priv_stmt'" + +until docker-compose exec mysql_slave sh -c 'export MYSQL_PWD=111; mysql -u root -e ";"' +do + echo "Waiting for mysql_slave database connection..." + sleep 4 +done + +docker-ip() { + docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$@" +} + +MS_STATUS=`docker exec mysql_master sh -c 'export MYSQL_PWD=111; mysql -u root -e "SHOW MASTER STATUS"'` +CURRENT_LOG=`echo $MS_STATUS | awk '{print $6}'` +CURRENT_POS=`echo $MS_STATUS | awk '{print $7}'` + +start_slave_stmt="CHANGE MASTER TO MASTER_HOST='$(docker-ip mysql_master)',MASTER_USER='mydb_slave_user',MASTER_PASSWORD='mydb_slave_pwd',MASTER_LOG_FILE='$CURRENT_LOG',MASTER_LOG_POS=$CURRENT_POS; START SLAVE;" +start_slave_cmd='export MYSQL_PWD=111; mysql -u root -e "' +start_slave_cmd+="$start_slave_stmt" +start_slave_cmd+='"' +docker exec mysql_slave sh -c "$start_slave_cmd" + +docker exec mysql_slave sh -c "export MYSQL_PWD=111; mysql -u root -e 'SHOW SLAVE STATUS \G'" \ No newline at end of file diff --git a/cicd/mysql/master_init/init.sql b/cicd/mysql/master_init/init.sql new file mode 100644 index 0000000..f1c7130 --- /dev/null +++ b/cicd/mysql/master_init/init.sql @@ -0,0 +1,3 @@ +CREATE DATABASE IF NOT EXISTS `app` CHARACTER SET utf8 COLLATE utf8_general_ci; +GRANT ALL ON `app`.* TO 'app'@'%' identified by 'app'; +GRANT REPLICATION SLAVE ON *.* TO 'app'@'%' identified by 'app'; \ No newline at end of file diff --git a/cicd/mysql/mysql_master.conf b/cicd/mysql/mysql_master.conf new file mode 100644 index 0000000..6280532 --- /dev/null +++ b/cicd/mysql/mysql_master.conf @@ -0,0 +1,9 @@ +[mysqld] + +skip-host-cache +skip-name-resolve + +server-id = 1 +log_bin = /var/log/mysql/mysql-bin.log +binlog_format = ROW +binlog_do_db = mydb \ No newline at end of file diff --git a/cicd/mysql/mysql_slave.conf b/cicd/mysql/mysql_slave.conf new file mode 100644 index 0000000..392f54f --- /dev/null +++ b/cicd/mysql/mysql_slave.conf @@ -0,0 +1,9 @@ +[mysqld] + +skip-host-cache +skip-name-resolve + +server-id=2 +relay-log = /var/log/mysql/mysql-relay-bin.log +log_bin = /var/log/mysql/mysql-bin.log +binlog_do_db = mydb \ No newline at end of file diff --git a/cicd/mysql/slave_init/init.sql b/cicd/mysql/slave_init/init.sql new file mode 100644 index 0000000..4d13934 --- /dev/null +++ b/cicd/mysql/slave_init/init.sql @@ -0,0 +1,2 @@ +CREATE DATABASE IF NOT EXISTS `app` CHARACTER SET utf8 COLLATE utf8_general_ci; +GRANT ALL ON `app`.* TO 'app'@'%' identified by 'app'; \ No newline at end of file