ProxySQL是使用C++語言開發的,官網文檔也很齊全,如下是其特點功能點:html
上面提到的MyCAT我Mysql哪個分類文章有親測過程,有興趣小夥伴能夠移步看看.node
http://www.javashuo.com/article/p-ehwkgnpr-k.htmlpython
經過上述,咱們能夠看到ProxySQL能夠作許多事情,已經不只僅是純粹的MySQL讀寫分離,其實咱們經過後面所述結合業務發散,ProxySQL還能夠支持如下高級功能:mysql
高可用架構
linux
list
git
CentOS7.3 proxysql-2.0.12-1-centos7.x86_64.rpm mysql-5.7.23-1.el7.x86_64.rpm-bundle.tar
主機 | 操做系統 | IP地址 | 硬件/網絡 |
---|---|---|---|
Mysql105 | CentOS7.3 | 192.168.0.105 | 2C4G / nat |
Mysql106 | CentOS7.3 | 192.168.0.106 | 2C4G / nat |
Mysql107 | CentOS7.3 | 192.168.0.107 | 2C4G / nat |
ProxySQL109 | CentOS7.3 | 192.168.0.109 | 2C4G / nat |
#!/usr/bin/env bash # Author: ZhouJian # Mail: 18621048481@163.com # Time: 2019-9-3 # Describe: CentOS 7 Install Mysql.rpm Script clear echo -ne "\\033[0;33m" cat<<EOT _oo0oo_ 088888880 88" . "88 (| -_- |) 0\\ = /0 ___/'---'\\___ .' \\\\\\\\| |// '. / \\\\\\\\||| : |||// \\\\ /_ ||||| -:- |||||- \\\\ | | \\\\\\\\\\\\ - /// | | | \\_| ''\\---/'' |_/ | \\ .-\\__ '-' __/-. / ___'. .' /--.--\\ '. .'___ ."" '< '.___\\_<|>_/___.' >' "". | | : '- \\'.;'\\ _ /';.'/ - ' : | | \\ \\ '_. \\_ __\\ /__ _/ .-' / / ====='-.____'.___ \\_____/___.-'____.-'===== '=---=' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 建議系統 CentOS7 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # PS:請儘可能使用純淨的CentOS7系統,咱們會在服務器安裝Mysql5.7, # 將mysql-5.7.23-1.el7.x86_64.rpm-bundle.tar包和腳本放到root目錄下執行便可,密碼爲ZHOUjian.20 EOT echo -ne "\\033[m" init_security() { systemctl stop firewalld systemctl disable firewalld &>/dev/null setenforce 0 sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config sed -i '/^GSSAPIAu/ s/yes/no/' /etc/ssh/sshd_config sed -i '/^#UseDNS/ {s/^#//;s/yes/no/}' /etc/ssh/sshd_config systemctl enable sshd crond &> /dev/null echo -e "\033[32m [安全配置] ==> OK \033[0m" } init_yumsource() { if [ ! -d /etc/yum.repos.d/backup ];then mkdir /etc/yum.repos.d/backup fi mv /etc/yum.repos.d/* /etc/yum.repos.d/backup 2>/dev/null if ! ping -c2 www.baidu.com &>/dev/null then echo "您沒法上外網,不能配置yum源" exit fi curl -o /etc/yum.repos.d/163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo timedatectl set-timezone Asia/Shanghai echo "nameserver 114.114.114.114" > /etc/resolv.conf echo "nameserver 8.8.8.8" >> /etc/resolv.conf chattr +i /etc/resolv.conf echo -e "\033[32m [YUM Source] ==> OK \033[0m" } init_mysql() { rpm -e mariadb-libs --nodeps rm -rf /var/lib/mysql rm -rf /etc/my.cnf tar xvf /root/mysql-5.7.23-1.el7.x86_64.rpm-bundle.tar -C /usr/local/ cd /usr/local rpm -ivh mysql-community-server-5.7.23-1.el7.x86_64.rpm \ mysql-community-client-5.7.23-1.el7.x86_64.rpm \ mysql-community-common-5.7.23-1.el7.x86_64.rpm \ mysql-community-libs-5.7.23-1.el7.x86_64.rpm rm -rf mysql-community-* } changepass() { sed -i '/\[mysqld]/ a skip-grant-tables' /etc/my.cnf systemctl restart mysqld mysql <<EOF update mysql.user set authentication_string='' where user='root' and Host='localhost'; flush privileges; EOF sed -i '/skip-grant/d' /etc/my.cnf systemctl restart mysqld yum -y install expect ntpdate expect <<-EOF spawn mysqladmin -uroot -p password "ZHOUjian.20" expect { "password" { send "\r" } } expect eof EOF systemctl restart mysqld } main() { init_hostname init_security init_yumsource init_mysql changepass } main
mysql主庫配置
github
[root@mysqlhost ~]# cat /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid server-id = 1 log-bin=mysql-bin
mysql從庫配置
sql
[root@mysql-from ~]# cat /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid server-id = 2 log-bin = mysql-bin
主從三臺服務器分別重啓服務
shell
service mysqld restart
數據庫
# 建立用於同步的用戶帳號及密碼 grant replication slave on *.* to 'slave'@'192.168.0.%' identified by 'ZHOUjian.200'; # 從新加載權限表,更新權限 flush privileges; # 查看master的狀態 #mysql> show master status; #+------------------+----------+--------------+------------------+-------------------+ #| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | #+------------------+----------+--------------+------------------+-------------------+ #| mysql-bin.000001 | 600 | | | | #+------------------+----------+--------------+------------------+-------------------+ #1 row in set (0.00 sec)
change master to master_host='192.168.0.102', master_user='slave', master_password='ZHOUjian.200', master_auto_position=0; mysql> start slave; # 查看從庫狀態 mysql> show slave status\G;
https://github.com/sysown/proxysql/releases
wget https://github.com/sysown/proxysql/releases/download/v2.0.12/proxysql-2.0.12-1-centos7.x86_64.rpm yum install perl-DBD-MySQL3 -y rpm -ivh proxysql-2.0.12-1-centos7.x86_64.rpm service proxysql start proxysql --version # ProxySQL version 2.0.12-38-g58a909a, codename Truls # 本地配置文件 # proxysql 有個配置文件/etc/proxysql.cnf,只在第一次啓動的時候有用, # 後續全部的配置修改都是對 SQLite 數據庫操做,而且不會更新到proxysql.cnf文件中。 # ProxySQL 絕大部分配置均可以在線修改,配置存儲在/var/lib/proxysql/proxysql.db ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:6032 *:* LISTEN 0 128 *:6033 *:* # 管理接口的端口是 6032 , 帳號密碼是 admin( 能夠動態修改 ) 只能經過本地鏈接 , # 客戶端接口的端口是 6033 , 帳號密碼經過管理接口去設置。
登陸管理界面,配置信息從啓動進程的配置文件查看
cat /etc/proxysql.cnf |grep admin admin_variables= admin_credentials="admin:admin" # mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock" mysql -uadmin -padmin -h 127.0.0.1 -P6032 --prompt='proxysql>' # 不推薦跟傳統服務同樣修改/etc/proxysql.conf # 之因此不推薦,是由於咱們能夠經過ProxySQL控制檯在線修改配置,無需重啓,當即生效。 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 | +-----+---------------+-------------------------------------+ # main:默認數據庫,存放用戶驗證、路由規則等信息。咱們要作的配置都是針對這個庫的 # disk:持久化到硬盤的配置 # stats:proxysql運行抓取的統計信息,如各命令的執行次數、查詢執行時間等 # monitor:monitor模塊收集的信息,db的健康狀況、各類檢查等 # 設置SQL日誌記錄[ProxySQL] set mysql-eventslog_filename='queries.log'; # 添加主從[ProxySQL] insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'192.168.0.105',3306,1,'主庫'); insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'192.168.0.106',3306,9,'從庫'); insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'192.168.0.107',3306,1,'從庫'); # 查看主從[ProxySQL] proxysql>select * from mysql_servers; +--------------+---------------+------+-----------+--------+--------+-------------+-----------------+----------- | hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | +--------------+---------------+------+-----------+--------+--------+-------------+-----------------+----------- | 1 | 192.168.0.105 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | 主庫 | | 1 | 192.168.0.106 | 3306 | 0 | ONLINE | 9 | 0 | 1000 | 0 | 0 | 0 | 從庫 | | 1 | 192.168.0.107 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | 從庫 | +--------------+---------------+------+-----------+--------+--------+-------------+-----------------+----------- # hostgroup_id:一個角色一個id,該表的主鍵是hostgroup_id+hostname+port # hostname:db實例IP # port:db實例端口 # weight:權重,若是有多個相同角色的實例,會優先選擇權重高的 # status:狀態 # -ONLINE 正常 # -SHUNNED 臨時被剔除 # -OFFLINE_SOFT 軟離線狀態,再也不接受新的鏈接,已創建的鏈接會等待 # -OFFLINE_HARD 離線,不接收新鏈接, 已創建的鏈接也會強制斷開(宕機或者網絡不可用) # max_connections:最大鏈接數 # max_replication_lag:容許的最大延遲 # 建立主從帳號[MySQL] create user 'proxysql'@'%' identified by 'ZHOUjian.21'; mysql> grant all privileges on *.* to 'proxysql'@'%' with grant option; # 添加主從帳號[ProxySQL] insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('proxysql','ZHOUjian.21',1,1); # 查看主從帳號 select * from mysql_users\G; *************************** 1. row *************************** username: proxysql password: ZHOUjian.21 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 comment: # 建立監控帳號[MySQL] CREATE USER 'monitor'@'%' IDENTIFIED BY 'ZHOUjian.21'; GRANT SELECT ON *.* TO 'monitor'@'%' WITH GRANT OPTION; # 添加監控帳號[ProxySQL] set mysql-monitor_username='monitor'; set mysql-monitor_password='ZHOUjian.21'; # 查看監控帳號[ProxySQL] select * from global_variables where variable_name like 'mysql-monitor_%'; +--------------------------------------------------------------+----------------+ | variable_name | variable_value | +--------------------------------------------------------------+----------------+ | mysql-monitor_enabled | true | | mysql-monitor_connect_timeout | 600 | | mysql-monitor_ping_max_failures | 3 | | mysql-monitor_ping_timeout | 1000 | | mysql-monitor_read_only_max_timeout_count | 3 | | mysql-monitor_replication_lag_interval | 10000 | | mysql-monitor_replication_lag_timeout | 1000 | | mysql-monitor_groupreplication_healthcheck_interval | 5000 | | mysql-monitor_groupreplication_healthcheck_timeout | 800 | | mysql-monitor_groupreplication_healthcheck_max_timeout_count | 3 | | mysql-monitor_groupreplication_max_transactions_behind_count | 3 | | mysql-monitor_galera_healthcheck_interval | 5000 | | mysql-monitor_galera_healthcheck_timeout | 800 | | mysql-monitor_galera_healthcheck_max_timeout_count | 3 | | mysql-monitor_replication_lag_use_percona_heartbeat | | | mysql-monitor_query_interval | 60000 | | mysql-monitor_query_timeout | 100 | | mysql-monitor_slave_lag_when_null | 60 | | mysql-monitor_threads_min | 8 | | mysql-monitor_threads_max | 128 | | mysql-monitor_threads_queue_maxsize | 128 | | mysql-monitor_wait_timeout | true | | mysql-monitor_writer_is_also_reader | true | | mysql-monitor_username | monitor | | mysql-monitor_password | ZHOUjian.21 | | mysql-monitor_history | 600000 | | mysql-monitor_connect_interval | 60000 | | mysql-monitor_ping_interval | 10000 | | mysql-monitor_read_only_interval | 1500 | | mysql-monitor_read_only_timeout | 500 | +--------------------------------------------------------------+----------------+ # 也能夠像下面這樣快速定位 select @@mysql-monitor_username; +--------------------------+ | @@mysql-monitor_username | +--------------------------+ | monitor | +--------------------------+ select @@mysql-monitor_password; +--------------------------+ | @@mysql-monitor_password | +--------------------------+ | ZHOUjian.21 | +--------------------------+
檢測監控
# 檢測上述配置是否正確:connect_error爲NULL則正確 SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 10; +---------------+------+------------------+-------------------------+------------------------------------------------------------------------+ | hostname | port | time_start_us | connect_success_time_us | connect_error | +---------------+------+------------------+-------------------------+------------------------------------------------------------------------+ | 192.168.0.106 | 3306 | 1591457209205112 | 0 | Access denied for user 'monitor'@'192.168.0.109' (using password: YES) | | 192.168.0.107 | 3306 | 1591457208536560 | 0 | Access denied for user 'monitor'@'192.168.0.109' (using password: YES) | | 192.168.0.105 | 3306 | 1591457207868147 | 0 | Access denied for user 'monitor'@'192.168.0.109' (using password: YES) | SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 10; +---------------+------+------------------+----------------------+------------------------------------------------------------------------+ | hostname | port | time_start_us | ping_success_time_us | ping_error | +---------------+------+------------------+----------------------+------------------------------------------------------------------------+ | 192.168.0.105 | 3306 | 1591457358442163 | 0 | Access denied for user 'monitor'@'192.168.0.109' (using password: YES) | | 192.168.0.106 | 3306 | 1591457358348350 | 0 | Access denied for user 'monitor'@'192.168.0.109' (using password: YES) | | 192.168.0.107 | 3306 | 1591457358252207 | 0 | Access denied for user 'monitor'@'192.168.0.109' (using password: YES) |
這裏配置主從自動切換: 互爲主從,自動切換,保證高可用
添加讀寫分離的路由規則
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 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 | +---------+--------+----------------------+-----------------------+-------+ # 將剛纔咱們修改的數據加載至RUNTIME中(參考ProxySQL的多層配置結構): # load進runtime,使配置生效 load mysql query rules to runtime; load admin variables to runtime; # save到磁盤(/var/lib/proxysql/proxysql.db)中,永久保存配置 save mysql query rules to disk; save admin variables to disk;
登陸用戶是剛纔咱們在mysql_user表中建立的用戶,端口爲6033
mysql -uproxysql -ppwproxysql -h127.0.0.1 -P6033
select * from stats_mysql_query_digest;