diff --git a/cicd/docker-compose.yml b/cicd/docker-compose.yml index b63f49c..225f6d6 100644 --- a/cicd/docker-compose.yml +++ b/cicd/docker-compose.yml @@ -2,7 +2,7 @@ version: '3' services: mysql_master: - image: mysql:5.7 + image: actency/docker-mysql-replication:5.7 hostname: "mysql_master" container_name: mysql_master restart: always @@ -11,17 +11,14 @@ services: MYSQL_USER: app MYSQL_PASSWORD: app MYSQL_DATABASE: app - MYSQL_PORT: 3306 - MYSQL_LOWER_CASE_TABLE_NAMES: 0 - ports: - - 13306:3306 + REPLICATION_USER: replication_user + REPLICATION_PASSWORD: myreplpassword 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 + image: actency/docker-mysql-replication:5.7 hostname: "mysql_slave" container_name: mysql_slave restart: always @@ -30,16 +27,13 @@ services: MYSQL_USER: app MYSQL_PASSWORD: app MYSQL_DATABASE: app - MYSQL_PORT: 3306 - MYSQL_LOWER_CASE_TABLE_NAMES: 0 - ports: - - 23306:3306 + REPLICATION_USER: replication_user + REPLICATION_PASSWORD: myreplpassword + MASTER_HOST: mysql_master 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: diff --git a/cicd/init.sh b/cicd/init.sh deleted file mode 100644 index 7da191f..0000000 --- a/cicd/init.sh +++ /dev/null @@ -1,59 +0,0 @@ -#!/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 deleted file mode 100644 index f1c7130..0000000 --- a/cicd/mysql/master_init/init.sql +++ /dev/null @@ -1,3 +0,0 @@ -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 deleted file mode 100644 index 6280532..0000000 --- a/cicd/mysql/mysql_master.conf +++ /dev/null @@ -1,9 +0,0 @@ -[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 deleted file mode 100644 index 392f54f..0000000 --- a/cicd/mysql/mysql_slave.conf +++ /dev/null @@ -1,9 +0,0 @@ -[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 deleted file mode 100644 index 4d13934..0000000 --- a/cicd/mysql/slave_init/init.sql +++ /dev/null @@ -1,2 +0,0 @@ -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