主機 | IP | 做用 |
---|---|---|
Master | 192.168.37.7 | 主服務器 |
Slave | 192.168.37.17 | 從服務器 |
ProxySQL | 192.168.37.27 | 中間件服務器 |
Clinet | 192.168.37.37 | 客戶主機 |
前期準備:
Master、Slave(從服務器my.cnf文件中必需要加上read_only,由於ProxSQL經過此語句判斷主從服務器)先完成主從複製。ProxySQL安裝Mariadb,Clinet最少要安裝MySQL客戶端 mysql
[root@poxysql ~]#vim /etc/yum.repos.d/proxysql.repo [proxysql] name=ProxySQLyum baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/\$releasever gpgcheck=1 gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key [root@poxysql ~]#yum install proxysql
[root@poxysql ~]#rpm -ql proxysql /etc/init.d/proxysql /etc/proxysql.cnf /usr/bin/proxysql /usr/share/proxysql/tools/proxysql_galera_checker.sh /usr/share/proxysql/tools/proxysql_galera_writer.pl
啓動ProxySQL sql
[root@poxysql ~]#service proxysql start Starting ProxySQL: 2019-05-10 14:15:18 [INFO] Using config file /etc/proxysql.cnf DONE!
登陸服務,ProxySQL默認用戶爲admin,密碼爲admin,管理端口爲6032,客戶使用端口爲6033 vim
[root@poxysql ~]#mysql -uadmin -padmin -P6032 -h127.0.0.1 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, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]>
MySQL [(none)]>insert into mysql_servers(hostgrup_id,hostname,prot)values(10,'192.168.37.7',3306); Query OK, 1 row affected (0.01sec) MySQL [(none)]>insert into mysql_servers(hostgrup_id,hostname,prot)values(10,'192.168.37.17',3306); Query OK, 1 row affected (0.01sec) MySQL [(none)]> load mysql servers to runtime; #加載到內存,使設置生效 Query OK, 0 row affected (0.00 sec) MySQL [(none)]> save mysql servers to disk; #保存到硬盤中 Query OK, 0 row affected (0.00 sec)
MariaDB [(none)]>grant replication client on *.* to monitor@'192.168.37.%' identified by 'centos';
MySQL[(none)]> set mysql-monitor_username='monitor'; Query OK, 1 row affected (0.00 sec) MySQL [(none)]> set mysql-monitor_password='centos'; Query OK, 1 row affected (0.00 sec) MySQL [(none)]> load mysql variables to runtime; #加載到內存,使設置生效 Query OK, 0 row affected (0.00 sec) MySQL [(none)]> save mysql variables to disk; #保存到硬盤中 Query OK, 0 row affected (0.00 sec)
MySQL [(none)]> select * from mysql_server_connect_log; +---------------+------+------------------+-------------------------+---------------+ | hostname | port | time_start_us | connect_success_time_us | connect_error | +---------------+------+------------------+-------------------------+---------------+ | 192.168.37.7 | 3306 | 1557470898315003 | 1898 | NULL | | 192.168.37.17 | 3306 | 1557470899044242 | 1950 | NULL | . . . | 192.168.37.7 | 3306 | 1557471439197688 | 4458 | NULL | +---------------+------+------------------+-------------------------+---------------+ 20 rows in set (0.01 sec)
查看監控心跳信息 (對ping指標的監控):
MySQL> select from mysql_server_ping_log;
查看read_only和replication_lag的監控日誌
MySQL> select from mysql_server_read_only_log;
MySQL> select * from mysql_server_replication_lag_log; centos
MySQL [(none)]> insert into mysql_replication_hostgroups values(10,20,"test"); #在分組表mysql_replication_hostgroups中添10(寫組)組和20(讀組)組,test爲描述信息 Query 0K, 1 row affacted (0.00sec) MySQL [(none)]> load mysql servers to runtime; #加載,使生效 Query OK, 0 rows affected (0.01 sec) MySQL [(none)]> save mysql servers to disk; #保存到硬盤中 Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> grant all on *.* to sqluser@'192.168.37.%' identified by 'centos';
MySQL[(none)]> insert into mysql_users(username,password,default_hostgroup) values('sqluser','centos',10); MySQL[(none)]> load mysql users to runtime; #加載,使設置生效 MySQL[(none)]> save mysql users to disk; #保存到硬盤
MySQL [(none)]> 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); #路由規則,讀語句到20組,其餘到10組 MySQL [(none)]> load mysql query rules to runtime; #加載,使設置生效 MySQL [(none)]> save mysql query rules to disk; #保存到硬盤
在client中執行命令查讀和寫的主機ID判斷是否完成讀寫分離
在尚未設置規則時;全部的訪問都發送到master機器上 bash
[root@Centos7 ~]#mysql -usqluser -pmagedu -P6033 -h192.168.37.27 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)]> select @@server_id; +-------------+ | @@server_id | +-------------+ | 7 | #主服務器(Master)ID +-------------+ 1 row in set (0.01 sec)
規設置成功後,select 語句被髮送到slave上執行,其餘語句發到master上執行 服務器
MySQL [(none)]> select @@server_id; +-------------+ | @@server_id | +-------------+ | 17 | #從服務器(slave)ID +-------------+ 1 row in set (0.00 sec)
MySQL [(none)]> SELECT hostgroup hg,sum_time, count_star, digest_text -> FROM stats_mysql_query_digest ORDER BY sum_time DESC; +----+----------+------------+----------------------------------+ | hg | sum_time | count_star | digest_text | +----+----------+------------+----------------------------------+ | 20 | 32464 | 5 | select @@server_id | | 10 | 15435 | 12 | select @@server_id | | 10 | 9941 | 1 | show @@server_id | | 10 | 0 | 1 | select @@version_comment limit ? | +----+----------+------------+----------------------------------+ 4 rows in set (0.00 sec) MySQL [(none)]>