MySQL主從復制讀寫分離(基于mysql-proxy實現)
http://mirror.bit.edu.cn/mysql/Downloads/MySQL-Proxy/mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit.tar.gz
解壓
tar zxvf mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit.tar.gz
創建mysql-proxy帳號并授權
分別在主從數據庫中創建mysqlproxy帳號
mysql> grant all on *.* to mysqlproxy@'192.168.64.%' identified by 'mysqlproxy';
mysql> flush privileges;
mysql> use mysql;
mysql> select User,Password,Host from user;
啟動mysql-proxy
sudo ./mysql-proxy \
--daemon \
--log-level=debug \
--keepalive \
--log-file=/var/log/mysql-proxy.log \
--plugins="proxy" \
--proxy-backend-addresses="192.168.64.131:3306" \
--proxy-read-only-backend-addresses="192.168.64.132:3306" \
--proxy-lua-script="/home/ubuntu/apps/mysql-proxy-0.8.4/share/doc/mysql-proxy/rw-splitting.lua" \
--plugins="admin" \
--admin-username="admin" \
--admin-password="admin" \
--admin-lua-script="/home/ubuntu/apps/mysql-proxy-0.8.4/lib/mysql-proxy/lua/admin.lua"
查看mysql-proxy進程
ubuntu@s4:~/apps/mysql-proxy-0.8.4/bin$ ps -ef | grep mysql-proxy
root 18249 1 0 02:22 ? 00:00:00 /home/ubuntu/apps/mysql-proxy-0.8.4/libexec/mysql-proxy --daemon --log-level=debug --keepalive --log-file=/var/log/mysql-proxy.log --plugins=proxy --proxy-backend-addresses=192.168.64.131:3306 --proxy-read-only-backend-addresses=192.168.64.132:3306 --proxy-lua-script=/home/ubuntu/apps/mysql-proxy-0.8.4/share/doc/mysql-proxy/rw-splitting.lua --plugins=admin --admin-username=admin --admin-password=admin --admin-lua-script=/home/ubuntu/apps/mysql-proxy-0.8.4/lib/mysql-proxy/lua/admin.lua
root 18250 18249 0 02:22 ? 00:00:00 /home/ubuntu/apps/mysql-proxy-0.8.4/libexec/mysql-proxy --daemon --log-level=debug --keepalive --log-file=/var/log/mysql-proxy.log --plugins=proxy --proxy-backend-addresses=192.168.64.131:3306 --proxy-read-only-backend-addresses=192.168.64.132:3306 --proxy-lua-script=/home/ubuntu/apps/mysql-proxy-0.8.4/share/doc/mysql-proxy/rw-splitting.lua --plugins=admin --admin-username=admin --admin-password=admin --admin-lua-script=/home/ubuntu/apps/mysql-proxy-0.8.4/lib/mysql-proxy/lua/admin.lua
ubuntu 18252 15744 0 02:22 pts/1 00:00:00 grep --color=auto mysql-proxy
查看mysql-proxy端口
ubuntu@s4:~/apps/mysql-proxy-0.8.4/bin$ sudo netstat -ntlp | grep mysql-proxy
tcp 0 0 0.0.0.0:4040 0.0.0.0:* LISTEN 18250/mysql-proxy
tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN 18250/mysql-proxy
4040是proxy端口,4041是admin端口
連接管理端口
mysql> mysql -uadmin -padmin -h192.168.64.131 -P4041 連接管理端口
具體如下
ubuntu@s4:~/apps/mysql-proxy-0.8.4/bin$ mysql -uadmin -padmin -h192.168.64.131 -P4041
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
ERROR 1105 (07000): use 'SELECT * FROM help' to see the supported commands
mysql> SELECT * FROM help;
+------------------------+------------------------------------+
| command | description |
+------------------------+------------------------------------+
| SELECT * FROM help | shows this help |
| SELECT * FROM backends | lists the backends and their state |
+------------------------+------------------------------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM backends;
+-------------+---------------------+---------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+---------------------+---------+------+------+-------------------+
| 1 | 192.168.64.131:3306 | up | rw | NULL | 0 |
| 2 | 192.168.64.132:3306 | unknown | ro | NULL | 0 |
+-------------+---------------------+---------+------+------+-------------------+
2 rows in set (0.00 sec)
多開幾個客戶端后其狀態變為
mysql> SELECT * FROM backends;
+-------------+---------------------+-------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+---------------------+-------+------+------+-------------------+
| 1 | 192.168.64.131:3306 | up | rw | NULL | 0 |
| 2 | 192.168.64.132:3306 | up | ro | NULL | 0 |
+-------------+---------------------+-------+------+------+-------------------+
2 rows in set (0.00 sec)
state都為up表正常
連接同步端口
mysql> mysql -umysqlproxy -pmysqlproxy -h192.168.64.131 -P4040
多開啟幾個同步端口,在同步端口連接的客戶端中插入和查詢數據,觀察讀寫分離。
結論:192.168.64.131:3306只寫,192.168.64.132:3306只讀。
操作演示
不使用proxy連接數據庫,查詢192.168.64.131:3306上的數據
mysql> select * from zhang;
+------+-------+----------------+
| id | name | address |
+------+-------+----------------+
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 2 | zhang | this_is_master |
| 3 | zhang | this_is_master |
| 4 | zhang | this_is_master |
| 5 | zhang | this_is_master |
| 6 | zhang | this_is_master |
| 7 | zhang | this_is_master |
+------+-------+----------------+
17 rows in set (0.00 sec)
不使用proxy連接數據庫,查詢192.168.64.132:3306上的數據
mysql> select * from zhang;
+------+-------+---------------+
| id | name | address |
+------+-------+---------------+
| 2 | zhang | this_is_slave |
+------+-------+---------------+
1 row in set (0.00 sec)
使用proxy連接數據庫,執行查詢和插入操作
ubuntu@s4:~/apps$ mysql -umysqlproxy -pmysqlproxy -h192.168.64.131 -P4040
Welcome to the MySQL monitor. Commands end with ; or \g.
server default db: crm
client default db:
syncronizing
Your MySQL connection id is 45
Server version: 5.5.47-0ubuntu0.12.04.1-log (Ubuntu)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use crm;
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> select * from zhang;
+------+-------+---------------+
| id | name | address |
+------+-------+---------------+
| 2 | zhang | this_is_slave |
+------+-------+---------------+
1 row in set (0.00 sec)
# 此處數據為192.168.64.132:3306中的數據
mysql> insert into zhang values('8','zhang','this_is_master');
Query OK, 1 row affected (0.00 sec)
# 該數據將插入192.168.64.131:3306數據庫中
mysql> select * from zhang;
server default db:
client default db: crm
syncronizing
+------+-------+---------------+
| id | name | address |
+------+-------+---------------+
| 2 | zhang | this_is_slave |
+------+-------+---------------+
1 row in set (0.00 sec)
# 該數據仍來自192.168.64.132:3306中數據
不使用proxy連接192.168.64.131:3306觀察數據是否插入
mysql> select * from zhang;
+------+-------+----------------+
| id | name | address |
+------+-------+----------------+
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 1 | zhang | this_is_master |
| 2 | zhang | this_is_master |
| 3 | zhang | this_is_master |
| 4 | zhang | this_is_master |
| 5 | zhang | this_is_master |
| 6 | zhang | this_is_master |
| 7 | zhang | this_is_master |
| 8 | zhang | this_is_master |
+------+-------+----------------+
18 rows in set (0.00 sec)
由此可見使用mysql-proxy讀寫分離成功。