您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)mysql中間件mycat怎么用,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
一,什么是mycat
一個(gè)徹底開源的,面向企業(yè)應(yīng)用開發(fā)的大數(shù)據(jù)庫集群
支持事務(wù)、ACID、可以替代MySQL的加強(qiáng)版數(shù)據(jù)庫
一個(gè)可以視為MySQL集群的企業(yè)級數(shù)據(jù)庫,用來替代昂貴的Oracle集群
一個(gè)融合內(nèi)存緩存技術(shù)、NoSQL技術(shù)、HDFS大數(shù)據(jù)的新型SQL Server
結(jié)合傳統(tǒng)數(shù)據(jù)庫和新型分布式數(shù)據(jù)倉庫的新一代企業(yè)級數(shù)據(jù)庫產(chǎn)品
一個(gè)新穎的數(shù)據(jù)庫中間件產(chǎn)品
以上是官方說明。其實(shí)就是數(shù)據(jù)庫的連接池。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 //添加以下內(nèi)容 <user name="user"> //mycat用戶名 <property name="password">user</property> //mycat密碼 <property name="schemas">mytest</property> //mycat虛擬數(shù)據(jù)庫名 <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里面沒有配置testdb,那就要把testdb改成schema.xml里面有的虛擬數(shù)據(jù)名。這里定義的用戶名和密碼,虛擬數(shù)據(jù)庫名,并不是在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ù)庫名mytest <dataNode name="my1" dataHost="test1" database="test" /> //真實(shí)數(shù)據(jù)庫名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ù)庫的連接方式 <readHost host="hostS1" url="192.168.5.214:3306" user="tank" password="123456" /> //同上 </writeHost> </dataHost> </mycat:schema>
mycat的配置參數(shù),相當(dāng)?shù)亩?。重點(diǎn)說一下 balance="1"與writeType="0"
a. balance 屬性負(fù)載均衡類型,目前的取值有 4 種:
1. balance="0", 不開啟讀寫分離機(jī)制,所有讀操作都發(fā)送到當(dāng)前可用的 writeHost 上。
2. balance="1",全部的 readHost 與 stand by writeHost 參與 select 語句的負(fù)載均衡,簡單的說,當(dāng)雙主雙從模式(M1 ->S1 , M2->S2,并且 M1 與 M2 互為主備),正常情況下, M2,S1,S2 都參與 select 語句的負(fù)載均衡。
3. balance="2",所有讀操作都隨機(jī)的在 writeHost、 readhost 上分發(fā)。
4. balance="3", 所有讀請求隨機(jī)的分發(fā)到 wiriterHost 對應(yīng)的 readhost 執(zhí)行,writerHost 不負(fù)擔(dān)讀壓力,注意 balance=3 只在 1.4 及其以后版本有, 1.3 沒有。
b. writeType 屬性
負(fù)載均衡類型,目前的取值有 3 種:
1. writeType="0", 所有寫操作發(fā)送到配置的第一個(gè) writeHost,第一個(gè)掛了切到還生存的第二個(gè)
writeHost,重新啟動(dòng)后已切換后的為準(zhǔn),切換記錄在配置文件中:dnindex.properties .
2. writeType="1",所有寫操作都隨機(jī)的發(fā)送到配置的 writeHost。
3. writeType="2",沒實(shí)現(xiàn)。
具體參數(shù):http://mycat.io/document/Mycat_V1.6.0.pdf
3,配置主從服務(wù)器,就不在這兒說了,博客中有
4,添加真實(shí)用戶
grant all privileges on test.* to tank@"192.168.%" identified by '123456'; flush privileges
在213,214二臺(tái)機(jī)器上添加用戶。
5,測試真實(shí)用戶連接,確保schema.xml中配置的真實(shí)用戶,能連上真實(shí)的數(shù)據(jù)庫。注意防火墻。
四,啟動(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).
五,測試讀寫分離
# 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ù)庫 +----------+ 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ù)庫的user表中的name,會(huì)發(fā)現(xiàn)讀是從從數(shù)據(jù)庫讀取的 +----+-----------+-------------+ | id | name | create_time | +----+-----------+-------------+ | 1 | tankzhang | 0 | +----+-----------+-------------+ 1 row in set (0.01 sec)
關(guān)于“mysql中間件mycat怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。