溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

數(shù)據(jù)庫(kù)中間件ProxySQL讀寫自動(dòng)分離實(shí)踐

發(fā)布時(shí)間:2020-07-25 15:15:13 來(lái)源:網(wǎng)絡(luò) 閱讀:249 作者:三杯水 欄目:MySQL數(shù)據(jù)庫(kù)

參考文檔

https://github.com/sysown/proxysql
http://www.fordba.com/mysql_proxysql_rw_split.html
https://www.cnblogs.com/zhoujinyi/p/6829983.html

騰訊云cdb可以提供主庫(kù)VIP+只讀從庫(kù)VIP的形式,這樣開發(fā)使用時(shí)需要維護(hù)2個(gè)VIP而且不能自動(dòng)讀寫分離,基于現(xiàn)狀計(jì)劃使用proxysql結(jié)合clb及cdb來(lái)實(shí)現(xiàn)讀寫自動(dòng)分離。
架構(gòu)如下:
app--clb四層--proxysql--主vip+自讀vip

一.ProxySQL部署
1.下載安裝啟動(dòng)

[root@VM_8_24_centos ~]# wget?https://github.com/sysown/proxysql/releases/download/v2.0.8/proxysql-2.0.8-1-centos7.x86_64.rpm
[root@VM_8_24_centos ~]# rpm -ivh proxysql-2.0.8-1-centos7.x86_64.rpm
[root@VM_8_24_centos ~]# systemctl start proxysql
[root@VM_8_24_centos ~]# systemctl enable proxysql
[root@VM_8_24_centos ~]# netstat -tulpn |grep 603
tcp        0      0 0.0.0.0:6032            0.0.0.0:*               LISTEN      26063/proxysql      
tcp        0      0 0.0.0.0:6033            0.0.0.0:*               LISTEN      26063/proxysql 

2.登陸測(cè)試

[root@VM_8_24_centos ~]# mysql -uadmin -padmin -h 127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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.01 sec)

MySQL [(none)]> 

二.ProxySQL配置

1.架構(gòu)
node1 (vip 192.168.0.9:3306) , mysql master
node2 (vip 192.168.0.42:3306) , mysql slave
prxoy (192.168.8.24:6033) , proxysql
clb (vip 192.168.9.27:6033), clb proxysql
app (192.168.0.26), mysql client

2.proxysql上添加mysql主從數(shù)據(jù)庫(kù)信息
proxysql上執(zhí)行,將主庫(kù)master也就是做寫入的節(jié)點(diǎn)放到group 0中,salve節(jié)點(diǎn)做讀放到group 1中
語(yǔ)法如下:

insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(0,'192.168.0.9',3306,1,2000,10,'TESTuser');
insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(1,'192.168.0.42',3306,1,2000,10,'TESTuser');
select * from mysql_servers;

例子:

MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,ax_replication_lag,comment) values(0,'192.168.0.9',3306,1,2000,10,'TESTuser');
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,ax_replication_lag,comment) values(1,'192.168.0.42',3306,1,2000,10,'TESTuser');
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> select * from mysql_servers;
+--------------+-----------+------+--------+--------+-------------+-----------------+--------------------+---------+----------------+---------+
| hostgroup_id | hostname  | port | status | weight | compression | max_connections | max_relication_lag | use_ssl | max_latency_ms | comment |
+--------------+-----------+------+--------+--------+-------------+-----------------+--------------------+---------+----------------+---------+
| 0            | 192.168.0.9  | 3306 | ONLINE | 1      | 0           | 2000            | 10                 | 0       | 0              | TESTuser    |
| 1            | 192.168.0.42 | 3306 | ONLINE | 1      | 0           | 2000            | 10                 | 0       | 0              | TESTuser    |
+--------------+-----------+------+--------+--------+-------------+-----------------+--------------------+---------+----------------+---------+
2 rows in set (0.01 sec)

3.mysql主庫(kù)上添加proxysql監(jiān)控賬號(hào)及應(yīng)用程序操作賬號(hào)
mysql master上執(zhí)行,注意應(yīng)用程序操作賬號(hào)根據(jù)實(shí)際情況授予權(quán)限,最好只授權(quán)給proxysql主機(jī)
語(yǔ)法如下:

grant usage on *.* to 'proxysql'@'192.168.8.24' identified by 'ProxySQL@20191226';
grant select, insert, update, delete ON *.* TO 'testuser'@'192.168.8.24' identified by 'TESTuser@20191226';
flush privileges;
select host,user from mysql.user;

例子:

MySQL [(none)]> grant usage on *.* to 'proxysql'@'192.168.8.24' identified by 'ProxySQL@20191226';
Query OK, 0 rows affected, 1 warning (0.00 sec)

