1. 程式人生 > >mysql5.7.24 gtid雙主復制+atlas+keepalived

mysql5.7.24 gtid雙主復制+atlas+keepalived

ola listen community ofo 優先 單點 removing 5.6 4.2

一環境介紹:

系統:

centos7

[root@mgr01 ~]# cat /etc/hosts
10.0.0.6 pxc01
10.0.0.7  pxc02
[root@pxc02 ~]# cat /etc/hosts
10.0.0.6 pxc01
10.0.0.7 pxc02

關閉selinux:
vim /etc/sysconfig/selinux
getenforce

保證服務器時間同步:

ntpdate ntp1.aliyun.com
*/5 * * * *  ntpdate ntp1.aliyun.com

關閉防火墻:

 systemctl status firewalld
systemctl stop firewalld
 systemctl stop iptables.service
 systemctl status iptables.service

關閉防火墻服務開機自啟動:

systemctl disable firewalld.service
systemctl disable iptables.service

關閉mysql服務開機自啟動:

[root@mgr01 ~]# systemctl disable mysql.service
mysql.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mysql off

二、環境部署說明

10.0.0.6 部署mysql5.7.24 gtid雙主復制+atlas+keepalived

10.0.0.7 部署mysql5.7.24 gtid雙主復制+atlas+keepalived

2.1MySQL安裝:

mysql 版本:MySQL5.7.24
10.0.0.6 pxc01 操作:

**MySQL采用二進制安裝以及安裝過程如下:**
useradd mysql -s /sbin/nologin -M
mkdir /data/mysql/mysql3306/{data,logs,binlog} -p
chown -R mysql.mysql  /data/mysql/mysql3306
tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
cd /usr/local/mysql
mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf --initialize
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf &

**修改mysql密碼:**
alter user user() identified by ‘654321‘;

10.0.0.7 pxc02 操作:
10.0.0.7 機器也按照上面的方式安裝mysql

2.2、mysql的配置文件介紹:

10.0.0.6 pxc01 主配置文件如下:
下面的參數是必須打開的:

[root@pxc01 mysql3306]# egrep ‘server_id|gtid_mode|enforce_gtid_consistency|log_bin|binlog_format|log-slave-updates|skip_slave_start|auto-increment‘ /data/mysql/mysql3306/my3306.cnf 
server_id                           =63306                        # 0
binlog_format                       =row                          # row
log_bin                             =/data/mysql/mysql3306/binlog/mysql-bin        #    off
gtid_mode                           =on                            #    off
enforce_gtid_consistency            =on                            #    off
skip_slave_start                     =1                              #
auto-increment-increment = 2
auto-increment-offset = 1

10.0.0.7 master02 主配置文件如下:
下面的參數是必須打開的:

[root@pxc02 local]# egrep ‘server_id|gtid_mode|enforce_gtid_consistency|log_bin|binlog_format|log-slave-updates|skip_slave_start|auto-increment‘ /data/mysql/mysql3306/my3306.cnf 
server_id                           =73306                        # 0
binlog_format                       =row                          # row
log_bin                             =/data/mysql/mysql3306/binlog/mysql-bin      #  off
gtid_mode                           =on                            #    off
enforce_gtid_consistency            =on                            #    off
skip_slave_start                     =1                              #
auto-increment-increment = 2
auto-increment-offset = 2

2.3、配置主從復制

10.0.0.6 pxc01 上操作:

配置復制用戶權限
grant replication slave on *.* to rep1@‘10.0.0.%‘ identified by ‘rep123321‘;flush privileges;

master02上chang master   to 來配置從庫:
 change master to master_host=‘10.0.0.6‘,master_user=‘rep1‘,master_password=‘rep123321‘,master_auto_position=1;start slave ; show slave status\G

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10.0.0.7 pxc02上操作:

配置復制用戶權限
grant replication slave on *.* to rep2@‘10.0.0.%‘ identified by ‘rep123321‘;flush privileges;

master01上chang master   to 來配置從庫:
 change master to master_host=‘10.0.0.7‘,master_user=‘rep2‘,master_password=‘rep123321‘,master_auto_position=1;start slave ; show slave status\G

配置完成

2.4、安裝atlas:

10.0.0.6 機器上安裝atlas:

 [root@mgr01 ~]# wget    https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm
[root@mgr01 ~]# yum  -y localinstall Atlas-2.2.1.el6.x86_64.rpm
或者  rpm -ivh Atlas-2.2.1.el6.x86_64.rpm 

登錄10.0.0.6 機器上登錄mysql創建用戶,會自動同步到10.0.0.7上:

 grant all on *.* to atlasuser@‘10.0.0.%‘ identified by ‘558996‘;flush privileges;

給用戶密碼加密:

[root@pxc01 ~]# /usr/local/mysql-proxy/bin/encrypt  558996
Otp11P90TDw=
[root@pxc01 ~]# 

加密後的密碼下入Atlas的配置文件中:

[root@pxc01 ~]# grep atlasuser /usr/local/mysql-proxy/conf/mgrmul.cnf 
pwds = atlasuser:Otp11P90TDw=
[root@pxc01 ~]# 

