mysql mycat 中間件安裝與使用

一,什麼是mycatjava

一個完全開源的,面向企業應用開發的大數據庫集羣mysql

支持事務、ACID、能夠替代MySQL的增強版數據庫linux

一個能夠視爲MySQL集羣的企業級數據庫,用來替代昂貴的Oracle集羣sql

一個融合內存緩存技術、NoSQL技術、HDFS大數據的新型SQL Server數據庫

結合傳統數據庫和新型分佈式數據倉庫的新一代企業級數據庫產品vim

一個新穎的數據庫中間件產品緩存

以上是官方說明。其實就是數據庫的鏈接池。mysql proxy也是一種鏈接池,可是效率很低。bash

二,mycat 安裝服務器

1,下載地址mycat負載均衡

http://dl.mycat.io/

2,安裝mycat

?
1
# tar zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/

三,配置mycat

1,配置server.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
# 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 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,添加真實用戶

?
1
2
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

?
1
2
3
4
5
6
7
8
# ./mycat start
Starting Mycat-server...
 
# netstat -tpnl |grep 8066
tcp 0 0 :::8066 :::* LISTEN 31728 /java
 
# ./mycat status
Mycat-server is running (31726).

五,測試讀寫分離

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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了。

相關文章
相關標籤/搜索