Mysql中介軟體——ProxySQL
ProxySQL
ProxySQL是靈活強大的MySQL代理層, 是一個能實實在在用在生產環境的MySQL中介軟體,可以實現讀寫分離,支援 Query 路由功能,支援動態指定某個 SQL 進行 cache,支援動態載入配置、故障切換和一些 SQL的過濾功能。還有一些同類產品比如 DBproxy、MyCAT、OneProxy 等。但經過反覆對比和測試之後,還是覺得ProxySQL是一款效能不諳,靠譜穩定的MySQL 中介軟體產品.
ProxySQL特點
- 支援動態載入配置,即一般可以線上修改配置,但有少部分引數還是需要重啟來生效。
- 將所有配置儲存寫入到SQLit表中
- 實現讀寫分離
ProxySQL執行機制
- runtime:執行中使用的配置檔案
- memory:提供使用者動態修改配置檔案
- disk:將修改的配置儲存到磁碟SQLit表中(即:proxysql.db)
- RUNTIME層
代表的是ProxySQL當前生效的配置,包括 global_variables, mysql_servers, mysql_users, mysql_query_rules。無法直接修改這裡的配置,必須要從下一層load進來。
- MEMORY層
是平時在mysql命令列修改的 main 裡頭配置,可以認為是SQLite資料庫在記憶體的映象。我們通過Set修改配置也是先修改此層的內容。
- DISK層
持久儲存的那份配置,一般在$(DATADIR)/proxysql.db,在重啟的時候會從硬盤裡載入。 /etc/proxysql.cnf檔案只在第一次初始化的時候用到,完了後,如果要修改監聽埠等,還是需要在管理命令列裡修改,再save到硬碟。
ProxySQL安裝
## 配置proxysql源 [root@localhost yum.repos.d]# vim ProxySQL.repo [proxysql_repo] name=ProxySQL baseurl=http://repo.proxysql.com/ProxySQL/proxysql-2.0.x/centos/8 gpgcheck=1 gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key [root@localhost ~]# rpm -ivh proxysql-2.0.15-1-centos8.x86_64.rpm warning: proxysql-2.0.15-1-centos8.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 79953b49: NOKEY Verifying... ################################# [100%] Preparing... ################################# [100%] Updating / installing... 1:proxysql-2.0.15-1 warning: group proxysql does not exist - using root warning: group proxysql does not exist - using root ################################# [100%] Created symlink /etc/systemd/system/multi-user.target.wants/proxysql.service → /etc/systemd/system/proxysql.service. ## 啟動proxysql服務 [root@localhost ~]# systemctl enable --now proxysql [root@localhost yum.repos.d]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:111 0.0.0.0:* LISTEN 0 128 0.0.0.0:6032 0.0.0.0:* LISTEN 0 128 0.0.0.0:6033 0.0.0.0:* LISTEN 0 128 0.0.0.0:6033 0.0.0.0:* LISTEN 0 128 0.0.0.0:6033 0.0.0.0:* LISTEN 0 128 0.0.0.0:6033 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:111 [::]:* LISTEN 0 128 [::]:22 [::]:*
ProxySQL的Admin管理介面
當Proxysql啟動後,將監聽兩個埠:
- admin管理介面,預設埠為6032。該埠用於檢視、配置ProxySQL
- 接收SQL語句的介面,預設埠為6033,這個介面類似於MySQL的3306埠
本地使用admin管理ProxySQL(admin是預設管理使用者,只允許本地登入)
[root@localhost ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
## 檢視sqlite庫中資料庫
MySQL [(none)]> show databases;
+-----+---------------+-------------------------------------+
| seq | name | file |
+-----+---------------+-------------------------------------+
| 0 | main | |
| 2 | disk | /var/lib/proxysql/proxysql.db |
| 3 | stats | |
| 4 | monitor | |
| 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
5 rows in set (0.001 sec)
由於 ProxySQL 的配置全部儲存在幾個自帶的庫中,所以通過管理介面,可以非常方便地通過傳送一些SQL命令去修改 ProxySQL 的配置。 ProxySQL 會解析通過該介面傳送的某些對ProxySQL 有效的特定命令,並將其合理轉換後傳送給內嵌的 SQLite3 資料庫引擎去執行
ProxySQL 的配置幾乎都是通過管理介面來操作的,通過 Admin 管理介面,可以線上修改幾乎所有的配置並使其生效。只有兩個變數的配置是必須重啟 ProxySQL 才能生效的,它們是:
mysql-threads 和 mysql-stacksize
admin相關變數詳解
admin-admin_credentials
admin-admin_credentials 變數控制的是admin管理介面的管理員賬戶。預設的管理員賬戶和密碼為admin:admin,但是這個預設的使用者只能在本地使用。如果想要遠端連線到ProxySQL,例如用windows上的navicat連線Linux上的ProxySQL管理介面,必須自定義一個管理員賬戶.
- 新增新管理員賬戶,並在異地登入ProxySQL庫
MySQL [(none)]> select @@admin-admin_credentials
-> ;
+---------------------------+
| @@admin-admin_credentials |
+---------------------------+
| admin:admin |
+---------------------------+
1 row in set (0.002 sec)
## 新增新使用者
MySQL [(none)]> set admin-admin_credentials='admin:admin;testuser:123456';
Query OK, 1 row affected (0.001 sec)
MySQL [(none)]> select @@admin-admin_credentials;
+-----------------------------+
| @@admin-admin_credentials |
+-----------------------------+
| admin:admin;testuser:123456 |
+-----------------------------+
1 row in set (0.002 sec)
## 載入到runtime層,立即生效;儲存到disk層,下午重啟服務也能生效。
MySQL [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.001 sec)
MySQL [(none)]> save admin variables to disk;
Query OK, 35 rows affected (0.004 sec)
## 遠端登入proxysql實現admin介面管理
[root@localhost ~]# mysql -utestuser -p123456 -P6032 -h192.168.197.141 ## 指定資料庫存在的ip地址
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.5.30 (ProxySQL Admin Module)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+-----+---------------+-------------------------------------+
| seq | name | file |
+-----+---------------+-------------------------------------+
| 0 | main | |
| 2 | disk | /var/lib/proxysql/proxysql.db |
| 3 | stats | |
| 4 | monitor | |
| 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
5 rows in set (0.002 sec)
- windows上利用navicat工具檢視disk庫資料
admin-stats_credentials
admin-stats_credentials 變數控制admin管理介面的普通使用者,這個變數中的使用者沒有超級管理員許可權,只能檢視monitor庫和main庫中關於統計的資料,其它庫都是不可見的,且沒有任何寫許可權
MySQL [(none)]> select @@admin-stats_credentials;
+---------------------------+
| @@admin-stats_credentials |
+---------------------------+
| stats:stats |
+---------------------------+
## 新增新的普通使用者
MySQL [(none)]> set admin-stats_credentials='selectuser:123456';
Query OK, 1 row affected (0.001 sec)
## 重新整理配置檔案
MySQL [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.000 sec)
MySQL [(none)]> save admin variables to disk;
Query OK, 35 rows affected (0.004 sec)
## 遠端訪問,檢視資料庫
[root@localhost ~]# mysql -uselectuser -p123456 -P6032 -h192.168.197.141;
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.30 (ProxySQL Admin Module)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+-----+---------------+-------------------------------------+
| seq | name | file |
+-----+---------------+-------------------------------------+
| 0 | main | |
| 2 | monitor | |
| 3 | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
3 rows in set (0.002 sec)
admin-mysql_ifaces
admin-mysql_ifaces 變數指定admin介面的監聽地址,格式為冒號分隔的hostname:port列表。預設監聽在 0.0.0.0:6032
MySQL [(none)]> select @@admin-mysql_ifaces;
+----------------------+
| @@admin-mysql_ifaces |
+----------------------+
| 0.0.0.0:6032 |
+----------------------+
1 row in set (0.002 sec)
## 修改預設管理埠
MySQL [(none)]> set admin-mysql_ifaces='0.0.0.0:1995';
Query OK, 1 row affected (0.001 sec)
MySQL [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.002 sec)
MySQL [(none)]> save admin variables to disk;
Query OK, 35 rows affected (0.004 sec)
## 再次檢視變數及服務埠號
MySQL [(none)]> select @@admin-mysql_ifaces;
+----------------------+
| @@admin-mysql_ifaces |
+----------------------+
| 0.0.0.0:1995 |
+----------------------+
1 row in set (0.003 sec)
[root@localhost proxysql]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:6033 0.0.0.0:*
LISTEN 0 128 0.0.0.0:6033 0.0.0.0:*
LISTEN 0 128 0.0.0.0:6033 0.0.0.0:*
LISTEN 0 128 0.0.0.0:6033 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:1995 0.0.0.0:* ## 6032埠變為1995
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*