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中間件安裝、配置和測試(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

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
 
        <schema name="mycat" checkSQLschema="true" sqlMaxLimit="100" dataNode="ga_announce,ga_area,ga_charge,ga_configure,ga_error,ga_flash_inside_state,ga_flashscanner,ga_flashstate,ga_inbound,ga_inbound_request,ga_levels,ga_lift,ga_locations,ga_message,ga_messagereceive,ga_outbound,ga_position,ga_script,ga_security,ga_servicestation,ga_task,ga_task_semiauto,web_config,web_download,web_info,web_user">
        </schema>
         
<dataNode name="ga_announce,ga_area,ga_charge,ga_configure,ga_error,ga_flash_inside_state,ga_flashscanner,ga_flashstate,ga_inbound,ga_inbound_request,ga_levels,ga_lift,ga_locations,ga_message,ga_messagereceive,ga_outbound,ga_position,ga_script,ga_security,ga_servicestation,ga_task,ga_task_semiauto,web_config,web_download,web_info,web_user" dataHost="Mycat-node" database="717" />
 
    <dataHost name="Mycat-node" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>show slave status</heartbeat>
        <writeHost host="Mysql-node1" url="10.0.1.44:3306" user="root" password="123456">
             <readHost host="Mysql-node2" url="10.0.1.45:3306" user="root" password="123456">
             </readHost>
        </writeHost>
        <writeHost host="Mysql-node2" url="10.0.1.45:3306" user="root" password="123456">
        </writeHost>
    </dataHost>
 
</mycat:schema>
[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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章