構建高效能的MYSQL資料庫系統-主從複製
阿新 • • 發佈:2021-08-23
構建高效能的MYSQL資料庫系統-主從複製
實驗環境:
DB1:172.16.1.100
DB2:172.16.1.101
VRRIP:172.16.1.99
步驟:
yum -y install mysql
1。修改DB1的mysql配置檔案
1 2 3 4 5 |
server-id = 2 #DB1和DB2的id 必須不同
log-bin=mysql-bin #開啟mysql的二進位制日誌功能< br >read_only=1 #定義主從複製,只讀模式
relay-log = mysql-relay-bin #定義中繼日誌的命名格式
replicate-wild-ignore-table=mysql.% #不復制的資料庫或表 replicate-wild-ignore-table=test.% #不復制的資料庫或表< br >replicate-wild-ignore-table=information_schema.% #不復制的資料庫或表
|
2. 在DB1建立複製使用者並授權
1 |
grant replication slave on *.* to "chen"@"172.16.1.101" identified by "123456";< br >show master status;< br >mysql> show master status;< br >+------------------+----------+--------------+------------------+< br >| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |< br >+------------------+----------+--------------+------------------+< br >| mysql-bin.000006 | 106 | | |< br >+------------------+----------+--------------+------------------+< br >1 row in set (0.00 sec)
|
3.在DB2的mysql庫將DB1設為自己的主伺服器
1 2 3 4 5 6 |
mysql > change master to \
master_host="172.16.1.100",
master_user="chen",
master_password="123456",
master_log_file="mysql-bin.000006",
master_log_pos=106;
|
4.然後執行
1 |
mysql > start slave;
|
5.查詢DB2執行狀態
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 |
mysql > show slave status;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.1.100
Master_User: chen
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 106
Relay_Log_File: mysql-relay-bin.000021
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table: mysql.%,test.%,information_schema.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 106
Relay_Log_Space: 551
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
ERROR:
No query specified
|
6.然後再DB1和DB2配置上面的配置
7.在DB1和DB2上下載keepalived
yum install keepalived
8.配置keepalived的配置檔案
[root@db1 ~]# vim /etc/keepalived/keepalived.conf
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 |
[root@db2 ~]# vim /etc/keepalived/keepalived.conf
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.99
}
}
virtual_server 172.16.1.99 3306 {
delay_loop 2
#lb_algo rr #LVS演算法,用不到,我們就關閉了
#lb_kind DR #LVS模式,如果不關閉,備用伺服器不能通過VIP連線主MySQL
persistence_timeout 50 #同一IP的連線60秒內被分配到同一臺真實伺服器
protocol TCP
real_server 172.16.1.101 3306 { #檢測本地mysql,backup也要寫檢測本地mysql
weight 3
notify_down /usr/local/keepalived/mysql.sh #當mysq服down時,執行此指令碼,殺死
keepalived實現切換
TCP_CHECK {
connect_timeout 3 #連線超時
nb_get_retry 3 #重試次數
delay_before_retry 3 #重試間隔時間
}
}
"/etc/keepalived/keepalived.conf" 40L, 1292C written
[root@db2 ~]# vim /etc/keepalived/keepalived.conf
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.99
}
}
virtual_server 172.16.1.99 3306 {
delay_loop 2
#lb_algo rr #LVS演算法,用不到,我們就關閉了
#lb_kind DR #LVS模式,如果不關閉,備用伺服器不能通過VIP連線主MySQL
persistence_timeout 50 #同一IP的連線60秒內被分配到同一臺真實伺服器
protocol TCP
real_server 172.16.1.101 3306 { #檢測本地mysql,backup也要寫檢測本地mysql
weight 3
notify_down /usr/local/keepalived/mysql.sh #當mysq服down時,執行此指令碼,殺死
keepalived實現切換
TCP_CHECK {
connect_timeout 3 #連線超時
nb_get_retry 3 #重試次數
delay_before_retry 3 #重試間隔時間
}
}
|
8./usr/local/keepalived/mysql.sh
#!/bin/bash
#function:監視mysql主從伺服器狀態
#written by:alec
#
user=mysql
host=172.16.1.150
password=123
#
check_mysql() {
IO=`/usr/local/mysql/bin/mysql -u$user -p$password -h$host -e 'show slave status\G'|grep IO_Running|tr -s ' '|awk '{print $2}'|grep Yes|wc -l`
SQL=`/usr/local/mysql/bin/mysql -u$user -p$password -h$host -e 'show slave status\G'|grep SQL_Running|tr -s ' '|sed -n '1p'|awk '{print $2}'|grep Yes|wc -l`
if [ "$IO" -eq 1 ]&&[ "$SQL" -eq 1 ]
then
service keepalived status|grep pid
if [ $? -eq 0 ]
then
echo "服務已經啟動"
else
service keepalived start
if [ $? -eq 0 ]
then
echo "服務啟動成功"
else
echo "服務啟動失敗"
fi
fi
else
service keepalived status|grep pid
if [ $? -eq 0 ]
then
service keepalived stop
if [ $? -eq 0 ]
then
echo "服務關閉成功"
else
echo "服務關閉失敗"
fi
else
echo "服務已經關閉"
fi
fi
}
main(){
check_mysql
}
main;
systemctl start keepalived
------------------------------------
錯誤一:database doesn't exist' on query. Default database
解決:
1.stop slave;
2.set global sql_slave_skip_counter=1;
3.start slave;