設置允許客戶端的ip連接Atlas:

[root@pxc01 ~]# grep client-ips  /usr/local/mysql-proxy/conf/mgrmul.cnf
client-ips = 127.0.0.1, 192.168.1, 10.0.0.20, 10.0.0.6
#Atlas前面掛接的LVS的物理網卡的IP(註意不是虛IP),若有LVS且設置了client-ips則此項必須設置,否則可以不設置。
此處的10.0.0.20 為後面的演示的VIP

10.0.0.6 atlas:配置文件內容如下:


[root@pxc01 ~]# cat /usr/local/mysql-proxy/conf/mgrmul.cnf 
[mysql-proxy]

#帶#號的為非必需的配置項目

#管理接口的用戶名
admin-username = zykjwtest

#管理接口的密碼
admin-password = zykjwtest01

#Atlas後端連接的MySQL主庫的IP和端口,可設置多項,用逗號分隔
proxy-backend-addresses=10.0.0.6:3306,10.0.0.7:3306
#Atlas後端連接的MySQL從庫的IP和端口,@後面的數字代表權重,用來作負載均衡,若省略則默認為1,可設置多項,用逗號分隔
proxy-read-only-backend-addresses = 10.0.0.6:3306@1,10.0.0.7:3306

#用戶名與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的用戶名和加密密碼!
pwds = atlasuser:Otp11P90TDw=

#設置Atlas的運行方式,設為true時為守護進程方式,設為false時為前臺方式,一般開發調試時設為false,線上運行時設為true,true後面不能有空格。
daemon = true

#設置Atlas的運行方式,設為true時Atlas會啟動兩個進程,一個為monitor,一個為worker,monitor在worker意外退出後會自動將其重啟,設為false時只有worker,沒有monitor,一般開發調試時設為false,線上運行時設為true,true後面不能有空格。
keepalive = true

#工作線程數,對Atlas的性能有很大影響,可根據情況適當設置
event-threads = 4

#日誌級別,分為message、warning、critical、error、debug五個級別
log-level = warning

#日誌存放的路徑
log-path = /usr/local/mysql-proxy/log

#SQL日誌的開關,可設置為OFF、ON、REALTIME,OFF代表不記錄SQL日誌,ON代表記錄SQL日誌,REALTIME代表記錄SQL日誌且實時寫入磁盤,默認為OFF
sql-log = REALTIME

#慢日誌輸出設置。當設置了該參數時,則日誌只輸出執行時間超過sql-log-slow(單位:ms)的日誌記錄。不設置該參數則輸出全部日誌。
#sql-log-slow = 10

#實例名稱,用於同一臺機器上多個Atlas實例間的區分
instance = mgrmul

#Atlas監聽的工作接口IP和端口
proxy-address = 0.0.0.0:52119

#Atlas監聽的管理接口IP和端口
admin-address = 0.0.0.0:52118

#分表設置,此例中person為庫名,mt為表名,id為分表字段,3為子表數量,可設置多項,以逗號分隔,若不分表則不需要設置該項
#tables = person.mt.id.3

#默認字符集,設置該項後客戶端不再需要執行SET NAMES語句
#charset = utf8

#允許連接Atlas的客戶端的IP,可以是精確IP,也可以是IP段,以逗號分隔,若不設置該項則允許所有IP連接,否則只允許列表中的IP連接
client-ips = 127.0.0.1, 192.168.1, 10.0.0.20, 10.0.0.6

#Atlas前面掛接的LVS的物理網卡的IP(註意不是虛IP),若有LVS且設置了client-ips則此項必須設置,否則可以不設置
#lvs-ips = 127.0.0.1, 10.0.0.20, 10.0.0.6

Atlas啟動和關閉:

/usr/local/mysql-proxy/bin/mysql-proxyd mgrmul start
/usr/local/mysql-proxy/bin/mysql-proxyd mgrmul stop
/usr/local/mysql-proxy/bin/mysql-proxyd mgrmul status
/usr/local/mysql-proxy/bin/mysql-proxyd mgrmul restart

啟動成功:

[root@pxc01 log]# ss -lntup|grep mysql-proxy
tcp    LISTEN     0      128       *:52118                 *:*                   users:(("mysql-proxy",pid=10543,fd=9))
tcp    LISTEN     0      128       *:52119                 *:*                   users:(("mysql-proxy",pid=10543,fd=10))

卸載掉atlas:

[root@mgr02 local]# rpm -qa|grep Atlas 
Atlas-2.2.1-1.x86_64
[root@mgr01 local]# rpm -e --nodeps Atlas-2.2.1-1.x86_64 
[root@mgr01 local]# rpm -qa|grep Atlas 
[root@mgr01 local]#

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10.0.0.7機器上安裝atlas:

若果只是單獨的在10.0.0.6 安裝atlas 服務,避免不了單點故障,一旦10.0.0.6 上的atlas服務掛掉了,或者是10.0.0.6 機器掛掉了,整個業務都會中斷。於是在10.0.0.7 上安裝atlas服務並引入keepalived 來實現高可用

