zoukankan      html  css  js  c++  java
  • mysql 主从同步

    1. master机器

    1.1 配置文件
    [mysqld]
    basedir=/qqc_data/mysql
    datadir=/qqc_data/mysql/data
    socket=/tmp/mysql.sock
    user=mysql
    log_error=/var/log/mysql.log
    log_bin=/qqc_data/mysql/data/mysql-bin  # 从库根据这里的日志文件同步
    server-id=100 # 与从库不能重复
    max_allowed_packet = 500M
    binlog-do-db = tb_test # 要同步的库名
    #skip-networking
    innodb_flush_log_at_trx_commit=1
    sync_binlog=1
    
    # 重启mysql
    
    1.2 创建用户
    GRANT REPLICATION SLAVE ON *.* TO 'rep'@'%' IDENTIFIED BY 'replpassword';
    
    到slave 机器上测试:
    [root@localhost ~]# mysql -h47.102.138.171 -urep -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 7
    Server version: 5.7.20-log MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    mysql> 
    
    
    1.3 获取bin-log和位置信息
    # 将所有数据刷进磁盘,并阻塞所有的写入操作
    # 如果master本来已存在数据,先将数据导出,执行此命令
    mysql> FLUSH TABLES WITH READ LOCK;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> SHOW MASTER STATUS;
    +------------------+----------+--------------+------------------+-------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000029 |      154 | tb_test      |                  |                   |
    +------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    
    # 主库数据备份 (主数据库为空库时,忽略此操作)
    mysqldump -h**** -uroot -p***** test > /tmp/test.sql
    # 将数据导入到从库
    source /tmp/test.sql
    
    # 获取文件位置,bin-log信息释放锁
    mysql> unlock tables;
    Query OK, 0 rows affected (0.00 sec)
    

    2. slave 机器配置

    2.1 配置文件
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    symbolic-links=0
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    server-id=666 # 不能与主库一致
    replicate-do-db = tb_test
    
    
    # 重启mysql
    [root@localhost ~]# systemctl restart mysqld
    
    2.2 从库复制
    mysql> stop slave;
    Query OK, 0 rows affected (0.01 sec)
    mysql> change master to
        -> master_host="47.102.138.171",master_user="rep",master_password="***",master_port=3306,
        -> master_log_file="mysql-bin.000029",master_log_pos=154;
    Query OK, 0 rows affected, 2 warnings (0.16 sec)
    
    mysql> start  slave;
    Query OK, 0 rows affected (0.06 sec)
    
    
    2.3 查看salve库状态
    mysql> show slave statusG;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 47.102.138.171
                      Master_User: rep
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000029
              Read_Master_Log_Pos: 154
                   Relay_Log_File: localhost-relay-bin.000002
                    Relay_Log_Pos: 320
            Relay_Master_Log_File: mysql-bin.000029
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: tb_test
              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: 154
                  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: 100
                      Master_UUID: 5ee1aade-d617-11ea-844c-00163e0c1278
                 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)
    
    注: Slave_IO_Running 与 Slave_SQL_Running 为yes即成功
    
    2.4 数据测试
    到主库新建表:
    mysql> CREATE TABLE `logs` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id', `type_id` VARCHAR(64) NOT NULL default '' COMMENT '操作类型id', `log_type` int(11) NOT NULL default 0 COMMENT '日志类型', `role` int(11) NOT NULL default 0 COMMENT '角色', `operator_id` int(11) NOT NULL default 0 COMMENT '操作人id', `operator` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '操作人', `content` VARCHAR(256) NOT NULL default ''  COMMENT '内容', `state` int(6) NOT NULL default 0 COMMENT '记录状态, 0-有效; 1-临时记录; 9-删除', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,     `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id),     KEY (type_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='日志表';
    Query OK, 0 rows affected (0.02 sec)
    mysql> show tables;
    +-------------------+
    | Tables_in_tb_test |
    +-------------------+
    | logs              |
    +-------------------+
    1 row in set (0.00 sec)
    
    从库查看:
    mysql> use tb_test;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> show tables;
    +-------------------+
    | Tables_in_tb_test |
    +-------------------+
    | logs              |
    +-------------------+
    1 row in set (0.00 sec)
    mysql> select * from logs;
    +----+---------+----------+------+-------------+----------+--------------------+-------+---------------------+---------------------+
    | id | type_id | log_type | role | operator_id | operator | content            | state | create_time         | update_time         |
    +----+---------+----------+------+-------------+----------+--------------------+-------+---------------------+---------------------+
    |  1 | 22      |        4 |    1 |          33 | test     | 测试主从同步       |     0 | 2020-09-01 11:15:33 | 2020-09-01 11:15:33 |
    +----+---------+----------+------+-------------+----------+--------------------+-------+---------------------+---------------------+
    1 row in set (0.00 sec)
    
    
  • 相关阅读:
    常见WINDOWS运行命令
    CSDN Blog 之七宗罪
    常见进程大全
    开始→运行→输入的命令集锦(网上搜来的)
    20200527:SpringCloud用了那些组件?分布式追踪链怎么做的?熔断器工作原理?
    20200520:分库分表后如何迁移?
    20200523:如何实现并发限流
    20200521:es底层读写原理?倒排索引原理?
    20200525:MQ应用场景、Kafka和rabbit区别?kafka为什么支撑高并发? 来自
    20200519:催收核心业务是什么?
  • 原文地址:https://www.cnblogs.com/quqinchao/p/13595582.html
Copyright © 2011-2022 走看看