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实现主从复制)