mysql+mycat實現讀寫分離
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> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> grant all on . to root@‘%‘ 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
2)加載環境變量
[root@192 ~]# 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
[root@192 ~]# source /etc/profile
3)配置serve.xml
[root@192 ~]# cd /usr/local/mycat/conf/
......
<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
[root@192 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服務器
[root@192 logs]# mycat start
[root@192 logs]# tailf wrapper.log #觀察啟動日誌,便於排錯
[root@192 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)驗證讀寫分離
①登錄到連接端口
[root@192 ~]# 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)
②登錄管理端口
[root@192 ~]# 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實現讀寫分離