1. 程式人生 > 其它 >Linux - MyCat 讀寫分離

Linux - MyCat 讀寫分離

# 本次實驗 共四臺機器 
# A: 10.0.0.11 為client
# B: 10.0.0.12 為主節點
# C: 10.0.0.13 為從節點1
# D: 10.0.0.14 為從節點2

# 首先配置BCD主從複製 (這裡不做重複描寫,參考我前面的文件)
# 開始安裝mycat相關包,以及資料庫客戶端
[root@centos8 ~]#yum -y install java mariadb
#確認安裝成功
[root@centos8 ~]#java -version
openjdk version "1.8.0_201"
OpenJDK Runtime Environment (build 1.8.0_201-b09)
OpenJDK 
64-Bit Server VM (build 25.201-b09, mixed mode) #下載並安裝 [root@centos8 ~]#wget http://dl.mycat.org.cn/1.6.7.4/Mycat-server-1.6.7.4-release/Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz [root@centos8 ~]#mkdir /apps [root@centos8 ~]#tar xvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz -C /apps #配置環境變數 [root@centos8
~]#echo 'PATH=/apps/mycat/bin:$PATH' > /etc/profile.d/mycat.sh [root@centos8 ~]#source /etc/profile.d/mycat.sh #啟動mycat [root@centos8 ~]#mycat start Starting Mycat-server... # 在wrapper.log能查詢到相關資訊 STATUS | wrapper | 2021/06/26 23:28:36 | --> Wrapper Started as Daemon STATUS | wrapper | 2021/06/26 23:28:36 | Launching a JVM... INFO
| jvm 1 | 2021/06/26 23:28:39 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org INFO | jvm 1 | 2021/06/26 23:28:39 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved. INFO | jvm 1 | 2021/06/26 23:28:39 | INFO | jvm 1 | 2021/06/26 23:28:40 | MyCAT Server startup successfully. see logs in logs/mycat.log # 配置server.xml,改變serverPort和root的登入密碼 <property name="serverPort">3306</property> <property name="managerPort">9066</property> <user name="root"> <property name="password">123456</property> <property name="schemas">TESTDB</property> <property name="defaultSchema">TESTDB</property> #配置schema.xml [08:43:34 root@centos8 conf]#cat schema.xml <?xml version="1.0" encoding="UTF-8"?> <!--?xml version="1.0"?--> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> <!--table name="students" dataNode="jdbc_dn1" rule="rule1" /--> </schema> <dataNode name="dn1" dataHost="localhost1" database="hellodb" /> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="host1" url="10.0.0.12:3306" user="admin" password="123456"> <readHost host="host2" url="10.0.0.13:3306" user="admin" password="123456" /> <readHost host="host3" url="10.0.0.14:3306" user="admin" password="123456" /> </writeHost> </dataHost> </mycat:schema> # schema.xml中配置有admin賬號,因此需要在主從節點建立admin賬號,並給予許可權GRANT ALL ON *.* TO 'admin'@'10.0.0.%' IDENTIFIED BY '123456' WITH GRANT OPTION; MariaDB [(none)]> select user,host from mysql.user; +----------------+-----------+ | user | host | +----------------+-----------+ | admin | 10.0.0.% | # 重啟mycat服務 [08:25:05 root@centos8 ~]#mycat restart [08:25:12 root@centos8 ~]#mycat console Running Mycat-server... Mycat-server is already running. [08:48:36 root@centos8 ~]#mysql -uroot -p123456 -h127.0.0.1 -P3306 -DTESTDB MySQL [TESTDB]> show databases; +----------+ | DATABASE | +----------+ | TESTDB | +----------+ 1 row in set (0.001 sec) MySQL [TESTDB]> use TESTDB; Database changed MySQL [TESTDB]> show tables; +-------------------+ | Tables_in_hellodb | +-------------------+ | classes | | coc | | courses | | scores | | students | | teachers | | toc | +-------------------+ 7 rows in set (0.012 sec) MySQL [TESTDB]> create table t1(id int); Query OK, 0 rows affected (0.014 sec) # 讀的話,只會在C,D節點迴圈 MySQL [TESTDB]> select @@server_id; +-------------+ | @@server_id | +-------------+ | 14 | +-------------+ 1 row in set (0.001 sec) MySQL [TESTDB]> select @@server_id; +-------------+ | @@server_id | +-------------+ | 13 | +-------------+ 1 row in set (0.001 sec) MySQL [TESTDB]> select @@server_id; +-------------+ | @@server_id | +-------------+ | 14 | +-------------+ # 新增的id為主節點,證明其寫已經分離 MySQL [TESTDB]> insert t1(id) values(@@server_id); Query OK, 1 row affected (0.003 sec) MySQL [TESTDB]> select * from t1; +------+ | id | +------+ | 12 | +------+ 1 row in set (0.006 sec) # 用通用日誌在主從節點觀察 # 主節點 [08:28:34 root@centos8 ~]#tail -f /var/lib/mysql/centos8.log 210627 8:27:44 16 Query select user() 210627 8:27:54 12 Query select user() 210627 8:30:23 12 Query SET names utf8;create table t1(id int) 210627 8:30:24 21 Query select user() 210627 8:30:34 24 Query select user() 210627 8:33:28 12 Query insert t1(id) values(@@server_id) 210627 8:33:34 21 Query select user() 210627 8:33:44 24 Query select user() 210627 8:33:54 23 Query select user() # 從節點C [08:29:18 root@centos8 ~]#tail -f /var/lib/mysql/centos8.log 210627 8:28:01 11 Query show variables like '%general_log%' 210627 8:28:03 12 Query select user() 210627 8:28:13 15 Query select user() 210627 8:28:23 13 Query select user() 210627 8:30:23 10 Query create table t1(id int) 13 Query select user() 210627 8:30:33 14 Query select user() 210627 8:30:43 12 Query select user() 210627 8:30:53 17 Query select user() 210627 8:31:03 16 Query select user() 210627 8:31:13 15 Query select user() 210627 8:31:23 13 Query select user() 210627 8:31:33 14 Query select user() 210627 8:31:43 12 Query select user() 210627 8:31:50 17 Query SET names utf8;select @@server_id 210627 8:31:52 16 Query SET names utf8;select @@server_id 15 Query SET names utf8;select @@server_id 210627 8:33:03 12 Query select user() 210627 8:33:13 17 Query select user() 210627 8:33:23 16 Query select user() 210627 8:33:28 10 Query BEGIN 10 Query COMMIT /* implicit, from Xid_log_event */ 210627 8:33:33 15 Query select user() 210627 8:33:43 13 Query select user() 210627 8:33:46 14 Query SET names utf8;select * from t1 210627 8:33:53 12 Query select user() 210627 8:34:03 17 Query select user() 210627 8:34:13 16 Query select user() 210627 8:34:23 15 Query select user() # 從節點D [08:29:29 root@centos8 ~]#tail -f /var/lib/mysql/centos8.log 210627 8:28:24 13 Query select user() 210627 8:28:34 12 Query select user() 210627 8:28:44 14 Query select user() 210627 8:28:54 13 Query select user() 210627 8:31:34 15 Query select user() 210627 8:31:44 16 Query SET names utf8;select @@server-id 14 Query select user() 210627 8:31:46 13 Query SET names utf8;select @@server_id 210627 8:31:52 12 Query SET names utf8;select @@server_id 210627 8:31:54 15 Query SET names utf8;select @@server_id 16 Query select user() 210627 8:32:04 14 Query select user() 210627 8:32:14 13 Query select user() 210627 8:32:54 13 Query select user() 210627 8:33:04 12 Query select user() 210627 8:33:14 15 Query select user() 210627 8:33:24 16 Query select user() 210627 8:33:28 10 Query BEGIN 10 Query COMMIT /* implicit, from Xid_log_event */ 210627 8:33:34 14 Query select user() 210627 8:33:44 13 Query select user() 210627 8:33:54 12 Query select user() 210627 8:34:04 15 Query select user() # 報錯分析:重啟mycat報錯 #1> 標籤問題,出現如下面這個類似的,一般是標籤問題,這是XML語法檢查,每個大標籤應該有自己的結束標籤 INFO | jvm 1 | 2021/06/26 23:43:36 | Caused by: org.xml.sax.SAXParseException; lineNumber: 33; columnNumber: 4; The element type "writeHost" must be terminated by the matching end-tag "</writeHost>". #2> 有兩個解決辦法 INFO | jvm 1 | 2021/06/27 00:05:16 | Caused by: io.mycat.config.util.ConfigException: schema TESTDB didn't config tables,so you must set dataNode property! <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> #方法1 把之前的randomDataNode改成databNode <!--table name="students" dataNode="jdbc_dn1" rule="rule1" /--> # 方法2,加上table </schema> #3> 這個出現system標籤問題,那就需要去檢視server.xml,明顯是中間system標籤中間有不是小標籤的內容,類似中間有不是xml的語法,但是未加<>這些 INFO | jvm 1 | 2021/06/27 08:09:19 | Caused by: io.mycat.config.util.ConfigException: org.xml.sax.SAXParseException; lineNumber: 89; columnNumber: 11; The content of element type "system" must match "(property)*". #4> 如果出現類似空指標的問題,請先檢查檔案格式,博主遇到很多都是因為windows格式傳到linux出問題