MySQL数据备份,主从数据同步,异地备份或集群

一、现状及说明

1、目标

两台服务器,分别安装了MySQL数据库,需要从一台主服务器(master)Windows Sevver 2008R2操作系统,将数据同步到另一台从服务器(slave)CentOS7操作系统上,实现异地备份。

2、服务器信息

  • 主服务器IP:192.168.1.10
  • 从数据库IP:192.168.1.20

注:两台服务器之间的数据库结构必须一致,否则无法构成同步。

二、配置master

master是Windows Server服务环境,所以配置文件是安装目录下的my.ini文件(如果是linux服务器则是my.cnf,配置方法同理)。

  1. 修改配置文件,并开启MySQL的日志功能,添加如下信息:
[mysqld]
log-bin=mysql-bin
server-id=10
log-bin=E:/DATA/MYSQL/back/ #日志文件输出路径
binlog-do-db=test #同步的数据库名称
  1. 修改完配置文件后,重新启动mysql服务。

  2. 修改slave访问权限。(如果默认配置了全部权限,即%,则不需要执行grant)

由于是windows服务器,可以在命令提示符下运行:

mysql -uroot -p'mysql数据库密码'

mysql> grant replication slave on *.* to root@'192.168.1.20' identified by 'mysql数据库密码';

或者有另一台CentOS服务器上,连接数据库:

mysql -h192.168.1.10 -uroot -p'mysql数据库密码'

mysql> grant replication slave on *.* to root@'192.168.1.20' identified by 'mysql数据库密码';
  1. 查看master状态。
mysql> show master status \G;

*************************** 1. row ***************************
             File: .000004
         Position: 15633
     Binlog_Do_DB: test
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

三、配置slave

  1. 运行vim /etc/my.cnf,修改mysql配置文件my.cnf
[mysqld]
server-id=20
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
replicate-do-db=test #同步的数据库名称
  1. 修改完配置文件后,重新启动mysql服务。
systemctl restart mysqld.service
  1. 登录slave;
mysql -uroot -p'mysql数据库密码'
  1. 根据master上显示的状态信息,配置slave参数。
mysql> change master to master_host='192.168.1.10',master_port=3306,master_user='root',master_password='mysql数据库密码',master_log_file='.000004',master_log_pos=15633;
  1. 查看master的状态 Slave_IO_Running: Yes、 Slave_SQL_Running: Yes,则配置成功,修改master的数据,slave会自动同步。
mysql> show slave status \G;

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.10
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: .000004
          Read_Master_Log_Pos: 15633
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 717
        Relay_Master_Log_File: .000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 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: 15633
              Relay_Log_Space: 924
              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: ae0a8ec4-6fc1-11e9-821a-4ccc6a4d7344
             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)

四、遇到的问题及解决方案

一、配置过程中查看show slave status \G的时候,提示Last_IO_Errno:Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file’。原因是master_log_file、master_log_pos配置的不正确;

解决办法:

  1. 打开master,进入mysql
mysql -uroot -p'mysql数据库密码'

#重新创建一个binlog文件;
mysql> flush logs;
mysql> show master status \G;

*************************** 1. row ***************************
             File: .000004
         Position: 15633
     Binlog_Do_DB: test
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)
  1. 打开slave,改变对应配置,再测试同步成功了。
mysql> stop slave;
mysql> change master to master_log_file='.000004',master_log_pos=15633;
mysql> start slave;
mysql> show slave status \G;
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

位步

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值