-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_runner.sh
54 lines (39 loc) · 2.59 KB
/
mysql_runner.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
BASE_PATH=$(dirname $0)
echo "+---------Waiting for MySQL containers to start"
sleep 60
echo "+---------Create replication user"
mysql --host slave -uroot -p$MYSQL_SLAVE_PASSWORD -AN -e 'STOP SLAVE;';
mysql --host slave -uroot -p$MYSQL_MASTER_PASSWORD -AN -e 'RESET SLAVE ALL;';
mysql --host master -uroot -p$MYSQL_MASTER_PASSWORD -AN -e "CREATE USER '$MYSQL_REPLICATION_USER'@'%';"
mysql --host master -uroot -p$MYSQL_MASTER_PASSWORD -AN -e "GRANT REPLICATION SLAVE ON *.* TO '$MYSQL_REPLICATION_USER'@'%' IDENTIFIED BY '$MYSQL_REPLICATION_PASSWORD';"
mysql --host master -uroot -p$MYSQL_MASTER_PASSWORD -AN -e 'flush privileges;'
echo "+---------Set MySQL01 as master on MySQL02"
MYSQL01_Position=$(eval "mysql --host master -uroot -p$MYSQL_MASTER_PASSWORD -e 'show master status \G' | grep Position | sed -n -e 's/^.*: //p'")
MYSQL01_File=$(eval "mysql --host master -uroot -p$MYSQL_MASTER_PASSWORD -e 'show master status \G' | grep File | sed -n -e 's/^.*: //p'")
MASTER_IP=$(eval "getent hosts master|awk '{print \$1}'")
echo $MASTER_IP
mysql --host slave -uroot -p$MYSQL_SLAVE_PASSWORD -AN -e "CHANGE MASTER TO master_host='master', master_port=3306, \
master_user='$MYSQL_REPLICATION_USER', master_password='$MYSQL_REPLICATION_PASSWORD', master_log_file='$MYSQL01_File', \
master_log_pos=$MYSQL01_Position;"
echo "+---------Set MySQL02 as master on MySQL01"
MYSQL02_Position=$(eval "mysql --host slave -uroot -p$MYSQL_SLAVE_PASSWORD -e 'show master status \G' | grep Position | sed -n -e 's/^.*: //p'")
MYSQL02_File=$(eval "mysql --host slave -uroot -p$MYSQL_SLAVE_PASSWORD -e 'show master status \G' | grep File | sed -n -e 's/^.*: //p'")
SLAVE_IP=$(eval "getent hosts slave|awk '{print \$1}'")
echo $SLAVE_IP
mysql --host master -uroot -p$MYSQL_MASTER_PASSWORD -AN -e "CHANGE MASTER TO master_host='slave', master_port=3306, \
master_user='$MYSQL_REPLICATION_USER', master_password='$MYSQL_REPLICATION_PASSWORD', master_log_file='$MYSQL02_File', \
master_log_pos=$MYSQL02_Position;"
echo "+---------Start Slave on both Servers"
mysql --host slave -uroot -p$MYSQL_SLAVE_PASSWORD -AN -e "start slave;"
echo "Increase the max_connections to 2000"
mysql --host master -uroot -p$MYSQL_MASTER_PASSWORD -AN -e 'set GLOBAL max_connections=2000';
mysql --host slave -uroot -p$MYSQL_SLAVE_PASSWORD -AN -e 'set GLOBAL max_connections=2000';
mysql --host slave -uroot -p$MYSQL_MASTER_PASSWORD -e "show slave status \G"
echo "MySQL servers created!"
echo "--------------------"
echo
echo Variables available fo you :-
echo
echo MYSQL01_IP : master
echo MYSQL02_IP : slave