Mycat 讀寫分離
阿新 • • 發佈:2020-12-20
實驗環境
主機名 | IP地址 | 系統 | 軟體 | 備註 |
mysql-master | 200.200.2.10 | CentOS7.5 | mysql5.6 | 主庫 |
mysql-slave | 200.200.2.11 | CentOS7.5 | mysql5.6 | 從庫 |
mycat | 200.200.2.12 | CentOS7.5 | mycat | 中介軟體 |
資料庫主從複製請參考前邊的文章進行配置
mycat伺服器
安裝jdk環境
tar xf jdk-8u60-linux-x64.tar.gz mv jdk1.8.0_60/ /usr/local/java8
配置java環境
vim /etc/profile.d/java.sh cat /etc/profile.d/java.sh export JAVA_HOME=/usr/local/java8 export CLASSPATH=$JAVA_HOME/lib/ export PATH=$PATH:$JAVA_HOME/bin 載入環境變數 source /etc/profile
檢視java環境版本
java -version
下載mycat
wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
解壓原始碼包
tar xf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz mvmycat/ /usr/local/
建立程式使用者
useradd -M -s /sbin/nologin mycat
chwon -R mycat:mycat /usr/local/mycat/
新增mycat命令到環境
vim /etc/profile.d/mycat.sh cat /etc/profile.d/mycat.sh export MYCAT_HOME=/usr/local/mycat export PATH=$PATH:$MYCAT_HOME/bin 載入環境變數 source /etc/profile.d/mycat.sh
更改配置檔案
vim /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="HEADSHOW" checkSQLschema='flase' dataNode='dn1'></schema> 定義一個邏輯庫HEADSHOW 節點在dn1 <dataNode name="dn1" dataHost="localhost1" database="abc" /> 對映成為一個真實的庫名 <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" 與上邊的localhost進行管理 最大連線數1000 最小連線數10 balance 0是不開始 1是wirehost內部進行隨機負載,2是dataNode內部進行隨機負載 writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> 檢視哪些使用者 <!-- can have multi write hosts --> <writeHost host="200.200.2.10" url="200.200.2.10:3306" user="root" 寫入的資料寫入到主庫200.200.2.10 3306埠 登陸使用者為root使用者 password="123456"> 登陸密碼為123456 <!-- can have multi read hosts --> <readHost host="200.200.2.11" url="200.200.2.11:3306" user="root" password="123456" /> 讀的資料將從從庫200.200.2.11中查詢 登陸使用者為root密碼是123456 </writeHost> <writeHost host="hostS1" url="localhost:3316" user="root" 備用 password="123456" /> <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> --> </dataHost> <!-- <dataHost name="sequoiadb1" maxCon="1000" minCon="1" balance="0" dbType="sequoiadb" dbDriver="jdbc"> <heartbeat> </heartbeat> <writeHost host="hostM1" url="sequoiadb://1426587161.dbaas.sequoialab.net:11920/SAMPLE" user="jifeng" password="jifeng"></writeHost> </dataHost> <dataHost name="oracle1" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="oracle" dbDriver="jdbc"> <heartbeat>select 1 from dual</heartbeat> <connectionInitSql>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'</connectionInitSql> <writeHost host="hostM1" url="jdbc:oracle:thin:@127.0.0.1:1521:nange" user="base" password="123456" > </writeHost> </dataHost> <dataHost name="jdbchost" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="mongodb" dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM" url="mongodb://192.168.0.99/test" user="admin" password="123456" ></writeHost> </dataHost> <dataHost name="sparksql" maxCon="1000" minCon="1" balance="0" dbType="spark" dbDriver="jdbc"> <heartbeat> </heartbeat> <writeHost host="hostM1" url="jdbc:hive2://feng01:10000" user="jifeng" password="jifeng"></writeHost> </dataHost> --> <!-- <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="0" dbType="mysql" dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="root" password="123456"> </writeHost> </dataHost> --> </mycat:schema>
vim /usr/local/mycat/conf/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="useSqlStat">1</property> <property name="useGlobleTableCheck">0</property> <property name="sequnceHandlerType">2</property> <property name="processorBufferPoolType">0</property> <property name="useHandshakeV10">1</property> <property name="useSqlStat">0</property> <property name="useGlobleTableCheck">0</property> <property name="serverPort">3310</property> 埠號 <property name="managerPort">9066</property> 埠號 <property name="maxStringLiteralLength">65535</property> <property name="sequnceHandlerType">0</property> <property name="backSocketNoDelay">1</property> <property name="frontSocketNoDelay">1</property> <property name="processorExecutor">16</property> <!-- off heap for merge/order/group/limit 1開啟 0關閉 --> <property name="useOffHeapForMerge">1</property> <!-- 單位為m --> <property name="memoryPageSize">1m</property> <!-- 單位為k --> <property name="spillsFileBufferSize">1k</property> <property name="useStreamOutput">0</property> <!-- 單位為m --> <property name="systemReserveMemorySize">384m</property> <!--是否採用zookeeper協調切換 --> <property name="useZKSwitch">true</property> </system> <!-- 全域性SQL防火牆設定 --> <!-- <firewall> <whitehost> <host host="127.0.0.1" user="mycat"/> <host host="127.0.0.2" user="mycat"/> </whitehost> <blacklist check="false"> </blacklist> </firewall> --> <user name="root"> <property name="password">123456</property> <property name="schemas">HEADSHOW</property> <!-- 表級 DML 許可權設定 --> <!-- <privileges check="false"> <schema name="TESTDB" dml="0110" > <table name="tb01" dml="0000"></table> <table name="tb02" dml="1111"></table> </schema> </privileges> --> </user> <user name="mycat"> <property name="password">123456</property> <property name="schemas">HEADSHOW</property> <property name="readOnly">true</property> </user> </mycat:server>
啟動mycat服務
mycat start
下載mariadb,訪問測試
yum -y install mariadb mysql -uroot -p123456 -h 127.0.0.1 -P9066
測試方法
將主從複製關閉,在mycat中插入資料
檢視主庫中是否存在資料,mycat查詢資料是否存在
主庫中存在資料則證明通過mycat寫入功能正常開啟
mycat中查詢不到資料則mycat讀取是從從伺服器上進行讀取