Gtid+MGR+atlas讀寫分離以及負載均衡高可用架構
MySQL5.7.24 Gtid+MGR+atlas讀寫分離以及負載均衡高可用架構
一.伺服器環境介紹:
騰訊雲的雲主機安裝360開源的mysql中間鍵Atlas
騰訊雲機器的外網ip:119.29.97.131
滄州233測試物理機器的外網ip192.168.1.233(安全起見故意寫成內網的ip,其實是外網ip)
二.mysqlGtid+MGR安裝
參考博文地址:
三.中間鍵Atlas簡介
(摘抄自https://github.com/Qihoo360/Atlas)
Atlas 是由 Qihoo 360公司Web平臺部基礎架構團隊開發維護的一個基於MySQL協議的資料中間層專案。它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基礎上,修改了大量bug,添加了很多功能特性。目前該專案在360公司內部得到了廣泛應用,很多MySQL業務已經接入了Atlas平 臺,每天承載的讀寫請求數達幾十億條。同時,有超過50家公司在生產環境中部署了Atlas,超過800人已加入了我們的開發者交流群,並且這些數字還在不斷增加。
主要功能:
1.讀寫分離
2.從庫負載均衡
3.IP過濾
4.自動分表
5.DBA可平滑上下線DB
6.自動摘除宕機的DB
官方連結:https://github.com/Qihoo360/Atlas
Atlas部分配置引數及原理詳解
參考文件:
https://github.com/Qihoo360/Atlas/wiki/Atlas部分配置引數及原理詳解
Atlas的執行及常見問題
參考文件:
https://github.com/Qihoo360/Atlas/wiki/Atlas的執行及常見問題
四.安裝Atlas以及配置過程:
wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm yum -y localinstall Atlas-2.2.1.el6.x86_64.rpm 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
4.1編輯Atlas配置檔案:
在資料庫裡準備使用者
由於在MGR中mysql3306 是主庫,mysql3307 和mysql3308 是slave從庫
所以在mysql3306上執行下面的sql就可以了,會自動同步到mysql3307 和mysql3308上
grant all on *.* to [email protected]'119.29.97.131' identified by '[email protected]';flush privileges;
登入騰訊雲的雲主機測試遠端是否可以登入192.168.1.233 機器上的mysql例項
mysql -ujianweiuser -p' [email protected]' -h192.168.1.233 -P3306
mysql -ujianweiuser -p'[email protected]' -h192.168.1.233 -P3307
mysql -ujianweiuser -p'[email protected]' -h192.168.1.233 -P3308
測試是可以登入的
對使用資料庫的使用者密碼進行加密:
執行/usr/local/mysql-proxy/bin/encrypt jianweiuser
獲得一個密碼串W7moDRl9VZ8jaj+4argKVA== 加入到atlas的配置檔案mgrmul.cnf
配置檔案具體引數解釋參考:
https://github.com/Qihoo360/Atlas/wiki/Atlas的安裝
4.2演示環境可用的配置檔案內容如下:
[[email protected]_82_178_centos ~]# cat /usr/local/mysql-proxy/conf/mgrmul.cnf
[mysql-proxy]
#帶#號的為非必需的配置專案
#管理介面的使用者名稱
admin-username = zykjwtest
#管理介面的密碼
admin-password = zykjwtest01
#Atlas後端連線的MySQL主庫的IP和埠,可設定多項,用逗號分隔
proxy-backend-addresses=192.168.1.233:3306,192.168.1.233:3307,192.168.1.233:3308
#Atlas後端連線的MySQL從庫的IP和埠,@後面的數字代表權重,用來作負載均衡,若省略則預設為1,可設定多項,用逗號分隔
proxy-read-only-backend-addresses = 192.168.1.233:[email protected],192.168.1.233:[email protected]
#使用者名稱與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程式encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的使用者名稱和加密密碼!
pwds = 01test:XU7LYbEX2MY62nXU/3RMPA==, jianweiuser:XU7LYbEX2MY12rq2Ryj6/g==
#設定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:1200
#Atlas監聽的管理介面IP和埠
admin-address = 0.0.0.0:1300
#分表設定,此例中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, 119.29.97.131
#Atlas前面掛接的LVS的物理網絡卡的IP(注意不是虛IP),若有LVS且設定了client-ips則此項必須設定,否則可以不設定
#lvs-ips = 192.168.1.1
[[email protected]_82_178_centos ~]#
4.3Atlas重要引數介紹:
(1)Atlas的工作埠
proxy-address項配置,例如proxy-address = 0.0.0.0:1200代表客戶端應該使用1200這個埠連線Atlas來發送SQL請求。
(2)Atlas的管理埠
admin-address項配置,例如admin-address = 0.0.0.0:1300代表DBA應該使用1300這個埠連線Atlas來執行運維管理操作。
(3)管理介面的使用者名稱和密碼
admin-username項和admin-password項設定,這兩項是用來進入Atlas的管理介面的,與後端連線的MySQL沒有關係,所以可以任意設定,不需要MySQL在配置上做任何改動。
(4)使用者名稱和密碼配置項,需要和主從mysql例項配置的使用者名稱和密碼配置一樣,多個使用者可以用逗號分開
pwds = 01test:XU7LYbEX2MY62nXU/3RMPA==, jianweiuser:XU7LYbEX2MY12rq2Ryj6/g==
(5)允許連線Atlas的客戶端的IP,可以是精確IP,也可以是IP段,以逗號分隔,若不設定該項則允許所>有IP連線,否則只允許列表中的IP連線
client-ips = 127.0.0.1, 192.168.1, 119.29.97.131
(6)#SQL日誌的開關,可設定為OFF、ON、REALTIME,OFF代表不記錄SQL日誌,ON代表記錄SQL日誌,REALTIME代表記錄SQL日誌且實時寫入磁碟,預設為OFF
sql-log = REALTIME ###此處為了演示效果,所以開啟次引數,讓sql語句記錄到SQL日誌中
(7)mgrmul.cnf 配置檔案引數instance 設定
mgrmul是conf目錄下配置檔案的名字,也是配置檔案裡instance項的名字,三者需要統一
[[email protected]_82_178_centos conf]# grep mgrmul /usr/local/mysql-proxy/conf/mgrmul.cnf
instance = mgrmul
五.Atlas應用演示
5.1Atlas的管理埠演示:
可以通過登入Atlas管理介面進行動態的上線和下線mysql的節點,檢視各個mysql節點的狀態,檢視允許登入的客戶端的ip資訊,可以允許線上新增和刪除客戶端的ip來連結Atlas,
可以線上檢視當前允許那些使用者登入Atlas,以及線上新增和刪除允許連結Atlas的賬戶和密碼。可以線上新增完資料後並儲存到Atlas的配置檔案等
[[email protected]_82_178_centos ~]# mysql -uzykjwtest -p'zykjwtest01' -P1300
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'zykjwtest'@'localhost' (using password: YES)
[[email protected]_82_178_centos ~]# mysql -uzykjwtest -p'zykjwtest01' -h127.0.0.1 -P1300
[email protected] [(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.01 sec)
[email protected] [(none)]>SELECT * FROM backends;
+-------------+--------------------+-------+------+
| backend_ndx | address | state | type |
+-------------+--------------------+-------+------+
| 1 | 192.168.1.233:3306 | up | rw |
| 2 | 192.168.1.233:3307 | up | rw |
| 3 | 192.168.1.233:3308 | up | rw |
| 4 | 192.168.1.233:3307 | up | ro |
| 5 | 192.168.1.233:3308 | up | ro |
+-------------+--------------------+-------+------+
5 rows in set (0.00 sec)
[email protected] [(none)]>
5.2 Aatlas負載均衡功能:
提示:騰訊雲主機要允許本機的外網的ip來訪問Atlas的埠:1200
所以在騰訊雲的控制檯安全組中 設定運行雲主機外網ip 訪問1200埠
只讀負載均衡測試:select @@port,可以檢視埠區分來自不同的可讀mysql例項;
[[email protected]_82_178_centos ~]# mysql -ujianweiuser -p'[email protected]' -h127.0.0.1 -P1200
[email protected] [(none)]>select @@port;
+--------+
| @@port |
+--------+
| 3307 |
+--------+
1 row in set (0.04 sec)
[email protected] [(none)]>select @@port;
+--------+
| @@port |
+--------+
| 3308 |
+--------+
1 row in set (0.05 sec)
[email protected] [(none)]>
[[email protected]_82_178_centos ~]# tailf /usr/local/mysql-proxy/log/sql_mgrmul.log
[12/29/2018 22:03:08] C:127.0.0.1:34600 S:192.168.1.233:3307 OK 43.210 "select @@port"
[12/29/2018 22:03:11] C:127.0.0.1:34600 S:192.168.1.233:3308 OK 42.856 "select @@port"
從SQL日誌看到已經實現了負載均衡的功能
5.3 Atlas讀寫分離測試
讀寫分離測試:Atlas代理的機制確定了它只會向配置檔案中記錄的可寫節點的第一個可用的去寫,如果第一個不可用就會去訪問第二個,直到找到一個可用的並一直只向該主機去寫;讀的話則從配置檔案中的所有可讀節點去讀;
mysql -ujianweiuser -p'[email protected]' -h127.0.0.1 -P1200
或者
mysql -ujianweiuser -p'[email protected]' -h119.29.97.131 -P1200
建立測試表進行測試:
create database test01;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());
insert into test01.test01 values(2,'班長',2,1000,1,now());
檢視sql日誌,發現已經實現了讀寫分離:
[[email protected]_82_178_centos ~]# tailf /usr/local/mysql-proxy/log/sql_mgrmul.log
[12/29/2018 22:07:23] C:127.0.0.1:34600 S:192.168.1.233:3306 OK 50.116 "create database test01"
[12/29/2018 22:07:23] C:127.0.0.1:34600 S:192.168.1.233:3307 OK 43.100 "SELECT DATABASE()"
[12/29/2018 22:07:23] C:127.0.0.1:34600 S:192.168.1.233:3307 OK 86.225 "show databases"
[12/29/2018 22:07:24] C:127.0.0.1:34600 S:192.168.1.233:3308 OK 85.390 "show tables"
[12/29/2018 22:07:35] C:127.0.0.1:34600 S:192.168.1.233:3306 OK 148.760 "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"
[12/29/2018 22:09:00] C:127.0.0.1:34600 S:192.168.1.233:3306 OK 95.845 "insert into test01.test01 values(1,'列兵',1,0,1,now())"
[12/29/2018 22:09:01] C:127.0.0.1:34600 S:192.168.1.233:3306 OK 96.185 "insert into test01.test01 values(2,'班長',2,1000,1,now())"
[12/29/2018 22:09:37] C:127.0.0.1:34600 S:192.168.1.233:3306 OK 86.894 "select * from test01.test01"
[12/29/2018 22:09:39] C:127.0.0.1:34600 S:192.168.1.233:3307 OK 86.323 "select * from test01.test01"
[12/29/2018 22:11:19] C:119.29.97.131:35378 S:192.168.1.233:3308 OK 277.726 "select @@version_comment limit 1"
[12/29/2018 22:11:19] C:119.29.97.131:35378 S:192.168.1.233:3307 OK 277.027 "select USER()"
關閉maste例項mysql3306演示
[[email protected] ~]# /etc/init.d/mysqld stop
Shutting down MySQL............. SUCCESS!
[[email protected] ~]#
建立test02庫和test02測試表:
mysql -ujianweiuser -p'[email protected]' -h119.29.97.131 -P1200
create database test02;use test02;
CREATE TABLE `test02` (
`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 test02.test02 values(1,'列兵',1,0,1,now());
insert into test02.test02 values(2,'班長',2,1000,1,now());
select * from test01.test01;select * from test02.test02;
發現寫庫切換到了mysql3307例項,讀庫在mysql3307 和mysql3308例項之間輪訓切換
[[email protected]_82_178_centos ~]# tailf /usr/local/mysql-proxy/log/sql_mgrmul.log
[12/29/2018 22:23:15] C:119.29.97.131:36168 S:192.168.1.233:3308 OK 42.392 "select @@version_comment limit 1"
[12/29/2018 22:23:15] C:119.29.97.131:36168 S:192.168.1.233:3307 OK 41.100 "select USER()"
[12/29/2018 22:25:20] C:119.29.97.131:36168 S:192.168.1.233:3307 OK 304.954 "create database test02"
[12/29/2018 22:25:20] C:119.29.97.131:36168 S:192.168.1.233:3308 OK 42.489 "SELECT DATABASE()"
[12/29/2018 22:25:21] C:119.29.97.131:36168 S:192.168.1.233:3308 OK 85.458 "show databases"
[12/29/2018 22:25:21] C:119.29.97.131:36168 S:192.168.1.233:3307 OK 77.739 "show tables"
[12/29/2018 22:25:21] C:119.29.97.131:36168 S:192.168.1.233:3307 OK 139.017 "CREATE TABLE `test02` (
`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"
[12/29/2018 22:25:21] C:119.29.97.131:36168 S:192.168.1.233:3307 OK 93.835 "insert into test02.test02 values(1,'列兵',1,0,1,now())"
[12/29/2018 22:25:24] C:119.29.97.131:36168 S:192.168.1.233:3307 OK 92.871 "insert into test02.test02 values(2,'班長',2,1000,1,now())"
[12/29/2018 22:27:53] C:119.29.97.131:36168 S:192.168.1.233:3307 OK 85.442 "select * from test01.test01"
[12/29/2018 22:27:53] C:119.29.97.131:36168 S:192.168.1.233:3308 OK 84.813 "select * from test02.test02"
Gtid+MGR+atlas讀寫分離以及負載均衡高可用架構演示到此結束,歡迎同學們一起交流和學習