1. 程式人生 > 實用技巧 >MySQL Atlas 讀寫分離軟體介紹

MySQL Atlas 讀寫分離軟體介紹

MySQL Atlas介紹

目錄

一、MySQL Atlas介紹

1.1.1 MySQL Atlas介紹

  1. 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官方

  1. Atlas 部署
MHA+Atlas 高可用讀寫分離(部署前提有MHA高可用環境)
MHA高可用部署:https://www.cnblogs.com/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      #配置檔案

  1. 配置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
  1. 啟動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
  1. 讀寫驗證
讀操作驗證:
[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)

  1. 生產使用者要求 (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基本管理

  1. 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)
  1. 臨時關閉維護和開啟
 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   |
+-------------+----------------+-------+------+
  1. 動態刪除和新增從庫(讀庫)
刪除:
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新增成功

#注意最後一定要儲存
  1. 動態刪除和新增使用者
刪除使用者:
刪除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)