zoukankan      html  css  js  c++  java
  • Mysql主从复制

    1、简介
      MySQL支持单双向、链式级联、异步复制。在复制过程中,一个服务器充当主服务器(Master),而一个或多个其它服务器充当从服务器(Slave)。
    如果设置了链式级联复制,那么,从(slave)服务器本身除了充当从服务器外,也会同时充当其下面的从服务器的主服务器。链式级联复制类似A->B->C->D的复制形式。

    生产环境建议:
    更新操作在主库,从库的用户仅授权select读权限,或在my.cnf配置文件中加read-only参数来确保从库只读,当然二者同时操作效果更佳。

    2、原理

      MySQL的主从复制是一个异步复制过程。
      在Master与Slave之间实现整个主从复制过程有三个线程参与完成。其中两个线程(SQL线程和IO线程)在Slave端,另一个线程(IO线程)在Master端。

      实现主从复制前提:必须打开Master端的binlog(MySQL-bin.XXXXX)功能
      整个复制过程实际上就是Slave从Master端获取binlog日志,然后再在Slave自身上以相同顺序执行binlog日志中所记录的各种操作。

    3、环境准备

      Centos7

      主:10.10.129.182

      从:10.10.129.183

    4、修改配置

      主环境修改/etc/my.cnf:

      1)设置server-id值并开启binlog参数

    [mysqld]
    log_bin = mysql-bin
    server_id = 2
    #不同步的数据库,可设置多个
    binlog-ignore-db=information_schmea
    binlog-ignore-db=mysql
    #需要同步的数据库, 默认所有
    # binlog-do-db=test

      2)赋予root用户权限

    grant replication slave on *.* to 'root'@'10.10.129.183' identified by 'password';
    FLUSH PRIVILEGES; 

      3)重启mysql,并查看主信息

    mysql -uroot -p'password'

    MariaDB [(none)]> show master status;
    +------------------+----------+--------------+---------------------------------------------+
    | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+---------------------------------------------+
    | mysql-bin.000007 | 1038976 | | information_schema,performance_schema,mysql |
    +------------------+----------+--------------+---------------------------------------------+

      主环境修改/etc/my.cnf:

      1)设置server-id值并开启binlog参数

    [mysqld]
    log-bin=mysql-bin
    server-id=3
    binlog-ignore-db=information_schema
    binlog-ignore-db=mysql#与主库配置一直
    # replicate-do-db=test

      2)重启mysql,并进行相关设置

    mysql -u root -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or g.
    Your MariaDB connection id is 507
    Server version: 10.2.9-MariaDB-log MariaDB Server
    
    Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    MariaDB [(none)]> stop slave;Ctrl-C -- exit!
    Aborted
    [root@mgt129183 nginx]# mysql -u root -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or g.
    Your MariaDB connection id is 508
    Server version: 10.2.9-MariaDB-log MariaDB Server
    
    Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    MariaDB [(none)]> stop slave;
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> change master to master_host='10.10.129.182',master_user='root',master_password='password',master_log_file='mysql-bin.000007'(与主节点对应), master_log_pos=1038976(与主节点对应);
    Query OK, 0 rows affected (0.02 sec)
    
    MariaDB [(none)]> start slave;
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> show slave statusG;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 10.10.129.182
                      Master_User: root
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000007
              Read_Master_Log_Pos: 6329460
                   Relay_Log_File: mgt129183-relay-bin.000002
                    Relay_Log_Pos: 1487
            Relay_Master_Log_File: mysql-bin.000006
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: 

    5、测试

    主库创建一个数据库:
    # mysql -uroot -p -e 'create database test_m_s;'
    从库检查:
    # mysql -uroot -p -e 'show databases;' |grep "test_m_s"
    Enter password: 
    test_m_s
  • 相关阅读:
    超级楼梯
    hdu1040
    hdu2033(惭愧)
    hdu2032杨辉三角
    hdu1013Digital Roots
    hdu2031
    Linux信号(signal) 机制分析
    android init重启service(进程)
    [android] init进程 .rc文件中service、action的parsing
    oom_adj
  • 原文地址:https://www.cnblogs.com/gange111/p/9629152.html
Copyright © 2011-2022 走看看