这里有新鲜出炉的 Mysql 教程,程序狗速度看过来!
MySQL 是一个开放源码的小型关联式数据库管理系统,开发者为瑞典 MySQL AB 公司。MySQL 被广泛地应用在 Internet 上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了 MySQL 作为网站数据库。
MyCAT 是 MySQL 中间件,前身是阿里大名鼎鼎的 Cobar,Cobar 在开源了一段时间后,不了了之。于是 MyCAT 扛起了这面大旗,在大数据时代,其重要性愈发彰显。这篇文章主要是 MyCAT 的入门部署。
一,什么是 mycat
一个彻底开源的,面向企业应用开发的大数据库集群
支持事务、ACID、可以替代 MySQL 的加强版数据库
一个可以视为 MySQL 集群的企业级数据库,用来替代昂贵的 Oracle 集群
一个融合内存缓存技术、NoSQL 技术、HDFS 大数据的新型 SQL Server
结合传统数据库和新型分布式数据仓库的新一代企业级数据库产品
一个新颖的数据库中间件产品
以上是官方说明。其实就是数据库的连接池。mysql proxy 也是一种连接池,但是效率很低。
二,mycat 安装
1,下载地址 mycat
http://dl.mycat.io/
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 种:
1. balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的 writeHost 上。
2. balance="1",全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡,简单的说,当双主双从模式 (M1 ->S1 , M2->S2,并且 M1 与 M2 互为主备),正常情况下, M2,S1,S2 都参与 select 语句的负载均衡。
3. balance="2",所有读操作都随机的在 writeHost、 readhost 上分发。
4. balance="3", 所有读请求随机的分发到 wiriterHost 对应的 readhost 执行, writerHost 不负担读压力,注意 balance=3 只在 1.4 及其以后版本有, 1.3 没有。
b. writeType 属性
负载均衡类型,目前的取值有 3 种:
1. writeType="0", 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个
writeHost,重新启动后已切换后的为准,切换记录在配置文件中: dnindex.properties .
2. writeType="1",所有写操作都随机的发送到配置的 writeHost。
3. writeType="2",没实现。
具体参数:http://mycat.io/document/Mycat_V1.6.0.pdf
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 了。
来源: http://www.phperz.com/article/17/0526/335536.html