Mycat dual-master four-slave high availability detailed configuration operation

Preface

Experimental environment:
System: centos7
Mysql version: 5.7
Mycat version: 1.6
Mycat host ip address: 10.8.161.14
Host 1 address: 10.8.161.9
Host 2 address: 10.8.161.18
Slave 1 address: 10.8.161.139
Slave 2 address: 10.8 .161.140
Slave 3 address: 10.8.161.141
Slave 4 address: 10.8.161.142

Experiment objectives:
① Host 1 and Host 2 are mutually master and slave
②Slave 1 and 2 are the slaves of Host 1. The slaves 3 and 4 are the slaves of the master 2.
③Mycat configuration read and write separation. Masters 1 and 2 are write masters, and the other slaves are read masters. When host 1 is down, host 2 is on top. The opposite is the same.

1. Master-slave configuration

Host 1
  1. vim /etc/my.cnf Add the following content under the configuration file
[mysqld] #这个下面添加
server-id=1                             #server的唯一标识
auto_increment_increment=2              #总共有2台主
auto_increment_offset=1                 #这是第1台
log-slave-updates=1                     # 共联同步 
log-bin = mysql-bin                           #打开二进制功能,MASTER主服务器必须打开此项
max_binlog_size=1024M                          #binlog单文件最大值                  
  1. Enter the master 1 database and create an account for the slave to connect to the master.
grant replication slave on *.*  TO  'cong'@'%'  identified by 'aA12345678.';
  1. Restart the database
systemctl restart mysqld
  1. Write down the log name and starting position
进入数据库
mysql> show master status\G

Insert picture description here

Host 2
  1. vim /etc/my.cnf Add the following content under the configuration file
[mysqld] #这个下面添加
server-id=1                             #server的唯一标识
auto_increment_increment=2              #总共有2台主
auto_increment_offset=2                 #这是第2台
log-slave-updates=1                     # 共联同步 
log-bin = mysql-bin                           #打开二进制功能,MASTER主服务器必须打开此项
max_binlog_size=1024M                          #binlog单文件最大值                  
  1. Enter the master 2 database and create an account for the slave to connect to the master.
grant replication slave on *.*  TO  'cong'@'%'  identified by 'aA12345678.';
  1. Restart the database
systemctl restart mysqld
  1. Write down the log name and starting position
进入数据库
mysql> show master status\G

Insert picture description here

Slave 1
  1. vim /etc/my.cnf Add the following content under the configuration file
[mysqld] #这个下面添加
server-id=103   # 和其他设备的id不同即可
autocommit=1    # 自动提交事务,相对安全
lower_case_table_names=1    #表不区分大小写
  1. View the log start of master 1

  2. Enter the database, connect to the main 1 database

mysql> \e
CHANGE MASTER TO
MASTER_HOST='10.8.161.9',     # 指定库1的地址
MASTER_USER='cong',           # 指定库1创建的用户名  
MASTER_PASSWORD='aA12345678.',   # 指定库1创建的密码
MASTER_LOG_FILE='mysql-bin.000002',    # 指定主1库的日志文件名
MASTER_LOG_POS=321;                    # 指定主1库的日志起始位置
    -> ;
The other slave configuration methods are similar

After configuration, start and view from the machine

mysql >  start slave;    #启动
mysql > show slave status\G     #查看

Insert picture description here

2. Mycat configuration

The following configuration jdk package and mycat package have uploaded my resources, which can be downloaded by yourself.
download link

https://download.csdn.net/download/qq_26129413/14009842
  1. Deploy jdk environment
[root@mycat ~]# tar xzf jdk-8u221-linux-x64.tar.gz -C /usr/local/
[root@mycat ~]# cd /usr/local/
[root@mycat local]# mv jdk1.8.0_221/ java    #改名为java
设置环境变量
[root@mycat local]# vim /etc/profile  #添加如下内容,
JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
[root@mycat local]# source /etc/profile
  1. Unzip the mycat package
