MySQL5.6 主从复制 (CentOS6.5)

2018-06-16 17:30:24 阅读:4 编辑

主从复制:

1. 准备两个服务器 ( 相同的 MySQL 版本), 局域网 ( 保证速度)
2. 主 IP:192.168.1.10; 从 IP:192.168.1.8

步骤:

一。主服务器的配置:/etc/my.cnf

[mysqld]
server_id=10
log-bin=MySQL-bin
binlog-format=mixed
注意配置文件中是否已含有该配置

二。从服务器的配置:/etc/my.cnf

[mysqld]
server_id=8
log-bin=MySQL-bin
binlog-format=mixed
relay-log=msyql-relay
注意配置文件中是否已含有该配置

三。主服务器授予 slave 帐号

创建一个用户名为 repl, 密码为:repl123, 授予同局域网可访问
grant replication client,replication slave on *.* to repl@'192.168.1.%' identified by 'repl123';
flush privileges;

四。通过命令获取 log_file 及 log_pos

MySQL [(none)]> show master status\G
*************************** 1. row ***************************
             File: MySQL-bin.000008
         Position: 120
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

五。从服务器 Slave 中设置 Master 信息

change master to master_host='192.168.1.10',master_user='repl',master_password='repl123',master_log_file='MySQL-bin.000008',master_log_pos=120;
start slave;

六。查看 slave 的状态

MySQL [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: MySQL-bin.000011
          Read_Master_Log_Pos: 120
               Relay_Log_File: msyql-relay.000010
                Relay_Log_Pos: 283
        Relay_Master_Log_File: MySQL-bin.000011
             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: 120
              Relay_Log_Space: 615
              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: 10
                  Master_UUID: 73 bfd8 b4-7135-11 e8-bfbd-000 c29 caf35 d
             Master_Info_File: /data/MySQL/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)

注意事项:

1). 重启服务器
service mysqld restart