ProxySQL 安裝配置及簡單使用

環境:

proxysql :192.168.65.2

master :192.168.65.3

slave :192.168.65.4

mysql版本:5.7.26

下載Proxysql

https://proxysql.com/

 

安裝

[root@test2 ~]# yum install -y proxysql-2.0.8-1-centos67.x86_64.rpm

 

啓動

[root@test2 ~]# service proxysql start

Starting ProxySQL: 2019-07-03 17:25:12 [INFO] Using config file /etc/proxysql.cnf

2019-07-03 17:25:12 [INFO] No SSL keys/certificates found in datadir (/var/lib/proxysql). Generating new keys/certificates.

DONE!

 

檢查狀態

[root@test2 ~]# service proxysql status

ProxySQL is running (7464).

 

檢查端口

[root@test2 ~]# netstat -ntl

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State

tcp 0 0 0.0.0.0:6032 0.0.0.0:* LISTEN

tcp 0 0 0.0.0.0:6033 0.0.0.0:* LISTEN

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN

tcp 0 0 :::3306 :::* LISTEN

tcp 0 0 :::22 :::* LISTEN

tcp 0 0 ::1:25 :::* LISTEN

6032 和 6033 就是 ProxySQL 的默認端口,6032 爲管理端口,6033 爲客戶端口

 

登入管理端口(默認管理用戶admin,密碼admin,管理端口6032,客戶端口6033)

[root@test2 ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032

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 1

Server version: 5.5.30 (ProxySQL Admin Module)

 

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.

 

mysql> 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)

 

配置proxysql

a.添加服務器信息

insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.65.3',3306);

insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.65.4',3306);

mysql> select * from mysql_servers;

+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

| hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |

+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

| 10 | 192.168.65.3 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |

| 10 | 192.168.65.4 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |

+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

保存修改

mysql> load mysql servers to runtime;

Query OK, 0 rows affected (0.01 sec)

 

mysql> save mysql servers to disk;

Query OK, 0 rows affected (0.03 sec)

 

b.master上添加監控賬號

create user monitor@'%' identified by '123';

grant all on *.* to monitor@'%';

 

c.proxysql 管理端口配置監控賬號信息

set mysql-monitor_username='monitor';

set mysql-monitor_password='123';

 

保存修改

load mysql variables to runtime;

save mysql variables to disk;

 

d.檢查監控狀態

連接狀態

mysql> select * from mysql_server_connect_log;

+--------------+------+------------------+-------------------------+----------------------------------------------------------------+

| hostname | port | time_start_us | connect_success_time_us | connect_error |

+--------------+------+------------------+-------------------------+----------------------------------------------------------------+

| 192.168.65.4 | 3306 | 1562146513024875 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |

| 192.168.65.3 | 3306 | 1562146513842424 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |

| 192.168.65.4 | 3306 | 1562146573025888 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |

| 192.168.65.3 | 3306 | 1562146574001548 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |

| 192.168.65.4 | 3306 | 1562146633026875 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |

| 192.168.65.3 | 3306 | 1562146633872452 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |

| 192.168.65.3 | 3306 | 1562147094371541 | 2289 | NULL |

| 192.168.65.4 | 3306 | 1562147095536307 | 2564 | NULL |

+--------------+------+------------------+-------------------------+----------------------------------------------------------------+

看最後如果出現 connect_error = NULL 則表示正常。

 

ping狀態

mysql> select * from mysql_server_ping_log;

+--------------+------+------------------+----------------------+----------------------------------------------------------------+

| hostname | port | time_start_us | ping_success_time_us | ping_error |

+--------------+------+------------------+----------------------+----------------------------------------------------------------+

| 192.168.65.4 | 3306 | 1562146593041343 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |

| 192.168.65.3 | 3306 | 1562146593231401 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |

| 192.168.65.4 | 3306 | 1562146603041844 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |

| 192.168.65.3 | 3306 | 1562146603142607 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |

