架構說明 10.0.0.18 master節點和10.0.0.22節點互為主 10.0.0.19 10.0.0.18的slave節點 10.0.0.22 master節點和10.0.0.19節點互為主 10.0.0.24 10.0.0.22的slave節點 10.0.0.23 mycat節點 mys ...
架構說明
10.0.0.18 master節點和10.0.0.22節點互為主 10.0.0.19 10.0.0.18的slave節點 10.0.0.22 master節點和10.0.0.19節點互為主 10.0.0.24 10.0.0.22的slave節點 10.0.0.23 mycat節點 mysql版本8.0.32 系統版本:rocky8.4
mysql主從搭建
#搭建雙主節點 #搭建第一個主10.0.0.18 #註釋掉/etc/my.cnf.d/mysql-server.cnf cat >/etc/my.cnf.d/mysql-server.cnf<<'EOF' # # This group are read by MySQL server. # Use it for options that only the server (but not clients) should see # # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mysqld according to the # instructions in http://fedoraproject.org/wiki/Systemd #[mysqld] #datadir=/var/lib/mysql #socket=/var/lib/mysql/mysql.sock #log-error=/var/log/mysql/mysqld.log #pid-file=/run/mysqld/mysqld.pid #log-bin=/data/mysql/logbin/mysql-bin EOF #配置主節點的my.cat配置 cat >/etc/my.cnf<<'EOF' # # This group is read both both by the client and the server # use it for options that affect everything # [client-server] # # include all files from the config directory # !includedir /etc/my.cnf.d [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysql/mysqld.log pid-file=/run/mysqld/mysqld.pid server-id=18 #read-only general_log gtid_mode=ON enforce_gtid_consistency log-bin=/data/mysql/logbin/mysql-bin EOF #創建存放二進位日誌的目錄 mkdir -p /data/mysql/logbin/ chown -R mysql.mysql /data #啟動資料庫 systemctl enable --now mysqld #配置賬號和授權 mysql create user 'repluser'@'10.0.0.%' identified by '123456'; grant replication slave on *.* to 'repluser'@'10.0.0.%'; #創建mycat使用的賬號 create user 'wbiao'@'10.0.0.%' IDENTIFIED BY '123456'; grant ALL ON hellodb.* TO 'wbiao'@'10.0.0.%'; #搭建第二個主10.0.0.22 #註釋掉/etc/my.cnf.d/mysql-server.cnf cat >/etc/my.cnf.d/mysql-server.cnf<<'EOF' # # This group are read by MySQL server. # Use it for options that only the server (but not clients) should see # # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mysqld according to the # instructions in http://fedoraproject.org/wiki/Systemd #[mysqld] #datadir=/var/lib/mysql #socket=/var/lib/mysql/mysql.sock #log-error=/var/log/mysql/mysqld.log #pid-file=/run/mysqld/mysqld.pid #log-bin=/data/mysql/logbin/mysql-bin EOF #配置主節點的my.cat配置 cat >/etc/my.cnf<<'EOF' # # This group is read both both by the client and the server # use it for options that affect everything # [client-server] # # include all files from the config directory # !includedir /etc/my.cnf.d [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysql/mysqld.log pid-file=/run/mysqld/mysqld.pid server-id=22 #read-only general_log gtid_mode=ON enforce_gtid_consistency log-bin=/data/mysql/logbin/mysql-bin EOF #創建存放二進位日誌的目錄 mkdir -p /data/mysql/logbin/ chown -R mysql.mysql /data #啟動資料庫 systemctl enable --now mysqld #10.0.0.22指向10.0.0.18 執行change master to CHANGE MASTER TO MASTER_HOST='10.0.0.18', MASTER_USER='repluser', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_AUTO_POSITION=1; #開啟IO線程和SQL線程 start slave; #檢查狀態 show slave status\G #檢查 mysql> select user,host from mysql.user; +------------------+-----------+ | user | host | +------------------+-----------+ | repluser | 10.0.0.% | | wbiao | 10.0.0.% | | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+ 6 rows in set (0.00 sec) 10.0.0.18指向10.0.0.22 執行change master to CHANGE MASTER TO MASTER_HOST='10.0.0.22', MASTER_USER='repluser', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_AUTO_POSITION=1; #開啟IO線程和SQL線程 start slave; #檢查狀態 show slave status\G #配置10.0.0.18的從節點10.0.0.19 #註釋掉/etc/my.cnf.d/mysql-server.cnf cat >/etc/my.cnf.d/mysql-server.cnf<<'EOF' # # This group are read by MySQL server. # Use it for options that only the server (but not clients) should see # # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mysqld according to the # instructions in http://fedoraproject.org/wiki/Systemd #[mysqld] #datadir=/var/lib/mysql #socket=/var/lib/mysql/mysql.sock #log-error=/var/log/mysql/mysqld.log #pid-file=/run/mysqld/mysqld.pid #log-bin=/data/mysql/logbin/mysql-bin EOF #配置從節點10.0.0.19的my.cat配置 cat >/etc/my.cnf<<'EOF' # # This group is read both both by the client and the server # use it for options that affect everything # [client-server] # # include all files from the config directory # !includedir /etc/my.cnf.d [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysql/mysqld.log pid-file=/run/mysqld/mysqld.pid server-id=19 read-only general_log gtid_mode=ON enforce_gtid_consistency log-bin=/data/mysql/logbin/mysql-bin EOF #創建存放二進位日誌的目錄 mkdir -p /data/mysql/logbin/ chown -R mysql.mysql /data #啟動資料庫 systemctl enable --now mysqld #從節點10.0.0.19執行change master to CHANGE MASTER TO MASTER_HOST='10.0.0.18', MASTER_USER='repluser', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_AUTO_POSITION=1; #開啟IO線程和SQL線程 start slave; #檢查狀態 show slave status\G ##配置10.0.0.22的從節點10.0.0.24 #註釋掉/etc/my.cnf.d/mysql-server.cnf cat >/etc/my.cnf.d/mysql-server.cnf<<'EOF' # # This group are read by MySQL server. # Use it for options that only the server (but not clients) should see # # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mysqld according to the # instructions in http://fedoraproject.org/wiki/Systemd #[mysqld] #datadir=/var/lib/mysql #socket=/var/lib/mysql/mysql.sock #log-error=/var/log/mysql/mysqld.log #pid-file=/run/mysqld/mysqld.pid #log-bin=/data/mysql/logbin/mysql-bin EOF #配置從節點10.0.0.24的my.cat配置 cat >/etc/my.cnf<<'EOF' # # This group is read both both by the client and the server # use it for options that affect everything # [client-server] # # include all files from the config directory # !includedir /etc/my.cnf.d [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysql/mysqld.log pid-file=/run/mysqld/mysqld.pid server-id=24 read-only general_log gtid_mode=ON enforce_gtid_consistency log-bin=/data/mysql/logbin/mysql-bin EOF #創建存放二進位日誌的目錄 mkdir -p /data/mysql/logbin/ chown -R mysql.mysql /data #啟動資料庫 systemctl enable --now mysqld #從節點10.0.0.24執行change master to CHANGE MASTER TO MASTER_HOST='10.0.0.22', MASTER_USER='repluser', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_AUTO_POSITION=1; #開啟IO線程和SQL線程 start slave; #檢查狀態 show slave status\G #10.0.0.18導入hellodb的資料庫 [root@10 ~]# mysql <hellodb_innodb.sql ##檢查狀態 show slave status\G #所有節點檢查數據 select * from hellodb.students; #雙主只能對一個主進行寫操作
mycat搭建10.0.0.23
#安裝java環境 yum -y install java #創建安裝目錄和解壓 mkdir -p /apps tar -xf Mycat-server-1.6.7.6-release-20210303094759-linux.tar.gz -C /app #配置環境變數 [root@10 ~]# echo 'PATH=/apps/mycat/bin:$PATH' > /etc/profile.d/mycat.sh [root@10 ~]# . /etc/profile.d/mycat.sh [root@10 ~]# echo $PATH /apps/mycat/bin:/usr/share/Modules/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin #配置mycat實現讀寫分離主從高可用 #修改schema.xml的配置文件 #balance="2",所有讀操作都隨機的在writeHost、readhost上分發。 #balance="1",全部的readHost與stand by writeHost參與select語句的負載均衡,簡單的說,當雙主雙從模式(M1->S1,M2->S2,並且M1與M2互為主備) #writeType屬性負載均衡類型,目前的取值有3種: #1.writeType="0", 所有寫操作發送到配置的第一個writeHost,第一個掛了切到還生存的第二個writeHost,重新啟動後已切換後的為準,切換記錄在配置文件中:dnindex.properties. #2.writeType="1",所有寫操作都隨機的發送到配置的writeHost,1.5以後廢棄不推薦。 #3.writeType="2",不執行寫操作 [root@10 conf]# cat schema.xml <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="wbiao" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> </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.18:3306" user="wbiao" password="123456"> <readHost host="host2" url="10.0.0.19:3306" user="wbiao" password="123456" /> </writeHost> <writeHost host="host3" url="10.0.0.22:3306" user="wbiao" password="123456"> <readHost host="host4" url="10.0.0.23:3306" user="wbiao" password="123456" /> </writeHost> </dataHost> </mycat:schema> #server.xml配置文件 [root@10 conf]# cat server.xml <?xml version="1.0" encoding="UTF-8"?> <!-- - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --> <!DOCTYPE mycat:server SYSTEM "server.dtd"> <mycat:server xmlns:mycat="http://io.mycat/"> <system> <property name="nonePasswordLogin">0</property> <!-- 0為需要密碼登陸、1為不需要密碼登陸 ,預設為0,設置為1則需要指定預設賬戶--> <property name="ignoreUnknownCommand">0</property><!-- 0遇上沒有實現的報文(Unknown command:),就會報錯、1為忽略該報文,返回ok報文。 在某些mysql客戶端存在客戶端已經登錄的時候還會繼續發送登錄報文,mycat會報錯,該設置可以繞過這個錯誤--> <property name="useHandshakeV10">1</property> <property name="removeGraveAccent">1</property> <property name="useSqlStat">0</property> <!-- 1為開啟實時統計、0為關閉 --> <property name="useGlobleTableCheck">0</property> <!-- 1為開啟全加班一致性檢測、0為關閉 --> <property name="sqlExecuteTimeout">300</property> <!-- SQL 執行超時 單位:秒--> <property name="sequenceHandlerType">1</property> <!--<property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property> INSERT INTO `travelrecord` (`id`,user_id) VALUES ('next value for MYCATSEQ_GLOBAL',"xxx"); --> <!--必須帶有MYCATSEQ_或者 mycatseq_進入序列匹配流程 註意MYCATSEQ_有空格的情況--> <property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property> <property name="subqueryRelationshipCheck">false</property> <!-- 子查詢中存在關聯查詢的情況下,檢查關聯欄位中是否有分片欄位 .預設 false --> <property name="sequenceHanlderClass">io.mycat.route.sequence.handler.HttpIncrSequenceHandler</property> <!-- <property name="useCompression">1</property>--> <!--1為開啟mysql壓縮協議--> <!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--設置模擬的MySQL版本號--> <!-- <property name="processorBufferChunk">40960</property> --> <!-- <property name="processors">1</property> <property name="processorExecutor">32</property> --> <!--預設為type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool --> <property name="processorBufferPoolType">0</property> <property name="serverPort">3306</property> <!--預設是65535 64K 用於sql解析時最大文本長度 --> <!--<property name="maxStringLiteralLength">65535</property>--> <!--<property name="sequenceHandlerType">0</property>--> <!--<property name="backSocketNoDelay">1</property>--> <!--<property name="frontSocketNoDelay">1</property>--> <!--<property name="processorExecutor">16</property>--> <!-- <property name="serverPort">8066</property> <property name="managerPort">9066</property> <property name="idleTimeout">300000</property> <property name="authTimeout">15000</property> <property name="bindIp">0.0.0.0</property> <property name="dataNodeIdleCheckPeriod">300000</property> 5 * 60 * 1000L; //連接空閑檢查 <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> --> <!--分散式事務開關,0為不過濾分散式事務,1為過濾分散式事務(如果分散式事務內只涉及全局表,則不過濾),2為不過濾分散式事務,但是記錄分散式事務日誌--> <property name="handleDistributedTransactions">0</property> <!-- off heap for merge/order/group/limit 1開啟 0關閉 --> <property name="useOffHeapForMerge">0</property> <!-- 單位為m --> <property name="memoryPageSize">64k</property> <!-- 單位為k --> <property name="spillsFileBufferSize">1k</property> <property name="useStreamOutput">0</property> <!-- 單位為m --> <property name="systemReserveMemorySize">384m</property> <!--是否採用zookeeper協調切換 --> <property name="useZKSwitch">false</property> <!-- XA Recovery Log日誌路徑 --> <!--<property name="XARecoveryLogBaseDir">./</property>--> <!-- XA Recovery Log日誌名稱 --> <!--<property name="XARecoveryLogBaseName">tmlog</property>--> <!--如果為 true的話 嚴格遵守隔離級別,不會在僅僅只有select語句的時候在事務中切換連接--> <property name="strictTxIsolation">false</property> <!--如果為0的話,涉及多個DataNode的catlet任務不會跨線程執行--> <property name="parallExecute">0</property> <property name="serverBacklog">2048</property> </system> <!-- 全局SQL防火牆設置 --> <!--白名單可以使用通配符%或著*--> <!--例如<host host="127.0.0.*" user="root"/>--> <!--例如<host host="127.0.*" user="root"/>--> <!--例如<host host="127.*" user="root"/>--> <!--例如<host host="1*7.*" user="root"/>--> <!--這些配置情況下對於127.0.0.1都能以root賬戶登錄--> <!-- <firewall> <whitehost> <host host="1*7.0.0.*" user="root"/> </whitehost> <blacklist check="false"> </blacklist> </firewall> --> <user name="root" defaultAccount="true"> <property name="password">qwe123</property> <property name="schemas">wbiao</property> <property name="defaultSchema">wbiao</property> <!--No MyCAT Database selected 錯誤前會嘗試使用該schema作為schema,不設置則為null,報錯 --> <!-- 表級 DML 許可權設置 --> <!-- <privileges check="false"> <schema name="wbiao" dml="0110" > <table name="tb01" dml="0000"></table> <table name="tb02" dml="1111"></table> </schema> </privileges> --> </user> <user name="user"> <property name="password">user</property> <property name="schemas">wbiao</property> <property name="readOnly">true</property> <property name="defaultSchema">wbiao</property> </user> </mycat:server> #啟動mycat [root@10 conf]# mycat start Starting Mycat-server... 監控日誌 [root@10 ~]# tail -F /apps/mycat/logs/wrapper.log STATUS | wrapper | 2023/05/24 02:44:41 | <-- Wrapper Stopped STATUS | wrapper | 2023/05/24 02:44:45 | --> Wrapper Started as Daemon STATUS | wrapper | 2023/05/24 02:44:45 | Launching a JVM... INFO | jvm 1 | 2023/05/24 02:44:46 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org INFO | jvm 1 | 2023/05/24 02:44:46 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved. INFO | jvm 1 | 2023/05/24 02:44:46 | INFO | jvm 1 | 2023/05/24 02:44:48 | MyCAT Server startup successfully. see logs in logs/mycat.log #鏈接測試 [root@10 conf]# mysql -uroot -pqwe123 -h10.0.0.23 #讀測試 mysql> select @@hostname; +------------+ | @@hostname | +------------+ | 10.0.0.24 | +------------+ 1 row in set (0.08 sec) mysql> select @@hostname; +------------+ | @@hostname | +------------+ | 10.0.0.22 | +------------+ 1 row in set (0.00 sec) mysql> select @@hostname; +------------+ | @@hostname | +------------+ | 10.0.0.19 | +------------+ 1 row in set (0.01 sec) mysql> select @@hostname; +------------+ | @@hostname | +------------+ | 10.0.0.19 | +------------+ 1 row in set (0.01 sec) mysql> select @@hostname; +------------+ | @@hostname | +------------+ | 10.0.0.24 | +------------+ 1 row in set (0.01 sec