美文网首页
Mysql主从配置

Mysql主从配置

作者: Impact | 来源:发表于2017-03-13 17:23 被阅读0次

    准备工作

    环境

    1. 两台虚拟机,地址:master-192.168.52.130/slave-192.168.52.131;
    2. 系统版本:Centos 7;
    3. 软件版本:mysql 5.6;

    数据库

    1. 同样的配置:端口-3307等;
    2. 同样的数据:database-TestDB等;

    主从配置

    主库配置

    1. 编辑 /etc/my.cnf:
    [mysqld]
    basedir=/var/lib/mysql
    datadir=/var/lib/mysql/data
    socket=/var/lib/mysql/mysql.sock
    port=3307
    
    log-bin=/var/lib/mysql/mysql-bin.log
    explicit_defaults_for_timestamp=true
    
    innodb_flush_log_at_trx_commit=1
    sync_binlog=1
    server-id=1
    
    binlog-do-db=TestDB
    binlog-ignore-db=performance_schema
    binlog-ignore-db=mysql
    binlog-ignore-db=test
    
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    # Recommended in standard MySQL setup
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    
    [mysqld_safe]
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    

    配置项解释:

    server-id=1 标识
    binlog-do-db=TestDB 需要同步的数据库
    binlog-ignore-db=performance_schema 不需要同步的数据库

    1. 创建同步用户,在主服务器上为从服务器建立一个连接帐户,该帐户必须授予REPLICAITON SLAVE权限。在主服务器登陆mysql上执行:
    grant replication slave on *.* to 'master'@'%' identified by '123456';
    
    1. 重启之后,查看状态:
    mysql> show master status\G;
    *************************** 1. row ***************************
                 File: mysql-bin.000009
             Position: 120
         Binlog_Do_DB: TestDB
     Binlog_Ignore_DB: performance_schema,mysql,test
    Executed_Gtid_Set: 
    1 row in set (0.01 sec)
    

    注意上面的 File 和 Position ,从库slave 配置的时候会使用。

    从库配置

    1. 编辑 /etc/my.cnf
    [mysqld]
    
    ...
    
    server_id=2
    log-bin=mysql-bin.log
    replicate-do-db=TestDB
    
    1. change master语句指定同步位置:
    mysql>change master to master_host='192.168.52.130', master_user='master', master_password='123456', master_log_file='mysql-bin.000007', master_log_pos=120,master_port=3307;
    
    
    1. 启用/关闭 slave;
    start slave;
    
    stop slave;
    
    1. 重启,查看slave :show slave status\G;
    mysql> show slave status\G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.52.130
                      Master_User: master
                      Master_Port: 3307
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000007
              Read_Master_Log_Pos: 120
                   Relay_Log_File: mysqld-relay-bin.000010
                    Relay_Log_Pos: 283
            Relay_Master_Log_File: mysql-bin.000007
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: TestDB
              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: 120
                  Relay_Log_Space: 620
                  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: 1cacfa49-04c4-11e7-b744-000c29d5b78c
                 Master_Info_File: /var/lib/mysql/data/master.info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
               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
    1 row in set (0.00 sec)
    

    测试

    在主库进行数据的操作,查看从库的数据。

    相关文章

      网友评论

          本文标题:Mysql主从配置

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