1. 程式人生 > >centos7 mycat安裝 ,分庫分表測試

centos7 mycat安裝 ,分庫分表測試

安裝mycat

  1. 安裝JDK;

下載jdk 解壓 放到/opt/jdk目錄下

配置環境變數

export JAVA_HOME=/opt/jdk
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JRE_HOME=$JAVA_HOME/jre
  1. 安裝mysql;
wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
rpm -Uvh mysql57-community-release-el7-10.noarch.rpm
yum install  -y  mysql-community-server
  1. 啟動mysql 並且修改root密碼;
systemctl status mysqld.service
#獲得初始root 密碼
grep 'temporary password' /var/log/mysqld.log
#修改root 賬戶密碼
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
#授權其他機器登入
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH  PRIVILEGES;
  1. 安裝mycat;
cd /opt
#下載
wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
#解壓
tar -xvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
# 啟動mycat
cd mycat/bin
./mycat start

測試分庫分表

  1. 建立資料庫
create database db01; 
use db01;
 CREATE TABLE user (  
    id INT NOT NULL AUTO_INCREMENT,  
    name varchar(50) NOT NULL default '',  
    indate DATETIME NOT NULL ,  
    PRIMARY KEY (id)  
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;  


create database db02;  
use db02;
 CREATE TABLE goods(  
    id INT NOT NULL AUTO_INCREMENT,  
    value INT NOT NULL default 0,  
    indate DATETIME NOT NULL ,  
    PRIMARY KEY (id)  
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;

create database db03;  
use db03;
CREATE TABLE goods(  
    id INT NOT NULL AUTO_INCREMENT,  
    value INT NOT NULL default 0,  
    indate DATETIME NOT NULL ,  
    PRIMARY KEY (id)  
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8; 

2.配置mycat
conf/server.xml 中定義使用者以及系統相關變數,如埠等.
conf/schema.xml 中定義邏輯庫,表、分片節點等內容.
conf/rule.xml 中定義分片規則.

#server.xml
#新增使用者
<user name="mycat">
            <property name="password">mycat</property>
            <property name="schemas">TESTDB</property>
</user>
		#schema.xml 
        <!-- 設定表的儲存方式.schema name="TESTDB" 與 server.xml中的 TESTDB 設定一致  -->  
		<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
                <table name="user" primaryKey="id"  dataNode="node_db01" />
                <table name="goods" primaryKey="id" dataNode="node_db02,node_db03" rule="rule1" />
        </schema>
        <!--設定DataNode-->
        <dataNode name="node_db01" dataHost="dataHost01" database="db01" />
        <dataNode name="node_db02" dataHost="dataHost01" database="db02" />
        <dataNode name="node_db03" dataHost="dataHost01" database="db03" />
        <!--設定DataHost-->
        <dataHost name="dataHost01" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
            <heartbeat>select user()</heartbeat>
            <writeHost host="server1" url="127.0.0.1:3306" user="root" password="123456"/>
        </dataHost>
#rule.xml
	<!--修改rule-->
    <tableRule name="rule1">
        <rule>
            <columns>id</columns>
            <algorithm>mod-long</algorithm>
        </rule>
    </tableRule>
     <!-- 分片規則為模2運算 -->  
    <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
        <!-- how many data nodes -->
        <property name="count">2</property>
    </function>

3.重啟mycat

./mycat restart

4.測試插入資料

insert into user(name,indate) values('hello',now());
insert into user(name,indate) values('world',now());
insert into goods(id,value,indate) values(1,100,now());
insert into goods(id,value,indate) values(2,100,now());

5.連線資料庫檢視結果
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
分庫分表成功