標籤:conf 服務 lease arc github oct max 斷行符號 com
Linux下mysql基於mycat實現主從複製和讀寫分離
1.基礎設施
兩台虛擬機器:172.20.79.232(主) 172.20.79.233(從)
1.1軟體設施
mysql5.6.39 , mycat1.6-RELEASE jdk1.7及其以上版本
2.實現步驟一(mycat實現讀寫分離)
1.首先在兩台伺服器安裝mysql
1.下載mysql的repo源
$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
2.安裝mysql-community-release-el7-5.noarch.rpm包
$ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
安裝這個包後,會獲得兩個mysql的yum repo源:
/etc/yum.repos.d/mysql-community.repo /etc/yum.repos.d/mysql-community-source.repo
3.安裝mysql
$ sudo yum install mysql-server
根據提示安裝就可以了,不過安裝完成後沒有密碼,需要重設密碼
4.重設mysql密碼
$ mysql -u root
登入時有可能報這樣的錯:ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2),原因是/var/lib/mysql的存取權限問題。下面的命令把/var/lib/mysql的擁有者改為目前使用者:
$ sudo chown -R root:root /var/lib/mysql
5.開啟mysql遠端連線
grant all on *.* to [email protected]‘%‘ identified by ‘admin123‘ with grant option;flush privileges;
6.重啟mysql服務
$ service mysqld restart
7.接下來登入重設密碼:
$ mysql -u root //直接斷行符號進入mysql控制台 mysql > use mysql; mysql > update user set password=password(‘123456‘) where user=‘root‘; mysql > exit;
2.在主伺服器安裝mycat
1.下載mycat1.6
https://github.com/MyCATApache/Mycat-download
2.上傳至主伺服器解壓安裝
tar zxvf Mycat-server-1.6-RELEASE-20160405120037-linux.tar.gz -C /usr/local cd /usr/local/mycat groupadd mycat useradd -g mycat mycat passwd mycat chown -R mycat.mycat /usr/local/mycat export MYCAT_HOME=/usr/local/mycat
2.1 修改(新增)
vi /etc/hosts 172.20.79.232 root232 172.20.79.233 root233
2.2 修改mycat啟動jdk路徑
vi /usr/local/mycat/conf/wrapper.conf 修改wrapper.java.command=%JAVA_HOME%/bin/java
2.3 配置mycat 檔案schema.xml
vi /usr/local/mycat/conf/schema.xml <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <!-- 這裡的mycatdb與server.xml 裡面名稱一致 dataNode有幾個節點就配置幾個節點,用逗號分隔--> <schema name="mycatdb" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1" /> <!-- database有幾個資料庫就配置幾行即可--> <dataNode name="dn1" dataHost="localhost1" database="db01" /> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <!-- balance="0", 不開啟讀寫分離機制,所有讀操作都發送到當前可用的writeHost上。 balance="1",全部的readHost與stand by writeHost參與select語句的負載平衡,簡單的說,當雙主雙從模式(M1->S1,M2->S2,並且M1與M2互為主備),正常情況下,M2,S1,S2都參與select語句的負載平衡。 balance="2",所有讀操作都隨機的在writeHost、readhost上分發。 balance="3",所有讀請求隨機的分發到wiriterHost對應的readhost執行,writerHost不負擔讀壓力 writeType表示寫入模式 writeType="0",所有的操作發送到配置的第一個writehost writeType="1",隨機發送到配置的所有writehost writeType="2",不執行寫操作 switchType指的是切換的模式,目前的取值也有4種: switchType=‘-1‘ 表示不自動切換 switchType=‘1‘ 預設值,表示自動切換 switchType=‘2‘ 基於MySQL主從同步的狀態決定是否切換,心跳語句為show slave status switchType=‘3‘基於MySQL galary cluster的切換機制(適合叢集)(1.4.1),心跳語句為show status like ‘wsrep%‘。 --> <heartbeat>select user()</heartbeat> <!--配置mysql寫入的節點--> <writeHost host="hostM1" url="172.20.79.232:3306" user="root" password="admin123"> <!--配置mysql讀取的節點--> <readHost host="hostS2" url="172.20.79.233:3306" user="root" password="admin123" /> </writeHost> </dataHost> </mycat:schema>
2.4 配置mycat 檔案server.xml
vi /usr/local/mycat/conf/schema.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mycat:server SYSTEM "server.dtd"> <mycat:server xmlns:mycat="http://io.mycat/"> <system> <property name="useSqlStat">0</property> <!-- 1為開啟即時統計、0為關閉 --> <property name="useGlobleTableCheck">0</property> <!-- 1為開啟全加班一致性檢測、0為關閉 --> <property name="sequnceHandlerType">2</property> <!--預設為type 0: DirectByteBufferPool | type 1 ByteBufferArena--> <property name="processorBufferPoolType">0</property> <property name="handleDistributedTransactions">0</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> <user name="root"> <property name="password">admin123</property> <property name="schemas">mycatdb</property> </user> <user name="user"> <property name="password">user</property> <property name="schemas">mycatdb</property> <property name="readOnly">true</property> </user> </mycat:server>
3.實現步驟二(mysql實現主從複製)
linux下mysql基於mycat做主從複製和讀寫分離之基礎篇