mysql mycat 中間件安裝與使用
一,什么是mycat
一個徹底開源的,面向企業(yè)應(yīng)用開發(fā)的大數(shù)據(jù)庫集群
支持事務(wù)、ACID、可以替代MySQL的加強版數(shù)據(jù)庫
一個可以視為MySQL集群的企業(yè)級數(shù)據(jù)庫,用來替代昂貴的Oracle集群
一個融合內(nèi)存緩存技術(shù)、NoSQL技術(shù)、HDFS大數(shù)據(jù)的新型SQL Server
結(jié)合傳統(tǒng)數(shù)據(jù)庫和新型分布式數(shù)據(jù)倉庫的新一代企業(yè)級數(shù)據(jù)庫產(chǎn)品
一個新穎的數(shù)據(jù)庫中間件產(chǎn)品
以上是官方說明。其實就是數(shù)據(jù)庫的連接池。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ù)庫名 <property name="readOnly">true</property> //只讀 </user> <user name="tankzhang"> <property name="password">admin</property> <property name="schemas">mytest</property> </user>
在這里要注意,默認的虛擬數(shù)據(jù)名是TESTDB,如果schema.xml里面沒有配置testdb,那就要把testdb改成schema.xml里面有的虛擬數(shù)據(jù)名。這里定義的用戶名和密碼,虛擬數(shù)據(jù)庫名,并不是在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ù)據(jù)庫名mytest <dataNode name="my1" dataHost="test1" database="test" /> //真實數(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ù)據(jù)庫的連接方式 <readHost host="hostS1" url="192.168.5.214:3306" user="tank" password="123456" /> //同上 </writeHost> </dataHost> </mycat:schema>
mycat的配置參數(shù),相當(dāng)?shù)亩?。重點說一下 balance="1"與writeType="0"
a. balance 屬性負載均衡類型,目前的取值有 4 種:
1. balance="0", 不開啟讀寫分離機制,所有讀操作都發(fā)送到當(dāng)前可用的 writeHost 上。
2. balance="1",全部的 readHost 與 stand by writeHost 參與 select 語句的負載均衡,簡單的說,當(dāng)雙主雙從模式(M1 ->S1 , M2->S2,并且 M1 與 M2 互為主備),正常情況下, M2,S1,S2 都參與 select 語句的負載均衡。
3. balance="2",所有讀操作都隨機的在 writeHost、 readhost 上分發(fā)。
4. balance="3", 所有讀請求隨機的分發(fā)到 wiriterHost 對應(yīng)的 readhost 執(zhí)行,writerHost 不負擔(dān)讀壓力,注意 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,配置主從服務(wù)器,就不在這兒說了,博客中有
4,添加真實用戶
grant all privileges on test.* to tank@"192.168.%" identified by '123456'; flush privileges
在213,214二臺機器上添加用戶。
5,測試真實用戶連接,確保schema.xml中配置的真實用戶,能連上真實的數(shù)據(jù)庫。注意防火墻。
四,啟動mycat
1,常用參數(shù)
./mycat start 啟動
./mycat stop 停止
./mycat console 前臺運行
./mycat restart 重啟服務(wù)
./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ù)據(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)建時間',
-> 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,會發(fā)現(xiàn)讀是從從數(shù)據(jù)庫讀取的
+----+-----------+-------------+
| id | name | create_time |
+----+-----------+-------------+
| 1 | tankzhang | 0 |
+----+-----------+-------------+
1 row in set (0.01 sec)
六,小結(jié)
mycat支持 mysql的分表,分片等等,但是不建議使用。mycat支持的集群不多,如果能配合mha使用就比較牛了。
相關(guān)文章
MySql使用mysqldump 導(dǎo)入與導(dǎo)出方法總結(jié)
這篇文章主要介紹了MySql使用mysqldump 導(dǎo)入與導(dǎo)出方法總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
坑人的Mysql5.7問題(默認不支持Group By語句)
這篇文章主要介紹了坑人的Mysql5.7問題(默認不支持Group By語句),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
Mysql中常用函數(shù)之分組,連接查詢功能實現(xiàn)
在MySQL中,函數(shù)可以進行各種數(shù)據(jù)操作,如字符處理、數(shù)學(xué)計算和日期格式化等,單行函數(shù)處理單條數(shù)據(jù)記錄,而分組函數(shù)則處理多條數(shù)據(jù)記錄,本文給大家介紹Mysql中常用函數(shù)之分組,連接查詢功能實現(xiàn),感興趣的朋友一起看看吧2024-10-10
mysql 報錯 incompatible with sql_mode=only
這篇文章主要為大家介紹了mysql 報錯 incompatible with sql_mode=only_full_group_by解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
MYSQL數(shù)據(jù)庫如何設(shè)置主從同步
大家好,本篇文章主要講的是MYSQL數(shù)據(jù)庫如何設(shè)置主從同步,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
MySQL創(chuàng)建用戶以及用戶權(quán)限詳細圖文教程
在MySQL中可以通過創(chuàng)建用戶來管理數(shù)據(jù)庫的訪問權(quán)限,下面這篇文章主要給大家介紹了關(guān)于MySQL創(chuàng)建用戶以及用戶權(quán)限的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2024-06-06