tar xf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz -C /usr/local/
  1. Configure mycat

    server.xml : used to configure system parameters, user information, access rights, SQL firewall and SQL interception functions, etc.
    schema.xml : used to configure the logic library, logic table related information
    rule.xml : if horizontal segmentation is used, it is required Use this file to configure segmentation rules
    log4j2.xml : Mycat log related configuration, such as log output format, log level, etc.

    When the application connects to the Mycat service, Mycat first authenticates the user through the configuration information in server.xml. After the user is authenticated, all the logical libraries and logical tables they see are configured in the schema.xml. When horizontal segmentation is used, Mycat will use the rules configured in rule.xml to locate the specific physical database location to complete writing/reading data.

① server.xml placement

<?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>
<property name="processorBufferPoolType">0</property>
        <property name="handleDistributedTransactions">0</property>
        <property name="useOffHeapForMerge">1</property>
        <property name="memoryPageSize">1m</property>
        <property name="spillsFileBufferSize">1k</property>

                <property name="useStreamOutput">0</property>

        <property name="systemReserveMemorySize">384m</property>

        <property name="useZKSwitch">true</property>


        </system>

        <user name="root" >
                <property name="password">abc123.456</property>
                <property name="schemas">testdb</property>
</user>
    <!--
        <user name="user">
                <property name="password">user</property>
                <property name="schemas">TESTDB</property>
                <property name="readOnly">true</property>
        </user>
    -->
</mycat:server>

Insert picture description here ② schema.xml placement

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">


<!--   1.  设置schema name 要和server.xml 配置的一样     
       2.   配置节点-->

    <schema name="testdb"   sqlMaxLimit="100" dataNode="dn1">
    </schema>


<!--  1.  配置节点 对应的虚拟主机池  
      2.  配置虚拟库 ,这里要对应和实际库一个名字-->
    <dataNode name="dn1" dataHost="localhost1" database="testdb" />



<!--  配置虚拟主机池的读写读写分离方式
1. balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。 
2. balance="1",全部的readHost与stand by writeHost参与select语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且M1与 M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡。 
3. balance="2",所有读操作都随机的在writeHost、readhost上分发。
4. balance="3",所有读请求随机的分发到wiriterHost对应的readhost执行,writerHost不负担读压力 -->
    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
                                  writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
    <!-- 心跳检测 -->
    <heartbeat>select user()</heartbeat>
   
     <!-- 设置读写分离,应为上面选择了balance="1"  ,所以这里最少要有连个写主机 ,主1、主2是写主机 ,从机1、2为主机1的从机。从机3、4为主机2的从机。 -->
    <writeHost host="mysql-master" url="10.8.161.9:3306" user="root" password="aA12345678.">
        
    <readHost host="mysql-slave" url="10.8.161.139:3306" user="root" password="aA12345678." />
    <readHost host="mysql-slave3" url="10.8.161.141:3306" user="root" password="aA12345678." />
           </writeHost>

    <writeHost host="mysql-master2" url="10.8.161.18:3306" user="root" password="aA12345678.">
        <!-- can have multi read hosts -->
    <readHost host="mysql-slave2" url="10.8.161.140:3306" user="root" password="aA12345678." />
    <readHost host="mysql-slave3" url="10.8.161.142:3306" user="root" password="aA12345678." />

           </writeHost>

</dataHost>
</mycat:schema>
  1. Start mycat service
cd  /usr/local/mycat/bin   # 进入mycat的启动bin目录
./mycat                    # 启动mycat
netstat -lntp              # 查看端口,可以看到有8086端口说明配置成功

Insert picture description here
5. Remote connection to mycat virtual library

mysql -uroot -h10.8.161.14 -p'abc123.456' -P 8066  #这里的账号密码是我们在server.xml里配置的

After logging in, you can view testdbthe virtual library we created . When we operate the library, it corresponds to the testdb library of several physical databases.
Insert picture description here

================================================= ================================================= ================================================= ================================================= ================================================= =============================================
Hard browsing and watching, if right You are helpful, please like it (σ゚∀゚)σ…:*☆

Guess you like

Origin blog.csdn.net/qq_26129413/article/details/111888271