mysql+mycat实现读写分离
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql+mycat实现读写分离相关的知识,希望对你有一定的参考价值。
centos7
master | slave | mycat1.6 | client |
---|---|---|---|
192.168.41.10 | 192.168.41.11 | 192.168.41.12 | 192.168.41.13 |
实验环境关闭自己的防火墙
配置所有主机hosts文件:
1、master(41.10):
vim /etc/my.cnf
systemctl restart mysqld 重启服务读取配置文件参数
1)配置复制用户以及root用户权限
2、slave(41.11)
配置/etc/my.cnf配置文件
systemctl restart mysqld
mysql> change master to master_host=‘192.168.41.10‘,master_user=‘myslave‘,master_password=‘123.com‘,master_log_file=‘mysql-bin.000002‘,master_log_pos=1334;
mysql> start slave ;
mysql> show slave statusG;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> grant all on . to [email protected]‘%‘ identified by ‘123.com‘;
mysql> flush privileges;
回到master主机创建test库
mysql> create database test;
3、配置mycat(41.12)
1)部署mycat
wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
[[email protected] ~]# tar zxf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local
2)加载环境变量
[[email protected] ~]# vi /etc/profile
.....
export PATH=$PATH:/usr/local/java/bin
export JAVA_HOME=/usr/local/java
export MYCAT_HOME=/usr/local/mycat
export PATH=$PATH:/usr/local/mycat/bin
[[email protected] ~]# source /etc/profile
3)配置serve.xml
[[email protected] ~]# cd /usr/local/mycat/conf/
[[email protected] conf]# vim server.xml
......
<user name="root">
<property name="password">123.com</property>
<property name="schemas">test</property><!-- 表级 DML 权限设置 --> <!-- <privileges check="false"> <schema name="TESTDB" dml="0110" > <table name="tb01" dml="0000"></table> <table name="tb02" dml="1111"></table> </schema> </privileges> --> </user> <user name="user"> <property name="password">user</property> <property name="schemas">test</property> <property name="readOnly">true</property> </user>
</mycat:server>
4)配置schema.xml
[[email protected] conf]# vim schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="test" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
</schema>
<dataNode name="dn1" dataHost="host01" database="test" />
<dataHost name="host01" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>show slave status</heartbeat>
<writeHost host="hostM1" url="192.168.41.10:3306" user="root" password="123.com">
<readHost host="hostS1" url="192.168.41.11:3306" user="root" password="123.com"/>
</writeHost>
</dataHost>
</mycat:schema>
balance?属性负载均衡类型,目前的取值有 4 种:
balance="0", 不开启读写分离机制,所有读操作都发送到当前可用 的writeHost 上。 balance="1",全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡,简单的说,当双主双从模式(M1 ->S1 , M2->S2,并且 M1 与 M2 互为主备),正常情况下, M2,S1,S2 都参与 select 语句的负载均衡。 balance="2",所有读操作都随机的在 writeHost、 readhost 上分发。 balance="3", 所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压力,注意 balance=3 只在 1.4 及其以后版本有, 1.3 没有。
writeType?属性,负载均衡类型,目前的取值有 3 种:
writeType="0", 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties . writeType="1",所有写操作都随机的发送到配置的 writeHost。 writeType="2",没实现。switchType?属性
-1 表示不自动切换 1 默认值,自动切换 2 基于MySQL 主从同步的状态决定是否切换
5)启动mycat服务器
[[email protected] logs]# mycat start
[[email protected] logs]# tailf wrapper.log #观察启动日志,便于排错
[[email protected] logs]# ss -anpt | grep java
LISTEN 0 1 127.0.0.1:32000 : users:(("java",pid=40133,fd=4))
LISTEN 0 50 :::50632 ::: users:(("java",pid=40133,fd=51))
LISTEN 0 100 :::9066 ::: users:(("java",pid=40133,fd=69))
LISTEN 0 50 :::33782 ::: users:(("java",pid=40133,fd=53))
LISTEN 0 50 :::1984 ::: users:(("java",pid=40133,fd=52))
LISTEN 0 100 :::8066 :::* users:(("java",pid=40133,fd=73))
4、客户端(41.13)验证读写分离
①登录到连接端口
[[email protected] ~]# mysql -h 192.168.41.12 -P 8066 -uroot -p123.com
mysql> show databases;
+----------+
| DATABASE |
+----------+
| test |
+----------+
1 row in set (0.01 sec)
mysql> use test
Database changed
mysql> create table tb (id int);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into tb values (1);
Query OK, 1 row affected (0.05 sec)
mysql> select * from tb;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.02 sec)
②登录管理端口
[[email protected] ~]# mysql -P9066 -uroot -p123.com -h 192.168.41.12
mysql> show @@datasource ;
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
| DATANODE | NAME | TYPE | HOST | PORT | W/R | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
| dn1 | hostM1 | mysql | 192.168.1.12 | 3306 | W | 0 | 10 | 1000 | 49 | 0 | 2 |
| dn1 | hostS1 | mysql | 192.168.1.10 | 3306 | R | 0 | 6 | 1000 | 44 | 2 | 0 |
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
从管理端口观察两主机负载变化,发现READ_LOAD在hostS1为2, WRITE_LOAD在hostM1为2,说明读写分离已经实现
以上是关于mysql+mycat实现读写分离的主要内容,如果未能解决你的问题,请参考以下文章