於是按照10.0.0.6 機器上安裝atlas的步驟在10.0.0.7 機器安裝atlas服務
按照上述過程在10.0.0.7 上執行一遍,安裝atlas服務,並開啟。

配置文件說明:

設置允許客戶端的ip連接Atlas:
[root@pxc02 ~]# grep client-ips  /usr/local/mysql-proxy/conf/mgrmul.cnf
client-ips = 127.0.0.1, 192.168.1, 10.0.0.20, 10.0.0.7
#Atlas前面掛接的LVS的物理網卡的IP(註意不是虛IP),若有LVS且設置了client-ips則此項必須設置,否則可以不設置

10.0.0.6 的配置文件和10.0.0.7的配置文件只有此處IP不一樣,需要修改為本地IP,其余都是一樣的參數

2.4、測試演示atlas:

+++++++++++++++++++在10.0.0.6 上測試+++++++++++++++++

登錄後端的mysql server:

[root@pxc01 log]# mysql -uatlasuser -p‘558996‘ -h10.0.0.6 -P52119
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.81-log MySQL Community Server (GPL)

Copyright (c) 2009-2018 Percona LLC and/or its affiliates
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

(atlasuser@‘mgr01‘:52119)[(none)]>show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test01             |
| test02             |
+--------------------+
6 rows in set (0.00 sec)

寫入測試數據演示:


use test01;
CREATE TABLE `test01` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `titles` char(15) NOT NULL,
  `icon` smallint(6) unsigned DEFAULT ‘0‘,
  `integral` int(10) NOT NULL DEFAULT ‘0‘,
  `isdefault` tinyint(1) unsigned NOT NULL DEFAULT ‘0‘,
`create_time` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `integral` (`integral`)
) ENGINE=Innodb AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
insert into test01.test01 values(1,‘列兵‘,1,0,1,now());

查看sql日誌:

[root@pxc01 log]# tailf /usr/local/mysql-proxy/log/sql_mgrmul.log 
[03/02/2019 18:56:51] C:10.0.0.6:58637 S:10.0.0.7:3306 OK 26.275 "select @@version_comment limit 1"
[03/02/2019 18:56:51] C:10.0.0.6:58637 S:10.0.0.6:3306 OK 21.822 "select USER()"
[03/02/2019 18:57:00] C:10.0.0.6:58637 S:10.0.0.7:3306 OK 6.994 "select @@port"
[03/02/2019 18:57:04] C:10.0.0.6:58637 S:10.0.0.6:3306 OK 2.230 "select @@port"
[03/02/2019 18:57:18] C:10.0.0.6:58637 S:10.0.0.7:3306 OK 3.958 "select @@hostname"
[03/02/2019 18:57:20] C:10.0.0.6:58637 S:10.0.0.6:3306 OK 0.937 "select @@hostname"
[03/02/2019 18:57:22] C:10.0.0.6:58637 S:10.0.0.7:3306 OK 3.855 "select @@hostname"
[03/02/2019 19:02:08] C:10.0.0.6:58637 S:10.0.0.6:3306 OK 2.456 "SELECT DATABASE()"
[03/02/2019 19:02:08] C:10.0.0.6:58637 S:10.0.0.6:3306 OK 0.807 "show databases"
[03/02/2019 19:02:08] C:10.0.0.6:58637 S:10.0.0.7:3306 OK 86.643 "show tables"
[03/02/2019 19:02:10] C:10.0.0.6:58637 S:10.0.0.6:3306 OK 377.687 "CREATE TABLE `test01` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `titles` char(15) NOT NULL,
  `icon` smallint(6) unsigned DEFAULT ‘0‘,
  `integral` int(10) NOT NULL DEFAULT ‘0‘,
  `isdefault` tinyint(1) unsigned NOT NULL DEFAULT ‘0‘,
`create_time` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `integral` (`integral`)
) ENGINE=Innodb AUTO_INCREMENT=0 DEFAULT CHARSET=utf8"
[03/02/2019 19:02:18] C:10.0.0.6:58637 S:10.0.0.6:3306 OK 425.538 "insert into test01.test01 values(1,‘列兵‘,1,0,1,now())"
[03/02/2019 19:02:36] C:10.0.0.6:58637 S:10.0.0.6:3306 OK 2.272 "select * from test01.test01"
[03/02/2019 19:02:39] C:10.0.0.6:58637 S:10.0.0.6:3306 OK 2.337 "select * from test01.test01"
[03/02/2019 19:02:55] C:10.0.0.6:58637 S:10.0.0.7:3306 OK 70.055 "select * from test01.test01"

讀寫分離成功

登錄atlas的管理端:

[root@pxc01 log]# mysql -uzykjwtest -pzykjwtest01 -h127.0.0.1 -P52118

查看atlas的操作命令:


(zykjwtest@‘mgr01‘:52118)[(none)]>SELECT * FROM help;
+----------------------------+---------------------------------------------------------+
| command                    | description                                             |
+----------------------------+---------------------------------------------------------+
| SELECT * FROM help         | shows this help                                         |
| SELECT * FROM backends     | lists the backends and their state                      |
| SET OFFLINE $backend_id    | offline backend server, $backend_id is backend_ndx‘s id |
| SET ONLINE $backend_id     | online backend server, ...                              |
| ADD MASTER $backend        | example: "add master 127.0.0.1:3306", ...               |
| ADD SLAVE $backend         | example: "add slave 127.0.0.1:3306", ...                |
| REMOVE BACKEND $backend_id | example: "remove backend 1", ...                        |
| SELECT * FROM clients      | lists the clients                                       |
| ADD CLIENT $client         | example: "add client 192.168.1.2", ...                  |
| REMOVE CLIENT $client      | example: "remove client 192.168.1.2", ...               |
| SELECT * FROM pwds         | lists the pwds                                          |
| ADD PWD $pwd               | example: "add pwd user:raw_password", ...               |
| ADD ENPWD $pwd             | example: "add enpwd user:encrypted_password", ...       |
| REMOVE PWD $pwd            | example: "remove pwd user", ...                         |
| SAVE CONFIG                | save the backends to config file                        |
| SELECT VERSION             | display the version of Atlas                            |
+----------------------------+---------------------------------------------------------+
16 rows in set (0.00 sec)

查看當前節點mysql在線狀態和讀寫狀態:

(zykjwtest@‘mgr01‘:52118)[(none)]>SELECT * FROM backends;
+-------------+---------------+-------+------+
| backend_ndx | address       | state | type |
+-------------+---------------+-------+------+
|           1 | 10.0.0.6:3306 | up    | rw   |
|           2 | 10.0.0.7:3306 | up    | rw   |
|           3 | 10.0.0.6:3306 | up    | ro   |
|           4 | 10.0.0.7:3306 | up    | ro   |
+-------------+---------------+-------+------+
4 rows in set (0.00 sec)

++++++++++++++++++++++++在10.0.0.7 上測試++++++++++++++++++++++++++
按照10.0.0.6 機器上安裝atlas的步驟在10.0.0.7 機器安裝atlas服務
並且啟動atlas

[root@pxc02 conf]# /usr/local/mysql-proxy/bin/mysql-proxyd mgrmul start
OK: MySQL-Proxy of mgrmul is started
[root@pxc02 conf]# 
[root@pxc02 conf]# 
[root@pxc02 conf]# ps -ef|grep mysql-proxy
root       9761      1  0 19:35 ?        00:00:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mgrmul.cnf
root       9762   9761 43 19:35 ?        00:00:03 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mgrmul.cnf
root       9770   7701  0 19:35 pts/3    00:00:00 grep --color=auto mysql-proxy
[root@pxc02 conf]# ss -lntup|grep mysql-proxy
tcp    LISTEN     0      128       *:52118                 *:*                   users:(("mysql-proxy",pid=9762,fd=9))
tcp    LISTEN     0      128       *:52119                 *:*                   users:(("mysql-proxy",pid=9762,fd=10))
[root@pxc02 conf]# 

測試10.0.0.7 上的atlas服務,經測試可以正常寫入和查詢


[root@pxc02 conf]# mysql -uatlasuser -p‘558996‘ -h10.0.0.7 -P52119
(atlasuser@‘mgr02‘:52119)[(none)]>insert into test01.test01 values(3,‘排長‘,3,2000,1,now());
Query OK, 1 row affected (0.10 sec)
(atlasuser@‘mgr02‘:52119)[(none)]>select * from test01.test01;
+----+--------+------+----------+-----------+---------------------+
| id | titles | icon | integral | isdefault | create_time         |
+----+--------+------+----------+-----------+---------------------+
|  1 | 列兵   |    1 |        0 |         1 | 2019-03-02 19:02:17 |
|  2 | 班長   |    2 |     1000 |         1 | 2019-03-02 19:26:01 |
|  3 | 排長   |    3 |     2000 |         1 | 2019-03-02 19:42:35 |
+----+--------+------+----------+-----------+---------------------+
3 rows in set (0.02 sec)

但是在10.0.0.7 測試登錄atlas 服務的管理端,提示錯誤:

[root@pxc02 conf]# mysql -uzykjwtest -pzykjwtest01 -h127.0.0.1 -P52118
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1105 (07000): use ‘SELECT * FROM help‘ to see the supported commands

**暫時未找到是什麽原因,
但是經測試,在單獨的另外一臺機器10.0.0.131上開啟Atlas服務,通過它訪問後端的10.0.0.6 和10.0.0.7數據庫,測可以正常的登錄atlas 服務的管理端

[root@pxc03 conf]# mysql -uzykjwtest -p‘zykjwtest01‘  -h10.0.0.131 -P52118
([email protected]:52118)[(none)]>select * from test01;
ERROR 1105 (07000): use ‘SELECT * FROM help‘ to see the supported commands
([email protected]:52118)[(none)]>SELECT * FROM backends;
+-------------+---------------+-------+------+
| backend_ndx | address       | state | type |
+-------------+---------------+-------+------+
|           1 | 10.0.0.6:3306 | up    | rw   |
|           2 | 10.0.0.7:3306 | up    | rw   |
|           3 | 10.0.0.6:3306 | up    | ro   |
|           4 | 10.0.0.7:3306 | down  | ro   |
+-------------+---------------+-------+------+
4 rows in set (0.00 sec)

