詳細的中文介紹:別人寫的,基本跟官方無差異mysql
wget https://github.com/sysown/proxysql/releases/download/v1.4.8/proxysql-1.4.8-1-centos7.x86_64.rpm yum install -y proxysql-1.4.8-1-centos7.x86_64.rpm #proxysql須要依賴一些perl庫,因此使用yum安裝 #安裝生成的文件: [root@ProxySQL ~]#rpm -ql proxysql /etc/init.d/proxysql #啓動腳本 /etc/proxysql.cnf #配置文件,僅在第一次(/var/lib/proxysql/proxysql.db文件不存在)啓動時有效 #啓動後能夠在proxysql管理端中經過修改數據庫的方式修改配置並生效(官方推薦方式。) /usr/bin/proxysql #主程序文件 /usr/share/proxysql/tools/proxysql_galera_checker.sh /usr/share/proxysql/tools/proxysql_galera_writer.pl
/etc/init.d/proxysql start #proxysql客戶端監聽在6033端口上,管理端監聽6032端口 [root@ProxySQL ~]#/etc/init.d/proxysql start Starting ProxySQL: DONE! [root@ProxySQL ~]#ss -tanl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:6032 *:* LISTEN 0 128 *:6033 *:* LISTEN 0 128 *:6033 *:* LISTEN 0 128 *:6033 *:* LISTEN 0 128 *:6033 *:*
mysql -uadmin -padmin -h127.0.0.1 -P6032 #默認的管理端帳號密碼都是admin,登陸進去以後能夠修改變量進行修改帳號密碼 [root@ProxySQL ~]# 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, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]>
insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'192.168.100.3',3306,1,'Write Group'); insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'192.168.100.4',3306,1,'Read Group'); #使用insert語句添加主機到mysql_servers表中,其中:hostgroup_id 1 表示寫組,2表示讀組。 MySQL [(none)]> select * from mysql_servers; +--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+-- | hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | u +--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+-- | 1 | 192.168.100.3 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 2 | 192.168.100.4 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 +--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+-- 2 rows in set (0.00 sec)
GRANT ALL ON *.* TO 'proxysql'@'192.168.100.%' IDENTIFIED BY '123456'; #在後端mysql中添加能夠增刪改查的帳號 insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('proxysql','123456',1,1); #在proxysql主機的mysql_users表中添加剛纔建立的帳號,proxysql客戶端須要使用這個帳號來訪問數據庫。 #default_hostgroup默認組設置爲寫組,也就是1 #當讀寫分離的路由規則不符合時,會訪問默認組的數據庫 MySQL [(none)]> insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('proxysql','123456',1,1); Query OK, 1 row affected (0.00 sec) MySQL [(none)]> select * from mysql_users\G *************************** 1. row *************************** username: proxysql password: 123456 active: 1 use_ssl: 0 default_hostgroup: 1 default_schema: NULL schema_locked: 0 transaction_persistent: 1 fast_forward: 0 backend: 1 frontend: 1 max_connections: 10000 1 row in set (0.00 sec)
GRANT SELECT ON *.* TO 'monitor'@'192.168.100.%' IDENTIFIED BY 'monitor'; #在後端主機中添加能夠訪問數據庫的帳號,SELECT權限便可 set mysql-monitor_username='monitor' set mysql-monitor_password='monitor' #在proxysql管理端中修改變量設置健康檢測的帳號
insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',1,1); insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,'^SELECT',2,1); #將select語句所有路由至hostgroup_id=2的組(也就是讀組) #可是select * from tb for update這樣的語句是修改數據的,因此須要單獨定義,將它路由至hostgroup_id=1的組(也就是寫組) #其餘沒有被規則匹配到的組將會被路由至用戶默認的組(mysql_users表中的default_hostgroup) MySQL [(none)]> select rule_id,active,match_digest,destination_hostgroup,apply from mysql_query_rules; +---------+--------+----------------------+-----------------------+-------+ | rule_id | active | match_digest | destination_hostgroup | apply | +---------+--------+----------------------+-----------------------+-------+ | 1 | 1 | ^SELECT.*FOR UPDATE$ | 1 | 1 | | 2 | 1 | ^SELECT | 2 | 1 | +---------+--------+----------------------+-----------------------+-------+ 2 rows in set (0.00 sec)
load mysql users to runtime; load mysql servers to runtime; load mysql query rules to runtime; load mysql variables to runtime; load admin variables to runtime; #load進runtime,是配置生效 save mysql users to disk; save mysql servers to disk; save mysql query rules to disk; save mysql variables to disk; save admin variables to disk; #save到磁盤(/var/lib/proxysql/proxysql.db)中,永久保存配置 MySQL [(none)]> load mysql users to runtime; Query OK, 0 rows affected (0.00 sec) ... ... ... ... MySQL [(none)]> save admin variables to disk; Query OK, 31 rows affected (0.01 sec)
mysql -uproxysql -p123456 -h127.0.0.1 -P6033 #登陸用戶是剛纔咱們在mysql_user表中建立的用戶,端口爲6033 [root@centos7 ~]#mysql -uproxysql -p123456 -h127.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, 2017, 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 | | test | +--------------------+ 4 rows in set (0.00 sec) MySQL [(none)]>
create database bigboss; create database weijinyun; select user,host from mysql.user; #建立兩個數據庫和查個表。 MySQL [(none)]> create database bigboss; Query OK, 1 row affected (0.01 sec) MySQL [(none)]> create database weijinyun; Query OK, 1 row affected (0.00 sec) MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | bigboss | | mysql | | performance_schema | | test | | weijinyun | +--------------------+ 6 rows in set (0.01 sec) MySQL [(none)]> select user,host from mysql.user; +-------------+---------------+ | user | host | +-------------+---------------+ | root | 127.0.0.1 | | monitor | 192.168.100.% | | proxysql | 192.168.100.% | | repliaction | 192.168.100.% | | root | ::1 | | | centos7 | | root | centos7 | | | localhost | | root | localhost | +-------------+---------------+ 9 rows in set (0.01 sec)
#proxysql有個相似審計的功能,能夠查看各種SQL的執行狀況。在proxysql管理端執行: select * from stats_mysql_query_digest; #從下面的hostgroup和digest_text值來看,全部的寫操做都被路由至1組,讀操做都被路由至2組, #其中1組爲寫組,2組爲讀組! #讀寫分離成功!!! 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 | information_schema | proxysql | 0xA6212D89D814BAC5 | create database weijinyun | 1 | 1523658457 | 1523658457 | 1244 | 1244 | 1244 | | 2 | information_schema | proxysql | 0x0F02B330C823D739 | select user,host from mysql.user | 1 | 1523658520 | 1523658520 | 12538 | 12538 | 12538 | | 1 | information_schema | proxysql | 0x02033E45904D3DF0 | show databases | 5 | 1523658103 | 1523658486 | 24852 | 1263 | 17592 | | 1 | information_schema | proxysql | 0xA175FD2982EC6396 | create database bigboss | 1 | 1523658437 | 1523658437 | 1833 | 1833 | 1833 | | 1 | information_schema | proxysql | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 3 | 1523658098 | 1523658473 | 0 | 0 | 0 | +-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+ 6 rows in set (0.00 sec)