MYSQL異步複製

Replication,複製是高可用的基礎,MHA、mycat等中間件的底層都依賴複製原理mysql

master 主實例 slave 從實例sql

分類:默認的異步複製,5.5版本後的半同步複製,5.6版本新增的GTID複製,5.7版本的多源複製,基於組提交的並行複製和加強半同步複製bash

複製方法:1.傳統方法:基於binlog日誌複製 2.GTID:基於事物複製架構

binlog能夠有不一樣的格式:基於語句、基於行數據、混合(行數據複製是默認)異步

下面搭建下常規的異步複製3d

必要條件:server_id在主從之間不一樣;主庫開啓binlog,建議從庫也開啓方便架構擴展日誌

首先編輯my.cnf開啓binlog並設置server_idcode

mysql> show variables like '%log_bin%';
+---------------------------------+---------------------------------------+
| Variable_name                   | Value                                 |
+---------------------------------+---------------------------------------+
| log_bin                         | ON                                    |
| log_bin_basename                | /usr/local/mysql/data/mysql-bin       |
| log_bin_index                   | /usr/local/mysql/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF                                   |
| log_bin_use_v1_row_events       | OFF                                   |
| sql_log_bin                     | ON                                    |
+---------------------------------+---------------------------------------+
6 rows in set (0.00 sec)

mysql> ^DBye
[root@localhost ~]$ cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
log_bin=mysql-bin
server_id=1
[root@localhost ~]$

而後建立主從複製的用戶,以前實驗已經建立了scott用戶,就用這個了,再賦權(由於會密碼會明文保存在slave的master.info因此實際上應該單獨創建個只有複製權限的用戶)server

mysql> grant replication slave on *.* to 'scott'@'%';
Query OK, 0 rows affected (0.00 sec)

表加鎖中止修改中間件

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)

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

mysql> drop table tmp;
ERROR 122

而後將主庫全量備份一個傳給備庫,能夠直接data目錄打包過去(innodb不推薦)或者直接mysqldump

[root@localhost ~]$ mysqldump --all-databases -uroot -pmysql --master-data > dbdump.db
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]$ ls
dbdump.db
[root@localhost ~]$ scp dbdump.db 192.0.1.11:~
The authenticity of host '192.0.1.11 (192.0.1.11)' can't be established.
ECDSA key fingerprint is SHA256:pmk8Q9EnT+TugRZ5rb2bc0GP20ZV3LkeuXP/Jrw5tbs.
ECDSA key fingerprint is MD5:13:d1:4a:14:3a:4d:fd:33:56:15:f9:1f:2f:44:87:2c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.0.1.11' (ECDSA) to the list of known hosts.
root@192.0.1.11's password: 
dbdump.db                                                                                                    100%  775KB  47.6MB/s   00:00

而後在備庫中應用

mysql> source dbdump.db

從庫中配置主庫信息

mysql> change master to
    -> master_host='192.0.1.10',
    -> master_user='scott',
    -> master_password='tiger',
    -> master_port=3306,
    -> master_log_file='mysql-bin.000003',
    ->  master_log_pos=556;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql>

開啓同步

mysql> start slave ;
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.0.1.10
                  Master_User: scott
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 556
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             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: 556
              Relay_Log_Space: 531
              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: 531fa6d1-627f-11e9-8dc7-000c297887a1
             Master_Info_File: /data/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>

釋放主庫鎖定

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

好了驗證下,主庫中插入記錄,從庫馬上就有了

imageimage

相關文章
相關標籤/搜索