系統:node
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
getenforcemysql
保證服務器時間同步:linux
ntpdate ntp1.aliyun.com */5 * * * * ntpdate ntp1.aliyun.com
關閉防火牆:git
systemctl status firewalld systemctl stop firewalld systemctl stop iptables.service systemctl status iptables.service
關閉防火牆服務開機自啓動:github
systemctl disable firewalld.service systemctl disable iptables.service
關閉mysql服務開機自啓動:sql
[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數據庫
mysql 版本:MySQL5.7.24
10.0.0.6 pxc01 操做:vim
**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 機器也按照上面的方式安裝mysqlsegmentfault
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
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
配置完成
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,其他都是同樣的參數
+++++++++++++++++++在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 (zykjwtest@10.0.0.131:52118)[(none)]>select * from test01; ERROR 1105 (07000): use 'SELECT * FROM help' to see the supported commands (zykjwtest@10.0.0.131: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 (atlasuser@10.0.0.131:52119)[(none)]>use test02; Database changed (atlasuser@10.0.0.131:52119)[test02]>show tables; Empty set (0.00 sec) (atlasuser@10.0.0.131: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) (atlasuser@10.0.0.131:52119)[test02]>insert into test02.test01 values(1,'列兵',1,0,1,now()); Query OK, 1 row affected (0.05 sec) (atlasuser@10.0.0.131: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 本地虛擬機的環境問題有關係,可是沒找到是什麼問題致使的
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主配置文件:
####參考文檔:
http://www.javashuo.com/article/p-fgcpdocf-gg.html
http://www.javashuo.com/article/p-hvsmrgkc-ew.html
[root@mgr01 keepalived]# cat keepalived.conf global_defs { notification_email { 3056977536@qq.com } notification_email_from Alexandre.Cassen@firewall.loc 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.三、MySQL雙主環境那些mysql函數是不建議使用的
生產上使用mysql雙主的話,注意不要使用mysql自身的觸發器,外鍵等特殊函數,同時也不建議使用mysql的存儲過程。若是採用這些功能出問題了,沒有專職DBA的維護能力,仍是建議由程序代碼上來實現這些功能。
可是掛掉的10.0.0.6 如何從新加入到雙主集羣,以及注意事項,請期待下次測試