而且可以正常的寫入數據:

[root@pxc03 conf]# mysql -uatlasuser -p‘558996‘  -h10.0.0.131 -P52119
([email protected]:52119)[(none)]>use test02;
Database changed
([email protected]:52119)[test02]>show tables;
Empty set (0.00 sec)

([email protected]:52119)[test02]>CREATE TABLE `test01` (
    ->   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    ->   `titles` char(15) NOT NULL,
    ->   `icon` smallint(6) unsigned DEFAULT ‘0‘,
    ->   `integral` int(10) NOT NULL DEFAULT ‘0‘,
    ->   `isdefault` tinyint(1) unsigned NOT NULL DEFAULT ‘0‘,
    -> `create_time` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
    ->   PRIMARY KEY (`id`),
    ->   KEY `integral` (`integral`)
    -> ) ENGINE=Innodb AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.51 sec)

([email protected]:52119)[test02]>insert into test02.test01 values(1,‘列兵‘,1,0,1,now());
Query OK, 1 row affected (0.05 sec)

([email protected]:52119)[test02]>select * from test01;
+----+--------+------+----------+-----------+---------------------+
| id | titles | icon | integral | isdefault | create_time         |
+----+--------+------+----------+-----------+---------------------+
|  1 | 列兵   |    1 |        0 |         1 | 2019-03-03 01:30:18 |
+----+--------+------+----------+-----------+---------------------+
1 row in set (0.02 sec)

上面的問題8成是和10.0.0.7 本地虛擬機的環境問題有關系,但是沒找到是什麽問題導致的

2.5、安裝keepalived

10.0.0.6 上安裝操作:

 wget http://www.keepalived.org/software/keepalived-1.4.0.tar.gz
 tar xf keepalived-1.4.0.tar.gz  -C /usr/local/
 cd /usr/local/keepalived-1.4.0/
  mkdir /etc/keepalived
 cp /usr/local/keepalived-1.4.0/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ 
 find / -name "keepalived"
 cp /usr/local/etc/sysconfig/keepalived  /etc/sysconfig/
 cp /usr/local/keepalived-1.4.0/keepalived/etc/init.d/keepalived /etc/init.d/
 chmod +x /etc/init.d/keepalived 
 cp /usr/local/sbin/keepalived /usr/sbin/
 which keepalived
 cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.ori
 systemctl disable keepalived.service
systemctl status keepalived.service
 /etc/init.d/keepalived status
 /etc/init.d/keepalived start

10.0.0.6 keepalived.conf主配置文件:
####參考文檔:
https://blog.51cto.com/13581826/2109449
https://segmentfault.com/a/1190000014484218


[root@mgr01 keepalived]# cat keepalived.conf
global_defs {
   notification_email {
   [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_01
}

vrrp_script chk_atlas {
     script "lsof -i:52118 | grep mysql-pro || exit 1"  ###此處意思是假如atlas的端口52118 存在,則keep的vip在本地,如果atlas的端口52118不 存在,則vip漂移到10.0.0.7 機器上
     interval 2                                         ###此處也可以寫成lsof -i:52118 | grep mysql-pro || pkill keepalived,表示atlas掛掉了,則kill掉本地的keepalived服務
     fail 1
}
vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 51
    #mcast_src_ip 10.0.0.6
    priority 120
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
   track_script {
           chk_atlas
    }
    virtual_ipaddress {
    10.0.0.20
    }

}

啟動keepalived服務:

[root@pxc01 log]# /etc/init.d/keepalived start
Starting keepalived (via systemctl):                       [  OK  ]
[root@pxc01 log]# 

確認啟動成功已經綁定VIP10.0.0.20

[root@pxc01 log]# /etc/init.d/keepalived status
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-03-02 19:59:32 CST; 16s ago
  Process: 10941 ExecStart=/usr/local/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 10942 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─10942 /usr/local/sbin/keepalived -D
           ├─10943 /usr/local/sbin/keepalived -D
           └─10944 /usr/local/sbin/keepalived -D

Mar 02 19:59:43 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 19:59:43 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 19:59:43 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 19:59:43 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 19:59:48 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 19:59:48 pxc01 Keepalived_vrrp[10944]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eno16777736 for 10.0.0.20
Mar 02 19:59:48 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 19:59:48 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 19:59:48 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 19:59:48 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20

[root@pxc01 log]# ip a|grep 10.0.0.20
    inet 10.0.0.20/32 scope global eno16777736
[root@pxc01 log]# 

確認是否可以通過atlas 連接後端的mysql server:

[root@pxc01 log]# mysql -uatlasuser -p‘558996‘ -h10.0.0.20 -P52119
(atlasuser@‘mgr01‘:52119)[(none)]>insert into test01.test01 values(4,‘連長‘,4,3000,1,now());
Query OK, 1 row affected (0.06 sec)

(atlasuser@‘mgr01‘:52119)[(none)]>select * from test01.test01;
+----+--------+------+----------+-----------+---------------------+
| id | titles | icon | integral | isdefault | create_time         |
+----+--------+------+----------+-----------+---------------------+
|  1 | 列兵   |    1 |        0 |         1 | 2019-03-02 19:02:17 |
|  2 | 班長   |    2 |     1000 |         1 | 2019-03-02 19:26:01 |
|  3 | 排長   |    3 |     2000 |         1 | 2019-03-02 19:42:35 |
|  4 | 連長   |    4 |     3000 |         1 | 2019-03-02 20:08:06 |
+----+--------+------+----------+-----------+---------------------+
4 rows in set (0.01 sec)

查看sql日誌確認是可以登錄mysql 庫寫入和查詢數據的:

3/02/2019 20:07:16] C:10.0.0.20:52938 S:10.0.0.6:3306 OK 17.113 "select * from test01.test01"
[03/02/2019 20:08:06] C:10.0.0.20:52938 S:10.0.0.6:3306 OK 56.423 "insert into test01.test01 values(4,‘連長‘,4,3000,1,now())"
[03/02/2019 20:08:08] C:10.0.0.20:52938 S:10.0.0.6:3306 OK 1.751 "select * from test01.test01"
[03/02/2019 20:08:09] C:10.0.0.20:52938 S:10.0.0.7:3306 OK 18.499 "select * from test01.test01"
[03/02/2019 20:08:10] C:10.0.0.20:52938 S:10.0.0.6:3306 OK 1.911 "select * from test01.test01"

