Mycat 主从切换(mysql)

centos7
mysql5.7
服务器信息如下(实验环境,关闭机器的iptables防火墙和selinux):

服务器主机名 ip 说明
Mycat-node 10.0.1.43 mycat服务器,
Mysql-node1 10.0.1.44 物理数据库Master数据库
Mysql-node2 10.0.1.44 物理数据库Slave数据库

三台机器分布修改主机名,并做hosts绑定
vim /etc/hosts
10.0.1.43 Mycat-node
10.0.1.44 Mysql-node1
10.0.1.45 Mysql-node2

Mysql主从复制(两个节点的mysql登录用户名和密码都是root/123456)
首先是Mysql-node1主节点配置操作
[root@Mysql-node1 ~]# cp /etc/my.cnf /etc/my.cnf.back
[root@Mysql-node1 ~]# vim /etc/my.cnf

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

server-id=1
log-bin=mysql-bin
binlog-ignore-db = mysql,information_schema
sync_binlog = 1     
binlog_checksum = none 
binlog_format = mixed


log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
:wq

[root@Mysql-node1 ~]# systemctl restart mysqld

登录mysql,授予slave从机复制权限

[root@Mysql-node1 ~]# mysql -uroot -p'123456'
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

mysql>  grant replication slave,replication client on *.* to slave@'10.0.1.45' identified by "slave@123";
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH TABLES WITH READ LOCK;   
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000001 |      734 |              | mysql,information_schema |                   |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)

接着是slave从节点操作
[root@Mysql-node2 ~]# cp /etc/my.cnf /etc/my.cnf.back
[root@Mysql-node2 ~]# vim /etc/my.cnf

 1 # For advice on how to change settings please see
  2 # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
  3 
  4 [mysqld]
  5 #
  6 # Remove leading # and set to the amount of RAM for the most important data
  7 # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
  8 # innodb_buffer_pool_size = 128M
  9 #
 10 # Remove leading # to turn on a very important data integrity option: logging
 11 # changes to the binary log between backups.
 12 # log_bin
 13 #
 14 # Remove leading # to set options mainly useful for reporting servers.
 15 # The server defaults are faster for transactions and fast SELECTs.
 16 # Adjust sizes as needed, experiment to find the optimal values.
 17 # join_buffer_size = 128M
 18 # sort_buffer_size = 2M
 19 # read_rnd_buffer_size = 2M
 20 datadir=/var/lib/mysql
 21 socket=/var/lib/mysql/mysql.sock
 22 
 23 # Disabling symbolic-links is recommended to prevent assorted security risks
 24 symbolic-links=0
 25 
 26 server-id=2
 27 log-bin=mysql-bin
 28 replicate-ignore-db=mysql 
 29 slave-skip-errors = all
 30 
 31 
 32 
 33 log-error=/var/log/mysqld.log
 34 pid-file=/var/run/mysqld/mysqld.pid
 :wq

[root@Mysql-node2 ~]# systemctl restart mysqld
登录slave节点的mysql,进行主从同步设置

[root@Mysql-node2 ~]# mysql -uroot -p'123456'
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change  master to master_host='10.0.1.44', \
   -> master_user='slave', \
   -> master_password='slave@123', \
   -> master_log_file='mysql-bin.000001', \
   -> master_log_pos=734;
