1. 程式人生 > >keeplived + mysql雙主復制部署 --原創

keeplived + mysql雙主復制部署 --原創

gpo oop rep _id init lvs note mon ipaddr

環境:

master 1: 192.168.100.10 oracle linux 7.4 mysql 5.7.1
master 2: 192.168.100.11 oracle linux 7.4 mysql 5.7.1
keepalived VIP 192.168.100.12

配置本地yum倉庫

vim /etc/yum.repo.d/public-yum-ol7.repo

[yum_repo] name=yum_repo baseurl=file:///share/repo gpgcheck=0 enabled=1

安裝mysql

檢查是否已經安裝

rpm -qa | grep mysql

1.mysql安裝包安裝順序 安裝包有依賴關系必須按照順序安裝

  1. mysql-community-common-5.7.21-1.el7.x86_64.rpm
  2. mysql-community-libs-5.7.21-1.el7.x86_64.rpm
  3. mysql-community-client-5.7.21-1.el7.x86_64.rpm
  4. mysql-community-server-5.7.21-1.el7.x86_64.rpm

2.初始化mysql 在初始化完成後會生成一個過期的root密碼,登錄mysql後需要立刻更改,否則無法使用任何命令

rpm -qa | grep mysql

3.啟動mysql

systemctl start mysqld

4.查看mysql root密碼

cat /var/log/mysqld.log | grep password

5.mysql安全設置 (可選項,可以重置root密碼)
  

mysql_secure_installation

6.登錄mysql
  

mysql -u root -p

7.修改root密碼
  

alter user root@% identified by Len#qwer;

8.設置root賬戶永不過期 (可選項)


  

alter user root@% password expire never;

配置雙主

修改mysql配置文件c

master1節點

vim /etc/my.cnf

[mysqld]    
log-bin=mysql-bin     #開啟二進制日誌
server-id=1       #設置server-id 主備庫必須不同
log_slave_updates=1

master2節點

vim /etc/my.cnf

[mysqld]    
log-bin=mysql-bin        #開啟二進制日誌
server-id=2          #設置server-id 主備庫必須不同
log_slave_updates=1

2.重啟mysql,
master1節點

systemctl restart mysqld

master2節點

systemctl restart mysqld

創建同步用戶
master1節點

CREATE USER repl@192.168.100.11 IDENTIFIED BY Phjj#qwer;
GRANT REPLICATION SLAVE ON *.* TO repl@192.168.100.11;


master2節點

CREATE USER repl@192.168.100.10 IDENTIFIED BY Phjj#qwer;
GRANT REPLICATION SLAVE ON *.* TO repl@192.168.100.10;

查看兩個節點的binlog文件名稱和post位置,用於配置復制

master1節點

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      619 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

master2節點

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |     1175 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

master1節點

start slave;

master2節點

start slave;

配置復制

master1節點

CHANGE MASTER TO
MASTER_HOST=192.168.100.11,
MASTER_USER=repl,
MASTER_PASSWORD=Phjj#qwer,
MASTER_LOG_FILE=mysql-bin.000002,
MASTER_LOG_POS=1175;

master2節點

CHANGE MASTER TO
MASTER_HOST=192.168.100.10,
MASTER_USER=repl,
MASTER_PASSWORD=Phjj#qwer,
MASTER_LOG_FILE=mysql-bin.000004,
MASTER_LOG_POS=619;

master1啟動復制

start slave;

master2啟動復制

start slave;

查看復制狀態 Slave_IO_Running,Slave_SQL_Running 狀態必須為running才為正常

mysql> show slave status\G;


Slave_IO_Running: Yes
Slave_SQL_Running: Yes

安裝Keepalived

yum install -y keepalived

配置keepalived

master1節點

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
# vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.12/24
}
}

virtual_server 192.168.100.12 3306 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP


real_server 192.168.100.10 3306 {
weight 3
notify_down /etc/keepalived/kill_keepalived.sh

TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306

}
}
}

master2節點

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]    
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
# vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.12/24
}
}

virtual_server 192.168.100.12 3306 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP


real_server 192.168.100.11 3306 {
weight 3
notify_down /etc/keepalived/kill_keepalived.sh

TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306

}
}
}

配置關閉keepalived腳本

vim /etc/keepalived/kill_keepalived.sh

#!/bin/bash
pkill keepalived

添加執行權限給 關閉keepalived腳本

chmod +x /etc/keepalived/kill_keepalived.sh

查看keepalived VIP地址

ip addr 

UUID相同導致mysql無法進行復制

故障現象

vim /var/log/mysqld.log

2018-02-12T02:13:14.499315Z 7 [Note] Slave SQL thread for channel ‘‘ initialized, starting replication in log mysql-bin.000001 at position 619, relay log ./ajiamysql1-relay-bin.000001 position: 4
2018-02-12T02:13:14.499839Z 6 [Note] Slave I/O thread for channel ‘‘: connected to master [email protected]:3306,replication started in log mysql-bin.000001 at position 619
2018-02-12T02:13:14.501900Z 6 [ERROR] Slave I/O for channel ‘‘: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Error_code: 1593

故障原因:直接復制已經安裝好mysql的虛擬機導致mysql UUID相同無法進行復制連接   註意:UUID和server_id一樣不能相同

故障解決: 修改mysql UUID

[root@ajiamysql1 mysql]# vim /var/lib/mysql/auto.cnf

[auto]
server-uuid=6fe77f0a-eae1-11e7-805e-005056b309100

keeplived + mysql雙主復制部署 --原創