读写分离-mysql-prxoy

1.安装mysql-proxy

下载安装:http://mirrors.163.com/mysql/Downloads/MySQL-Proxy/mysql-proxy-0.8.4-linuxel6-x8664bit.tar.gz 
tar xf mysql-proxy-0.8.4-linux-el6-x86-64bit.tar.gz -C /usr/loca
mv /usr/local/mysql-proxy-0.8.4-linux-el6-x86-64bit mysql-proxy
设置环境变量
echo "export PATH=/usr/local/mysql-proxy/bin:$PATH" > /etc/profile.d/mysql-proxy.sh 
source /etc/profile.d/mysql-proxy.sh

2.启动mysql-proxy

mysql-proxy --daemon --log-level=debug --user=mysql-proxy --keepalive --log-file=/var/log/mysql-proxy.log --plugins="proxy" --proxy-backend-addresses="192.168.1.22:3306" --proxy-read-only-backend-addresses="192.168.1.23:3306" --proxy-lua-script="/usr/local/mysqlproxy/share/doc/mysql-proxy/rw-splitting.lua" --plugins=admin --admin-username="admin" --admin-password="admin" --admin-lua-script="/usr/local/mysql-proxy/lib/mysqlproxy/lua/admin.lua"
 
 以上字段的涵义为: 
mysql-proxy –daemon #以守护进程启动 mysql-proxy 
 
--log-level=debug  #指定日志级别为 debug 
--user=mysql-proxy  #运行守护进程的用户 
--keepalive   #在 keepalive 崩溃时尝试重启进程 
--log-file=/var/log/mysql-proxy.log  #指定日志文件 
--plugins="proxy"  #mysql-proxy 启动时加载的插件 
--proxy-backend-addresses="192.168.182.147:3306" #后端主 mysql 服务器的地址 和端口; 
--proxy-read-only-backend-addresses="192.168.182.140:3306" #后端只读 mysql 服务 器的地址和端口; 
--proxy-lua-script="/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua" #完成 mysql 代理功能的 Lua 脚本 
--plugins=admin #指定 admin 插件 
--admin-username="admin"  #设置登录 mysql-proxy 的管理账户 
--admin-password="admin"  #设置登录 mysql-proxy 的密码 

3.查看启动状态

netstat -nutlp

4.数据库授权mysql-proxy

#在主库执行授权信息,从库会自动同步:
Mariadb [(nane)]> grant all on *.* to "mycat-proxy"@"192.168.1.20" ientified by "123456";
5.查看读写分离状态:
基于4041端口mysql-proxy查看读写分离状态,登录4041端口:mysql-prxoyIP登录
mysql -h192.168.75.133 -uadmin -padmin -P4041
# 这时可以看到后端数据库信息,只是状态为unknown,表示还没有客户端连接,可以通过4040代理端口通过 查询数据等操作激活。 
mysql> select * from backends; 
+-------------+---------------------+---------+------+------+------------------
+ 
| backend_ndx | address             | state   | type | uuid | connected_clients | 
+-------------+---------------------+---------+------+------+------------------
+ 
|           1 | 192.168.75.134:3306 | unknown | rw   | NULL |                 0 | 
|           2 | 192.168.75.135:3306 | unknown | ro   | NULL |                 0 | 
+-------------+---------------------+---------+------+------+------------------+
 2 rows in set (0.00 sec)
6.激活mysql-proxy:
通过4040代理端口插入数据,该sql语句会走master,于是可以激活master状态:
mysql -h192.168.75.133 -umysql-proxy -p123456 -P4040 -e "create database lutixia charset utf8;"
在4041管理端口,再次查看:

mysql -h192.168.75.133 -uadmin -padmin -P4041
mysql> select * from backends; 
+-------------+---------------------+---------+------+------+------------------
+ 
| backend_ndx | address             | state   | type | uuid | connected_clients | 
+-------------+---------------------+---------+------+------+------------------
+ 
|           1 | 192.168.75.134:3306 | up | rw   | NULL |                 0 | 
|           2 | 192.168.75.135:3306 | unknown | ro   | NULL |                 0 | 
+-------------+---------------------+---------+------+------+------------------+
 2 rows in set (0.00 sec)
通过4040代理端口查询数据,该sql语句会走slave,于是可以激活slave状态:

# 先在从库选择lutixia数据库(因为主从关系,在主库创建的lutixia会同步至从库),创建表格,并插 入数据: 
 MariaDB [(none)]> use lutixia 
 Database changed 
 MariaDB [lutixia]> create table t1( id int, name varchar(20) ); 
 Query OK, 0 rows affected (0.00 sec)
MariaDB [lutixia]> insert t1 values(1,"xiaoming"); 
Query OK, 1 row affected (0.00 sec)
在4041管理端口,再次查看:

mysql -h192.168.75.133 -uadmin -padmin -P4041
mysql> select * from backends; 
+-------------+---------------------+---------+------+------+------------------
+ 
| backend_ndx | address             | state   | type | uuid | connected_clients | 
+-------------+---------------------+---------+------+------+------------------
+ 
|           1 | 192.168.75.134:3306 | up | rw   | NULL |                 0 | 
|           2 | 192.168.75.135:3306 | up | ro   | NULL |                 0 | 
+-------------+---------------------+---------+------+------+------------------+
 2 rows in set (0.00 sec)

 

posted on 2021-03-08 10:08  蜂蜜log  阅读(102)  评论(0编辑  收藏  举报

导航