Query OK, 0 rows affected, 2 warnings (0.30 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql>  show slave status \G;
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                 Master_Host: 10.0.1.44
                 Master_User: slave
                 Master_Port: 3306
               Connect_Retry: 60
             Master_Log_File: mysql-bin.000001
         Read_Master_Log_Pos: 734
              Relay_Log_File: Mysql-node2-relay-bin.000002
               Relay_Log_Pos: 320
       Relay_Master_Log_File: mysql-bin.000001
            Slave_IO_Running: Yes     #状态为YES主从复制环境已经OK ,不是YES要找原因
           Slave_SQL_Running: Yes		#状态为YES主从复制环境已经OK
             Replicate_Do_DB: 
         Replicate_Ignore_DB: mysql
          Replicate_Do_Table: 
      Replicate_Ignore_Table: 
     Replicate_Wild_Do_Table: 
 Replicate_Wild_Ignore_Table: 
                  Last_Errno: 0
                  Last_Error: 
                Skip_Counter: 0
         Exec_Master_Log_Pos: 734
             Relay_Log_Space: 533
             Until_Condition: None
              Until_Log_File: 
               Until_Log_Pos: 0
          Master_SSL_Allowed: No
          Master_SSL_CA_File: 
          Master_SSL_CA_Path: 
             Master_SSL_Cert: 
           Master_SSL_Cipher: 
              Master_SSL_Key: 
       Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
               Last_IO_Errno: 0
               Last_IO_Error: 
              Last_SQL_Errno: 0
              Last_SQL_Error: 
 Replicate_Ignore_Server_Ids: 
            Master_Server_Id: 1
                 Master_UUID: d04f522b-6c79-11e9-bf10-000c2970f7f9
            Master_Info_File: /var/lib/mysql/master.info
                   SQL_Delay: 0
         SQL_Remaining_Delay: NULL
     Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
          Master_Retry_Count: 86400
                 Master_Bind: 
     Last_IO_Error_Timestamp: 
    Last_SQL_Error_Timestamp: 
              Master_SSL_Crl: 
          Master_SSL_Crlpath: 
          Retrieved_Gtid_Set: 
           Executed_Gtid_Set: 
               Auto_Position: 0
        Replicate_Rewrite_DB: 
                Channel_Name: 
          Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

在Mysql-node1主节点上操作

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

在Mysql-node1上创建数据库
Mycat 主从切换(mysql)_第1张图片
Mycat 主从切换(mysql)_第2张图片
Mycat 主从切换(mysql)_第3张图片
Mycat中间件安装、配置和测试(Mycat-node节点机器上操作)
Mycat安装

mkdir /test
cd /test
[root@Mycat-node test]# wget http://dl.mycat.io/1.6.6.1/Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz
[root@Mycat-node test]# tar zxf Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz 
[root@Mycat-node test]# ll
total 198460
-rw-r--r-- 1 root root 185646832 May  2 09:12 jdk-8u181-linux-x64.tar.gz
drwxr-xr-x 7 root root        85 May  7 21:19 mycat
-rw-r--r-- 1 root root  17544969 Nov  7 12:25 Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz
-rw-r--r-- 1 root root     25680 Apr 27  2017 mysql57-community-release-el7-11.noarch.rpm
[root@Mycat-node test]# mv mycat/ /usr/local/
[root@Mycat-node test]# cd /usr/local/
[root@Mycat-node local]# ll
total 0
drwxr-xr-x. 2 root root  6 Apr 11  2018 bin
drwxr-xr-x. 2 root root  6 Apr 11  2018 etc
drwxr-xr-x. 2 root root  6 Apr 11  2018 games
drwxr-xr-x. 2 root root  6 Apr 11  2018 include
drwxr-xr-x  3 root root 26 May  2 09:20 jdk
drwxr-xr-x. 2 root root  6 Apr 11  2018 lib
drwxr-xr-x. 2 root root  6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root  6 Apr 11  2018 libexec
drwxr-xr-x  7 root root 85 May  7 21:19 mycat
drwxr-xr-x. 2 root root  6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root 49 Apr 29 00:05 share
drwxr-xr-x. 2 root root  6 Apr 11  2018 src
[root@Mycat-node local]# chmod 755 -R mycat/
[root@Mycat-node local]# ll
total 0
drwxr-xr-x. 2 root root  6 Apr 11  2018 bin
drwxr-xr-x. 2 root root  6 Apr 11  2018 etc
drwxr-xr-x. 2 root root  6 Apr 11  2018 games
drwxr-xr-x. 2 root root  6 Apr 11  2018 include
drwxr-xr-x  3 root root 26 May  2 09:20 jdk
drwxr-xr-x. 2 root root  6 Apr 11  2018 lib
drwxr-xr-x. 2 root root  6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root  6 Apr 11  2018 libexec
drwxr-xr-x  7 root root 85 May  7 21:19 mycat
drwxr-xr-x. 2 root root  6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root 49 Apr 29 00:05 share
drwxr-xr-x. 2 root root  6 Apr 11  2018 src

Mycat相关配置

[root@Mycat-node conf]# cd /usr/local/mycat/conf
[root@Mycat-node conf]# ll
total 104
-rwxr-xr-x 1 root root   88 Oct 31  2018 autopartition-long.txt
-rwxr-xr-x 1 root root   48 Oct 31  2018 auto-sharding-long.txt
-rwxr-xr-x 1 root root   62 Oct 31  2018 auto-sharding-rang-mod.txt
-rwxr-xr-x 1 root root  334 Oct 31  2018 cacheservice.properties
-rwxr-xr-x 1 root root 3244 Oct 31  2018 dbseq.sql
-rwxr-xr-x 1 root root  439 Oct 31  2018 ehcache.xml
-rwxr-xr-x 1 root root 2132 Oct 31  2018 index_to_charset.properties
-rwxr-xr-x 1 root root 1253 Oct 31  2018 log4j2.xml
-rwxr-xr-x 1 root root  178 Oct 31  2018 migrateTables.properties
-rwxr-xr-x 1 root root  262 Oct 31  2018 myid.properties
-rwxr-xr-x 1 root root   15 Oct 31  2018 partition-hash-int.txt
-rwxr-xr-x 1 root root  102 Oct 31  2018 partition-range-mod.txt
-rwxr-xr-x 1 root root 4678 Oct 31  2018 rule.xml
-rwxr-xr-x 1 root root 4219 Oct 31  2018 schema.xml
-rwxr-xr-x 1 root root  413 Oct 31  2018 sequence_conf.properties
-rwxr-xr-x 1 root root   75 Oct 31  2018 sequence_db_conf.properties
-rwxr-xr-x 1 root root   27 Oct 31  2018 sequence_distributed_conf.properties
-rwxr-xr-x 1 root root   51 Oct 31  2018 sequence_time_conf.properties
-rwxr-xr-x 1 root root 4851 Oct 31  2018 server.xml
-rwxr-xr-x 1 root root   16 Oct 31  2018 sharding-by-enum.txt
-rwxr-xr-x 1 root root 4299 Oct 31  2018 wrapper.conf
drwxr-xr-x 2 root root 4096 May  7 21:19 zkconf
drwxr-xr-x 2 root root   36 May  7 21:19 zkdownload
[root@Mycat-node conf]# cp server.xml server.xml.back
[root@Mycat-node conf]# cp schema.xml schema.xml.back
[root@Mycat-node conf]# vim server.xml
        <user name="galaxis" defaultAccount="true">
                <property name="password">123456</property>
                <property name="schemas">mycat</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="user">
                <property name="password">user</property>
                <property name="schemas">TESTDB</property>
                <property name="readOnly">true</property>
        </user>
-->

实现主从自动切换的schema.xml配置:MySQL写节点宕机后自动切换到备用节点(也就是把从机也配置成writeHosts):
[root@Mycat-node conf]# vim schema.xml




 
        
        
         

 
    
        show slave status
        
             
             
        
        
        
    
 

[root@Mycat-node conf]# cd ../bin/
[root@Mycat-node bin]# ll
total 384
-rwxr-xr-x 1 root root   3567 Oct 31  2018 dataMigrate.sh
-rwxr-xr-x 1 root root   1225 Oct 31  2018 init_zk_data.sh
-rwxr-xr-x 1 root root  15714 Oct 31  2018 mycat
-rwxr-xr-x 1 root root   2941 Oct 31  2018 rehash.sh
-rwxr-xr-x 1 root root   2496 Oct 31  2018 startup_nowrap.sh
-rwxr-xr-x 1 root root 140198 Oct 31  2018 wrapper-linux-ppc-64
-rwxr-xr-x 1 root root  99401 Oct 31  2018 wrapper-linux-x86-32
-rwxr-xr-x 1 root root 111027 Oct 31  2018 wrapper-linux-x86-64
[root@Mycat-node bin]# ./mycat start
Starting Mycat-server...
[root@Mycat-node bin]# ./mycat status
Mycat-server is running (7700).

参考:https://www.cnblogs.com/kevingrace/p/9365840.html

你可能感兴趣的:(Mycat 主从切换(mysql))