mysql主从备份

场景:

1、主服务器192.168.0.225、从服务器192.168.0.226。其中,主服务器上已有数据。

2、主从服务器上的mysql版本及安装配置相同。

一、主从备份的原理:

主服务器数据库的每次操作都会记录在二进制日志文件mysql-bin.xxx中。从服务器的I/O线程使用专用帐号登陆到主服务器中读取该二进制文件,并将文件内容写入到自己本地的中继日志relay-log文件中。然后从服务器的SQL线程会根据中继日志中的内容执行SQL语句。

这要求两台服务器有同样的初态。

二、同步初态:

1、将主服务器要同步的数据库加锁,避免同步时发生改变:

>use database_name;
>flush tables with read lock;

2、使用mysqldump工具导出数据:

mysqldump -uroot -pxxx database_name >database_name.sql

3、备份完成后,解锁数据库:

>unlock tables;

4、将初始数据导入从数据库:

>create database database_name;
>use database_name;
>source database_name.sql;

 完成以上操作后,主从服务器就有一样的初态了。

三、主从同步设置:

1、配置从数据库:

/etc/my.cnf主要配置如下:

log-bin=mysql-bin                                 #开启二进制日志
server-id       = 2                               #主数据库id为1,不能相同。
replicate_wild_do_table=test.%                    #只同步test库下的表
relay_log=mysqld-relay-bin                        #记录中继日志
log-slave-updates=YES                             #从服务器同步后记录日志
修改完成后重启mysql服务。

2、查看主服务器日记记录位置:

>show master status\G

显示内容如下:

***************** 1. row ****************
            File: mysql-bin.000001       #当前记录的日志
        Position: 80647293               #日志中记录的位置
    Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

没有出现说明主服务器没有配置

vim /etc/my.cnf

log_bin=mysql-bin-master
server-id       = 1
binlog-do-db=confluence
binlog-ignore-db=mysql

3、主服务器创建允许从服务器同步数据的账户:

例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。

mysql>GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

mysql>FLUSH   PRIVILEGES;

如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器,并使用mypassword作为密码

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

FLUSH   PRIVILEGES;

如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器的dk数据库,并使用mypassword作为密码

GRANT ALL PRIVILEGES ON dk.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

FLUSH   PRIVILEGES;

密码最好是数字+字母+特殊字符。

4、从服务器开启同步:

>change master to     
              master_host='192.168.0.225',               
              master_user='user_name',    
              master_password='123456',   
              master_log_file='mysql-bin.000001',    
              master_log_pos=80647293;
配置完以上后,重启从服务器mysql服务。

5、查看从服务器是否已经成功开启同步:

>show slave status\G

显示如下:

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.225
                  Master_User: user_name
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1114
               Relay_Log_File: mysqld-relay-bin.000004
                Relay_Log_Pos: 1260
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: test.%
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1114
              Relay_Log_Space: 1563
              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
1 row in set (0.00 sec)

其中:Slave_IO_Running和Slave_SQL_Running的状态都是YES,说明同步开启成功。

现在就可以去主服务器上的test库下创建表开测试同步了。

'Could not find first log file name in binary log index file'的解决办法

数据库主从出错:

 Slave_IO_Running:  No 一方面原因是因为网络通信的问题也有可能是日志读取错误的问题。以下是日志出错问题的解决方案:

 

Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

 

解决办法:
从机器停止slave

mysql> stop slave;

 

到master机器登陆mysql:

记录master的bin的位置,例如:mysql> show master status;
+-------------------+----------+--------------+-------------------------------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB                                                                            |
+-------------------+----------+--------------+-------------------------------------------+
| mysqld-bin.000010 |      106 |              | information_schema,mysql |
+-------------------+----------+--------------+-------------------------------------------+
日志为mysqld-bin.000010

 

刷新日志:mysql> flush logs;

因为刷新日志file的位置会+1,即File变成为:mysqld-bin.000011

 

马上到slave执行

mysql> CHANGE MASTER TO MASTER_LOG_FILE='mysqld-bin.000011',MASTER_LOG_POS=106;

mysql> start slave;

mysql> show slave status\G;

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

R助手

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

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

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

打赏作者

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

抵扣说明:

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

余额充值