美文网首页
Mysql主从复制

Mysql主从复制

作者: xiaojingzhou | 来源:发表于2019-01-22 18:49 被阅读0次

    主数据库(1号机器)

    数据库创建基本环境数据

    mysql> create database a;                                     //创建数据库a
    mysql> create table a.a (id int);                             //创建列表a
    mysql> insert into a.a values (1);                            //添加数据1
    mysql> select * from a.a;                                     //查看数据
    +------+
    | id   |
    +------+
    |    1 |
    +------+
    1 row in set (0.00 sec)
    

    开启二进制文件

    echo -e 'log_bin\nserver-id=3' >> /etc/my.cnf
    

    创建一个复制的授权用户

    grant replication slave, replication client on *.* to 'rep'@'192.168.139.%' identified by 'QianFeng@123';
    

    查看主数据库存储位置

    mysql> show master status\G
    *************************** 1. row ***************************
                 File: MYSQL-bin.000005     //master_log_pos=889
             Position: 889                            //master_log_file='MYSQL-bin.000005'
         Binlog_Do_DB: 
     Binlog_Ignore_DB: 
    Executed_Gtid_Set: 
    1 row in set (0.00 sec)
    

    将主数据库整个数据库备份

    mysqldump -p'QianFeng@123' --all-databases --single-transaction --master-data=2 --flush-logs > `date +%F`-mysql-all.sql
    

    将备份文件发送给从数据库

    scp 2019-10-04-mysql-all.sql master2:/tmp/
    

    查看当前备份到哪个点

    sed -n '22p' 备份的文件名
    -- CHANGE MASTER TO MASTER_LOG_FILE='MYSQL-bin.000004', MASTER_LOG_POS=154;
    

    模拟增加数据

    insert into a.a values (2);
    

    查看当前用户在本数据库的权限

    show grants;
    

    从数据库(2号机器)

    用新授权的账号测试是否能连接主数据库

    目的是为了测试这个用户是否可以登陆,为后面复制做准备

    mysql -hmaster1 -urep -p'QianFeng@123'
    
    

    登陆自己的数据库进行数据导入

    mysql -uroot -p'QianFeng@123'
    

    临时关闭二进制文件

    mysql> set sql_log_bin=0;
    

    导入主数据库的二进制文件

    mysql> source /tmp/2019-10-04-mysql-all.sql;
    

    复制连接主数据库

    mysql> change master to
    master_host='master1',
    master_user='rep',
    master_password='QianFeng@123';
    master_log_file='MYSQL-bin.000004'
    master_log_pos=154;
    

    启动从设备

    mysql> start slave;
    

    查看启动状态

    mysql> show slave status\G
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: master1
                      Master_User: rep
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: MYSQL-bin.000005
              Read_Master_Log_Pos: 1134
                   Relay_Log_File: web3-relay-bin.000003
                    Relay_Log_Pos: 1301
            Relay_Master_Log_File: MYSQL-bin.000005
                 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: 1134
                  Relay_Log_Space: 1508
                  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: 196f050b-e631-11e9-88d1-000c29ea4f01
                 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_TLS_Version: 
           Master_public_key_path: 
            Get_master_public_key: 0
    1 row in set (0.01 sec)
    

    至此部署完成

    查看测试结果

    主数据库插入新的数据
    mysql> insert into a.a values (10);
    Query OK, 1 row affected (0.00 sec)
    mysql> select * from a.a;
    +------+
    | id   |
    +------+
    |    1 |
    |   10 |
    +------+
    2 rows in set (0.01 sec)
    

    在从数据库上查看是否更新成功

    mysql> select * from a.a;
    +------+
    | id   |
    +------+
    |    1 |
    |   10 |
    +------+
    2 rows in set (0.01 sec)
    

    完成

    相关文章

      网友评论

          本文标题:Mysql主从复制

          本文链接:https://www.haomeiwen.com/subject/xuerjqtx.html