Explorar o código

working replication setup

Chris Mague %!s(int64=2) %!d(string=hai) anos
pai
achega
464ec8d473
Modificáronse 5 ficheiros con 62 adicións e 0 borrados
  1. 11 0
      README.md
  2. 6 0
      docker-compose.yml
  3. 9 0
      master.cnf
  4. 8 0
      replica.cnf
  5. 28 0
      setup_replication.sh

+ 11 - 0
README.md

@@ -1,3 +1,14 @@
+## Starting up
+
+```
+docker-compose rm --force
+docker-compose up
+
+# Wait
+./setup_replication.sh
+
+```
+
 ## Checking the ProxySQL
 
 ```

+ 6 - 0
docker-compose.yml

@@ -7,6 +7,8 @@ services:
     image: "mysql"
     ports:
       - "3307:3307"
+    volumes: 
+      - ./master.cnf:/etc/mysql/conf.d/mysql.conf.cnf
     environment:
       - MYSQL_ROOT_PASSWORD=mysql1
       - MYSQL_TCP_PORT=3307
@@ -18,12 +20,16 @@ services:
     image: "mysql"
     ports:
       - "3308:3308"
+    volumes: 
+      - ./replica.cnf:/etc/mysql/conf.d/mysql.conf.cnf
     environment:
       - MYSQL_ROOT_PASSWORD=mysql2
       - MYSQL_TCP_PORT=3308
       - MYSQL_USER=monitoruser
       - MYSQL_PASSWORD=monitorpass
       - MYSQL_DATABASE=application
+    links: 
+      - "mysql1:mysql1"
   mysqlproxy:
     container_name: mysqlproxy
     image: "proxysql/proxysql"

+ 9 - 0
master.cnf

@@ -0,0 +1,9 @@
+[mysqld]
+
+skip-name-resolve
+default_authentication_plugin = mysql_native_password
+
+server-id = 1
+log_bin = 1
+binlog_format = ROW
+binlog_do_db = mydb

+ 8 - 0
replica.cnf

@@ -0,0 +1,8 @@
+[mysqld]
+
+skip-name-resolve
+default_authentication_plugin = mysql_native_password
+
+server-id = 2
+log_bin = 1
+binlog_do_db = mydb

+ 28 - 0
setup_replication.sh

@@ -0,0 +1,28 @@
+#!/bin/bash -ex
+
+until docker exec mysql1 sh -c 'export MYSQL_PWD=mysql1; mysql -u root -e ";"'
+do
+    echo "Waiting for mysql1 database connection..."
+    sleep 4
+done
+
+priv_stmt='CREATE USER "mydb_slave_user"@"%" IDENTIFIED BY "mydb_slave_pwd"; GRANT REPLICATION SLAVE ON *.* TO "mydb_slave_user"@"%"; FLUSH PRIVILEGES;'
+docker exec mysql1 sh -c "export MYSQL_PWD=mysql1; mysql -u root -e '$priv_stmt'"
+
+until docker-compose exec mysql2 sh -c 'export MYSQL_PWD=mysql2; mysql -u root -e ";"'
+do
+    echo "Waiting for mysql2 database connection..."
+    sleep 4
+done
+
+MS_STATUS=`docker exec mysql1 sh -c 'export MYSQL_PWD=mysql1; 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='mysql1',MASTER_PORT=3307,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=mysql2; mysql -u root -e "'
+start_slave_cmd+="$start_slave_stmt"
+start_slave_cmd+='"'
+docker exec mysql2 sh -c "$start_slave_cmd"
+
+docker exec mysql2 sh -c "export MYSQL_PWD=mysql2; mysql -u root -e 'SHOW SLAVE STATUS \G'"