主从复制总结

原理:建立两个数据库,库中生成两个线程(i/o ,SQL)。
主库中i/o线程会将修改的数据保存在二进制bin-log文件中,而从库也会开启i/o线程从而将主库上bin-log日志文件写到本地的Realy log(中继日志)里面。
从服务器上面同时开启一个 SQL 线程,定时检查 Realy log,如果有更新立即把更新的内容在本机的数据库上面执行一遍。从而最后主从库达成一致。
步骤:
主服务器(192.168.140.145):
1.进入mysql配置文件中在【mysqld】添加如下内容

vim /etc/my.cnf
[mysqld]
bin-log=/var/log/mysql/mysql-bin
sever-id=1
mkdir  /var/log/mysql
chown  -R mysql.mysql /var/log/mysql
systemctl restart mysqld

3.建立个用户,并授权

mysqld -uroot -pWww.2.com
mysql> grant replication slave on *.* 'wdx'@'%' identified by 'Www.2.com';

4.导出数据,并远程拷贝到从服务器192.168.140.146

mysqldump --all-databases --master-data=1 --user=root --password=Www.2.com >dump.sql
scp dump.sql [email protected]:/root/

从服务器(192.168.140.146):
用新建用户登入数据库测试

mysql -uwdx -pWww.2.com -h192.168.140.145

1.导入数据库

mysql -uroot -pWww.2.com < dump.sql
mysql -uroot -pWww.2.com
mysql> change master to
    ->master_host='192.168.140.145',
    ->master_user='wdx',
    ->master_password='Www.2.com',
    ->master_log_file=''mysql-bin.000001'
    -> master_log_pos=600;
mysql>start slave;
mysql>show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.140.145
                  Master_User: wdx
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 600
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             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: 600
              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: 23be8b2a-2ddd-11e9-b5ba-000c29e25853
             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)


你可能感兴趣的:(主从复制总结)