CentOS7.5下安裝Mycat連接MySQL8.0
Posted on 2018-11-08 11:07 許愛琪 閱讀(645) 評論(2) 編輯 收藏MyCat詳細介紹,請參考https://www.biaodianfu.com/mycat.html
9066管理端口,請參考https://www.cnblogs.com/parryyang/p/5606071.html
1.創建mycat用戶
1
2
|
useradd mycat
passwd mycat
|
2.下載安裝包、解壓並賦權
1
2
3
|
wget http:
//dl.mycat.io/1.6.6.1/Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz
tar -xzvf Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz -C /usr/local
chown -R mycat:mycat /usr/local/mycat
|
3.配置環境變量
1
2
3
4
5
6
7
8
9
10
11
|
vi /etc/profile
增加
## jdk env
JAVA_HOME=/usr/java/jdk1.8.0_192
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH
## mycat env
export MYCAT_HOME=/usr/local/mycat
export PATH=$PATH:$MYCAT_HOME/bin
source /etc/profile
|
4.配置server.xml
vi /usr/local/mycat/conf/server.xml
5.配置schema.xml
vi /usr/local/mycat/conf/schema.xml
6.啟動Mycat
1
|
mycat start
|
7.查看mycat啟動日志
[root@mycat-server2 data]# mycat console
Running Mycat-server...
Mycat-server is already running.
8.連接MyCat
連接管理端口:
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
[root@mycat-server2 data]# mysql -h192.168.0.252 -P9066 -utest -ptest
mysql: [Warning] Using a password
on
the command line
interface
can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id
is
21
Server version: 5.6.29-mycat-1.6.6.1-release-20181031195535 MyCat Server (monitor)
Copyright (c) 2000, 2018, 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 @@help;
+--------------------------------------------------------------+--------------------------------------------+
| STATEMENT | DESCRIPTION |
+--------------------------------------------------------------+--------------------------------------------+
| show @@time.current | Report current timestamp |
| show @@time.startup | Report startup timestamp |
| show @@version | Report Mycat Server version |
| show @@server | Report server status |
| show @@threadpool | Report threadPool status |
| show @@database | Report databases |
| show @@datanode | Report dataNodes |
| show @@datanode
where
schema = ? | Report dataNodes |
| show @@datasource | Report dataSources |
| show @@datasource
where
dataNode = ? | Report dataSources |
| show @@datasource.synstatus | Report datasource data synchronous |
| show @@datasource.syndetail
where
name=? | Report datasource data synchronous detail |
| show @@datasource.cluster | Report datasource galary cluster variables |
| show @@processor | Report processor status |
| show @@command | Report commands status |
| show @@connection | Report connection status |
| show @@cache | Report system cache usage |
| show @@backend | Report backend connection status | ##顯示后端物理庫連接信息,包括當前連接數,端口等信息
| show @@session | Report front session details |
| show @@connection.sql | Report connection sql | ## 顯示當前前端客戶端連接情況
| show @@sql.execute | Report execute status |
| show @@sql.detail
where
id = ? | Report execute detail status |
| show @@sql | Report SQL list |
| show @@sql.high | Report Hight Frequency SQL |
| show @@sql.slow | Report slow SQL |
| show @@sql.resultset | Report BIG RESULTSET SQL |
| show @@sql.sum | Report User RW Stat |
| show @@sql.sum.user | Report User RW Stat |
| show @@sql.sum.table | Report Table RW Stat |
| show @@parser | Report parser status |
| show @@router | Report router status |
| show @@heartbeat | Report heartbeat status |
| show @@heartbeat.detail
where
name=? | Report heartbeat current detail |
| show @@slow
where
schema = ? | Report schema slow sql |
| show @@slow
where
datanode = ? | Report datanode slow sql |
| show @@sysparam | Report system param |
| show @@syslog limit=? | Report system mycat.log |
| show @@white | show mycat white host |
| show @@white.
set
=?,? |
set
mycat white host,[ip,user] |
| show @@directmemory=1 or 2 | show mycat direct memory usage |
| show @@check_global -SCHEMA= ? -TABLE=? -retry=? -interval=? | check mycat global table consistency |
|
switch
@@datasource name:index | Switch dataSource |
| kill @@connection id1,id2,... | Kill the specified connections |
| stop @@heartbeat name:time | Pause dataNode heartbeat |
| reload @@config | Reload basic config
from
file |
| reload @@config_all | Reload all config
from
file |
| reload @@route | Reload route config
from
file |
| reload @@user | Reload user config
from
file |
| reload @@sqlslow= | Set Slow SQL Time(ms) |
| reload @@user_stat | Reset show @@sql @@sql.sum @@sql.slow |
| rollback @@config | Rollback all config
from
memory |
| rollback @@route | Rollback route config
from
memory |
| rollback @@user | Rollback user config
from
memory |
| reload @@sqlstat=open | Open real-time sql stat analyzer |
| reload @@sqlstat=close | Close real-time sql stat analyzer |
| offline | Change MyCat status to OFF |
| online | Change MyCat status to ON |
| clear @@slow
where
schema = ? | Clear slow sql
by
schema |
| clear @@slow
where
datanode = ? | Clear slow sql
by
datanode |
+--------------------------------------------------------------+--------------------------------------------+
59 rows
in
set
(0.01 sec)
# 查看所有的邏輯數據庫:
mysql> show @@database;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
1 row
in
set
(0.00 sec)
# 查看所有的數據節點:
mysql> show @@datanode;
Empty
set
(0.00 sec)
mysql> show @@datasource;
+----------+----------+-------+---------------+------+------+--------+------+------+---------+-----------+------------+
| DATANODE | NAME | TYPE | HOST | PORT | W/R | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
+----------+----------+-------+---------------+------+------+--------+------+------+---------+-----------+------------+
| dn1 | mysql_b1 | mysql | 192.168.0.242 | 3306 | W | 0 | 10 | 1000 | 6215 | 0 | 2 |
| dn1 | mysql_b2 | mysql | 192.168.0.240 | 3306 | R | 0 | 9 | 1000 | 6299 | 84 | 0 |
+----------+----------+-------+---------------+------+------+--------+------+------+---------+-----------+------------+
2 rows
in
set
(0.00 sec)
mysql> quit
Bye
|
連接數據操作端口:
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
|
[root@mycat-server2 data]# mysql -h192.168.0.252 -P8066 -utest -ptest
mysql: [Warning] Using a password
on
the command line
interface
can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id
is
22
Server version: 5.6.29-mycat-1.6.6.1-release-20181031195535 MyCat Server (OpenCloudDB)
Copyright (c) 2000, 2018, 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 |
+----------+
| TESTDB |
+----------+
1 row
in
set
(0.00 sec)
# 使用指定的邏輯數據庫:
mysql> use TESTDB;
Reading table information
for
completion of table and column names
You can turn off
this
feature to
get
a quicker startup with -A
Database changed
# 查看所有的數據表:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| member |
| t01 |
| t02 |
| t03 |
+----------------+
4 rows
in
set
(0.03 sec)
|
參數說明:
-h 后面是主機,即當前 mycat 安裝的主機地址,本地可用 127.0.0.1 遠程需要遠程 ip;
-u Mycat server.xml 中配置的邏輯庫用戶;
-p Mycat server.xml 中配置的邏輯庫密碼;
-P 后面是端口 默認 9066,注意 P 是大寫;
-d Mycat server.xml 中配置的邏輯庫。
9.MyCat操作命令:
啟動MyCat: mycat start;
停止MyCat: mycat stop;
前台運行: mycat console;
重啟服務: mycat restart;
暫停: mycat pause;
查看啟動狀態: mycat status;
10.修改log4j的配置
為了讓信息啟動時更好定位,需要將conf/log4j2.xml的文件修改為debug級別。
附:其他問題
MySQL8.0的客戶端連不上Mycat?
解決方案:使用mysql5的客戶端可以正常訪問,待詳細分析
Feedback
## sudo vim /usr/lib/systemd/system/mycat.service
## 注意: ExecStart/ExecStop 取值兩側不要用引號
[Unit]
Description=MyCAT Service
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/usr/local/mycat/bin/mycat start
ExecStop=/usr/local/mycat/bin/mycat stop
[Install]
WantedBy=multi-user.target
設置開機自動啟動、並立即啟動:
sudo systemctl enable mycat # 設置為開機自動啟動
sudo systemctl start mycat # 立即啟動
# sudo systemctl stop mycat # 立即停止
# sudo systemctl disable mycat # 取消開機自動啟動
CentOS7.5 搭建MyCat1.6.6
1、環境及版本
操作系統: CentOS 7.5
MySQL:5.7.23
jdk:1.8.0_191
MyCat:1.6.6.1
1
2
3
|
cat /etc/centos-release
mysql -V
java -version
|
2、安裝包准備
下載網站:http://dl.mycat.io/1.6.6/
【2】上傳至linux服務器:
1
2
3
|
mkdir /home/rhr/soft/mycat
cd /home/rhr/soft/mycat
rz
|
3、開始安裝
【1】解壓
1
|
tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
|
【2】安裝tree(可選,使目錄結構更好看)
1
|
yum -y install tree
|
查看tree:
4、創建用戶並修改配置文件
【1】為了更好的進入mycat,設置MYCAT_HOME的變量
1
|
vim /etc/profile
|
添加綠色部分:
export JAVA_HOME=/home/rhr/soft/jdk/jdk1.8.0_191
export MYCAT_HOME=/home/rhr/soft/mycat/mycat
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:/usr/local/mysql/bin:$MYCAT_HOME/bin:$PATH
使得變量生效
1
|
source /etc/profile
|
檢查
【2】配置mycat文件
(1)修改server.xml
1
2
3
|
cd home/rhr/soft/mycat/mycat/conf
cp server.xml server_bak.xml
vim server.xml
|
修改前:
修改后:
(2)修改schema.xml
1) 修改schema 標簽
修改前:
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100"> <!-- auto sharding by id (long) --> <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="ID" type="global" dataNode="dn1,dn2,dn3" /> <table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" /> <!-- random sharding using mod sharind rule --> <table name="hotnews" primaryKey="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="ID" dataNode="dn1,dn2" rule="sharding-by-intfile" /> <table name="customer" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile"> <childTable name="orders" primaryKey="ID" joinKey="customer_id" parentKey="id"> <childTable name="order_items" joinKey="order_id" parentKey="id" /> </childTable> <childTable name="customer_addr" primaryKey="ID" joinKey="customer_id" parentKey="id" /> </table> <!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate" /> --> </schema>
修改后:
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100"> <!-- auto sharding by id (long) --> <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> <!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate" /> --> </schema>
2) 修改schema 標簽
修改前
<!-- can have multi write hosts --> <writeHost host="hostM1" url="localhost:3306" user="root" password="123"> <!-- can have multi read hosts --> <readHost host="hostS2" url="localhost:3306" user="root" password="123" /> </writeHost> <writeHost host="hostS1" url="localhost:3306" user="root" password="123" /> <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
修改后:
<!-- can have multi write hosts --> <writeHost host="hostM1" url="localhost:3306" user="root" password="YYBrhr_2018"> <!-- can have multi read hosts --> <!-- <readHost host="hostS2" url="localhost:3306" user="root" password="123" /> --> </writeHost> <!-- <writeHost host="hostS1" url="localhost:3306" user="root" password="123" /> --> <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> --> </dataHost>
(3)修改role.xml
修改前:
<tableRule name="sharding-by-intfile"> <rule> <columns>sharding_id</columns> <algorithm>hash-int</algorithm> </rule> </tableRule> <tableRule name="auto-sharding-long"> <rule> <columns>id</columns> <algorithm>rang-long</algorithm> </rule> </tableRule> <tableRule name="mod-long"> <rule> <columns>id</columns> <algorithm>mod-long</algorithm> </rule> </tableRule> <tableRule name="sharding-by-murmur"> <rule> <columns>id</columns> <algorithm>murmur</algorithm> </rule> </tableRule>
修改后:
<tableRule name="sharding-by-intfile"> <rule> <columns>employee_id</columns> <algorithm>hash-int</algorithm> </rule> </tableRule> <tableRule name="auto-sharding-long"> <rule> <columns>id</columns> <algorithm>rang-long</algorithm> </rule> </tableRule> <tableRule name="mod-long"> <rule> <columns>hotnews_id</columns> <algorithm>mod-long</algorithm> </rule> </tableRule> <tableRule name="sharding-by-murmur"> <rule> <columns>customer_id</columns> <algorithm>murmur</algorithm> </rule> </tableRule>
5、啟動mycat並測試
【1】啟動mycat
mycat start mycat status
【2】測試
mysql -u root -p -P8066 -h 127.0.0.1
這里輸入的密碼,是在/home/rhr/soft/mycat/mycat/conf/server.xml里面看到的密碼
這樣就說明安裝成功了,安裝或者啟動mycat之前
解決方案:這個是由參數 useGlobleTableCheck 控制的全局表一致性檢測,原理通過在全局表增加_MYCAT_OP_TIME字段來進行一致性檢測,類型為bigint,create語句通過mycat執行會自動加上這個字段,其他情況請自己手工添加。
所以創建表不要在 datanode 各節點去創建,而是通過 mycat 去創建。