1. 程式人生 > >Mycat資料庫中介軟體的安裝與使用

Mycat資料庫中介軟體的安裝與使用

MyCAT是mysql中介軟體,前身是阿里大名鼎鼎的Cobar,Cobar在開源了一段時間後,不了了之。於是MyCAT扛起了這面大旗,在大資料時代,其重要性愈發彰顯。這篇文章主要是MyCAT的入門部署。

一、安裝java

因Mycat是用java開發的,所以需要在實驗環境下安裝java,官方建議jdk1.7及以上版本

Java Oracle官方下載地址為:

解壓jdk-7u79-linux-x64.tar.gz檔案,並配置java的環境變數

# tar xvf jdk-7u79-linux-x64.tar.gz

# mv jdk1.7.0_79/ /usr/local/

編輯/etc/profile檔案

# vim /etc/profile  新增如下內容

JAVA_HOME=/usr/local/jdk1.7.0_79
JAVA_BIN=/usr/local/jdk1.7.0_79/bin
PATH=$JAVA_BIN:$PATH
CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH

# source /etc/profile --使/etc/profile檔案生效

# java -version

java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 
1.7.0_79-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

二、安裝MySQL

MySQL官方下載地址如下:

裡面有各種版本,包括RPM,二進位制,原始碼包。

為了方便起見,我這裡選擇是RPM包

# yum install -y mysql-community-server-5.6.26-2.el5.x86_64.rpm

啟動MySQL

# /etc/init.d/mysqld start

Starting mysqld (via systemctl):                           [  OK  ]

為root賬戶建立密碼

# mysqladmin -u root password "123456"

建議為123456,後面MyCAT配置檔案schema.xml中會用到

三、安裝MyCAT

建立MyCAT使用者並設定密碼

# useradd mycat

# passwd mycat

解壓MyCAT檔案

# tar xvf Mycat-server-1.5-alpha-20151221110028-linux.tar.gz

# mv mycat/ /usr/local/

設定mycat目錄的屬主和屬組

# chown -R mycat.mycat /usr/local/mycat/

四、測試MyCAT

首先在MySQL上建立三個資料庫:db1,db2,db3。

mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> create database db2;
Query OK, 1 row affected (0.00 sec)

mysql> create database db3;
Query OK, 1 row affected (0.00 sec) 

啟動mycat服務

# cd /usr/local/mycat/bin/

# ./mycat start

Starting Mycat-server...

檢視mycat服務是否啟動

# ps -ef |grep mycat

root       9640   7257  0 22:47 pts/3    00:00:00 grep --color=auto mycat

並沒有啟動

檢視日誌

# cd /usr/local/mycat/logs/

# cat wrapper.log

STATUS | wrapper  | 2016/01/07 22:44:23 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2016/01/07 22:44:23 | Launching a JVM...
ERROR  | wrapper  | 2016/01/07 22:44:25 | JVM exited while loading the application.
INFO   | jvm 1    | 2016/01/07 22:44:25 | Error: Exception thrown by the agent : java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: MySQL-Server1: MySQL-Server1: Name or service not known
STATUS | wrapper  | 2016/01/07 22:44:29 | Launching a JVM...
ERROR  | wrapper  | 2016/01/07 22:44:29 | JVM exited while loading the application.
INFO   | jvm 2    | 2016/01/07 22:44:29 | Error: Exception thrown by the agent : java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: MySQL-Server1: MySQL-Server1: Name or service not known
STATUS | wrapper  | 2016/01/07 22:44:34 | Launching a JVM...
ERROR  | wrapper  | 2016/01/07 22:44:34 | JVM exited while loading the application.
INFO   | jvm 3    | 2016/01/07 22:44:34 | Error: Exception thrown by the agent : java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: MySQL-Server1: MySQL-Server1: Name or service not known
STATUS | wrapper  | 2016/01/07 22:44:38 | Launching a JVM...
ERROR  | wrapper  | 2016/01/07 22:44:38 | JVM exited while loading the application.
INFO   | jvm 4    | 2016/01/07 22:44:38 | Error: Exception thrown by the agent : java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: MySQL-Server1: MySQL-Server1: Name or service not known
STATUS | wrapper  | 2016/01/07 22:44:42 | Launching a JVM...
ERROR  | wrapper  | 2016/01/07 22:44:43 | JVM exited while loading the application.
INFO   | jvm 5    | 2016/01/07 22:44:43 | Error: Exception thrown by the agent : java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: MySQL-Server1: MySQL-Server1: Name or service not known
FATAL  | wrapper  | 2016/01/07 22:44:43 | There were 5 failed launches in a row, each lasting less than 300 seconds.  Giving up.
FATAL  | wrapper  | 2016/01/07 22:44:43 |   There may be a configuration problem: please check the logs.
STATUS | wrapper  | 2016/01/07 22:44:43 | <-- Wrapper Stopped 

根據報錯資訊,懷疑主機名沒有繫結

修改hosts檔案,繫結主機名

# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.244.144 MySQL-Server1

重新啟動mycat服務

# cd /usr/local/mycat/bin/

# ./mycat start

