MySQL Atlas 讀寫分離軟件介紹
MySQL Atlas介紹
一、MySQL Atlas介紹
1.1.1 MySQL Atlas介紹
- MySQL Atlas介紹
Atlas是由 Qihoo 360, Web平臺部基礎架構團隊開發維護的一個基于MySQL協議的數據中間層項目。
它是在mysql-proxy 0.8.2版本的基礎上,對其進行了優化,增加了一些新的功能特性。
360內部使用Atlas運行的mysql業務,每天承載的讀寫請求數達幾十億條。
軟件下載地址:https://github.com/Qihoo360/Atlas/releases
常用讀寫分離軟件
1、Atlas 下載地址:https://github.com/Qihoo360/Atlas/releases
2、proxySQL 官方地址:https://www.proxysql.com/
3、maxscale 官方地址:https://mariadb.com/kb/en/maxscale-22-getting-started/
4、MySQL-Router MySQL官方
- Atlas 部署
MHA+Atlas 高可用讀寫分離(部署前提有MHA高可用環境)
MHA高可用部署:http://www.rzrgm.cn/woaiyunwei/p/13210749.html
[root@db03 /server/tools]# ll Atlas-2.2.1.el6.x86_64.rpm
-rw-r--r-- 1 root root 4963681 Jul 2 14:41 Atlas-2.2.1.el6.x86_64.rpm
[root@db03 /server/tools]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:Atlas-2.2.1-1 ################################# [100%]
[root@db03 /server/tools]# rpm -qa Atlas
Atlas-2.2.1-1.x86_64
[root@db03 /server/tools]# rpm -ql Atlas
/usr/local/mysql-proxy/bin/VERSION
/usr/local/mysql-proxy/bin/encrypt
/usr/local/mysql-proxy/bin/mysql-proxy
/usr/local/mysql-proxy/bin/mysql-proxyd
/usr/local/mysql-proxy/conf/test.cnf #配置文件
- 配置Atlas配置文件
[root@db03 /server/tools]# cd /usr/local/mysql-proxy/conf/
[root@db03 /usr/local/mysql-proxy/conf]# cp test.cnf{,.backup}
[root@db03 /usr/local/mysql-proxy/conf]# ll
total 8
-rw-r--r-- 1 root root 2810 Dec 17 2014 test.cnf
-rw-r--r-- 1 root root 2810 Jul 2 14:52 test.cnf.backup
[root@db03 /usr/local/mysql-proxy/conf]# cat test.cnf
[mysql-proxy]
#帶#號的為非必需的配置項目
#管理接口的用戶名
admin-username = user
#管理接口的密碼
admin-password = pwd
#Atlas后端連接的MySQL主庫的IP和端口,可設置多項,用逗號分隔
proxy-backend-addresses = 127.0.0.1:3306
#Atlas后端連接的MySQL從庫的IP和端口,@后面的數字代表權重,用來作負載均衡,若省略則默認為1,可設置多項,用逗號分隔
#proxy-read-only-backend-addresses = 127.0.0.1:3305@1
#用戶名與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的用戶名和加密密碼!
pwds = user1:+jKsgB3YAG8=, user2:GS+tr4TPgqc=
#設置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 = 8
#日志級別,分為message、warning、critical、error、debug五個級別
log-level = message
#日志存放的路徑
log-path = /usr/local/mysql-proxy/log
#SQL日志的開關,可設置為OFF、ON、REALTIME,OFF代表不記錄SQL日志,ON代表記錄SQL日志,REALTIME代表記錄SQL日志且實時寫入磁盤,默認為OFF
#sql-log = OFF
#慢日志輸出設置。當設置了該參數時,則日志只輸出執行時間超過sql-log-slow(單位:ms)的日志記錄。不設置該參數則輸出全部日志。
#sql-log-slow = 10
#實例名稱,用于同一臺機器上多個Atlas實例間的區分
#instance = test
#Atlas監聽的工作接口IP和端口
proxy-address = 0.0.0.0:1234
#Atlas監聽的管理接口IP和端口
admin-address = 0.0.0.0:2345
#分表設置,此例中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
#Atlas前面掛接的LVS的物理網卡的IP(注意不是虛IP),若有LVS且設置了client-ips則此項必須設置,否則可以不設置
#lvs-ips = 192.168.1.1
配置mysql管理員和后端連接用戶的密碼加密
rep用戶
[root@db03 /usr/local/mysql-proxy/bin]# ./encrypt 123456
/iZxz+0GRoA=
mha用戶
[root@db03 /usr/local/mysql-proxy/bin]# ./encrypt mha
O2jBXONX098=
修改如下:
[root@db03 /usr/local/mysql-proxy/conf]# cat test.cnf
[mysql-proxy]
admin-username = user #atlas 用戶
admin-password = pwd #atlas 密碼
proxy-backend-addresses = 10.4.7.55:3306 #寫操作數據庫 一般為MHAVIP
proxy-read-only-backend-addresses = 10.4.7.52:3306,10.4.7.53:3306 #讀操作數據庫
pwds = rep:/iZxz+0GRoA=,mha:O2jBXONX098= #mysql數據庫管理員,后端連接用戶的用戶名和密碼(這里是測試不代表生產環境) 上面的加密密碼就放在這里(注意:密碼需要對應用戶)
daemon = true
keepalive = true
event-threads = 8
log-level = message
log-path = /usr/local/mysql-proxy/log #log日志
sql-log=ON
proxy-address = 0.0.0.0:33060
admin-address = 0.0.0.0:2345
charset=utf8
- 啟動Atlas
[root@db03 /usr/local/mysql-proxy/bin]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
注意:Atlas可以通過不同配置文件,管理多個讀寫分離項目
比如:
/usr/local/mysql-proxy/bin/mysql-proxyd test start (test=test.conf的前綴)
/usr/local/mysql-proxy/bin/mysql-proxyd test1 start
查看進程
[root@db03 /usr/local/mysql-proxy/bin]# ps -ef |grep proxy
root 53814 1 0 15:52 ? 00:00:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root 53815 53814 0 15:52 ? 00:00:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root 53980 18787 0 15:57 pts/5 00:00:00 grep --color=auto proxy
- 讀寫驗證
讀操作驗證:
[root@db02 ~]# mysql -umha -pmha -h 10.4.7.53 -P33060 #這里的端口是Atlas proxy-address 代理的端口 而不是mysql的端口
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 2
Server version: 5.0.81-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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.
db02 [(none)]>select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 52 |
+-------------+
1 row in set (0.00 sec)
db02 [(none)]>select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 53 |
+-------------+
1 row in set (0.00 sec)
讀操作的sever_id輪詢到52 53 證明讀寫分離 讀操作成功
寫操作驗證:
db02 [(none)]>begin;select @@server_id;commit; #這是三條語句,分號隔開的。
Query OK, 0 rows affected (0.00 sec)
+-------------+
| @@server_id |
+-------------+
| 51 |
+-------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
- 生產用戶要求 (Atlas+MHA+VIP+SENDREPORT+BINLOG)
需求:開發人員申請一個應用用戶 app( select update insert) 密碼123456,要通過10網段登錄
1. 在主庫中,創建用戶
grant select ,update,insert on *.* to app@'10.0.0.%' identified by '123456';
2. 在atlas中添加生產用戶
/usr/local/mysql-proxy/bin/encrypt 123456 ---->制作加密密碼
3. 改配置文件
vim test.cnf
pwds = rep:/iZxz+0GRoA=,mha:O2jBXONX098=,app:/iZxz+0GRoA=
/usr/local/mysql-proxy/bin/mysql-proxyd test restart
[root@db03 conf]# mysql -uapp -p123456 -h 10.4.7.53 -P 33060
1.1.2 Atlas基本管理
- Atlas基本管理
登錄:
[root@db03 ~]# mysql -uuser -ppwd -h 10.4.7.53 -P2345
查看幫助:
db03 [(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)
查看后端節點:
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.4.7.55:3306 | up | rw |
| 2 | 10.4.7.52:3306 | up | ro |
| 3 | 10.4.7.53:3306 | up | ro |
+-------------+----------------+-------+------+
3 rows in set (0.00 sec)
- 臨時關閉維護和開啟
SET OFFLINE $backend_id | offline backend server, $backend_id is backend_ndx's id |
| SET ONLINE $backend_id | online backend server, ...
例如:需要臨時維護一下53 ,就可以臨時關閉53,維護好之后在開啟
db03 [(none)]>set offline 3; #3是backend_ndx 索引號
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 3 | 10.4.7.53:3306 | offline | ro |
+-------------+----------------+---------+------+
1 row in set (0.00 sec)
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 1 | 10.4.7.55:3306 | up | rw |
| 2 | 10.4.7.52:3306 | up | ro |
| 3 | 10.4.7.53:3306 | offline | ro |
+-------------+----------------+---------+------+
3 rows in set (0.00 sec)
維護完成開啟
db03 [(none)]>set online 3;
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 3 | 10.4.7.53:3306 | unknown | ro |
+-------------+----------------+---------+------+
1 row in set (0.00 sec)
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.4.7.55:3306 | up | rw |
| 2 | 10.4.7.52:3306 | up | ro |
| 3 | 10.4.7.53:3306 | up | ro |
+-------------+----------------+-------+------+
- 動態刪除和添加從庫(讀庫)
刪除:
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.4.7.55:3306 | up | rw |
| 2 | 10.4.7.52:3306 | up | ro |
| 3 | 10.4.7.53:3306 | up | ro |
+-------------+----------------+-------+------+
3 rows in set (0.00 sec)
db03 [(none)]>remove backend 3;
Empty set (0.00 sec)
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.4.7.55:3306 | up | rw |
| 2 | 10.4.7.52:3306 | up | ro |
+-------------+----------------+-------+------+
2 rows in set (0.00 sec)
保存:
db03 [(none)]>SAVE CONFIG; 保存到配置文件
Empty set (0.00 sec)
查看配置文件:
[root@db03 /usr/local/mysql-proxy/bin]# grep -i read-only /usr/local/mysql-proxy/conf/test.cnf
proxy-read-only-backend-addresses=10.4.7.52:3306
#配置文件里只剩下52了
添加從庫(讀庫)
db03 [(none)]>add slave 10.4.7.53:3306;
Empty set (0.00 sec)
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.4.7.55:3306 | up | rw |
| 2 | 10.4.7.52:3306 | up | ro |
| 3 | 10.4.7.53:3306 | up | ro |
+-------------+----------------+-------+------+
3 rows in set (0.00 sec)
保存到配置文件:
db03 [(none)]>SAVE CONFIG;
Empty set (0.00 sec)
查看配置文件:
[root@db03 /usr/local/mysql-proxy/bin]# grep -i read-only /usr/local/mysql-proxy/conf/test.cnf
proxy-read-only-backend-addresses=10.4.7.52:3306,10.4.7.53:3306
#53添加成功
#注意最后一定要保存
- 動態刪除和添加用戶
刪除用戶:
刪除rep用戶:
db03 [(none)]>remove pwd rep;
Empty set (0.00 sec)
保存:
db03 [(none)]>save config;
Empty set (0.00 sec)
查看配置文件:
[root@db03 /usr/local/mysql-proxy/bin]# grep -i pwds /usr/local/mysql-proxy/conf/test.cnf
pwds=mha:O2jBXONX098=
#rep 用戶被刪除了
添加用戶:
db03 [(none)]>add pwd rep:123456; #add pwd 用戶:密碼
Empty set (0.00 sec)
保存:
db03 [(none)]>save config;
Empty set (0.00 sec)
查看配置文件:
[root@db03 /usr/local/mysql-proxy/bin]# grep -i pwds /usr/local/mysql-proxy/conf/test.cnf
pwds=mha:O2jBXONX098=,rep:/iZxz+0GRoA=
#添加成功
db03 [(none)]>SELECT * FROM pwds ;
+----------+--------------+
| username | password |
+----------+--------------+
| mha | O2jBXONX098= |
| rep | /iZxz+0GRoA= |
+----------+--------------+
2 rows in set (0.00 sec)
-----你努力獲得的,都是自己的,不容易忘掉。
星辰和大海都需要門票,詩和遠方的路費都很貴。來人間一次,努力成為最好的自己。人間值得,未來可期。


浙公網安備 33010602011771號