MySQL [(none)]> grant select, insert, update, delete ON *.* TO 'testuser'@'192.168.8.24' identified by 'TESTuser@20191226';
Query OK, 0 rows affected, 1 warning (0.00 sec)

MySQL [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> select host,user from mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | TESTuser_data_servic |
| %         | TESTuser_rule_rw     |
| 192.168.8.24 | proxysql         |
| 192.168.8.24 | testuser         |
| 127.0.0.1 | root             |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
7 rows in set (0.01 sec)

4.proxysql上添加應(yīng)用操作賬號(hào)
proxysql上執(zhí)行,注意應(yīng)用賬號(hào)授權(quán)給主hostgroup,這里是0
語(yǔ)法如下:

insert into mysql_users(username,password,active,default_hostgroup) values ('testuser','TESTuser@20191226',1,0);
select * from mysql_users;

例子:

MySQL [(none)]> insert into mysql_users(username,password,active,default_hostgroup) values ('testuser','TESTuser@20191226',1,0);
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> select * from mysql_users;
+----------+---------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| username | password      | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
+----------+---------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| testuser | TESTuser@20191226 | 1      | 0       | 0                 | NULL           | 0             | 1                      | 0            | 1       | 1        | 10000           |
+----------+---------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
1 row in set (0.00 sec)

注意:要是是用明文密碼設(shè)置mysql_users,在這里可以用save命令來(lái)轉(zhuǎn)換成了hash值的密碼

語(yǔ)法如下:

save mysql users to mem;

例子:

MySQL [(none)]> save mysql users to mem;
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> select username,password from mysql_users;
+----------+-------------------------------------------+
| username | password                                  |
+----------+-------------------------------------------+
| testuser | *59D7241B6C5F951F331FF88505C582CBCD42482F |
+----------+-------------------------------------------+
1 row in set (0.00 sec)

5.設(shè)置監(jiān)控賬號(hào)
proxysql上操作,賬號(hào)密碼與mysql主庫(kù)上賬號(hào)密碼保持一致
語(yǔ)法如下:

update global_variables set variable_value='proxysql' where variable_name='mysql-monitor_username';
update global_variables set variable_value='ProxySQL@20191226' where variable_name='mysql-monitor_password';

例子:

MySQL [(none)]> update global_variables set variable_value='proxysql' where variable_name='mysql-monitor_username';
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> update global_variables set variable_value='ProxySQL@20191226' where variable_name='mysql-monitor_password';
Query OK, 1 row affected (0.00 sec)

6.應(yīng)用到線上
proxysql上操作
語(yǔ)法如下:

load mysql servers to runtime;
load mysql users to runtime;
load mysql variables to runtime;

例子:

MySQL [(none)]> load mysql servers to runtime;
Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]> load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> load mysql variables to runtime;
Query OK, 0 rows affected (0.00 sec)

7.持久化
proxysql上操作
語(yǔ)法如下:

save mysql servers to disk;
save mysql users to disk;
save mysql variables to disk;

例子:

MySQL [(none)]> save mysql servers to disk;
Query OK, 0 rows affected (0.07 sec)

MySQL [(none)]> save mysql users to disk;
Query OK, 0 rows affected (0.04 sec)

MySQL [(none)]> save mysql variables to disk;
Query OK, 97 rows affected (0.02 sec)

8.自動(dòng)讀寫分離配置
proxysql上操作,定義路由規(guī)則,如:除select * from tb for update的select全部發(fā)送到slave,其他的的語(yǔ)句發(fā)送到master。

語(yǔ)法如下:

insert into mysql_query_rules(active,match_pattern,destination_hostgroup,apply) values(1,'^SELECT.*FOR UPDATE$',0,1);
insert into mysql_query_rules(active,match_pattern,destination_hostgroup,apply) values(1,'^SELECT',1,1);
load mysql query rules to runtime;
save mysql  query rules to disk;

例子:

MySQL [(none)]> insert into mysql_query_rules(active,match_pattern,destination_hostgroup,apply) values(1,'^SELECT.*FOR UPDATE$',0,1);
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> insert into mysql_query_rules(active,match_pattern,destination_hostgroup,apply) values(1,'^SELECT',1,1);
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> load mysql query rules to runtime;
Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]> save mysql  query rules to disk;
Query OK, 0 rows affected (0.10 sec)

說(shuō)明:active表示是否啟用這個(gè)sql路由項(xiàng),match_pattern就是我們正則匹配項(xiàng),destination_hostgroup表示我們要將該類sql轉(zhuǎn)發(fā)到哪些mysql上面去,apply為1表示該正則匹配后,將不再接受其他匹配,直接轉(zhuǎn)發(fā)。

9.客戶端測(cè)試效果
連接數(shù)據(jù)庫(kù),通過proxysql的客戶端接口訪問(6033)

