简单了解mysql mycat 中间件

2022-10-16,,,

一,什么是mycat

一个彻底开源的,面向企业应用开发的大数据库集群

支持事务、acid、可以替代mysql的加强版数据库

一个可以视为mysql集群的企业级数据库,用来替代昂贵的oracle集群

一个融合内存缓存技术、nosql技术、hdfs大数据的新型sql server

结合传统数据库和新型分布式数据仓库的新一代企业级数据库产品

一个新颖的数据库中间件产品

以上是官方说明。其实就是数据库的连接池。mysql proxy也是一种连接池,但是效率很低。

二,mycat 安装

1,下载

2,安装mycat

# tar zxvf mycat-server-1.6-release-20161028204710-linux.tar.gz -c /usr/local/ 

三,配置mycat

1,配置server.xml

# vim /usr/local/mycat/conf/server.xml //添加以下内容 
<user name="user"> //mycat用户名 
<property name="password">user</property> //mycat密码 
<property name="schemas">mytest</property> //mycat虚拟数据库名 
<property name="readonly">true</property> //只读 
</user> 
<user name="tankzhang"> 
<property name="password">admin</property> 
<property name="schemas">mytest</property> 
</user> 

在这里要注意,默认的虚拟数据名是testdb,如果schema.xml里面没有配置testdb,那就要把testdb改成schema.xml里面有的虚拟数据名。这里定义的用户名和密码,虚拟数据库名,并不是在mysql中真实存在的。

2,配置schema.xml

# cat schema.xml 
<?xml version="1.0"?> 
<!doctype mycat:schema system "schema.dtd"> 
<mycat:schema xmlns:mycat="http://io.mycat/"> 
<schema name="mytest" checksqlschema="false" sqlmaxlimit="100" datanode="my1" />//定义虚拟数据库名mytest 
<datanode name="my1" datahost="test1" database="test" /> //真实数据库名test 
<datahost name="test1" maxcon="1000" mincon="10" balance="1" writetype="0" dbtype="mysql" dbdriver="native" > 
<heartbeat>select user()</heartbeat> 
<writehost host="hostm1" url="192.168.5.213:3306" user="tank" password="123456" > //真实数据库的连接方式 
<readhost host="hosts1" url="192.168.5.214:3306" user="tank" password="123456" /> //同上 
</writehost> 
</datahost> 
</mycat:schema> 

mycat的配置参数,相当的多。重点说一下 balance="1"与writetype="0"

a. 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 没有。

b. writetype 属性

负载均衡类型,目前的取值有 3 种:

  •  writetype="0", 所有写操作发送到配置的第一个 writehost,第一个挂了切到还生存的第二个writehost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .
  • writetype="1",所有写操作都随机的发送到配置的 writehost。
  •  writetype="2",没实现。

具体参数

3,配置主从服务器

4,添加真实用户

grant all privileges on test.* to tank@"192.168.%" identified by '123456'; 
flush privileges 

在213,214二台机器上添加用户。

5,测试真实用户连接

确保schema.xml中配置的真实用户,能连上真实的数据库。注意防火墙。

四,启动mycat

1,常用参数

./mycat start 启动 
./mycat stop 停止 
./mycat console 前台运行 
./mycat restart 重启服务 
./mycat pause 暂停 
./mycat status 查看启动状态 

2,启动,并查看mycat

# ./mycat start 
starting mycat-server... 
# netstat -tpnl |grep 8066 
tcp 0 0 :::8066 :::* listen 31728/java 
# ./mycat status 
mycat-server is running (31726). 

五,测试读写分离

# mysql -u tankzhang -p -p 8066 -h 127.0.0.1 //一定要带上127.0.0.1 
enter password: 
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> show databases; 
+----------+ 
| database | 
+----------+ 
| mytest | //虚拟数据库 
+----------+ 
1 row in set (0.00 sec) 

mysql> use mytest; 
reading table information for completion of table and column names 
you can turn off this feature to get a quicker startup with -a 

mysql> create table if not exists `user` ( 
-> `id` int(11) unsigned not null default '0' comment 'id', 
-> `name` varchar(20) not null default '' comment '姓名', 
-> `create_time` int(10) not null default '0' comment '创建时间', 
-> primary key (`id`) 
-> ) engine=myisam default charset=utf8 auto_increment=1 ; 
query ok, 0 rows affected (0.08 sec) 
database changed 
mysql> show tables; 
+----------------+ 
| tables_in_test | 
+----------------+ 
| user | 
+----------------+ 
1 row in set (0.01 sec) 
mysql> insert into `user` (`id` ,`name`)values ('1', 'tank'); 
query ok, 1 row affected (0.00 sec) 

mysql> select * from user; //修改从数据库的user表中的name,会发现读是从从数据库读取的 
+----+-----------+-------------+ 
| id | name | create_time | 
+----+-----------+-------------+ 
| 1 | tankzhang | 0 | 
+----+-----------+-------------+ 
1 row in set (0.01 sec) 

六,小结

mycat支持 mysql的分表,分片等等,但是不建议使用。mycat支持的集群不多,如果能配合mha使用就比较牛b了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《简单了解mysql mycat 中间件.doc》

下载本文的Word格式文档,以方便收藏与打印。