安裝環境mysql
[root@localhost ~]# uname -a Linux localhost 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
mysql安裝過程不作描述,這裏安裝的版本是sql
Server version: 5.7.18 MySQL Community Server (GPL)
總體的實驗架構架構
Mysql雙主複製ide
新建repl_user用戶oop
create user 'repl_user'@'%' identified by 'Repl@123456';
受權測試
grant all on *.* to 'repl_user'@'%';
修改182的/etc/my.cnf文件,添加以下的logbin和replicate配置spa
[mysqld] log-bin=mysql-bin relay-log=mysql-relay-bin server-id=1 replicate-wild-ignore-table=mysql.% replicate-wild-ignore-table=test.% replicate-wild-ignore-table=information_schema.%
修改183的/etc/my.cnf文件,添加以下的logbin和replicate配置rest
[mysqld] log-bin=mysql-bin relay-log=mysql-relay-bin server-id=2 replicate-wild-ignore-table=mysql.% replicate-wild-ignore-table=test.% replicate-wild-ignore-table=information_schema.%
推薦在主從庫上使用replicate-wild-ignore-table和replicate_wild_do_table兩個選項來解決複製過濾的問題。日誌
同步配置前,保證數據的同步一致,能夠的操做是導出code
配置182爲186的主
登錄182,執行以下命令
mysql> show master status\G *************************** 1. row *************************** File: mysql-bin.000001 Position: 1870 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)
登陸186
#設置182爲186的master,master_log_file和master_log_pos的值取自182上獲取的值
mysql> change master to master_host='192.168.1.182',master_user='repl_user',master_password='Repl@123456',master_log_file='mysql-bin.000001',master_log_pos=1870;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
#啓動slave mysql> start slave; Query OK, 0 rows affected (0.00 sec)
#查看slave的狀態 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.182 Master_User: repl_user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 1870 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 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: 1870 Relay_Log_Space: 527 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: 8c57082a-b0e7-11e7-b51a-000c29d77e66 Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
配置186爲182的主,過程和設置182爲186的主同樣的邏輯,不在熬述
mysql> change master to master_host='192.168.1.183',master_user='repl_user',master_password='Repl@123456',master_log_file='mysql-bin.000001',master_log_pos=154; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.183 Master_User: repl_user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 154 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 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: 154 Relay_Log_Space: 527 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 2 Master_UUID: 1dcdd77b-b26c-11e7-a821-000c2965ea8c Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
測試驗證,mysql雙主複製正常運行
182機器上keepalived
yum install -y keepalived
安裝後修改/etc/keepalived/keepalived.conf
global_defs { notification_email { *********@qq.com } smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id Mysql_lvs } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.160/24 brd 192.168.1.255 dev ens33 } } virtual_server 192.168.1.160 3306 { delay_loop 2 lb_algo wrr lb_kind NAT persistence_timeout 50 protocol TCP real_server 192.168.1.182 3306 { weight 2 TCP_CHECK { connect_timeout 10 #10秒無響應超時 delay_before_retry 3 connect_port 3306 } } }
啓動keepalived
systemctl start keepalived
183機器上安裝keepalived
yum install -y keepalived
修改keepalived配置文件
! Configuration File for keepalived global_defs { notification_email { ***********@qq.com } smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id Mysql_lvs } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 50 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.160/24 brd 192.168.1.255 dev ens33 } } virtual_server 192.168.1.160 3306 { delay_loop 2 lb_algo wrr lb_kind NAT persistence_timeout 50 protocol TCP real_server 192.168.1.183 3306 { weight 2 TCP_CHECK { connect_timeout 10 #10秒無響應超時 delay_before_retry 3 connect_port 3306 } } }
啓動keepalived
systemctl restart keepalived
驗證keepalived啓動成功
[root@localhost keepalived]# ping 192.168.1.160 PING 192.168.1.160 (192.168.1.160) 56(84) bytes of data. 64 bytes from 192.168.1.160: icmp_seq=1 ttl=64 time=0.275 ms 64 bytes from 192.168.1.160: icmp_seq=2 ttl=64 time=1.34 ms 64 bytes from 192.168.1.160: icmp_seq=3 ttl=64 time=4.20 ms 64 bytes from 192.168.1.160: icmp_seq=4 ttl=64 time=5.58 ms 64 bytes from 192.168.1.160: icmp_seq=5 ttl=64 time=4.47 ms ^C --- 192.168.1.160 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4029ms rtt min/avg/max/mdev = 0.275/3.175/5.587/2.016 ms
用mysql workbench連接mysql,驗證mysql功能正確性,驗證經過
關於keepalived日誌的問題,keepalived的日誌經過rsyslog記錄,默認是記錄的/var/log/messages系統日誌目錄,修改keepalived的日誌須要修改兩個地方
1 修改keepalived的啓動參數,將/etc/sysconfig/keepalived修改成以下
# Options for keepalived. See `keepalived --help' output and keepalived(8) and # keepalived.conf(5) man pages for a list of all options. Here are the most # common ones : # # --vrrp -P Only run with VRRP subsystem. # --check -C Only run with Health-checker subsystem. # --dont-release-vrrp -V Dont remove VRRP VIPs & VROUTEs on daemon stop. # --dont-release-ipvs -I Dont remove IPVS topology on daemon stop. # --dump-conf -d Dump the configuration data. # --log-detail -D Detailed log messages. # --log-facility -S 0-7 Set local syslog facility (default=LOG_DAEMON) KEEPALIVED_OPTIONS="-D -d -S 0"
2 修改rsyslog的配置文件 /etc/rsyslog.conf,添加以下紅色部分
# Save boot messages also to boot.log local7.* /var/log/boot.log # keepalived -S 0 local0.* /var/log/keepalived.log
3 配置後重啓rsyslog和keepalived服務
#重啓keepalived
systemctl restart keepalived
#重啓rsyslog服務
systemctl restart rsyslog
完成配置後keepalived的日誌將被單獨輸出到文件/var/log/keepalived.log文件中