mysql基於mysql-proxy的讀寫分離

此次環境基於之前的MySQL實驗的基礎:

檢測:
在server1上創建表及數據,查看server2是否同步成功

  • server1:
mysql> CREATE DATABASE redhat;
Query OK, 1 row affected (0.01 sec)

mysql> USE redhat;
Database changed
mysql> CREATE table usertb (
    -> username varchar(10) not null,
    -> password varchar(15) not null);
Query OK, 0 rows affected (0.14 sec)

mysql> DESC usertb;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO   |     | NULL    |       |
| password | varchar(15) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> insert into usertb values ('user1','123');
Query OK, 1 row affected (0.04 sec)

mysql> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 123      |
+----------+----------+
1 row in set (0.00 sec)
  • server2查看:
mysql> select * from usertb;
ERROR 1046 (3D000): No database selected
mysql> select * from redhat.usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 123      |
+----------+----------+
1 row in set (0.00 sec)

主從複製配置成功!!!

一、server3部署mysql-proxy服務

[root@server3 ~]# ls
mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
[root@server3 ~]# tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local/
[root@server3 ~]# cd /usr/local/
[root@server3 local]# ls
bin  games    lib    libexec                                sbin   src
etc  include  lib64  mysql-proxy-0.8.5-linux-el6-x86-64bit  share
[root@server3 local]# ln -s mysql-proxy-0.8.5-linux-el6-x86-64bit mysql-proxy
[root@server3 local]# ll
total 0
drwxr-xr-x. 2 root root   6 Mar 10  2016 bin
drwxr-xr-x. 2 root root   6 Mar 10  2016 etc
drwxr-xr-x. 2 root root   6 Mar 10  2016 games
drwxr-xr-x. 2 root root   6 Mar 10  2016 include
drwxr-xr-x. 2 root root   6 Mar 10  2016 lib
drwxr-xr-x. 2 root root   6 Mar 10  2016 lib64
drwxr-xr-x. 2 root root   6 Mar 10  2016 libexec
lrwxrwxrwx  1 root root  37 May 10 10:49 mysql-proxy -> mysql-proxy-0.8.5-linux-el6-x86-64bit
drwxr-xr-x  8 7161 wheel 87 Aug 19  2014 mysql-proxy-0.8.5-linux-el6-x86-64bit
drwxr-xr-x. 2 root root   6 Mar 10  2016 sbin
drwxr-xr-x. 5 root root  49 May  1 20:38 share
drwxr-xr-x. 2 root root   6 Mar 10  2016 src
[root@server3 local]# cd mysql-proxy
[root@server3 mysql-proxy]# ls
bin  include  lib  libexec  licenses  share
[root@server3 mysql-proxy]# cd bin/
[root@server3 bin]# ls
mysql-binlog-dump  mysql-myisam-dump  mysql-proxy
[root@server3 bin]# cd ..
[root@server3 mysql-proxy]# ls
bin  include  lib  libexec  licenses  share
[root@server3 mysql-proxy]# mkdir conf
[root@server3 mysql-proxy]# cd conf/
[root@server3 conf]# vim mysql-proxy.conf
[root@server3 conf]# cd ../share/doc/mysql-proxy/
[root@server3 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@server3 mysql-proxy]# vim rw-splitting.lua 
if not proxy.global.config.rwsplit then
        proxy.global.config.rwsplit = {
                min_idle_connections = 1,
                max_idle_connections = 2,		#大於兩個請求啓動讀寫分離,方便測試

二、通過編寫lua腳本實現讀寫分離

[root@server3 conf]# pwd
/usr/local/mysql-proxy/conf
[root@server3 conf]# vim mysql-proxy.conf 
[mysql-proxy]
proxy-address=0.0.0.0:3306
proxy-backend-addresses=172.25.70.1:3306		#指定後端主master寫入數據
proxy-read-only-backend-addresses=172.25.70.2:3306		#指定後端從slave讀取數據
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua	#指定讀寫分離配置文件位置
pid-file=/usr/local/mysql-proxy/log/mysql-proxy.pid
log-file=/usr/local/mysql-proxy/log/mysql-proxy.log		#日誌位置
plugins=proxy
log-level=debug		#定義log日誌級別,由高到低分別有(error|warning|info|message|debug)
daemon=true			#以守護進程方式運行
keepalive=true		#mysql-proxy崩潰時,嘗試重啓
[root@server3 conf]# mkdir /usr/local/mysql-proxy/log

三、測試啓動mysql-proxy

[root@server3 bin]# pwd
/usr/local/mysql-proxy/bin
[root@server3 bin]# ls
mysql-binlog-dump  mysql-myisam-dump  mysql-proxy
[root@server3 bin]# chmod 660 /usr/local/mysql-proxy/conf/mysql-proxy.conf 
[root@server3 bin]# ./mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
[root@server3 bin]# cat /usr/local/mysql-proxy/log/mysql-proxy.log 
2019-05-10 11:47:03: (message) chassis-unix-daemon.c:136: [angel] we try to keep PID=11472 alive
2019-05-10 11:47:03: (debug) chassis-unix-daemon.c:157: waiting for 11472
2019-05-10 11:47:03: (debug) chassis-unix-daemon.c:121: we are the child: 11472
2019-05-10 11:47:03: (critical) plugin proxy 0.8.5 started
2019-05-10 11:47:03: (debug) max open file-descriptors = 1024
2019-05-10 11:47:03: (message) proxy listening on port 0.0.0.0:3306
2019-05-10 11:47:03: (message) added read/write backend: 172.25.70.1:3306
2019-05-10 11:47:03: (message) added read-only backend: 172.25.70.2:3306
[root@server3 bin]# yum whatprovides /usr/bin/killall
[root@server3 bin]# yum install psmisc-22.20-11.el7.x86_64 -y
[root@server3 bin]# ps ax | grep mysql-proxy
11471 ?        S      0:00 /usr/local/mysql-proxy/libexec/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
11472 ?        S      0:00 /usr/local/mysql-proxy/libexec/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
11484 pts/0    R+     0:00 grep --color=auto mysql-proxy
[root@server3 bin]# killall -9 mysql-proxy
[root@server3 bin]# ps ax | grep mysql-proxy
11487 pts/0    S+     0:00 grep --color=auto mysql-proxy

四、啓動mysql-proxy

  • server3:
[root@server3 bin]# ./mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf

五、server1配置用戶權限,測試

mysql> grant insert,update,select on *.* to ma@'%' identified by 'Mahao+001';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

添加數據測試:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| redhat             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use redhat;
Database changed
mysql> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 123      |
+----------+----------+
1 row in set (0.00 sec)

mysql> insert into usertb values ('user2','456');
Query OK, 1 row affected (0.02 sec)

mysql> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 123      |
| user2    | 456      |
+----------+----------+
2 rows in set (0.00 sec)

六、測試遠程登錄mysql-proxy服務端能否同步:

[root@vits Desktop]# mysql -h 172.25.70.3 -uma -pMahao+001
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.24-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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| redhat             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

MySQL [(none)]> use redhat;
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 [redhat]> select * from usertb;		#數據同步成功
+----------+----------+
| username | password |
+----------+----------+
| user1    | 123      |
| user2    | 456      |
+----------+----------+
2 rows in set (0.00 sec)

七、測試是否讀寫成功分離

1、在客戶端多開幾個終端遠程連接mysql-proxy

至少開三臺纔可以:

2、在server3監聽3306端口

[root@server3 bin]# yum install lsof -y
[root@server3 bin]# lsof -i:3306

3、server2關閉異步複製,遠程添加數據(可以看到實驗效果)

  • server2:
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

在客戶端遠程登錄mysql-proxy,添加數據

[kiosk@vits Desktop]$ mysql -h 172.25.70.3 -uma -pMahao+001
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.24-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 redhat;
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 [redhat]> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 123      |
| user2    | 456      |
+----------+----------+
2 rows in set (0.00 sec)

MySQL [redhat]> insert into usertb values ('user3','789');
Query OK, 1 row affected (0.02 sec)

MySQL [redhat]> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 123      |
| user2    | 456      |
+----------+----------+
2 rows in set (0.00 sec)

可以發現數據寫入成功,但是查看不到。因爲我們把主從複製關掉了,寫在了server1裏面,但是讀的內容卻是server2,server2沒有同步server1的數據,故查看不到。

4、在server1查看數據是否添加成功:

mysql> select * from usertb;

可以看到數據確實添加到server1上面了

在server2查看數據:

mysql> select * from redhat.usertb;

server2確實沒有數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章