騰訊雲的雲主機安裝360開源的mysql中間鍵Atlas
騰訊雲機器的外網ip:119.29.97.131
滄州233測試物理機器的外網ip192.168.1.233(安全起見故意寫成內網的ip,實際上是外網ip)
mysql
參考博文地址:http://www.javashuo.com/article/p-bfypcjmp-vc.htmlgit
(摘抄自https://github.com/Qihoo360/Atlas)github
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.自動摘除宕機的DBsql
官方連接:https://github.com/Qihoo360/Atlas數據庫
Atlas部分配置參數及原理詳解
參考文檔:
https://github.com/Qihoo360/Atlas/wiki/Atlas部分配置參數及原理詳解後端
Atlas的運行及常見問題
參考文檔:
https://github.com/Qihoo360/Atlas/wiki/Atlas的運行及常見問題centos
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 jianweiuser@'119.29.97.131' identified by 'jianwei@559886';flush privileges;
登陸騰訊雲的雲主機測試遠程是否能夠登陸192.168.1.233 機器上的mysql實例架構
mysql -ujianweiuser -p'jianwei@559886' -h192.168.1.233 -P3306 mysql -ujianweiuser -p'jianwei@559886' -h192.168.1.233 -P3307 mysql -ujianweiuser -p'jianwei@559886' -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演示環境可用的配置文件內容以下:
[root@VM_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:3307@1,192.168.1.233:3308@1 #用戶名與其對應的加密過的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 [root@VM_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項的名字,三者須要統一 [root@VM_82_178_centos conf]# grep mgrmul /usr/local/mysql-proxy/conf/mgrmul.cnf instance = mgrmul
5.1Atlas的管理端口演示:
能夠經過登陸Atlas管理界面進行動態的上線和下線mysql的節點,查看各個mysql節點的狀態,查看容許登陸的客戶端的ip信息,能夠容許在線添加和刪除客戶端的ip來連接Atlas,
能夠在線查看當前容許那些用戶登陸Atlas,以及在線添加和刪除容許連接Atlas的帳戶和密碼。能夠在線添加完數據後並保存到Atlas的配置文件等
[root@VM_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) [root@VM_82_178_centos ~]# mysql -uzykjwtest -p'zykjwtest01' -h127.0.0.1 -P1300 zykjwtest@127.0.0.1 [(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) zykjwtest@127.0.0.1 [(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) zykjwtest@127.0.0.1 [(none)]>
5.2 Aatlas負載均衡功能:
提示:騰訊雲主機要容許本機的外網的ip來訪問Atlas的端口:1200
因此在騰訊雲的控制檯安全組中 設置運行雲主機外網ip 訪問1200端口
只讀負載均衡測試:select @@port,能夠查看端口區分來自不一樣的可讀mysql實例;
[root@VM_82_178_centos ~]# mysql -ujianweiuser -p'jianwei@559886' -h127.0.0.1 -P1200 jianweiuser@127.0.0.1 [(none)]>select @@port; +--------+ | @@port | +--------+ | 3307 | +--------+ 1 row in set (0.04 sec) jianweiuser@127.0.0.1 [(none)]>select @@port; +--------+ | @@port | +--------+ | 3308 | +--------+ 1 row in set (0.05 sec) jianweiuser@127.0.0.1 [(none)]> [root@VM_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'jianwei@559886' -h127.0.0.1 -P1200 或者 mysql -ujianweiuser -p'jianwei@559886' -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日誌,發現已經實現了讀寫分離:
[root@VM_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演示
[root@localhost ~]# /etc/init.d/mysqld stop
Shutting down MySQL............. SUCCESS!
[root@localhost ~]#
建立test02庫和test02測試表:
mysql -ujianweiuser -p'jianwei@559886' -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實例之間輪訓切換
[root@VM_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"
提示說明:以上MGR採用的是multi master的方式演示的,若是採用默認的single master模式,再選主上會存在問題,例如出現read-only,提示新主庫不可寫
Gtid+MGR+atlas讀寫分離以及負載均衡高可用架構演示到此結束,歡迎同窗們一塊兒交流和學習