| 192.168.65.4 | 3306 | 1562146613042174 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |

| 192.168.65.4 | 3306 | 1562147174354918 | 850 | NULL |

| 192.168.65.3 | 3306 | 1562147174490797 | 820 | NULL |

| 192.168.65.3 | 3306 | 1562147184355293 | 718 | NULL |

| 192.168.65.4 | 3306 | 1562147184510190 | 773 | NULL |

+--------------+------+------------------+----------------------+----------------------------------------------------------------+

同樣,看最後如出現NULL則表示正常

 

e.配置proxysql組信息

mysql> insert into mysql_replication_hostgroups(writer_hostgroup,reader_hostgroup,comment) values(10,20,'test');

Query OK, 1 row affected (0.00 sec)

 

mysql> load mysql servers to runtime;

Query OK, 0 rows affected (0.01 sec)

 

mysql> save mysql servers to disk;

Query OK, 0 rows affected (0.04 sec)

 

如果配置成功,則mysql_servers中hostgroup_id 會根據 master和slave 中的read_only的狀態修改mysql_servers 中服務器的分組。

mysql> select * from mysql_servers;

+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

| hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |

+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

| 10 | 192.168.65.3 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |

| 20 | 192.168.65.4 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |

+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

最開始我們添加服務器信息的時候 hostgroup_id設置的都是10,現在 組配置信息添加後則變爲 10,20,說明配置成功,如果沒有變化,則檢查是否配置正確或slave沒有設置成read_only。

 

f.master上創建使用sql的賬號

create user sqluser@'%' identified by '123';

grant all on *.* to sqluser@'%';

 

g.proxysql管理端口配置 sql使用賬號的信息

insert into mysql_users(username,password,default_hostgroup) values('sqluser','123',10);

load mysql users to runtime;

save mysql users to disk;

mysql> mysql> select * from mysql_users;

+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+

| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | comment |

+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+

| sqluser | 123 | 1 | 0 | 10 | NULL | 0 | 1 | 0 | 1 | 1 | 10000 | |

+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+

 

h.測試,proxysql 客戶端口(端口:6033)

[root@test2 ~]# mysql -usqluser -p123 -h127.0.0.1 -P6033

mysql> select @@server_id;

+-------------+

| @@server_id |

+-------------+

| 2 |

+-------------+

1 row in set (0.01 sec)

 

mysql> create database ll;

Query OK, 1 row affected (0.01 sec)

查詢和ddl 都可以正常進行

 

i.proxysql管理端口配置 路由信息

insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) VALUES(1,1,'^SELECT.*FOR UPDATE$',10,1),(2,1,'^SELECT',20,1);

mysql> load mysql query rules to runtime;

Query OK, 0 rows affected (0.00 sec)

 

mysql> save mysql query rules to disk;

Query OK, 0 rows affected (0.02 sec)

 

mysql> select * from mysql_query_rules\G

*************************** 1. row ***************************

rule_id: 1

active: 1

username: NULL

schemaname: NULL

flagIN: 0

client_addr: NULL

proxy_addr: NULL

proxy_port: NULL

digest: NULL

match_digest: ^SELECT.*FOR UPDATE$

match_pattern: NULL

negate_match_pattern: 0

re_modifiers: CASELESS

flagOUT: NULL

replace_pattern: NULL

destination_hostgroup: 10

cache_ttl: NULL

cache_empty_result: NULL

cache_timeout: NULL

reconnect: NULL

timeout: NULL

retries: NULL

delay: NULL

next_query_flagIN: NULL

mirror_flagOUT: NULL

mirror_hostgroup: NULL

error_msg: NULL

OK_msg: NULL

sticky_conn: NULL

multiplex: NULL

gtid_from_hostgroup: NULL

log: NULL

apply: 1

comment: NULL

*************************** 2. row ***************************

rule_id: 2

active: 1

username: NULL

schemaname: NULL

flagIN: 0

client_addr: NULL

proxy_addr: NULL

proxy_port: NULL

