简单了解mysql mycat 中间件

吾爱主题 阅读:122 2024-04-05 14:21:44 评论:0

一,什么是mycat

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

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

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

一个融合内存缓存技术、NoSQL技术、HDFS大数据的新型SQL Server

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

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

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

二,mycat 安装

1,下载mycat

2,安装mycat

?
1 # tar zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/

三,配置mycat

1,配置server.xml

?
1 2 3 4 5 6 7 8 9 10 # 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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 # 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,添加真实用户

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

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

5,测试真实用户连接

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

四,启动mycat

1,常用参数

?
1 2 3 4 5 6 ./mycat start 启动 ./mycat stop 停止 ./mycat console 前台运行 ./mycat restart 重启服务 ./mycat pause 暂停 ./mycat status 查看启动状态

2,启动,并查看mycat

?
1 2 3 4 5 6 # ./mycat start Starting Mycat-server... # netstat -tpnl |grep 8066 tcp 0 0 :::8066 :::* LISTEN 31728/java # ./mycat status Mycat-server is running (31726).

五,测试读写分离

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 # 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了。

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

原文链接:http://blog.51yip.com/mysql/1854.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容