mysql mycat 中間件安裝與使用
一,什么是mycat
一個徹底開源的,面向企業(yè)應用開發(fā)的大數(shù)據庫集群
支持事務、ACID、可以替代MySQL的加強版數(shù)據庫
一個可以視為MySQL集群的企業(yè)級數(shù)據庫,用來替代昂貴的Oracle集群
一個融合內存緩存技術、NoSQL技術、HDFS大數(shù)據的新型SQL Server
結合傳統(tǒng)數(shù)據庫和新型分布式數(shù)據倉庫的新一代企業(yè)級數(shù)據庫產品
一個新穎的數(shù)據庫中間件產品
以上是官方說明。其實就是數(shù)據庫的連接池。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 //添加以下內容 <user name="user"> //mycat用戶名 <property name="password">user</property> //mycat密碼 <property name="schemas">mytest</property> //mycat虛擬數(shù)據庫名 <property name="readOnly">true</property> //只讀 </user> <user name="tankzhang"> <property name="password">admin</property> <property name="schemas">mytest</property> </user>
在這里要注意,默認的虛擬數(shù)據名是TESTDB,如果schema.xml里面沒有配置testdb,那就要把testdb改成schema.xml里面有的虛擬數(shù)據名。這里定義的用戶名和密碼,虛擬數(shù)據庫名,并不是在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" />//定義虛擬數(shù)據庫名mytest <dataNode name="my1" dataHost="test1" database="test" /> //真實數(shù)據庫名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ù)據庫的連接方式 <readHost host="hostS1" url="192.168.5.214:3306" user="tank" password="123456" /> //同上 </writeHost> </dataHost> </mycat:schema>
mycat的配置參數(shù),相當?shù)亩?。重點說一下 balance="1"與writeType="0"
a. balance 屬性負載均衡類型,目前的取值有 4 種:
1. balance="0", 不開啟讀寫分離機制,所有讀操作都發(fā)送到當前可用的 writeHost 上。
2. balance="1",全部的 readHost 與 stand by writeHost 參與 select 語句的負載均衡,簡單的說,當雙主雙從模式(M1 ->S1 , M2->S2,并且 M1 與 M2 互為主備),正常情況下, M2,S1,S2 都參與 select 語句的負載均衡。
3. balance="2",所有讀操作都隨機的在 writeHost、 readhost 上分發(fā)。
4. balance="3", 所有讀請求隨機的分發(fā)到 wiriterHost 對應的 readhost 執(zhí)行,writerHost 不負擔讀壓力,注意 balance=3 只在 1.4 及其以后版本有, 1.3 沒有。
b. writeType 屬性
負載均衡類型,目前的取值有 3 種:
1. writeType="0", 所有寫操作發(fā)送到配置的第一個 writeHost,第一個掛了切到還生存的第二個
writeHost,重新啟動后已切換后的為準,切換記錄在配置文件中:dnindex.properties .
2. writeType="1",所有寫操作都隨機的發(fā)送到配置的 writeHost。
3. writeType="2",沒實現(xiàn)。
具體參數(shù):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中配置的真實用戶,能連上真實的數(shù)據庫。注意防火墻。
四,啟動mycat
1,常用參數(shù)
./mycat start 啟動
./mycat stop 停止
./mycat console 前臺運行
./mycat restart 重啟服務
./mycat pause 暫停
./mycat status 查看啟動狀態(tài)
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 | //虛擬數(shù)據庫 +----------+ 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)建時間', -> 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ù)據庫的user表中的name,會發(fā)現(xiàn)讀是從從數(shù)據庫讀取的 +----+-----------+-------------+ | id | name | create_time | +----+-----------+-------------+ | 1 | tankzhang | 0 | +----+-----------+-------------+ 1 row in set (0.01 sec)
六,小結
mycat支持 mysql的分表,分片等等,但是不建議使用。mycat支持的集群不多,如果能配合mha使用就比較牛了。
相關文章
Jaspersoft?Studio添加mysql數(shù)據庫配置步驟
這篇文章主要為大家介紹了Jaspersoft?Studio添加mysql數(shù)據庫配置的步驟過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02在MySQL中用正則表達式替換數(shù)據庫中的內容的方法
在MySQL中用正則表達式替換數(shù)據庫中的內容的方法...2007-03-03MySQL SUM()帶條件的求和方法與多條件的求和方法解讀
這篇文章主要介紹了MySQL SUM()帶條件的求和方法與多條件的求和方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05mysql5.7創(chuàng)建用戶授權刪除用戶撤銷授權
這篇文章主要介紹了mysql5.7創(chuàng)建用戶授權刪除用戶撤銷授權的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02