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端进行登陆测试

[root@mycat ~]# 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
[root@kvm3 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:配置虚拟数据库的用户名和密码,让客户端通过虚拟的用户名和密码进行访问。

#主要修改
[root@mycat ~]# 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中设置与后端数据库相连的真实用户等。

[root@kvm3 ~]# 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)

客户端可以通过虚拟帐号登陆来查看

[root@chenjiaqi ~]# 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)

猜你喜欢

转载自blog.csdn.net/weixin_42275939/article/details/83039913