MySQLProxy介绍
下面使用MySQL官方提供的数据库代理层产品MySQLProxy搭建读写分离。
MySQLProxy实际上是在客户端请求与MySQLServer之间建立了一个连接池。所有客户端请求都是发向MySQLProxy,然后经由 MySQLProxy进行相应的分析,判断出是读操作还是写操作,分发至对应的MySQLServer上。对于多节点Slave集群,也可以起做到负载均 衡的效果。
[root@server1 ~]# ls
mysql-community-client-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm
mysql-community-devel-5.7.17-1.el6.x86_64.rpm
mysql-community-embedded-5.7.17-1.el6.x86_64.rpm
mysql-community-embedded-devel-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-server-5.7.17-1.el6.x86_64.rpm
mysql-community-test-5.7.17-1.el6.x86_64.rpm
[root@server1 ~]# yum install mysql-community-client-5.7.17-1.el6.x86_64.rpm mysql-community-common-5.7.17-1.el6.x86_64.rpm mysql-community-libs-5.7.17-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm mysql-community-server-5.7.17-1.el6.x86_64.rpm -y
[root@server1 ~]# vim /etc/my.cnf
[root@server1 ~]# /etc/init.d/mysqld start
Initializing MySQL database: [ OK ]
Installing validate password plugin: [ OK ]
Starting mysqld: [ OK ]
[root@server1 ~]# grep password /var/log/mysqld.log
2018-08-13T16:39:33.244792Z 1 [Note] A temporary password is generated for root@localhost: s+kaBThyW13_
[root@server1 ~]# mysql -p
Enter password:
mysql> grant replication slave on *.* to repl@'172.25.254.%' identified by 'XDwestos+007';
Query OK, 0 rows affected, 1 warning (0.09 sec)
[root@server5~]# ls
mysql-community-client-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-server-5.7.17-1.el6.x86_64.rpm
[root@server5 ~]# yum install -y *
[root@server5 ~]# vim /etc/my.cnf
[root@server25~]# /etc/init.d/mysqld start
Initializing MySQL database: [ OK ]
[root@server5~]# grep password /var/log/mysqld.log
2018-08-13T16:45:29.568575Z 1 [Note] A temporary password is generated for root@localhost: Nu=v*rJ:#9d,
[root@server5~]# mysql_secure_installation
[root@server5 ~]# mysql -p
Enter password:
mysql> change master to master_host='172.25.254.1', master_user='repl', master_passw
Query OK, 0 rows affected, 2 warnings (1.15 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.254.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: server2-relay-bin.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
[root@server6 ~]# ls
mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
[root@server6 ~]# tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
[root@server6 ~]# ls
mysql-proxy-0.8.5-linux-el6-x86-64bit
mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
[root@server6 ~]# cd mysql-proxy-0.8.5-linux-el6-x86-64bit
[root@server6 mysql-proxy-0.8.5-linux-el6-x86-64bit]# cd
[root@server6 ~]# ls
mysql-proxy-0.8.5-linux-el6-x86-64bit
[root@server6 ~]# mv mysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/mysql-pro[root@server6 ~]# mv mysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/mysql-proxy
[root@server6 ~]# cd /usr/local/mysql-proxy/
[root@server6 mysql-proxy]# mkdir conf
[root@server6 mysql-proxy]# ls
bin conf include lib libexec licenses share
[root@server6 mysql-proxy]# cd conf
[root@server6 conf]# vim mysql-proxy.conf
[root@server6 conf]# cd ..
[root@server6 mysql-proxy]# cd bin
[root@server6 bin]# vim ~/.bash_profile
[root@server6 bin]# source ~/.bash_profile
[root@server6 bin]# cd ..
[root@server6 mysql-proxy]# cd conf
[root@server6 conf]# ls
mysql-proxy.conf
[root@server6 conf]# vim mysql-proxy.conf
[root@server6 conf]# cd ..
[root@server6 mysql-proxy]# ls
bin conf include lib libexec licenses share
[root@server6 mysql-proxy]# cd share/
[root@server6 share]# ls
doc
[root@server6 share]# cd doc/
[root@server6 doc]# ls
mysql-proxy
[root@server6 doc]# cd mysql-proxy/
[root@server6 mysql-proxy]# ls
active-queries.lua ro-balance.lua tutorial-resultset.lua
active-transactions.lua ro-pooling.lua tutorial-rewrite.lua
admin-sql.lua rw-splitting.lua tutorial-routing.lua
analyze-query.lua tutorial-basic.lua tutorial-scramble.lua
auditing.lua tutorial-constants.lua tutorial-states.lua
commit-obfuscator.lua tutorial-inject.lua tutorial-tokenize.lua
commit-obfuscator.msc tutorial-keepalive.lua tutorial-union.lua
COPYING tutorial-monitor.lua tutorial-warnings.lua
histogram.lua tutorial-packets.lua xtab.lua
load-multi.lua tutorial-prep-stmts.lua
README tutorial-query-time.lua
[root@server6 mysql-proxy]# vim rw-splitting.lua
[root@server6 mysql-proxy]# pwd
/usr/local/mysql-proxy/share/doc/mysql-proxy
[root@server6 mysql-proxy]# ls
active-queries.lua ro-balance.lua tutorial-resultset.lua
active-transactions.lua ro-pooling.lua tutorial-rewrite.lua
admin-sql.lua rw-splitting.lua tutorial-routing.lua
analyze-query.lua tutorial-basic.lua tutorial-scramble.lua
auditing.lua tutorial-constants.lua tutorial-states.lua
commit-obfuscator.lua tutorial-inject.lua tutorial-tokenize.lua
commit-obfuscator.msc tutorial-keepalive.lua tutorial-union.lua
COPYING tutorial-monitor.lua tutorial-warnings.lua
histogram.lua tutorial-packets.lua xtab.lua
load-multi.lua tutorial-prep-stmts.lua
README tutorial-query-time.lua
[root@server6 mysql-proxy]# cd ..
[root@server6 doc]# cd ..
[root@server6 share]# cd ..
[root@server6 mysql-proxy]# ls
bin conf include lib libexec licenses share
[root@server6 mysql-proxy]# cd conf/
[root@server6 conf]# ls
mysql-proxy.conf
[root@server6 conf]# vim mysql-proxy.conf
[root@server6 conf]# ls
mysql-proxy.conf
[root@server6 conf]# cd ..
[root@server6 mysql-proxy]# ls
bin conf include lib libexec licenses share
[root@server6 mysql-proxy]# mkdir logs
[root@server6 mysql-proxy]# cd logs
[root@server6 logs]# ls
[root@server6 logs]# cd ..
[root@server6 mysql-proxy]# cd conf/
[root@server6 conf]# ls
mysql-proxy.conf
[root@server6 conf]# vim mysql-proxy.conf
#####################################################################################
[mysql-proxy]
user=root
proxy-address=0.0.0.0:3306
proxy-backend-addresses=172.25.26.2:3306
proxy-read-only-backend-addresses=172.25.26.6:3306
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log
log-level=debug
daemon=true
keepalive=true
###############################################################################################
[root@server6 conf]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 916/redis-server 0.
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 909/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 994/master
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1284/mysql-proxy
tcp 0 0 172.25.26.7:54772 172.25.26.2:3306 ESTABLISHED 1284/mysql-proxy
tcp 0 0 172.25.26.7:22 172.25.26.250:59958 ESTABLISHED 1043/sshd
tcp 0 0 172.25.26.7:3306 172.25.26.250:58178 ESTABLISHED 1284/mysql-proxy
tcp 0 0 :::22 :::* LISTEN 909/sshd
tcp 0 0 ::1:25 :::* LISTEN 994/master
在开启主从复制的情况下进行插入操作,主从的结果一致,查询的结果也是一致的
关闭slave:stop slave
然后进行插入操作,结果是master上的数据正常插入,如果开启了半同步的话,操作会有一个10s的停顿,然后进行查询发现刚才插入的数据并没有返回, 这是因为写操作在master上,关闭了主从复制,所以数据还没有同步到slave上去,而读操作是在slave上进行的,所以没有获得刚才写入的数据
开启slave:start slave
然后再次查询发现数据一致了
[root@foundation26 Desktop]# mysql -h 172.25.26.7 -u root -pXDwestos+007
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> use westos
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 [westos]> select * from t1;
+----+------+
| c1 | c2 |
+----+------+
| 1 | Luis |
| 2 | xix |
| 3 | haha |
| 4 | aa |
| 5 | qq |
| 6 | ff |
+----+------+
6 rows in set (0.01 sec)
[root@server1 ~]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 1238 mysql 34u IPv6 8625 0t0 TCP *:mysql (LISTEN)
mysqld 1238 mysql 53u IPv6 9208 0t0 TCP server1:mysql->server5:36524 (ESTABLISHED)
mysqld 1238 mysql 56u IPv6 9238 0t0 TCP server1:mysql->server6:54772 (ESTABLISHED)
[root@server1 ~]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 1238 mysql 34u IPv6 8625 0t0 TCP *:mysql (LISTEN)
mysqld 1238 mysql 50u IPv6 9775 0t0 TCP server1:mysql->server6:54773 (ESTABLISHED)
mysqld 1238 mysql 53u IPv6 9208 0t0 TCP server1:mysql->server5:36524 (ESTABLISHED)
mysqld 1238 mysql 56u IPv6 9238 0t0 TCP server1:mysql->server6:54772 (ESTABLISHED)
MySQL [westos]> insert into t1 values ('7','ee');
Query OK, 1 row affected (0.08 sec)
mysql> select * from westos.t1;
+----+------+
| c1 | c2 |
+----+------+
| 1 | Luis |
| 2 | xix |
| 3 | haha |
| 4 | aa |
| 5 | qq |
| 6 | ff |
| 7 | ee |
+----+------+
7 rows in set (0.00 sec)
[root@server5 ~]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 1246 mysql 20u IPv6 8560 0t0 TCP *:mysql (LISTEN)
mysqld 1246 mysql 46u IPv6 9248 0t0 TCP server5:mysql->172.25.26.7:59034 (ESTABLISHED)
mysqld 1246 mysql 47u IPv4 9213 0t0 TCP server5:36524->server1:mysql (ESTABLISHED)
通过tcpdump抓取数据分析读写分离