MYSQL异步复制

Replication,复制是高可用的基础,MHA、mycat等中间件的底层都依赖复制原理

master 主实例 slave 从实例

分类:默认的异步复制,5.5版本后的半同步复制,5.6版本新增的GTID复制,5.7版本的多源复制,基于组提交的并行复制和增强半同步复制

复制方法:1.传统方法:基于binlog日志复制 2.GTID:基于事物复制

binlog可以有不同的格式:基于语句、基于行数据、混合(行数据复制是默认)

下面搭建下常规的异步复制

必要条件:server_id在主从之间不同;主库开启binlog,建议从库也开启方便架构扩展

首先编辑my.cnf开启binlog并设置server_id

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所以实际上应该单独建立个只有复制权限的用户)

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

posted @ 2019-08-15 09:07  九命猫幺  阅读(895)  评论(0编辑  收藏  举报