国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL 5.7搭建多源復制

2024-07-24 12:34:31
字體:
來源:轉載
供稿:網友
  多源復制可以讓多個主節點同時并行進行復制到一個從節點上。一個slave為每個master創建一個復制通道。
  至少需要兩臺主庫和一臺從庫。
  多源復制中的主庫,可以配置成基于全局事務標準(GTID)的復制,或者基于二進制日志的復制。
 
  IP規劃
 
  主庫01192.168.174.201
  主庫02192.168.174.202
  從庫192.168.174.203
 
  1. 在多源復制的從庫中,需要基于表的repositories,和基于文件的repositories不兼容。
  在從庫上面操作
  可以將下面參數添加到參數文件中
  master-info-repository=TABLE
  relay-log-info-repository=TABLE
 
  master_info_repository
  決定包含master狀態和連接信息的slave日志,是以文件格式(master.info),還是以表格式(mysql.slave_master_info)存在。
  當沒有復制線程執行的時候,可以改變這個參數的值。
  這個參數還會對sync_master_info系統參數有直接的影響。
 
  relay_log_info_repository
  這個參數決定寫到文件(relay-log.info)或表(mysql.slave_relay_log_info)中的中繼日志slave節點的位置。只有當沒有復制線程執行時,才可修改這個參數的值。
  這個參數用于存放中繼日志的信息。默認是文件格式,文件的默認名是relay-log.info。
  如果是TABLE格式,日志信息會寫到mysql.slave_relay_log_info。
 
  動態修改,使用下面命令
  STOP SLAVE;
  SET GLOBAL master_info_repository = 'TABLE';
  STOP SLAVE;
  SET GLOBAL relay_log_info_repository = 'TABLE';
 
  mysql> show global variables like '%repositor%';
  +---------------------------+-------+
  | Variable_name             | Value |
  +---------------------------+-------+
  | master_info_repository    | FILE  |
  | relay_log_info_repository | FILE  |
  +---------------------------+-------+
  2 rows in set (0.03 sec)
 
  mysql> SET GLOBAL master_info_repository = 'TABLE';
  Query OK, 0 rows affected (0.00 sec)
 
  mysql> SET GLOBAL relay_log_info_repository = 'TABLE';
  ERROR 1766 (HY000): Unknown error 1766
 
  mysql> stop slave;
  Query OK, 0 rows affected, 1 warning (0.00 sec)
 
  mysql> show global variables like '%repositor%';
  +---------------------------+-------+
  | Variable_name             | Value |
  +---------------------------+-------+
  | master_info_repository    | TABLE |
  | relay_log_info_repository | TABLE |
  +---------------------------+-------+
  2 rows in set (0.01 sec)
 
  編輯從庫的其他配置文件
  [root@localhost 5505]# vim /mysql_data/cnf/my.cnf
  # Log
  server-id = 300
  log-bin = /mysql_log/binlog/mysql-bin
  relay-log = /mysql_log/binlog/product-relay-bin
  relay-log-index = /mysql_log/binlog/product-relay-index
 
  之后重啟數據庫,使得參數生效
 
  2. 修改主庫1和主庫2參數文件,創建復制用戶,創建測試數據
  編輯主庫的配置文件,注意,每個庫的server-id不能相同
  [root@localhost install]# vim /etc/my.cnf
  # Log
  server-id = 100
  log-bin = /log/binlog/mysql-bin
 
  之后重啟數據庫,使得參數生效
 
  創建復制用戶
  mysql> grant replication slave on *.* to 'repl'@'192.168.174.%' identified by 'repl';
  Query OK, 0 rows affected, 1 warning (0.06 sec)
  mysql> select version();
  +---------------+
  | version()     |
  +---------------+
  | 5.7.17-11-log |
  +---------------+
  1 row in set (0.00 sec)
 
  創建測試數據
  主庫1
  mysql> create database sale;
  Query OK, 1 row affected (0.06 sec)
 
  mysql> use sale;
  Database changed
  mysql> create table sale_record(id int);
  Query OK, 0 rows affected (0.16 sec)
 
  mysql> insert into sale_record values(10),(20);
  Query OK, 2 rows affected (0.95 sec)
  Records: 2  Duplicates: 0  Warnings: 0
 
  mysql> commit;
  Query OK, 0 rows affected (0.01 sec)
 
  主庫2
  mysql> use market;
  Database changed
  mysql> create table market_record(id int)
      -> ;
  Query OK, 0 rows affected (0.04 sec)
 
  mysql> insert into market_record values (100),(200);
  Query OK, 2 rows affected (0.25 sec)
  Records: 2  Duplicates: 0  Warnings: 0
 
  mysql> commit;
  Query OK, 0 rows affected (0.01 sec)
 
  3. 備份主庫1和主庫2
  主庫1
  [root@MySQL01 mysql_software_57]# bin/mysqldump -uroot -p'root' -h 127.0.0.1 -q --single-transaction --master-data=2 -B sale > /tmp/20171211_sale.sql
 
  主庫2
  [root@localhost mysql_software_57]# bin/mysqldump -uroot -p'root' -S /mysql_data_57/mysql.sock -q --single-transaction --master-data=2 -B market > /tmp/20171211_market.sql
 
  將主庫1和主庫2上面的備份文件拷貝到從庫上面,進行恢復
  在從庫上面操作
  [root@MySQL03 mysql_software_57]# bin/mysql -uroot -p'root' -h 127.0.0.1 < /tmp/20171211_sale.sql
  mysql: [Warning] Using a password on the command line interface can be insecure.
  [root@MySQL03 mysql_software_57]# bin/mysql -uroot -p'root' -h 127.0.0.1 < /tmp/20171211_market.sql
  mysql: [Warning] Using a password on the command line interface can be insecure.
 
  4. 搭建多源復制
  搭建到主庫1的復制,將通道起名為master-1
  mysql> change master to
      ->      master_host='192.168.174.201',
      ->      master_port=3306,
      ->      master_user='repl',
      ->      master_password='repl',
      ->      master_log_file='mysql-bin.000017',
      ->      master_log_pos=1209
      ->      FOR CHANNEL 'master-1';
  Query OK, 0 rows affected, 1 warning (0.04 sec)
 
  mysql> show slave status/G
  *************************** 1. row ***************************
                 Slave_IO_State:
                    Master_Host: 192.168.174.201
                    Master_User: repl
                    Master_Port: 3306
                  Connect_Retry: 60
                Master_Log_File: mysql-bin.000017
            Read_Master_Log_Pos: 1209
                 Relay_Log_File: mysqld-relay-bin-master@002d1.000001
                  Relay_Log_Pos: 4
          Relay_Master_Log_File: mysql-bin.000017
               Slave_IO_Running: No
              Slave_SQL_Running: No
                Replicate_Do_DB:
            Replicate_Ignore_DB:
             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: 1209
                Relay_Log_Space: 154
                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: NULL
  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: 0
                    Master_UUID:
               Master_Info_File: mysql.slave_master_info
                      SQL_Delay: 0
            SQL_Remaining_Delay: NULL
        Slave_SQL_Running_State:
             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-1
             Master_TLS_Version:
  1 row in set (0.00 sec)
 
  啟動通道master-1的IO和SQL線程
  mysql> START SLAVE IO_THREAD FOR CHANNEL 'master-1';
  Query OK, 0 rows affected (0.00 sec)
 
  mysql> START SLAVE SQL_THREAD FOR CHANNEL 'master-1';
  Query OK, 0 rows affected (0.01 sec)
 
  mysql> show slave status/G
  *************************** 1. row ***************************
                 Slave_IO_State: Waiting for master to send event
                    Master_Host: 192.168.174.201
                    Master_User: repl
                    Master_Port: 3306
                  Connect_Retry: 60
                Master_Log_File: mysql-bin.000017
            Read_Master_Log_Pos: 1209
                 Relay_Log_File: mysqld-relay-bin-master@002d1.000002
                  Relay_Log_Pos: 320
          Relay_Master_Log_File: mysql-bin.000017
               Slave_IO_Running: Yes
              Slave_SQL_Running: Yes
                Replicate_Do_DB:
            Replicate_Ignore_DB:
             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: 1209
                Relay_Log_Space: 541
                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: 203fe772-177e-11e7-b15c-000c296b3b20
               Master_Info_File: mysql.slave_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-1
             Master_TLS_Version:
  1 row in set (0.00 sec)
 
  啟動通道master-2的IO線程和SQL線程
  mysql> change master to
      ->      master_host='192.168.174.202',
      ->      master_port=3306,
      ->      master_user='repl',
      ->      master_password='repl',
      ->      master_log_file='mysql-bin.000014',
      ->      master_log_pos=1239
      ->      FOR CHANNEL 'master-2';
  Query OK, 0 rows affected, 1 warning (0.02 sec)
 
  mysql> START SLAVE IO_THREAD FOR CHANNEL 'master-2';
  Query OK, 0 rows affected (0.00 sec)
 
  mysql> START SLAVE SQL_THREAD FOR CHANNEL 'master-2';
  Query OK, 0 rows affected (0.01 sec)
 
  mysql> show slave status/G
  *************************** 1. row ***************************
                 Slave_IO_State: Waiting for master to send event
                    Master_Host: 192.168.174.201
                    Master_User: repl
                    Master_Port: 3306
                  Connect_Retry: 60
                Master_Log_File: mysql-bin.000017
            Read_Master_Log_Pos: 1481
                 Relay_Log_File: mysqld-relay-bin-master@002d1.000002
                  Relay_Log_Pos: 592
          Relay_Master_Log_File: mysql-bin.000017
               Slave_IO_Running: Yes
              Slave_SQL_Running: Yes
                Replicate_Do_DB:
            Replicate_Ignore_DB:
             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: 1481
                Relay_Log_Space: 813
                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: 203fe772-177e-11e7-b15c-000c296b3b20
               Master_Info_File: mysql.slave_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-1
             Master_TLS_Version:
  *************************** 2. row ***************************
                 Slave_IO_State: Waiting for master to send event
                    Master_Host: 192.168.174.202
                    Master_User: repl
                    Master_Port: 3306
                  Connect_Retry: 60
                Master_Log_File: mysql-bin.000014
            Read_Master_Log_Pos: 1239
                 Relay_Log_File: mysqld-relay-bin-master@002d2.000002
                  Relay_Log_Pos: 320
          Relay_Master_Log_File: mysql-bin.000014
               Slave_IO_Running: Yes
              Slave_SQL_Running: Yes
                Replicate_Do_DB:
            Replicate_Ignore_DB:
             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: 1239
                Relay_Log_Space: 541
                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: 2
                    Master_UUID: 2efd664c-177f-11e7-8323-000c29fcf2cd
               Master_Info_File: mysql.slave_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-2
             Master_TLS_Version:
  2 rows in set (0.01 sec)
 
  進行測試
  在主庫1上面插入數據
  mysql> insert into sale_record values(30),(40),(50);
  Query OK, 3 rows affected (0.00 sec)
  Records: 3  Duplicates: 0  Warnings: 0
 
  mysql> commit;
  Query OK, 0 rows affected (0.01 sec)
 
  在主庫2上面插入數據
  mysql> insert into market_record values(300),(400),(500);
  Query OK, 3 rows affected (0.01 sec)
  Records: 3  Duplicates: 0  Warnings: 0
 
  mysql> commit;
  Query OK, 0 rows affected (0.01 sec)
 
  在從庫上面進行查詢
  mysql> select * from sale.sale_record;
  +------+
  | id   |
  +------+
  |   10 |
  |   20 |
  |   30 |
  |   40 |
  |   50 |
  +------+
  5 rows in set (0.00 sec)
 
  mysql> select * from market.market_record;
  +------+
  | id   |
  +------+
  |  100 |
  |  200 |
  |  300 |
  |  400 |
  |  500 |
  +------+
  5 rows in set (0.00 sec)
  常用命令
 
  啟動多源復制
 
  啟動所有配置的復制通道的IO線程
  mysql> start slave io_thread;
  Query OK, 0 rows affected (0.01 sec)
 
  啟動所有配置的復制通道的SQL線程
  mysql> start slave sql_thread;
  Query OK, 0 rows affected (0.01 sec)
 
  啟動指定的復制通道
  mysql> start slave io_thread for channel 'master-1';
  Query OK, 0 rows affected (0.00 sec)
 
  停止多源復制
 
  停止所有配置的復制通道的IO線程
  mysql> stop slave io_thread;
  Query OK, 0 rows affected (0.01 sec)
 
  停止所有配置的復制通道的SQL線程
  mysql> stop slave sql_thread;
  Query OK, 0 rows affected (0.00 sec)
  停止指定的復制通道
  mysql> stop slave sql_thread for channel 'master-1';
  Query OK, 0 rows affected, 1 warning (0.00 sec)
  重置多源復制的從庫
 
  重置所有配置的復制通道
  RESET SLAVE;
 
  重置指定的通道
  RESET SLAVE FOR CHANNEL 'master-1';

(編輯:武林網)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武穴市| 盐津县| 诏安县| 南木林县| 澎湖县| 固镇县| 张北县| 正定县| 陆河县| 嵊州市| 南部县| 仁布县| 册亨县| 搜索| 滦南县| 武冈市| 武汉市| 铁力市| 克什克腾旗| 绥滨县| 宁晋县| 开平市| 马鞍山市| 廊坊市| 南汇区| 钟祥市| 西华县| 五华县| 恩平市| 钦州市| 竹溪县| 仙游县| 杭州市| 彩票| 平南县| 前郭尔| 马山县| 都江堰市| 玉环县| 东明县| 合阳县|