10.0.0.7 上安裝keepalived服務:

同樣的方式在10.0.0.7 機器上部署keepalived 並啟動
註意:此時的keepalived的配置文件參數priority 的優先級要比10.0.0.6 上的小,同時10.0.0.6和10.0.0.7 上的 “state BACKUP” 都要設置成費搶占模式BACKUP。
原因如下:
兩個節點的模式最好都為BACKUP模式,避免因為網絡延遲,超過心跳檢查時間,發生腦裂情況相互搶占MASTER導致寫入相同數據引發的沖突

啟動10.0.0.7 keepalived服務:


[root@pxc02 conf]# /etc/init.d/keepalived start
Starting keepalived (via systemctl):                       [  OK  ]
You have new mail in /var/spool/mail/root
[root@pxc02 conf]# 
[root@pxc02 conf]# ps -ef|grep keep
root       9977      1  1 20:11 ?        00:00:00 /usr/local/sbin/keepalived -D
root       9978   9977  0 20:11 ?        00:00:00 /usr/local/sbin/keepalived -D
root       9979   9977  1 20:11 ?        00:00:00 /usr/local/sbin/keepalived -D
root       9993   7701  0 20:11 pts/3    00:00:00 grep --color=auto keep
[root@pxc02 conf]# /etc/init.d/keepalived status
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-03-02 20:11:19 CST; 15s ago
  Process: 9976 ExecStart=/usr/local/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 9977 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─9977 /usr/local/sbin/keepalived -D
           ├─9978 /usr/local/sbin/keepalived -D
           └─9979 /usr/local/sbin/keepalived -D

Mar 02 20:11:19 pxc02 Keepalived_vrrp[9979]: WARNING - default user ‘keepalived_script‘ for script execution does not exist - please create.
Mar 02 20:11:19 pxc02 Keepalived_vrrp[9979]: WARNING - script `lsof` resolved by path search to `/usr/sbin/lsof`. Please specify full path.
Mar 02 20:11:19 pxc02 Keepalived_vrrp[9979]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
Mar 02 20:11:19 pxc02 Keepalived_vrrp[9979]: VRRP_Instance(VI_1) removing protocol VIPs.
Mar 02 20:11:19 pxc02 Keepalived_vrrp[9979]: Using LinkWatch kernel netlink reflector...
Mar 02 20:11:19 pxc02 Keepalived_vrrp[9979]: VRRP_Instance(VI_1) Entering BACKUP STATE
Mar 02 20:11:19 pxc02 Keepalived_vrrp[9979]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Mar 02 20:11:19 pxc02 Keepalived_vrrp[9979]: VRRP_Instance(VI_1) Now in FAULT state
Mar 02 20:11:19 pxc02 Keepalived_vrrp[9979]: VRRP_Script(chk_atlas) succeeded
Mar 02 20:11:20 pxc02 Keepalived_vrrp[9979]: VRRP_Instance(VI_1) Entering BACKUP STATE
[root@pxc02 conf]# 

由於10.0.0.6 上的主庫keepalived 的優先級高於10.0.0.7,所以VIP被=綁定到了10.0.0.6 網卡上

三、故障模擬演示

關掉10.0.0.6 上的atlas服務測試:
10.0.0.6 機器上查看keepalived 的狀態 此時keepalived已經被pkill keepalived

[root@pxc01 keepalived]# /etc/init.d/keepalived status
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