digest: NULL

match_digest: ^SELECT

match_pattern: NULL

negate_match_pattern: 0

re_modifiers: CASELESS

flagOUT: NULL

replace_pattern: NULL

destination_hostgroup: 20

cache_ttl: NULL

cache_empty_result: NULL

cache_timeout: NULL

reconnect: NULL

timeout: NULL

retries: NULL

delay: NULL

next_query_flagIN: NULL

mirror_flagOUT: NULL

mirror_hostgroup: NULL

error_msg: NULL

OK_msg: NULL

sticky_conn: NULL

multiplex: NULL

gtid_from_hostgroup: NULL

log: NULL

apply: 1

comment: NULL

2 rows in set (0.00 sec)

 

測試

測試,proxysql 客戶端口(端口:6033)

[root@test2 ~]# mysql -usqluser -p123 -h127.0.0.1 -P6033

mysql> select * from ty.b;

+------+-------+------+

| id | bname | s |

+------+-------+------+

| 1 | ae | aa |

| 2 | cc | cc |

| 3 | ee | eec |

| 4 | ppe | ppc |

| 5 | yy | yy |

+------+-------+------+

5 rows in set (0.02 sec)

 

mysql> create database ooo;

Query OK, 1 row affected (0.01 sec)

 

進管理端口查看

mysql> select hostgroup,schemaname,username,digest_text from stats_mysql_query_digest;

+-----------+--------------------+----------+----------------------------------+

| hostgroup | schemaname | username | digest_text |

+-----------+--------------------+----------+----------------------------------+

| 10 | information_schema | sqluser | create database ooo |

| 10 | information_schema | sqluser | select * from ty.a |

| 10 | information_schema | sqluser | create database ll |

| 20 | information_schema | sqluser | select * from ty.b |

| 10 | information_schema | sqluser | select @@server_id |

| 10 | information_schema | sqluser | select @@version_comment limit ? |

+-----------+--------------------+----------+----------------------------------+

select * from ty.b 對應的組是20,也就是192.168.65.4 (slave)

create database ooo 對應的組是10,也就是192.168.65.3(master)

 

到此就實現了簡單的讀寫分離

 

 

啓用Web統計功能

 

([email protected]:6032) [main]>  select * from global_variables where variable_name like 'admin-web%';
+-------------------+----------------+
| variable_name     | variable_value |
+-------------------+----------------+
| admin-web_enabled | false          |
| admin-web_port    | 6080           |
+-------------------+----------------+
啓用變量admin-web_enabled
([email protected]:6032) [main]> set admin-web_enabled='true';
Query OK, 1 row affected (0.00 sec)
([email protected]:6032) [main]> load admin variables to runtime;
Query OK, 0 rows affected (0.00 sec)
([email protected]:6032) [main]> save admin variables to disk;
Query OK, 31 rows affected (0.01 sec)
([email protected]:6032) [main]>  select * from global_variables where variable_name like 'admin-web%';
+-------------------+----------------+
| variable_name     | variable_value |
+-------------------+----------------+
| admin-web_enabled | true           |
| admin-web_port    | 6080           |
+-------------------+----------------+
查看登錄Web界面的用戶名、密碼和端口
([email protected]:6032) [main]> select * from global_variables where variable_name like 'admin-web%' or variable_name LIKE 'admin-stats%';
+-----------------------------------+----------------+
| variable_name                     | variable_value |
+-----------------------------------+----------------+
| admin-stats_credentials           | stats:stats    |
| admin-stats_mysql_connections     | 60             |
| admin-stats_mysql_connection_pool | 60             |
| admin-stats_mysql_query_cache     | 60             |
| admin-stats_system_cpu            | 60             |
| admin-stats_system_memory         | 60             |
| admin-web_enabled                 | true           |
| admin-web_port                    | 6080           |
+-----------------------------------+----------------+
8 rows in set (0.01 sec)

發佈了32 篇原創文章 · 獲贊 3 · 訪問量 9923
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章