首先咱們來回顧下代理的概念,所謂代理就是指的是一端面向客戶端,另一端面向服務端,代理客戶端訪問服務端,咱們把這種代理叫正向代理;代理服務端響應客戶端咱們叫作反向代理,這個咱們在以前nginx系列博客中闡述過這樣的概念;不論是正向代理仍是反向代理他們都是代理,他們都有一個共同點就是表明一端(客戶端/服務端)訪問或響應另外一端;簡單講代理就是即充當服務端角色又充當客戶端角色;在mariadb的主從複製集羣中,讀的能力被擴展了,而寫的能力始終沒有被擴展;這樣一來對於主服務器就存在單點的問題,一般除了作雙主可解決主節點單點的問題,咱們還能夠給主節點作高可用;而對於mariadb的主從複製集羣來說,雖然讀的能力提高了,但一般狀況後端數據庫服務器是直接面向程序,這意味着程序要知道讀請求和寫請求該發往不一樣的數據庫服務器上;在用戶發來讀請求,這個程序它會分析用戶的請求,而後把用戶的請求代理到後端server上;也就是說咱們須要一個程序可以解析用戶的讀寫操做,把對應的操做代理到後端不一樣的節點上;這樣一來用戶的讀操做始終均衡的被調度到從節點,寫操做調度到主節點;proxysql這款軟件就有咱們上面說的功能,它可以將用戶發來的讀寫操做,經過proxysql的語句路由,把對應請求分別發送到不一樣節點執行;以下圖所示:node
從上面的圖片能夠看到,proxysql就是一代理,面向程序它就是一數據庫服務器,程序把讀操做和寫操做都發送給它,而後proxysql經過咱們定義的路由規則,把對應語句再代理到不一樣的後端主從架構節點上執行;接下來咱們來看看proxysql的配置和使用吧;如下實驗室基於mariadb的主從複製集羣上作的,有關主從複製的配置請參考https://www.cnblogs.com/qiuhom-1874/tag/mariadb%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6/;mysql
一、安裝proxysql(添加yum源,直接用yum安裝)nginx
二、查看proxysql的簡介git
[root@lxc ~]# yum info proxysql Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Available Packages Name : proxysql Arch : x86_64 Version : 2.0.12 Release : 1 Size : 9.8 M Repo : proxysql_repo/7 Summary : A high-performance MySQL proxy URL : https://proxysql.com/ License : GPL+ Description : A high-performance MySQL proxy [root@lxc ~]#
提示:若是添加了yum直接yum info proxysql可以看到以上信息,表示咱們添加到yum源已經生效;github
三、安裝proxysqlsql
[root@lxc ~]# yum install proxysql Loaded plugins: fastestmirror proxysql_repo | 2.9 kB 00:00:00 Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ---> Package proxysql.x86_64 0:2.0.12-1 will be installed --> Processing Dependency: libgnutls.so.28(GNUTLS_3_1_0)(64bit) for package: proxysql-2.0.12-1.x86_64 --> Processing Dependency: libgnutls.so.28(GNUTLS_3_0_0)(64bit) for package: proxysql-2.0.12-1.x86_64 --> Processing Dependency: gnutls for package: proxysql-2.0.12-1.x86_64 --> Processing Dependency: libgnutls.so.28(GNUTLS_1_4)(64bit) for package: proxysql-2.0.12-1.x86_64 --> Processing Dependency: libgnutls.so.28()(64bit) for package: proxysql-2.0.12-1.x86_64 --> Running transaction check ---> Package gnutls.x86_64 0:3.3.29-9.el7_6 will be installed --> Processing Dependency: trousers >= 0.3.11.2 for package: gnutls-3.3.29-9.el7_6.x86_64 --> Processing Dependency: libnettle.so.4()(64bit) for package: gnutls-3.3.29-9.el7_6.x86_64 --> Processing Dependency: libhogweed.so.2()(64bit) for package: gnutls-3.3.29-9.el7_6.x86_64 --> Running transaction check ---> Package nettle.x86_64 0:2.7.1-8.el7 will be installed ---> Package trousers.x86_64 0:0.3.14-2.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ===================================================================================================================================================================== Package Arch Version Repository Size ===================================================================================================================================================================== Installing: proxysql x86_64 2.0.12-1 proxysql_repo 9.8 M Installing for dependencies: gnutls x86_64 3.3.29-9.el7_6 base 680 k nettle x86_64 2.7.1-8.el7 base 327 k trousers x86_64 0.3.14-2.el7 base 289 k Transaction Summary ===================================================================================================================================================================== Install 1 Package (+3 Dependent packages) Total download size: 11 M Installed size: 41 M Is this ok [y/d/N]: y Downloading packages: (1/4): gnutls-3.3.29-9.el7_6.x86_64.rpm | 680 kB 00:00:00 (2/4): nettle-2.7.1-8.el7.x86_64.rpm | 327 kB 00:00:00 (3/4): trousers-0.3.14-2.el7.x86_64.rpm | 289 kB 00:00:00 warning: /var/cache/yum/x86_64/7/proxysql_repo/packages/proxysql-2.0.12-1-centos7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 79953b49: NOKEYMB 00:00:04 ETA Public key for proxysql-2.0.12-1-centos7.x86_64.rpm is not installed (4/4): proxysql-2.0.12-1-centos7.x86_64.rpm | 9.8 MB 00:28:05 --------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 6.7 kB/s | 11 MB 00:28:05 Retrieving key from https://repo.proxysql.com/ProxySQL/repo_pub_key Importing GPG key 0x79953B49: Userid : "rene cannnao (Proxysql Repository) <rene.cannao@gmail.com>" Fingerprint: 1448 bf69 3ca6 00c7 99eb 9358 04a5 62fb 7995 3b49 From : https://repo.proxysql.com/ProxySQL/repo_pub_key Is this ok [y/N]: y Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : trousers-0.3.14-2.el7.x86_64 1/4 Installing : nettle-2.7.1-8.el7.x86_64 2/4 Installing : gnutls-3.3.29-9.el7_6.x86_64 3/4 Installing : proxysql-2.0.12-1.x86_64 4/4 warning: group proxysql does not exist - using root warning: group proxysql does not exist - using root Created symlink from /etc/systemd/system/multi-user.target.wants/proxysql.service to /etc/systemd/system/proxysql.service. Verifying : gnutls-3.3.29-9.el7_6.x86_64 1/4 Verifying : proxysql-2.0.12-1.x86_64 2/4 Verifying : nettle-2.7.1-8.el7.x86_64 3/4 Verifying : trousers-0.3.14-2.el7.x86_64 4/4 Installed: proxysql.x86_64 0:2.0.12-1 Dependency Installed: gnutls.x86_64 0:3.3.29-9.el7_6 nettle.x86_64 0:2.7.1-8.el7 trousers.x86_64 0:0.3.14-2.el7 Complete! [root@lxc ~]#
提示:除此以上方式安裝proxysql,固然也能夠直接使用wget 去github上的項目地址下載docker
[root@lxc ~]# wget https://github.com/sysown/proxysql/releases/download/v2.0.12/proxysql-2.0.12-1-centos7.x86_64.rpm
提示:下載好proxysql 包後,而後經過yum直接安裝便可;推薦使用yum來安裝下載好的包,它能夠解決依賴關係,不推薦使用rpm 安裝;數據庫
四、查看proxysql包安裝的文件列表後端
[root@lxc ~]# rpm -ql proxysql /etc/logrotate.d/proxysql /etc/proxysql.cnf /etc/systemd/system/proxysql-initial.service /etc/systemd/system/proxysql.service /usr/bin/proxysql /usr/share/proxysql/tools/proxysql_galera_checker.sh /usr/share/proxysql/tools/proxysql_galera_writer.pl [root@lxc ~]#
提示:從上面的信息能夠看到proxysql的配置文件是/etc/proxysql.cnf,二進制文件是/usr/bin/proxysql,unit file是/etc/systemd/system/proxysql.servicecentos
五、配置proxysql
[root@lxc ~]# cat /etc/proxysql.cnf ######################################################################################## datadir="/var/lib/proxysql" errorlog="/var/lib/proxysql/proxysql.log" admin_variables= { admin_credentials="admin:admin" # mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock" mysql_ifaces="0.0.0.0:6033" # refresh_interval=2000 # debug=true } mysql_variables= { threads=4 max_connections=2048 default_query_delay=0 default_query_timeout=36000000 have_compress=true poll_timeout=2000 # interfaces="0.0.0.0:6033;/tmp/proxysql.sock" interfaces="0.0.0.0:3306" default_schema="information_schema" stacksize=1048576 server_version="5.5.30" connect_timeout_server=3000 # make sure to configure monitor username and password # https://github.com/sysown/proxysql/wiki/Global-variables#mysql-monitor_username-mysql-monitor_password monitor_username="monitor" monitor_password="monitor" monitor_history=600000 monitor_connect_interval=60000 monitor_ping_interval=10000 monitor_read_only_interval=1500 monitor_read_only_timeout=500 ping_interval_server_msec=120000 ping_timeout_server=500 commands_stats=true sessions_sort=true connect_retries_on_failure=10 } # defines all the MySQL servers mysql_servers = ( { address = "192.168.0.22" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain port = 3306 # no default, required . If port is 0 , address is interpred as a Unix Socket Domain hostgroup = 3 # no default, required status = "ONLINE" # default: ONLINE weight = 1 # default: 1 compression = 0 # default: 0 max_replication_lag = 10 # default 0 . If greater than 0 and replication lag passes such threshold, the server is shunned }, { address = "192.168.0.23" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain port = 3306 # no default, required . If port is 0 , address is interpred as a Unix Socket Domain hostgroup = 4 # no default, required status = "ONLINE" # default: ONLINE weight = 1 # default: 1 compression = 0 # default: 0 max_replication_lag = 10 # default 0 . If greater than 0 and replication lag passes such threshold, the server is shunned } ) # defines all the MySQL users mysql_users: ( { username = "root" # no default , required password = "admin123.com" # default: '' default_hostgroup = 3 # default: 0 active = 1 # default: 1 } ) #defines MySQL Query Rules mysql_query_rules: ( { rule_id=1 active=1 match_pattern="^SELECT .* FOR UPDATE$" destination_hostgroup=3 apply=1 }, { rule_id=2 active=1 match_pattern="^SELECT" destination_hostgroup=4 apply=1 } ) mysql_replication_hostgroups= ( { writer_hostgroup=3 reader_hostgroup=4 comment="test repl 1" } ) [root@lxc ~]#
提示:proxysql配置文件分幾個大段,admin_variables這個段裏主要配置管理相關變量,好比管理用戶名和密碼,監聽的地址和端口等等;mysql_variables配置proxysql鏈接後端mysql相關配置,裏面有鏈接後端的server監聽的端口,線程數,最大鏈接數,以及監控相關參數;mysql_servers配置後端mysql/mariadb服務器的地址端口,以及權重,所屬組等;mysql_users配置鏈接後端mysql/mariadb所需的帳號和密碼以及默認鏈接到的組;mysql_query_rules配置查詢規則,那些語句是寫操做,那些語句是讀操做;mysql_replication_hostgroups配置寫操做對應的組號和讀操做對應組號;這裏須要注意一點,若是一個配置段中有多個大括號,相互之間用逗號隔離,若是是最後一個大括號須要把後面的逗號去掉;
六、在主從複製節點建立上面配置的帳號信息
建立root帳號
[root@docker_node01 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 5.5.65-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> select user,host,password from mysql.user; +---------+-------------------+-------------------------------------------+ | user | host | password | +---------+-------------------+-------------------------------------------+ | root | localhost | | | root | docker\_node01.io | | | root | 127.0.0.1 | | | root | ::1 | | | rpluser | 192.168.0.% | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | +---------+-------------------+-------------------------------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> grant all on *.* to 'root'@'192.168.0.%' identified by 'admin123.com' with grant option; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> select user,host,password from mysql.user; +---------+-------------------+-------------------------------------------+ | user | host | password | +---------+-------------------+-------------------------------------------+ | root | localhost | | | root | docker\_node01.io | | | root | 127.0.0.1 | | | root | ::1 | | | rpluser | 192.168.0.% | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | | root | 192.168.0.% | *2D9AC2437F9E59A51BE8BA89A3D59E76F32F55E8 | +---------+-------------------+-------------------------------------------+ 6 rows in set (0.00 sec) MariaDB [(none)]>
提示:在主節點建立帳號會自動同步到從節點,因此從節點能夠不用再建立;
建立monitor帳號
MariaDB [(none)]> grant all on *.* to 'monitor'@'192.168.0.%' identified by 'monitor' with grant option; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> select user,host,password from mysql.user; +---------+-------------------+-------------------------------------------+ | user | host | password | +---------+-------------------+-------------------------------------------+ | root | localhost | | | root | docker\_node01.io | | | root | 127.0.0.1 | | | root | ::1 | | | rpluser | 192.168.0.% | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | | root | 192.168.0.% | *2D9AC2437F9E59A51BE8BA89A3D59E76F32F55E8 | | monitor | 192.168.0.% | *1975D095AC033CAF4E1BF94F7202A9BBFEEB66F1 | +---------+-------------------+-------------------------------------------+ 7 rows in set (0.00 sec) MariaDB [(none)]>
七、啓動proxysql
提示:能夠看到咱們配置的proxysql管理端監聽在6033,啓動4個線程來鏈接後端mariadb服務器,對應的端口都已經處於監聽狀態了;
八、鏈接管理端口
[root@lxc ~]# mysql -uadmin -padmin -h192.168.0.21 -P6033 ERROR 1040 (42000): User 'admin' can only connect locally [root@lxc ~]# mysql -uadmin -padmin -h127.0.0.1 -P6033 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 7 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.001 sec) MySQL [(none)]>
提示:管理接口默認只能本地鏈接,可是咱們配置管理接口監聽在本機全部地址上,因此鏈接時指定主機地址要使用127.0.0.1才能夠,若是使用localhost,它默認會經過sock文件去鏈接;從上面的結果能夠看到,在proxysql上有5個庫,其中main庫主要存放咱們剛纔配置相關的表在裏面;以下
MySQL [(none)]> use main Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MySQL [main]> show tables; +----------------------------------------------------+ | tables | +----------------------------------------------------+ | global_variables | | mysql_aws_aurora_hostgroups | | mysql_collations | | mysql_firewall_whitelist_rules | | mysql_firewall_whitelist_sqli_fingerprints | | mysql_firewall_whitelist_users | | mysql_galera_hostgroups | | mysql_group_replication_hostgroups | | mysql_query_rules | | mysql_query_rules_fast_routing | | mysql_replication_hostgroups | | mysql_servers | | mysql_users | | proxysql_servers | | restapi_routes | | runtime_checksums_values | | runtime_global_variables | | runtime_mysql_aws_aurora_hostgroups | | runtime_mysql_firewall_whitelist_rules | | runtime_mysql_firewall_whitelist_sqli_fingerprints | | runtime_mysql_firewall_whitelist_users | | runtime_mysql_galera_hostgroups | | runtime_mysql_group_replication_hostgroups | | runtime_mysql_query_rules | | runtime_mysql_query_rules_fast_routing | | runtime_mysql_replication_hostgroups | | runtime_mysql_servers | | runtime_mysql_users | | runtime_proxysql_servers | | runtime_restapi_routes | | runtime_scheduler | | scheduler | +----------------------------------------------------+ 32 rows in set (0.001 sec) MySQL [main]> select * from mysql_servers; +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | 3 | 192.168.0.22 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | | 4 | 192.168.0.23 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ 2 rows in set (0.001 sec) MySQL [main]>
提示:main庫中的表以runtime開頭的表示當前生效的配置,若是咱們要運行時修改配置,咱們須要先修改不是runtime開頭的表,而後經過load 加載到runtime開頭的表中,而後在save存儲到文件;
proxysql的多層配置系統
+-------------------------+ | RUNTIME | +-------------------------+ /|\ | | | [1] | [2] | | \|/ +-------------------------+ | MEMORY | +-------------------------+ _ /|\ | |\ | | \ [3] | [4] | \ [5] | \|/ \ +-------------------------+ +-------------------------+ | DISK | | CONFIG FILE | +-------------------------+ +-------------------------+
提示:從上面的圖能夠看到proxysql的配置系統分三層,最上面一層是runtime層,該層主要是說經過讀取第二層的memory層,而memory主要經過disk或configfile層來獲取配置;第一次啓動proxysql時,它會從config file層讀取配置文件內容到memory層,而後runtime層讀取memory層,隨後咱們就能夠經過修改memory層,而後經過load加載到runtime層,或者save存儲到disk層;
在各層間移動配置
MySQL user相關操做: LOAD MYSQL USERS FROM MEMORY / LOAD MYSQL USERS TO RUNTIME 將MySQL user從內存數據庫加載到運行時數據結構,反之亦然 SAVE MYSQL USERS TO MEMORY / SAVE MYSQL USERS FROM RUNTIME 將MySQL user從運行時持久化到內存數據庫 LOAD MYSQL USERS TO MEMORY / LOAD MYSQL USERS FROM DISK 將持久化的MySQL user從磁盤數據庫加載到內存數據庫 SAVE MYSQL USERS FROM MEMORY / SAVE MYSQL USERS TO DISK 將MySQL user從內存數據庫持久化到磁盤數據庫 LOAD MYSQL USERS FROM CONFIG 從配置文件中將MySQL user加載到內存數據庫中 MySQL server相關操做: LOAD MYSQL SERVERS FROM MEMORY / LOAD MYSQL SERVERS TO RUNTIME 將MySQL server從內存數據庫加載到運行時 SAVE MYSQL SERVERS TO MEMORY / SAVE MYSQL SERVERS FROM RUNTIME 將MySQL server從運行時持久化到內存數據庫 LOAD MYSQL SERVERS TO MEMORY / LOAD MYSQL SERVERS FROM DISK 將MySQL server從磁盤數據庫加載到內存數據庫 SAVE MYSQL SERVERS FROM MEMORY / SAVE MYSQL SERVERS TO DISK 將MySQL server從內存數據庫持久化到磁盤數據庫 LOAD MYSQL SERVERS FROM CONFIG 從配置文件將服務器加載到內存數據庫中 MySQL query rules相關操做: LOAD MYSQL QUERY RULES FROM MEMORY / LOAD MYSQL QUERY RULES TO RUNTIME 將MySQL查詢規則從內存數據庫加載到運行時數據結構 SAVE MYSQL QUERY RULES TO MEMORY / SAVE MYSQL QUERY RULES FROM RUNTIME 將MySQL查詢規則從運行時數據結構持久化到內存數據庫 LOAD MYSQL QUERY RULES TO MEMORY / LOAD MYSQL QUERY RULES FROM DISK 將MySQL查詢規則從磁盤數據庫加載到內存數據庫 SAVE MYSQL QUERY RULES FROM MEMORY / SAVE MYSQL QUERY RULES TO DISK 將MySQL查詢規則從內存數據庫持久化到磁盤數據庫 LOAD MYSQL QUERY RULES FROM CONFIG 從配置文件查詢規則加載到內存數據庫中 MySQL variables相關操做: LOAD MYSQL VARIABLES FROM MEMORY / LOAD MYSQL VARIABLES TO RUNTIME 將MySQL變量從內存數據庫加載到運行時數據結構 SAVE MYSQL VARIABLES FROM MEMORY / SAVE MYSQL VARIABLES TO DISK 將MySQL變量從內存數據庫持久化到磁盤數據庫 LOAD MYSQL VARIABLES TO MEMORY / LOAD MYSQL VARIABLES FROM DISK 將MySQL變量從磁盤數據庫加載到內存數據庫 SAVE MYSQL VARIABLES TO MEMORY / SAVE MYSQL VARIABLES FROM RUNTIME 將MySQL變量從運行時數據結構持久存儲到內存數據庫中 LOAD MYSQL VARIABLES FROM CONFIG 將配置文件變量加載到內存數據庫中 admin variables相關操做: LOAD ADMIN VARIABLES FROM MEMORY / LOAD ADMIN VARIABLES TO RUNTIME 將內存數據庫中的管理變量加載到運行時 SAVE ADMIN VARIABLES TO MEMORY / SAVE ADMIN VARIABLES FROM RUNTIME 將管理變量從運行時持久化到內存數據庫 LOAD ADMIN VARIABLES TO MEMORY / LOAD ADMIN VARIABLES FROM DISK 將管理變量從磁盤數據庫加載到內存數據庫 SAVE ADMIN VARIABLES FROM MEMORY / SAVE ADMIN VARIABLES TO DISK 將管理變量從內存數據庫持久化到磁盤數據庫 LOAD ADMIN VARIABLES FROM CONFIG 將配置文件管理變量加載到內存數據庫中 注意:以上命令容許使用如下快捷方式: MEM for MEMORY RUN for RUNTIME 例如,這兩個命令是等效的: SAVE ADMIN VARIABLES TO MEMORY SAVE ADMIN VARIABLES TO MEM
提示:以上命令在配置更改加載到RUNTIME以前不會激活任何修改,也就是說只有runtime中的配置生效;
測試:在線增長後端mysql server的地址
[root@lxc ~]# mysql -uadmin -padmin -h127.0.0.1 -P6033 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 3 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)]> select * from mysql_servers; +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | 3 | 192.168.0.22 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | | 4 | 192.168.0.23 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ 2 rows in set (0.001 sec) MySQL [(none)]> insert into mysql_servers values (4,'192.168.0.21',3307,0,'ONLINE',1,0,1000,10,0,0,''); Query OK, 1 row affected (0.001 sec) MySQL [(none)]> select * from mysql_servers; +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | 3 | 192.168.0.22 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | | 4 | 192.168.0.23 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | | 4 | 192.168.0.21 | 3307 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ 3 rows in set (0.001 sec) MySQL [(none)]>
提示:以上操做只在memory層中修改,並未加載到runtime,因此在runtime_mysql_servers表中仍是兩臺後端server
加載mysql server memory層到runtime層
MySQL [(none)]> show tables; +----------------------------------------------------+ | tables | +----------------------------------------------------+ | global_variables | | mysql_aws_aurora_hostgroups | | mysql_collations | | mysql_firewall_whitelist_rules | | mysql_firewall_whitelist_sqli_fingerprints | | mysql_firewall_whitelist_users | | mysql_galera_hostgroups | | mysql_group_replication_hostgroups | | mysql_query_rules | | mysql_query_rules_fast_routing | | mysql_replication_hostgroups | | mysql_servers | | mysql_users | | proxysql_servers | | restapi_routes | | runtime_checksums_values | | runtime_global_variables | | runtime_mysql_aws_aurora_hostgroups | | runtime_mysql_firewall_whitelist_rules | | runtime_mysql_firewall_whitelist_sqli_fingerprints | | runtime_mysql_firewall_whitelist_users | | runtime_mysql_galera_hostgroups | | runtime_mysql_group_replication_hostgroups | | runtime_mysql_query_rules | | runtime_mysql_query_rules_fast_routing | | runtime_mysql_replication_hostgroups | | runtime_mysql_servers | | runtime_mysql_users | | runtime_proxysql_servers | | runtime_restapi_routes | | runtime_scheduler | | scheduler | +----------------------------------------------------+ 32 rows in set (0.001 sec) MySQL [(none)]> select * from runtime_mysql_servers; +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | 3 | 192.168.0.22 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | | 4 | 192.168.0.23 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ 2 rows in set (0.006 sec) MySQL [(none)]> select * from mysql_servers; +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | 3 | 192.168.0.22 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | | 4 | 192.168.0.23 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | | 4 | 192.168.0.21 | 3307 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ 3 rows in set (0.001 sec) MySQL [(none)]> load mysql servers to runtime; Query OK, 0 rows affected (0.015 sec) MySQL [(none)]> select * from runtime_mysql_servers; +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | 3 | 192.168.0.22 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | | 4 | 192.168.0.21 | 3307 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | | 4 | 192.168.0.23 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | | +--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ 3 rows in set (0.006 sec) MySQL [(none)]>
提示:能夠看到執行load操做後,runtime_mysql_servers就加載進去了;此時咱們就把memory層的信息加載到runtime層生效了;
驗證:經過鏈接proxysql所在主機的3306端口,執行寫操做,看看是否可以同步到其餘從節點?
提示:能夠看到在proxysql所在主機鏈接3306線程,是能夠鏈接到主庫,由於默認配置鏈接是到主庫,而後在其上建立一個mydb的數據庫,也可以及時的同步到其餘從庫;
測試:讀寫操做是否分別調度到不一樣的節點上?
[root@lxc ~]# for i in {1..10} ;do mysql -uroot -padmin123.com -P3306 -h192.168.0.21 -e "select @@server_id" ; done +-------------+ | @@server_id | +-------------+ | 2 | +-------------+ +-------------+ | @@server_id | +-------------+ | 2 | +-------------+ +-------------+ | @@server_id | +-------------+ | 2 | +-------------+ +-------------+ | @@server_id | +-------------+ | 3 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 3 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 3 | +-------------+ [root@lxc ~]# for i in {1..10} ;do mysql -uroot -padmin123.com -P3306 -h192.168.0.21 -e "select @@server_id for update" ; done +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ [root@lxc ~]#
提示:能夠看到當執行有寫鎖時,就馬上把操做調度到server_id=1的主庫上去操做;讀操做是隨機調度到各個節點上進行處理;