這次mycat正常啟動

# ps -ef |grep mycat

root      10725      1  0 22:54 ?        00:00:00 /usr/local/mycat/bin/./wrapper-linux-x86-64 /usr/local/mycat/conf/wrapper.conf wrapper.syslog.ident=mycat wrapper.pidfile=/usr/local/mycat/logs/mycat.pid wrapper.daemonize=TRUE wrapper.lockfile=/var/lock/subsys/mycat
root      10881   7257  0 22:55 pts/3    00:00:00 grep --color=auto mycat

下面以Travelrecord為例,來進行插入,查詢,路由分析等基本操作。

首先利用mycat連線mysql資料庫

# mysql -utest -ptest -h127.0.0.1 -P8066 -DTESTDB

其中8066是mycat的監聽埠,類似於mysql的3306埠,其中-u,-p,-h分別是使用者名稱,密碼和主機,-D是連線的邏輯庫。

至於為什麼是這些,這個跟配置檔案有關。

紅色部分表明連線的是mycat。

建立Travelrecord表

create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);

插入資料

mysql> insert into travelrecord(id,user_id,traveldate,fee,days)  values(1,'Victor',20160101,100,10);
Query OK, 1 row affected (0.00 sec)

mysql> insert into travelrecord(id,user_id,traveldate,fee,days)  values(5000001,'Job',20160102,100,10);
Query OK, 1 row affected (0.00 sec)

mysql> insert into travelrecord(id,user_id,traveldate,fee,days)  values(10000001,'Slow',20160103,100,10);
Query OK, 1 row affected (0.00 sec)

至於ID為什麼取三個值,這個與conf目錄下autopartition-long.txt的定義有關,這個檔案主要定義auto-sharding-long的規則。

# range start-end ,data node index
# K=1000,M=10000.
0-500M=0
500M-1000M=1
1000M-1500M=2

我主要是測試在id取不同區間的值時,分片的效果。

下面來看看分片的效果

mysql> select * from db1.travelrecord;
+----+---------+------------+------+------+
| id | user_id | traveldate | fee  | days |
+----+---------+------------+------+------+
|  1 | Victor  | 2016-01-01 |  100 |   10 |
+----+---------+------------+------+------+
1 row in set (0.00 sec)

mysql> select * from db2.travelrecord;
+---------+---------+------------+------+------+
| id      | user_id | traveldate | fee  | days |
+---------+---------+------------+------+------+
| 5000001 | Job     | 2016-01-02 |  100 |   10 |
+---------+---------+------------+------+------+
1 row in set (0.00 sec)

mysql> select * from db3.travelrecord;
+----------+---------+------------+------+------+
| id       | user_id | traveldate | fee  | days |
+----------+---------+------------+------+------+
| 10000001 | Slow    | 2016-01-03 |  100 |   10 |
+----------+---------+------------+------+------+
1 row in set (0.00 sec)

果然是分配到三個不同的節點上了。

如果想看MyCAT具體會將資料分配到哪個節點上,可通過路由分析。

語法其實蠻簡單,就是SQL語句前加上explain語句。

下面根據explain命令檢視create語句和insert語句具體會分配到哪些Datanode上。

mysql> explain create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
+-----------+-----------------------------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL                                                                                                                   |
+-----------+-----------------------------------------------------------------------------------------------------------------------+
| dn1       | create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int) |
| dn2       | create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int) |
| dn3       | create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int) |
+-----------+-----------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

mysql>  explain insert into travelrecord(id,user_id,traveldate,fee,days)  values(1,'Victor',20160101,100,10);
+-----------+----------------------------------------------------------------------------------------------+
| DATA_NODE | SQL                                                                                          |
+-----------+----------------------------------------------------------------------------------------------+
| dn1       | insert into travelrecord(id,user_id,traveldate,fee,days)  values(1,'Victor',20160101,100,10) |
+-----------+----------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

mysql> explain insert into travelrecord(id,user_id,traveldate,fee,days)  values(5000001,'Job',20160102,100,10);
+-----------+-------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL                                                                                             |
+-----------+-------------------------------------------------------------------------------------------------+
| dn2       | insert into travelrecord(id,user_id,traveldate,fee,days)  values(5000001,'Job',20160102,100,10) |
+-----------+-------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> explain insert into travelrecord(id,user_id,traveldate,fee,days)  values(10000001,'Slow',20160103,100,10);
+-----------+---------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL                                                                                               |
+-----------+---------------------------------------------------------------------------------------------------+
| dn3       | insert into travelrecord(id,user_id,traveldate,fee,days)  values(10000001,'Slow',20160103,100,10) |
+-----------+---------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) 

總結:

關於MyCAT的配置其實是蠻簡單的,最主要的是熟悉各配置檔案的規則。以上使用者名稱,密碼,如何分庫,都是在配置檔案中定義的,後續,有時間再一一詳表。

關於配置檔案,conf目錄下主要以下三個需要熟悉。

server.xml是Mycat伺服器引數調整和使用者授權的配置檔案

schema.xml是邏輯庫定義和表以及分片定義的配置檔案

rule.xml是分片規則的配置檔案