setup_replication.sh 1.2 KB

12345678910111213141516171819202122232425262728
  1. #!/bin/bash -ex
  2. until docker exec mysql1 sh -c 'export MYSQL_PWD=mysql1; mysql -u root -e ";"'
  3. do
  4. echo "Waiting for mysql1 database connection..."
  5. sleep 4
  6. done
  7. priv_stmt='CREATE USER "mydb_slave_user"@"%" IDENTIFIED BY "mydb_slave_pwd"; GRANT REPLICATION SLAVE ON *.* TO "mydb_slave_user"@"%"; FLUSH PRIVILEGES;'
  8. docker exec mysql1 sh -c "export MYSQL_PWD=mysql1; mysql -u root -e '$priv_stmt'"
  9. until docker-compose exec mysql2 sh -c 'export MYSQL_PWD=mysql2; mysql -u root -e ";"'
  10. do
  11. echo "Waiting for mysql2 database connection..."
  12. sleep 4
  13. done
  14. MS_STATUS=`docker exec mysql1 sh -c 'export MYSQL_PWD=mysql1; mysql -u root -e "SHOW MASTER STATUS"'`
  15. CURRENT_LOG=`echo $MS_STATUS | awk '{print $6}'`
  16. CURRENT_POS=`echo $MS_STATUS | awk '{print $7}'`
  17. 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;"
  18. start_slave_cmd='export MYSQL_PWD=mysql2; mysql -u root -e "'
  19. start_slave_cmd+="$start_slave_stmt"
  20. start_slave_cmd+='"'
  21. docker exec mysql2 sh -c "$start_slave_cmd"
  22. docker exec mysql2 sh -c "export MYSQL_PWD=mysql2; mysql -u root -e 'SHOW SLAVE STATUS \G'"