[root@VM_8_24_centos ~]# mysql -utestuser -pTESTuser@20191226 -h 127.0.0.1 -P6033
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.30 (ProxySQL)

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;
+-------------------------+
| Database                |
+-------------------------+
| information_schema      |
| mysql                   |
| performance_schema      |
| TESTuser_data_services_dev  |
| TESTuser_data_services_test |
| TESTuser_rule               |
+-------------------------+
7 rows in set (0.01 sec)

MySQL [(none)]> use TESTuser_rule;
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 [TESTuser_rule]> show tables;
+-------------------------------+
| Tables_in_TESTuser_rule           |
+-------------------------------+
| organ_1_b_12                  |
| organ_1_b_13                  |
| organ_1_b_14                  |
| organ_1_b_15                  |
| organ_1_b_16                  |
| organ_1_b_19                  |
+-------------------------------+
6 rows in set (0.00 sec)

MySQL [TESTuser_rule]> select * from organ_1_b_12;
+----+--------------------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+---------+-----------+---------------------+
| id | t0                 | t1   | t2   | t3   | t4   | t5   | t6   | t7   | t8   | t9   | t10  | t11  | t12  | t13  | t14  | t15  | t16  | t17  | t18  | t19  | user_id | nick_name | created             |
+----+--------------------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+---------+-----------+---------------------+
|  1 | 513427199402063414 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |       1 | admin     | 2018-10-31 11:50:38 |
|  2 | 140502198811102244 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |       1 | admin     | 2018-10-31 14:35:36 |
+----+--------------------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+---------+-----------+---------------------+
2 rows in set (0.00 sec)

10.proxysql統(tǒng)計(jì)效果
通過proxysql接口正常操作數(shù)據(jù),從管理接口看看ProxySQL的統(tǒng)計(jì)信息:
語(yǔ)法如下:

select Command,Total_Time_us,Total_cnt from stats_mysql_commands_counters where Total_cnt >0;

例子:

MySQL [(none)]> select Command,Total_Time_us,Total_cnt from stats_mysql_commands_counters where Total_cnt >0;
+---------+---------------+-----------+
| Command | Total_Time_us | Total_cnt |
+---------+---------------+-----------+
| SELECT  | 5650          | 3         |
| SHOW    | 7044          | 4         |
+---------+---------------+-----------+
2 rows in set (0.00 sec)

查看各類SQL的執(zhí)行情況
語(yǔ)法如下:

select * from stats_mysql_query_digest;

例子:

MySQL [(none)]> select * from stats_mysql_query_digest;
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname         | username | digest             | digest_text                      | count_star | first_seen | last_seen  | sum_time | min_time | max_time |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| 1         | TESTuser_rule          | testuser | 0x504CF0771C9E548B | select * from organ_1_b_12       | 1          | 1577341448 | 1577341448 | 3746     | 3746     | 3746     |
| 0         | TESTuser_rule          | testuser | 0x99531AEFF718C501 | show tables                      | 2          | 1577341415 | 1577341425 | 2252     | 669      | 1583     |
| 1         | information_schema | testuser | 0x620B328FE9D6D71A | SELECT DATABASE()                | 1          | 1577341415 | 1577341415 | 1904     | 1904     | 1904     |
| 0         | information_schema | testuser | 0x02033E45904D3DF0 | show databases                   | 1          | 1577341374 | 1577341374 | 2326     | 2326     | 2326     |
| 0         | TESTuser_rule          | testuser | 0x02033E45904D3DF0 | show databases                   | 1          | 1577341415 | 1577341415 | 2466     | 2466     | 2466     |
| 0         | information_schema | testuser | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 1          | 1577341364 | 1577341364 | 0        | 0        | 0        |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
6 rows in set (0.00 sec)

11.通過clb解決proxysql單點(diǎn)故障并負(fù)債均衡
內(nèi)網(wǎng)clb 192.168.9.27 tcp 6033端口到192.168.8.24 6033端口,其他proxysql綁定即可。

[root@VM_0_26_centos ~]# mysql -utestuser -p -h 192.168.9.27 -P6033
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2699
Server version: 5.5.30 (ProxySQL)

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;
+-------------------------+
| Database                |
+-------------------------+
| information_schema      |
| mysql                   |
| performance_schema      |
| TESTuser_data_services_dev  |
| TESTuser_data_services_test |
| TESTuser_rule               |
+-------------------------+
7 rows in set (0.01 sec)

MySQL [(none)]> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| TESTuser_data_servic | %         |
| TESTuser_rule_rw     | %         |
| proxysql         | 192.168.8.24 |
| testuser         | 192.168.8.24 |
| root             | 127.0.0.1 |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
7 rows in set (0.01 sec)
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI