簡(jiǎn)單了解mysql mycat 中間件
一,什么是mycat
一個(gè)徹底開源的,面向企業(yè)應(yīng)用開發(fā)的大數(shù)據(jù)庫(kù)集群
支持事務(wù)、ACID、可以替代MySQL的加強(qiáng)版數(shù)據(jù)庫(kù)
一個(gè)可以視為MySQL集群的企業(yè)級(jí)數(shù)據(jù)庫(kù),用來(lái)替代昂貴的Oracle集群
一個(gè)融合內(nèi)存緩存技術(shù)、NoSQL技術(shù)、HDFS大數(shù)據(jù)的新型SQL Server
結(jié)合傳統(tǒng)數(shù)據(jù)庫(kù)和新型分布式數(shù)據(jù)倉(cāng)庫(kù)的新一代企業(yè)級(jí)數(shù)據(jù)庫(kù)產(chǎn)品
一個(gè)新穎的數(shù)據(jù)庫(kù)中間件產(chǎn)品
以上是官方說(shuō)明。其實(shí)就是數(shù)據(jù)庫(kù)的連接池。mysql proxy也是一種連接池,但是效率很低。
二,mycat 安裝
1,下載mycat
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 //添加以下內(nèi)容 <user name="user"> //mycat用戶名 <property name="password">user</property> //mycat密碼 <property name="schemas">mytest</property> //mycat虛擬數(shù)據(jù)庫(kù)名 <property name="readOnly">true</property> //只讀 </user> <user name="tankzhang"> <property name="password">admin</property> <property name="schemas">mytest</property> </user>
在這里要注意,默認(rèn)的虛擬數(shù)據(jù)名是TESTDB,如果schema.xml里面沒(méi)有配置testdb,那就要把testdb改成schema.xml里面有的虛擬數(shù)據(jù)名。這里定義的用戶名和密碼,虛擬數(shù)據(jù)庫(kù)名,并不是在mysql中真實(shí)存在的。
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" />//定義虛擬數(shù)據(jù)庫(kù)名mytest <dataNode name="my1" dataHost="test1" database="test" /> //真實(shí)數(shù)據(jù)庫(kù)名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" > //真實(shí)數(shù)據(jù)庫(kù)的連接方式 <readHost host="hostS1" url="192.168.5.214:3306" user="tank" password="123456" /> //同上 </writeHost> </dataHost> </mycat:schema>
mycat的配置參數(shù),相當(dāng)?shù)亩?。重點(diǎn)說(shuō)一下 balance="1"與writeType="0"
a. balance 屬性負(fù)載均衡類型,目前的取值有 4 種:
- balance="0", 不開啟讀寫分離機(jī)制,所有讀操作都發(fā)送到當(dāng)前可用的 writeHost 上。
- balance="1",全部的 readHost 與 stand by writeHost 參與 select 語(yǔ)句的負(fù)載均衡,簡(jiǎn)單的說(shuō),當(dāng)雙主雙從模式(M1 ->S1 , M2->S2,并且 M1 與 M2 互為主備),正常情況下, M2,S1,S2 都參與 select 語(yǔ)句的負(fù)載均衡。
- balance="2",所有讀操作都隨機(jī)的在 writeHost、 readhost 上分發(fā)。
- balance="3", 所有讀請(qǐng)求隨機(jī)的分發(fā)到 wiriterHost 對(duì)應(yīng)的 readhost 執(zhí)行,writerHost 不負(fù)擔(dān)讀壓力,注意 balance=3 只在 1.4 及其以后版本有, 1.3 沒(méi)有。
b. writeType 屬性
負(fù)載均衡類型,目前的取值有 3 種:
- writeType="0", 所有寫操作發(fā)送到配置的第一個(gè) writeHost,第一個(gè)掛了切到還生存的第二個(gè)writeHost,重新啟動(dòng)后已切換后的為準(zhǔn),切換記錄在配置文件中:dnindex.properties .
- writeType="1",所有寫操作都隨機(jī)的發(fā)送到配置的 writeHost。
- writeType="2",沒(méi)實(shí)現(xiàn)。
具體參數(shù)
3,配置主從服務(wù)器
4,添加真實(shí)用戶
grant all privileges on test.* to tank@"192.168.%" identified by '123456'; flush privileges
在213,214二臺(tái)機(jī)器上添加用戶。
5,測(cè)試真實(shí)用戶連接
確保schema.xml中配置的真實(shí)用戶,能連上真實(shí)的數(shù)據(jù)庫(kù)。注意防火墻。
四,啟動(dòng)mycat
1,常用參數(shù)
./mycat start 啟動(dòng) ./mycat stop 停止 ./mycat console 前臺(tái)運(yùn)行 ./mycat restart 重啟服務(wù) ./mycat pause 暫停 ./mycat status 查看啟動(dòng)狀態(tài)
2,啟動(dòng),并查看mycat
# ./mycat start Starting Mycat-server... # netstat -tpnl |grep 8066 tcp 0 0 :::8066 :::* LISTEN 31728/java # ./mycat status Mycat-server is running (31726).
五,測(cè)試讀寫分離
# 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 | //虛擬數(shù)據(jù)庫(kù)
+----------+
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 '創(chuàng)建時(shí)間',
-> 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; //修改從數(shù)據(jù)庫(kù)的user表中的name,會(huì)發(fā)現(xiàn)讀是從從數(shù)據(jù)庫(kù)讀取的
+----+-----------+-------------+
| id | name | create_time |
+----+-----------+-------------+
| 1 | tankzhang | 0 |
+----+-----------+-------------+
1 row in set (0.01 sec)
六,小結(jié)
mycat支持 mysql的分表,分片等等,但是不建議使用。mycat支持的集群不多,如果能配合mha使用就比較牛B了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信