mycat中介軟體(二)linux版本mycat1.6版本安裝
前言
前面的文章,介紹了關於mycat的相關概念,為了簡要描述,故而去掉了有關mycat的起源等內容。
本節內容則是講述,如何安裝mycat1.6版本。
安裝前環境要求
環境要求如下:
環境 | 版本 |
---|---|
mysql | 5.5版本以上(本文使用5.6.33) |
jdk | 1.7版本以上(本文環境1.8.0_65) |
centOS | 7.2 |
虛擬機器 | VMware12 |
遠端連線 | MobaXterm Personal Edition 9.1 |
mycat | 1.6 |
MyCAT使用Java開發,因為用到了JDK 7的部分功能,所以在使用前請確保安裝了JDK 7.0,並設定了正確的Java環境變數(可在命令列視窗輸入:“java –version”獲知是否安裝成功,以及獲取JDK的版本)。
安裝
1、檢視jdk版本
java -version
2、安裝mysql
2.1、下載mysql並解壓
wget http://ftp.ntu.edu.tw/pub/MySQL/Downloads/MySQL-5.6/mysql-5.6.33.tar.gz
tar -zxvf mysql-5.6.33.tar.gz
2.2 、安裝cmake編譯工具
yum install cmake -y
2.3、安裝依賴
yum install ncurses-devel –y ##用於終端操作的開發包
2.4、新增使用者
useradd -u 8001 -s /sbin/nologin mysql
2.5 、建立資料庫存放目錄
mkdir /data
2.6 、編譯安裝
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE =1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data -DMYSQL_USER=mysql
擴充套件
引數說明:
-DCMAKE_INSTALL_PREFIX #資料檔案存放目錄
-DMYSQL_UNIX_ADDR #sock檔案路徑
-DDEFAULT_CHARSET #預設字符集
-DDEFAULT_COLLATION #預設字元校對
-DWITH_EXTRA_CHARSETS #擴充套件字元支援 預設all
-DWITH_storage_STORAGE_ENGINE #儲存引擎的支援,預設支援MyISAM,MERGE,MEMORY,CVS儲存引擎
-DENABLED_LOCAL_INFILE=1 #啟用載入本地資料
-DMYSQL_DATADIR #資料存放目錄
-DMYSQL_USER #mysql執行使用者擴充套件:
-DWITH_PARTITION_STORAGE_ENGINE=1 #支援分割槽表
-DINSTALL_LIBDIR=dir_name
-DSYSCONFDIR=dir_name #The default my.cnf option file directory
make -j 4 && make install
該編譯過程過長,各位耐心等待些哈
2.7 、授權使用者
chown -R mysql:mysql /usr/local/mysql/
chown -R mysql:mysql /data
chmod 1777 /tmp
2.8 、建立配置檔案
cd /usr/local/mysql/
cp support-files/my-default.cnf /etc/my.cnf
2.9、設定環境變數
echo 'export PATH=/usr/local/mysql/bin:$PATH' >>/etc/profile
source !$
2.10、建立服務啟動指令碼,新增開機啟動
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld ##給指令碼新增執行許可權
修改mysqld的內容,修改這兩個屬性欄位的內容
basedir=/usr/local/mysql MySQL安裝目錄
datadir= /data 資料存放目錄
chkconfig mysqld on 新增開機啟動
檢視是否已經在開機自啟動列表中
chkconfig --list
2.11 、初始化資料庫
chmod +x scripts/mysql_install_db
/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data --user=mysql
2.12 、初始化安全配置
mysql_secure_installation ## 安全初始化配置
沒有啟動mysql然後這樣使用命令,必然會報錯
所以先用ctrl+c終止執行,然後啟動mysql
service mysqld restart
或
service mysqld start
我們檢視mysql是否已經真的啟動了
再次執行上訴的命令
mysql_secure_installation ## 安全初始化配置
其餘情況下,輸入y
2.13 、登入測試
此時此刻,可以確定,mysql安裝成功。
3、安裝mycat
1、下載mycat
為了方便,我已經下載下來。我選擇的版本是1.6版本
2、解壓
tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
cp -r mycat/ /usr/local/
為了以後更好看目錄結構,下tree(可選)
yum -y install tree
3.3、建立使用者並修改配置檔案
為了更好的進入mycat,設定MYCAT_HOME的變數
vim /etc/profile
修改增加下列內容
MYCAT_HOME=/usr/local/Mycat
使得變數生效
source !$
修改server.xml
vim conf/server.xml
開啟實時統計,便於後期安裝mycat-eye的監測
<property name="useSqlStat">1</property>
<!-- 1為開啟實時統計、0為關閉 -->
這四項都去掉註釋,使得生效
<!--預設是65535 64K 用於sql解析時最大文字長度 -->
<property name="maxStringLiteralLength">65535</property>
<property name="sequnceHandlerType">0</property>
<property name="backSocketNoDelay">1</property>
<property name="frontSocketNoDelay">1</property>
最主要的看下面的配置,這是連線mycat的時候的使用者名稱和密碼
mycat的使用者名稱和密碼
使用者名稱 | 密碼 |
---|---|
root | 123456 |
user | user |
修改schema.xml
vim conf/schema.xml
修改的內容是有關於table的地方,主要是將主鍵名改掉
<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />
<!-- global table is auto cloned to all defined data nodes ,so can join
with any table whose sharding node is in the same data node -->
<table name="company" primaryKey="company_id" type="global" dataNode="dn1,dn2,dn3" />
<table name="goods" primaryKey="goods_id" type="global" dataNode="dn1,dn2" />
<!-- random sharding using mod sharind rule -->
<table name="hotnews" primaryKey="hotnews_id" autoIncrement="true" dataNode="dn1,dn2,dn3"
rule="mod-long" />
<!-- <table name="dual" primaryKey="ID" dataNode="dnx,dnoracle2" type="global"
needAddLimit="false"/> <table name="worker" primaryKey="ID" dataNode="jdbc_dn1,jdbc_dn2,jdbc_dn3"
rule="mod-long" /> -->
<table name="employee" primaryKey="employee_id" dataNode="dn1,dn2"
rule="sharding-by-intfile" />
<table name="customer" primaryKey="customer_id" dataNode="dn1,dn2"
rule="sharding-by-murmur">
<childTable name="orders" primaryKey="order_id" joinKey="customer_id"
parentKey="customer_id">
<childTable name="order_items" joinKey="order_id"
parentKey="order_id" />
</childTable>
<childTable name="customer_addr" primaryKey="customer_addr_id" joinKey="customer_id"
parentKey="customer_id" />
</table>
其次,需要修改的還有writeHost部分,去掉非localhost(將其改成127.0.0.1更好)
修改rule.xml
既然修改了表的主鍵名,所應用的規則,也應該一併修改
具體按照實際情況來,不一定非要按照我所寫的。
3.4、建表和庫初始化連線資料
準備3個sql檔案,用於初始化mycat測試。
db1.sql
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50623
Source Host : localhost:3306
Source Database : db1
Target Server Type : MYSQL
Target Server Version : 50623
File Encoding : 65001
Date: 2016-11-09 14:32:13
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `company`
-- ----------------------------
DROP TABLE IF EXISTS `company`;
CREATE TABLE `company` (
`company_id` varchar(64) NOT NULL DEFAULT 'company',
`company_name` varchar(64) DEFAULT NULL,
PRIMARY KEY (`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of company
-- ----------------------------
INSERT INTO `company` VALUES ('1', 'hp');
-- ----------------------------
-- Table structure for `customer`
-- ----------------------------
DROP TABLE IF EXISTS `customer`;
CREATE TABLE `customer` (
`customer_id` varchar(64) NOT NULL,
`customer_name` varchar(64) DEFAULT NULL,
PRIMARY KEY (`customer_id`),
KEY `customer_id` (`customer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of customer
-- ----------------------------
-- ----------------------------
-- Table structure for `customer_addr`
-- ----------------------------
DROP TABLE IF EXISTS `customer_addr`;
CREATE TABLE `customer_addr` (
`customer_addr` varchar(64) NOT NULL,
`customer_id` varchar(64) DEFAULT NULL,
PRIMARY KEY (`customer_addr`),
KEY `custom` (`customer_id`),
CONSTRAINT `custom` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of customer_addr
-- ----------------------------
-- ----------------------------
-- Table structure for `employee`
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`employee_id` varchar(64) NOT NULL,
`employee_name` varchar(64) DEFAULT NULL,
`employee_address` varchar(64) DEFAULT NULL,
PRIMARY KEY (`employee_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of employee
-- ----------------------------
-- ----------------------------
-- Table structure for `goods`
-- ----------------------------
DROP TABLE IF EXISTS `goods`;
CREATE TABLE `goods` (
`good_id` varchar(64) NOT NULL,
`good_name` varchar(64) DEFAULT NULL,
`good_price` varchar(64) DEFAULT NULL,
PRIMARY KEY (`good_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of goods
-- ----------------------------
-- ----------------------------
-- Table structure for `hotnews`
-- ----------------------------
DROP TABLE IF EXISTS `hotnews`;
CREATE TABLE `hotnews` (
`hotnews_id` varchar(64) NOT NULL,
`hotnews_an` varchar(64) DEFAULT NULL,
PRIMARY KEY (`hotnews_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of hotnews
-- ----------------------------
INSERT INTO `hotnews` VALUES ('795528865165479936', 'hp');
-- ----------------------------
-- Table structure for `orders`
-- ----------------------------
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`orders_id` varchar(64) NOT NULL,
`customer_id` varchar(64) NOT NULL,
PRIMARY KEY (`orders_id`),
KEY `customer_id` (`customer_id`),
CONSTRAINT `customer_id` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of orders
-- ----------------------------
-- ----------------------------
-- Table structure for `order_items`
-- ----------------------------
DROP TABLE IF EXISTS `order_items`;
CREATE TABLE `order_items` (
`order_items_id` varchar(64) NOT NULL,
`order_id` varchar(64) DEFAULT NULL,
PRIMARY KEY (`order_items_id`),
KEY `order` (`order_id`),
CONSTRAINT `order` FOREIGN KEY (`order_id`) REFERENCES `orders` (`orders_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of order_items
-- ----------------------------
-- ----------------------------
-- Table structure for `travelrecord`
-- ----------------------------
DROP TABLE IF EXISTS `travelrecord`;
CREATE TABLE `travelrecord` (
`travelrecord_id` varchar(255) NOT NULL,
`travelrecord_name` varchar(64) DEFAULT NULL,
PRIMARY KEY (`travelrecord_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of travelrecord
-- ----------------------------
db2.sql
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50623
Source Host : localhost:3306
Source Database : db2
Target Server Type : MYSQL
Target Server Version : 50623
File Encoding : 65001
Date: 2016-11-09 14:32:27
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `company`
-- ----------------------------
DROP TABLE IF EXISTS `company`;
CREATE TABLE `company` (
`company_id` varchar(64) NOT NULL DEFAULT 'company',
`company_name` varchar(64) DEFAULT NULL,
PRIMARY KEY (`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of company
-- ----------------------------
INSERT INTO `company` VALUES ('1', 'hp');
-- ----------------------------
-- Table structure for `customer`
-- ----------------------------
DROP TABLE IF EXISTS `customer`;
CREATE TABLE `customer` (
`customer_id` varchar(64) NOT NULL,
`customer_name` varchar(64) DEFAULT NULL,
PRIMARY KEY (`customer_id`),
KEY `customer_id` (`customer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of customer
-- ----------------------------
-- ----------------------------
-- Table structure for `customer_addr`
-- ----------------------------
DROP TABLE IF EXISTS `customer_addr`;
CREATE TABLE `customer_addr` (
`customer_addr` varchar(64) NOT NULL,
`customer_id` varchar(64) DEFAULT NULL,
PRIMARY KEY (`customer_addr`),
KEY `custom` (`customer_id`),
CONSTRAINT `custom` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of customer_addr
-- ----------------------------
-- ----------------------------
-- Table structure for `employee`
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`employee_id` varchar(64) NOT NULL,
`employee_name` varchar(64) DEFAULT NULL,
`employee_address` varchar(64) DEFAULT NULL,
PRIMARY KEY (`employee_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of employee
-- ----------------------------
-- ----------------------------
-- Table structure for `goods`
-- ----------------------------
DROP TABLE IF EXISTS `goods`;
CREATE TABLE `goods` (
`good_id` varchar(64) NOT NULL,
`good_name` varchar(64) DEFAULT NULL,
`good_price` varchar(64) DEFAULT NULL,
PRIMARY KEY (`good_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of goods
-- ----------------------------
-- ----------------------------
-- Table structure for `hotnews`
-- ----------------------------
DROP TABLE IF EXISTS `hotnews`;
CREATE TABLE `hotnews` (
`hotnews_id` varchar(64) NOT NULL,
`hotnews_an` varchar(64) DEFAULT NULL,
PRIMARY KEY (`hotnews_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of hotnews
-- ----------------------------
INSERT INTO `hotnews` VALUES ('1', 'hp');
-- ----------------------------
-- Table structure for `orders`
-- ----------------------------
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`orders_id` varchar(64) NOT NULL,
`customer_id` varchar(64) NOT NULL,
PRIMARY KEY (`orders_id`),
KEY `customer_id` (`customer_id`),
CONSTRAINT `customer_id` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of orders
-- ----------------------------
-- ----------------------------
-- Table structure for `order_items`
-- ----------------------------
DROP TABLE IF EXISTS `order_items`;
CREATE TABLE `order_items` (
`order_items_id` varchar(64) NOT NULL,
`order_id` varchar(64) DEFAULT NULL,
PRIMARY KEY (`order_items_id`),
KEY `order` (`order_id`),
CONSTRAINT `order` FOREIGN KEY (`order_id`) REFERENCES `orders` (`orders_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of order_items
-- ----------------------------
-- ----------------------------
-- Table structure for `travelrecord`
-- ----------------------------
DROP TABLE IF EXISTS `travelrecord`;
CREATE TABLE `travelrecord` (
`travelrecord_id` varchar(255) NOT NULL,
`travelrecord_name` varchar(64) DEFAULT NULL,
PRIMARY KEY (`travelrecord_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of travelrecord
-- ----------------------------
db3.sql
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50623
Source Host : localhost:3306
Source Database : db3
Target Server Type : MYSQL
Target Server Version : 50623
File Encoding : 65001
Date: 2016-11-09 14:32:36
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `company`
-- ----------------------------
DROP TABLE IF EXISTS `company`;
CREATE TABLE `company` (
`company_id` varchar(64) NOT NULL DEFAULT 'company',
`company_name` varchar(64) DEFAULT NULL,
PRIMARY KEY (`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of company
-- ----------------------------
INSERT INTO `company` VALUES ('1', 'hp');
-- ----------------------------
-- Table structure for `hotnews`
-- ----------------------------
DROP TABLE IF EXISTS `hotnews`;
CREATE TABLE `hotnews` (
`hotnews_id` varchar(64) NOT NULL,
`hotnews_an` varchar(64) DEFAULT NULL,
PRIMARY KEY (`hotnews_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of hotnews
-- ----------------------------
-- ----------------------------
-- Table structure for `travelrecord`
-- ----------------------------
DROP TABLE IF EXISTS `travelrecord`;
CREATE TABLE `travelrecord` (
`travelrecord_id` varchar(255) NOT NULL,
`travelrecord_name` varchar(64) DEFAULT NULL,
PRIMARY KEY (`travelrecord_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of travelrecord
-- ----------------------------
3.5 、建表和庫
將sql檔案均放在了root/init_sql/下
mkdir /usr/local/init_sql
mv /root/init_sql/* /usr/local/init_sql/
mysql -u root -p
建立db1,並執行sql檔案
create schema db1;
use db1
source /usr/local/init_sql/db1.sql
create schema db2;
use db2
source /usr/local/init_sql/db2.sql
create schema db3;
use db3
source /usr/local/init_sql/db3.sql