Mar 02 19:59:48 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 19:59:48 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 19:59:48 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 19:59:48 pxc01 Keepalived_vrrp[10944]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 20:16:29 pxc01 Keepalived[10942]: Stopping
Mar 02 20:16:29 pxc01 Keepalived_vrrp[10944]: VRRP_Script(chk_atlas) failed (due to signal 15)
Mar 02 20:16:29 pxc01 Keepalived_vrrp[10944]: VRRP_Instance(VI_1) sent 0 priority
Mar 02 20:16:29 pxc01 Keepalived_vrrp[10944]: VRRP_Instance(VI_1) removing protocol VIPs.
Mar 02 20:16:30 pxc01 Keepalived_vrrp[10944]: Stopped
Mar 02 20:16:30 pxc01 Keepalived[10942]: Stopped Keepalived v1.4.0 (12/29,2017)
[root@pxc01 keepalived]# 

10.0.0.7 機器上查看keepalived 的狀態 VIP已經綁定了:

[root@pxc02 ~]# /etc/init.d/keepalived status
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-03-02 20:11:19 CST; 11min ago
  Process: 9976 ExecStart=/usr/local/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 9977 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─9977 /usr/local/sbin/keepalived -D
           ├─9978 /usr/local/sbin/keepalived -D
           └─9979 /usr/local/sbin/keepalived -D
Mar 02 20:16:30 pxc02 Keepalived_vrrp[9979]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 20:16:30 pxc02 Keepalived_vrrp[9979]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 20:16:30 pxc02 Keepalived_vrrp[9979]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 20:16:30 pxc02 Keepalived_vrrp[9979]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 20:16:35 pxc02 Keepalived_vrrp[9979]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 20:16:35 pxc02 Keepalived_vrrp[9979]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eno16777736 for 10.0.0.20
Mar 02 20:16:35 pxc02 Keepalived_vrrp[9979]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 20:16:35 pxc02 Keepalived_vrrp[9979]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 20:16:35 pxc02 Keepalived_vrrp[9979]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
Mar 02 20:16:35 pxc02 Keepalived_vrrp[9979]: Sending gratuitous ARP on eno16777736 for 10.0.0.20
[root@pxc02 ~]# 

10.0.0.7 機器上通過atlas登錄mysql server :


[root@pxc02 conf]# mysql -uatlasuser -p‘558996‘ -h10.0.0.20 -P52119
(atlasuser@‘mgr02‘:52119)[(none)]>insert into test01.test01 values(5,‘營長‘,5,4000,1,now());
Query OK, 1 row affected (0.06 sec)

(atlasuser@‘mgr02‘:52119)[(none)]>select * from test01.test01;
+----+--------+------+----------+-----------+---------------------+
| id | titles | icon | integral | isdefault | create_time         |
+----+--------+------+----------+-----------+---------------------+
|  1 | 列兵   |    1 |        0 |         1 | 2019-03-02 19:02:17 |
|  2 | 班長   |    2 |     1000 |         1 | 2019-03-02 19:26:01 |
|  3 | 排長   |    3 |     2000 |         1 | 2019-03-02 19:42:35 |
|  4 | 連長   |    4 |     3000 |         1 | 2019-03-02 20:08:06 |
|  5 | 營長   |    5 |     4000 |         1 | 2019-03-02 20:20:43 |
+----+--------+------+----------+-----------+---------------------+
5 rows in set (0.01 sec)

查看10.0.0.7 atlas的sql 日誌,正常提供讀寫分離工作:


[03/02/2019 20:19:31] C:10.0.0.20:35793 S:10.0.0.7:3306 OK 25.475 "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=1000,MAX_JOIN_SIZE=1000000"
[03/02/2019 20:19:31] C:10.0.0.20:35793 S:10.0.0.6:3306 OK 34.604 "select @@version_comment limit 1"
[03/02/2019 20:19:31] C:10.0.0.20:35793 S:10.0.0.7:3306 OK 0.245 "select USER()"
[03/02/2019 20:19:36] C:10.0.0.20:35793 S:10.0.0.6:3306 OK 23.035 "select * from test01.test01"
[03/02/2019 20:20:43] C:10.0.0.20:35793 S:10.0.0.6:3306 OK 60.000 "insert into test01.test01 values(5,‘營長‘,5,4000,1,now())"
[03/02/2019 20:20:45] C:10.0.0.20:35793 S:10.0.0.6:3306 OK 3.905 "select * from test01.test01"
[03/02/2019 20:20:47] C:10.0.0.20:35793 S:10.0.0.7:3306 OK 3.395 "select * from test01.test01"
[03/02/2019 20:20:55] C:10.0.0.20:35793 S:10.0.0.6:3306 OK 2.126 "select * from test01.test01"

+++++++++++++++++++++++++++++++++++++++++++++
關掉10.0.0.6 上的mysql服務測試:

centos7的系統上安裝mysql client的方式

yum install -y mariadb.x86_64 mariadb-libs.x86_64
[root@pxc02 ~]# rpm -qa|grep mariadb
mariadb-5.5.60-1.el7_5.x86_64
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@pxc02 ~]# mysql -V
mysql  Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1
[root@pxc02 ~]# 
[root@pxc02 ~]# mysql -uatlasuser -p‘558996‘ -h10.0.0.20 -P52119
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.0.81-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

(atlasuser@‘mgr02‘:52119)[(none)]>insert into test01.test01 values(6,‘團長‘,6,5000,1,now());
Query OK, 1 row affected (0.15 sec)

