mycat讀寫分離測試
環境:四臺主機,兩臺mysql做主從:master和slave
一臺mysql做測試,ip:10.30.162.142
一臺mycat做讀寫分離,ip:192.168.122.230
客戶端訪問mycat端的虛擬資料庫
mycat端虛擬資料庫設定如下:
user:admin
password:redhat
mycat通過一個真實資料庫授過權的使用者來對資料庫端進行資料的操作,在這個實驗中這個使用者為mycatuser,密碼為123,能訪問的資料庫是mycat。
一、實現mysal主從,建立mycat庫。
過程略,master:192.168.122.82
slave: 192.168.122.217
二、在master端對mycatuser授權給mycatuser授權,
mysql> GRANT all ON *.* TO "mycatuser"@"%" IDENTIFIED BY "123"; Query OK, 0 rows affected, 1 warning (0.11 sec)
(授權最好是針對mycat端,某個具體的資料庫。也可以給所有庫所有表,如上)
三、mycat端進行登陸測試
[[email protected] ~]# mysql -umycatuser -p'123' -h 192.168.122.82 mysql: [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 20 Server version: 5.7.17-log Source distribution 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mycat | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
四、配置mycat
4.1、安裝
MyCat的官方網站:http://www.mycat.org.cn/
(mycat需要jdk環境的支援)
4.2、啟動服務
不一定成功,一定要看埠,或者jps命令看java程序
/usr/local/mycat/bin/mycat start [[email protected] mycat]# ss -antp | grep -E "8066|9066" LISTEN 0 100 :::9066 :::* users:(("java",pid=18644,fd=72)) LISTEN 0 100 :::8066 :::* users:(("java",pid=18644,fd=76))
4.3、修改配置檔案()
server.xml:配置虛擬資料庫的使用者名稱和密碼,讓客戶端通過虛擬的使用者名稱和密碼進行訪問。
#主要修改
[[email protected] ~]# vim /usr/local/mycat/conf/server.xml
<user name="admin">
<property name="password">redhat</property>
<property name="schemas">game</property>
</user>
user name="admin"指定虛擬使用者名稱
redhat 指定admin使用者的密碼是redhat
game 指定admin使用者相應的schema是game
在server.xml中一套 必須對應schema.xml配置中相應的schemal
否則有語法錯誤。
server.xml本來有配置如下:
<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user>
但在schema.xml中沒有做相應的schema配置,則出現語法錯誤,這一段不用則必須刪掉。
在schema.xml中設定與後端資料庫相連的真實使用者等。
[[email protected] ~]# cat /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="game" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
</schema>
<!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
/> -->
<dataNode name="dn1" dataHost="localhost1" database="mycat" />
<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.122.82:3306" user="mycatuser" password="123">
<readHost host="hostS2" url="192.168.122.217:3306" user="mycatuser" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
<schema name="game" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
給server.xml中配置的scheal(game)設定相應的節點為dn1。
<dataNode name="dn1" dataHost="localhost1" database="mycat" />
給dn1設定對應的主機池localhost1(邏輯上的,並不真實存在),對應的真實資料庫mycat
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
對該主機池的設定,balance=1表示開啟讀寫分離,預設為0指不開啟。
<writeHost host="hostM1" url="192.168.122.82:3306" user="mycatuser" password="123">
<readHost host="hostS2" url="192.168.122.217:3306" user="mycatuser" password="123" />
</writeHost>
配置後端真實的資料庫用於讀寫,用mycat端用後端資料庫中的真實使用者mycatuser和密碼去連線後端資料庫對mycat庫進行讀寫。
4.4、重啟服務
五、客戶端進行測試
首先在master端的mycat資料庫建立一個表
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mycat |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql> use mycat
Database changed
mysql> create table table1(name int(3),age tinyint);
Query OK, 0 rows affected (0.05 sec)
客戶端可以通過虛擬帳號登陸來檢視
[[email protected] ~]# mysql -uadmin -p -h 192.168.122.230 -P 8066
MySQL [(none)]> show databases;
+----------+
| DATABASE |
+----------+
| game |
+----------+
MySQL [(none)]> show tables from game;
+-----------------+
| Tables_in_mycat |
+-----------------+
| table1 |
+-----------------+
在客戶端插入資料
MySQL [game]> insert into table1 values(777,12);
Query OK, 1 row affected (0.02 sec)
master端檢視
mysql> use mycat
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 table1;
+------+------+
| name | age |
+------+------+
| 777 | 12 |
+------+------+
1 row in set (0.00 sec)