MYSQL 主从配置

来源:互联网 发布:java编写贪吃蛇教程 编辑:程序博客网 时间:2024/06/16 15:17

遇到的报错 (都是数据库版本不同导致的问题

主数据库5.7     从5.1的   

Slave_IO_Running: NO   第一个问题  对应下面1


Slave_SQL_Running: No  第二个问题 对应下面2


1、mysql同步复制抛Got fatal error 1236 Slave can not handle replication events with the checksum

这应该也是版本的问题

解决办法1:

master2的数据库换成5.6.12;

解决办法2:

在master1中设置binlog_checksum =none;

参数资料:http://blog.csdn.net/wantingtingting/article/details/41944047


2、当往MASTER数据库插数据的时候 

SLVAE报错!!

Last_SQL_Errno: 1594

Last_SQL_Error: Relay log read failure: Could not parse relay log event entry. 


虽然网上很多说stop slave;change master to master_log_file='mysql-bin.000001' ,master_log_pos=150;start slave; 有效

但是重新添加数据的时候又报错,应该是binarylog版本的问题了

没去看binarylog的版本  先把从数据升级



-----------------------------------------------------------------------------开始配置------------------------------------------------------------------------------------

1、准备两台主机

主 - 本地IP:192.168.10.225

从 - 虚拟机:192.168.11.96

在主数据库创建一个新的用户 用于同步 ‘rep‘;  

从数据库我还是用root的用户  


2、登录主数据库

创建新用户用于同步

CREATE USER 'rep'@'localhost' IDENTIFIED BY 'hello';

主数据库登录 rep 的账号


授予从数据库权限

  1. GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.11.96'IDENTIFIED BY '123456' WITH GRANT OPTION;   
  2. flush privileges;  


  1. GRANT REPLICATION SLAVE ON *.* TO 'root'@192.168.11.96 IDENTIFIED BY '123456'; 
  2. GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO 'root'@192.168.11.96 IDENTIFIED BY '123456';


备注:如果想要在Slave上有权限执行"LOAD TABLE FROM MASTER" 或 "LOAD DATA FROM MASTER"语句的话,必须授予全局的 FILE 和 SELECT 权限:
GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO 'root'@192.168.11.96 IDENTIFIED BY '123456';


3、配置主数据master 的my.cnf

  1. [mysqld]  
  2. #master configure  
  3. server-id = 1  
  4. log-bin=mysql-bin  
  5. binlog-do-db=test 
  6. binlog-ignore-db=mysql  
  7. #master configure  
  8. datadir=/var/lib/mysql  
  9. socket=/var/lib/mysql/mysql.sock  

4、备份master的数据库

导出要同步的数据库  这里是test  

由于master数据库是Windows的,直接手动导出 ,如果是linux的就方便点。。

MASTER数据库  主机

  1. mysql> FLUSH TABLES WITH READ LOCK;                  //先锁表
  2. # mysqldump -uroot -pmydb test >e:\mysql\test.sql
  3. # scp test.sql  192.168.11.96:/tmp/                     //注意路径问题

SLAVE数据库  从机
  1. mysql -uroot -p123456 test < /tmp/test.sql         //记得先建好数据库test      

成功后,将master 解表   

  1. mysql> UNLOCK TABLES;                


5、在从机(192.168.11.96)上登录 主机的数据库检验  从机是否有权限

  1. mysql -h192.168.10.225 -urep -phello  
  2. SHOW GRANTS; 

会看到下面的,如果登录不了改下权限

  1. mysql> SHOW GRANTS;  
  2. +-------------------------------------------------------------------------------------------------------------------------------------------+  
  3. | Grants for rep@192.168.11.96                                                                                                             |  
  4. +-------------------------------------------------------------------------------------------------------------------------------------------+  
  5. | GRANT SELECT, FILE, REPLICATION SLAVE ON *.* TO 'rep'@'192.168.11.96' IDENTIFIED BY PASSWORD '*6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119' |  
  6. +-------------------------------------------------------------------------------------------------------------------------------------------+  
  7. 1 row in set (0.00 sec)  


6、关键的步骤!!! 

在master数据库 show master status;

+------------------------+-----------+--------------------+-------------------------+---------------------------+
| File                       | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------+-----------+--------------------+-------------------------+---------------------------+
| mysql-bin.000003 |     2110 | test                    | mysql                    |                                 |
+------------------------+-----------+--------------------+-------------------------+---------------------------+
1 row in set


在SLAVE数据库
  1. mysql> CHANGE MASTER TO MASTER_HOST='192.168.11.225',MASTER_USER='rep',MASTER_PASSWORD='hello',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=2110;  


  1. mysql> start slave; 
  2. mysql> SHOW SLAVE STATUS\G  

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.225
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 1864
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 526
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql
           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: 1864
              Relay_Log_Space: 696
              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: df35b1ba-2321-11e7-be6c-00e04c12323b
             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 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)


完成!!!!!!!!

有空再跑一遍!!!!


参考http://blog.csdn.net/xluren/article/details/10144165