(atlasuser@‘mgr02‘:52119)[(none)]>select * from test01.test01;
+----+--------+------+----------+-----------+---------------------+
| id | titles | icon | integral | isdefault | create_time         |
+----+--------+------+----------+-----------+---------------------+
|  1 | 列兵   |    1 |        0 |         1 | 2019-03-02 19:02:17 |
|  2 | 班長   |    2 |     1000 |         1 | 2019-03-02 19:26:01 |
|  3 | 排長   |    3 |     2000 |         1 | 2019-03-02 19:42:35 |
|  4 | 連長   |    4 |     3000 |         1 | 2019-03-02 20:08:06 |
|  5 | 營長   |    5 |     4000 |         1 | 2019-03-02 20:20:43 |
|  6 | 團長   |    6 |     5000 |         1 | 2019-03-02 21:05:40 |
+----+--------+------+----------+-----------+---------------------+
6 rows in set (0.00 sec)

查看10.0.0.7 atlas的sql 日誌:
可以正常提供寫和讀

[03/02/2019 21:03:14] C:10.0.0.20:36004 S:10.0.0.7:3306 OK 0.464 "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=1000,MAX_JOIN_SIZE=1000000"
[03/02/2019 21:03:14] C:10.0.0.20:36004 S:10.0.0.7:3306 OK 0.322 "select @@version_comment limit 1"
[03/02/2019 21:03:14] C:10.0.0.20:36004 S:10.0.0.7:3306 OK 0.245 "select USER()"
[03/02/2019 21:03:51] C:10.0.0.20:36004 S:10.0.0.7:3306 OK 0.572 "select * from test01.test01"
[03/02/2019 21:05:40] C:10.0.0.20:36004 S:10.0.0.7:3306 OK 141.829 "insert into test01.test01 values(6,‘團長‘,6,5000,1,now())"
[03/02/2019 21:05:43] C:10.0.0.20:36004 S:10.0.0.7:3306 OK 0.723 "select * from test01.test01"
[03/02/2019 21:05:44] C:10.0.0.20:36004 S:10.0.0.7:3306 OK 3.542 "select * from test01.test01"

此時只能是10.0.0.7 一個節點提供讀和寫了

四、關此數據庫架構的總結

4.1在配置雙主互從過程中需要註意什麽?

①keepalived+mysql雙主一般來說,主要應用中小型規模數據庫。master節點發生故障後,利用keepalived的VIP機制實現快速切換備用節點
②在keepalived部署配置文件中,兩個節點的模式最好都為BACKUP模式,或者建議keepalived不要開機自啟動。主要是避免因為網絡延遲,超過心跳檢查時間,發生腦裂情況相互搶占MASTER導致寫入相同數據引發的沖突
③兩個節點的auto_increment_incremenet(自增步長)和auto_increment_offset(自增起始點)設為不同值。目的為了避免master意外宕機,可能會有部分binlog未能及時復制到slave上被應用,從而導致slave新寫入數據的自增值和原先的master沖突,從offset起始點開始就錯開了,避免了主鍵id的沖突,當然,如有合適的容錯機制解決沖突話,也可以不這麽設置

4.2如果遇到主從延遲怎麽解決?

①首先需要通過show slave statusG中 Seconds_Behind_Master觀察主從之間延遲的狀態,但是僅僅依靠這個參考值不是準確的。

②slave節點服務器硬件配置不能與master節點相差太大,會大大導致復制的延遲
③由於mysql5.7版本引入了多線程復制的參數和並行復制參數,可以很大程度降低復制延遲。可以在slave庫上開啟這些參數,網絡延遲導致復制延遲可以得到很好的解決。同時考慮更換mariadb分支版本,也是一種解決延遲復制的方式
    mysql> show global variables like ‘slave_paralle%‘;
    Variable_name   Value
    slave_parallel_type     DATABASE
    slave_parallel_workers  0
    slave_parallel_workers:默認為0,表示為單線程
    slave_parallel_type:默認多線程機制為一個線程處理一個DATABASE
    mysql> set global slave_parallel_workers=4; #修改為四個線程操作
    mysql> set global slave_parallel_type=‘logical_clock‘; #修改為並行復制
④調整master節點服務器DDL速度還有就是主庫是寫,對數據安全性較高,比如sync_binlog=1,innodb_flush_log_at_trx_commit= 1 之類的設置,而slave則不需要這麽高的數據安全,完全可以講sync_binlog設置為0或者關閉binlog,innodb_flushlog也可以設置為0來提高sql的執行效率。另外就是使用比主庫更好的硬件設備作為slave

4.3、MySQL雙主環境那些mysql函數是不建議使用的
生產上使用mysql雙主的話,註意不要使用mysql自身的觸發器,外鍵等特殊函數,同時也不建議使用mysql的存儲過程。如果采用這些功能出問題了,沒有專職DBA的維護能力,還是建議由程序代碼上來實現這些功能。

但是掛掉的10.0.0.6 如何重新加入到雙主集群,以及註意事項,請期待下次測試

mysql5.7.24 gtid雙主復制+atlas+keepalived