2、Mycat+MySQL主從複製讀寫分離
Mycat實現MySQL主從複製讀寫分離
1、部署jdk環境
MyCAT用Java開發,需要有JAVA執行環境,mycat依賴jdk1.7的環境
1)上傳jdk
[root@localhost tools]# ll jdk-7u45-linux-x64.tar.gz
-rw-r--r-- 1 root root 138094686 10月 24 2013 jdk-7u45-linux-x64.tar.gz
2)安裝jdk
[root@localhost tools]# mkdir /usr/java
[root@localhost tools]# tar xf jdk-7u45-linux-x64.tar.gz -C /usr/java/
3)設定環境變數
[root@localhost tools]# vim /etc/profile.d/java.sh
內容如下:
export JAVA_HOME=/usr/java/jdk1.7.0_45/
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
使環境變數當前終端生效
[root@localhost tools]# source /etc/profile.d/java.sh
4)測試
[root@localhost tools]# java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
2、安裝Mycat
1)下載並上傳mycat包
[root@localhost tools]# ll Mycat-server-1.5.1-RELEASE-20161130213509-linux.tar.gz
-rw-r--r--. 1 root root 11499865 8月 31 16:33 Mycat-server-1.5.1-RELEASE-20181130213509-linux.tar.gz
2)解壓
[root@localhost tools]# tar xf Mycat-server-1.5.1-RELEASE-20161130213509-linux.tar.gz -C /usr/local/
解壓後內容如下:
[root@localhost tools]# ll /usr/local/mycat
3)新增環境變數
[root@localhost tools]# vim /etc/profile.d/mycat.sh
export PATH=$PATH:/usr/local/mycat/bin
[root@localhost tools]# source /etc/profile.d/mycat.sh
3、讀寫分離配置
1)不使用Mycat託管MySQL主從伺服器,簡單使用如下配置
#注意:配置前備份下配置檔案
[root@localhost tools]# cd /usr/local/mycat/conf
[root@localhost conf]# cp schema.xml{,.bak}
修改主配置檔案 vim /usr/local/mycat/conf/schema.xml
(1)<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
這裡的TESTDB就是我們所宣稱的資料庫名稱,必須和server.xml中的使用者指定的資料庫名稱一致。新增一個dataNode="dn1",是指定了我們這個庫只有在dn1上,沒有分庫。
(2)<dataNode name="dn1" dataHost="localhost1" database="db1" />
這裡只需要改database的名字,就是你真是的資料庫上的資料庫名,可根據自己的資料庫名稱修改。
(3) <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
需要配置的位置:
balance="1" writeType="0" switchType="1"
balance
1、balance=0 不開啟讀寫分離機制,所有讀操作都發送到當前可用的writehostle .
2、balance=1 全部的readhost與stand by writeHost 參與select語句的負載均衡。簡單的說,雙主雙從模式(M1->S1,M2->S2,並且M1和M2互為主備),正常情況下,M1,S1,S2都參與select語句的複雜均衡。
3、balance=2 所有讀操作都隨機的在readhost和writehost上分發
writeType
負載均衡型別,目前的取值有3種:
1、writeType="0", 所有寫操作傳送到配置的第一個writeHost。
2、writeType="1",所有寫操作都隨機的傳送到配置的writeHost。
3、writeType="2",不執行寫操作。
switchType
1、switchType=-1 表示不自動切換
2、switchType=1 預設值,自動切換
3、switchType=2 基於MySQL 主從同步的狀態決定是否切換
(4)<writeHost host="hostM1" url="192.168.46.129:3306" user="mycat" password="123456">
<!– can have multi read hosts –>
<readHost host="hostS2" url="192.168.46.130:3306" user="mycat_r" password="123456" />
<readHost host="hostS3" url="192.168.46.130:3307" user="mycat_r" password="123456" />
<!--<writeHost host="hostS1" url="localhost:3316" user="root"
password="123456" />-->
4配置好檔案內容如下:
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/" >
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
</schema>
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.46.129:3306" user="root"
password="123456">
<readHost host="hostR1" url="192.168.46.129:3306" user="root" password="123456" />
<readHost host="hostR2" url="192.168.46.129:3307" user="root" password="123456" />
</writeHost>
</dataHost>
</mycat:schema>
5、建立管理使用者
主庫上對mycat使用者授權如下:
使用者:mycat 密碼:123456 埠:3306
許可權:insert,delete,update,select
命令:grant insert,delete,update,select on TD_OA.* to mycat@'192.168.46.%' identified by '123456';
flush privileges;
從庫上mycat_r使用者授權如下:
使用者:mycat_r 密碼:123456 埠:3306/3307
許可權: select
grant select on TD_OA.* to mycat@'192.168.46.%' identified by '123456';
flush privileges;
測試環境可以直接使用root使用者,授予所有許可權:
mysql> grant all on *.* to root@'192.168.46.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to root@'localhost' identified by '123456';
6、修改mycat配置檔案
採用預設配置
注意:
①這裡配置的是可以連線主庫的兩個使用者
使用者:test 密碼:test 給予此使用者TESTDB資料庫增刪改查的許可權。
使用者:user 密碼:password 給予此使用者TESTDB資料庫讀的許可權。
②這裡的TESTDB,不一定是你資料庫上的真實庫名,可以任意指定,只要接下來和schema.xml的配置檔案的庫名統一即可。
7、啟動Mycat
方法一:# mycat console #<=通過console命令啟動mycat,這樣方便提取資訊
方法二:# mycat start
方法三:# startup_nowrap.sh#服務指令碼方式啟動
[root@localhost conf]# netstat -lnupt | egrep "(8|9)066"
tcp6 0 0 :::9066 :::* LISTEN 3342/java
tcp6 0 0 :::8066 :::* LISTEN 3342/java
重啟:# mycat restart
8、在客戶端連線mysql主庫伺服器:
# mysql -uuser -puser -h192.168.46.133 -P8066 -DTESTDB
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2016, 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>
9、主從同步讀寫分離測試
管理端建立表:
[root@localhost ~]# mysql -utest -ptest -h192.168.46.133 -P8066 -DTESTDB
CREATE TABLE test1 (id int(10),name varchar(10),address varchar(20) DEFAULT NULL);
手動停止主從同步: stop slave;
分別在主從庫插入資料:
master: insert into test1 values(1,'test1','master');
slave1: insert into test1 values(2,'test1','slave1');
slave2: insert into test1 values(3,'test1','slave2');
管理端驗證
負載均衡:
mysql> select * from test1;
+------+-------+---------+
| id | name | address |
+------+-------+---------+
| 2 | test1 | slave1 |
+------+-------+---------+
1 row in set (0.00 sec)
mysql> select * from test1;
+------+-------+---------+
| id | name | address |
+------+-------+---------+
| 3 | test1 | slave2 |
+------+-------+---------+
1 row in set (0.00 sec)
讀寫功能:
管理端再次插入資料 insert into test1 values(4,'test1','write');
mysql> insert into test1 values(4,'test1','write');
Query OK, 1 row affected (0.00 sec)
#注意:測試完畢啟動主從同步功能。
10、管理命令與監控
mycat自身有類似其他資料庫的管理監控方式,可通過mysql命令列,登陸埠9066執行相應的SQL操作,也可通過jdbc的方式進行遠端連線管理。
登入:目前mycat有兩個埠,8066資料埠,9066管理埠。命令列登入時通過9066管理埠來執行:
# mysql -uuser -puser -h192.168.46.133 -P9066 -DTESTDB
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (monitor)
Copyright (c) 2000, 2016, 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>
選項:
-h 後面接主機
-u mycat server.xml配置的邏輯庫使用者
-p mycat server.xml配置的邏輯庫密碼
-P 後面接的埠9066,注意P大寫
-D Mycat server.xml中配置的邏輯庫
1)檢視所有的命令,如下:
mysql> show @@help;
2)顯示mycat資料庫的列表,對應的在scehma.xml配置的邏輯庫
mysql> show @@databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
1 row in set (0.00 sec)
3)顯示mycat資料節點的列表,對應的是scehma.xml配置檔案的dataNode節點
mysql> show @@datanode;
其中,NAME表示datanode的名稱;dataHost 對應的是dataHost屬性的值,資料主機的名稱,ACTIVE表示活躍的連線數,IDIE表示閒置的連線數,SIZE對應的是總連線的數量。
mysql> show @@heartbeat;
RS_CODE狀態為1,正常狀態
4、獲取當前mycat的版本
mysql> show @@version;/
5、顯示mycat前端連線狀態
mysql> show @@connection;
6、顯示mycat後端連線狀態
mysql> show @@backend;
7、顯示資料來源
mysql> show @@datasource