mysql主从备份_MySQL
bitsCN.com
一、准备服务器
由于MySQL不同版本之间的(二进制日志)binlog格式可能会不一样,因此最好的搭配组合是Master的MySQL版本和Slave的版本相同或者更低,Master的版本肯定不能高于Slave版本。
我们假设主服务器(以下简称Master)和从服务器(以下简称Slave)的版本都是5.0.63。
假设同步Master的主机名为:A(IP:192.168.0.1),Slave主机名为:B(IP:192.168.0.2),2个MySQL的basedir目录都是/usr/local/mysql,datadir都是:/var/mysql。
二、设置同步服务器
1、设置同步Master
修改 my.cnf 文件,在
# Replication Master Server (default)
# binary logging is required for replication
添加如下内容:

- log-bin=mysql-bin
- server-id = 1
- binlog-do-db=test
- binlog-ignore-db=mysql
重启MySQL,创建一个MySQL帐号为同步专用

- GRANT REPLICATION SLAVE,RELOAD,SUPER, ON *.* TO backup@192.168.0.2 IDENTIFIED BY 'slavepass';
- FLUSH PRIVILEGES ;
复制数据库:

- FLUSH TABLES WITH READ LOCK;
锁定表清除写入操作。

- SHOW MASTER STATUS;
File列显示日志名,而Position显示偏移量,记录备用。
备份数据库,建议对于MYI存储的MYSQL采用直接文件复制,效率会好很多。备份完解锁。

- UNLOCK TABLES;
备份后对于从服务器导入数据库
以上复制过程也可以用mysqldump,如果数据库在提供服务,必须加入--lock-all-tables以保证数据为同一时间的。加入--master-data=1在dump时会记录binlog名称和偏移值,以CHANGE MASTER TO形式存在文件中,这样导入slave后不用自己设置master的binlog文件和偏移值了。
2、设置同步Slave
修改my.cnf文件,添加

- server-id = 2
- master-host = 192.168.0.1
- master-user = backup
- master-password = slavepass
- master-port = 3306
- replicate-ignore-db=mysql
- replicate-do-db=test
server-id不能与master相同
重启MySQL
3、启动同步
在主服务器A MySQL命令符下:

- show master status;
显示(当然这个是我机器的情况,你的不可能跟我一样哈,只是个例子):
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000028 | 313361 | test | mysql |
+------------------+----------+-------------------+------------------+
在从服务器B MySQL命令符下:

- slave stop;
- MySQL> CHANGE MASTER TO
- -> MASTER_HOST='master_host_name',
- -> MASTER_USER='replication_user_name',
- -> MASTER_PASSWORD='replication_password',
- -> MASTER_LOG_FILE='recorded_log_file_name',
- -> MASTER_LOG_POS=recorded_log_position;
- slave start;
用show slave status;看一下从服务器的同步情况
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果都是yes,那代表已经在同步
往表里面写点数据测试一下看是否同步成功,如果不成功,绝对不是你的RP问题,再检查一下操作步骤!
4、设置双向同步
其实也就是A->B单向同步的反向操作!双向同步,就这么简单啦!
三、同步错误处理
发现mysql slave服务器经常因为一些特殊字符或者符号产生的更新语句报错,整个同步也会因此而卡在那,最初的办法只是手动去出错的机器,执行下面三条sql语句,跳过错误即可。

- slave stop;
- set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
- slave start;
四、备份底层知识
1、binlog
mysql的binlog记录了数据库的所有操作,比如我有个新的数据库bbs,建立数据库时候也开启了binlog,那么mysql会在var目录生成个mysql-bin.000001,这个文件记录了对数据库bbs的所有操作sql命令。每个binlog文件默认1G,超过了会自动换到mysql-bin.000002。mysql-bin.index记录了所有mysql-bin的名字。
mysql-bin.index示例:

- ./mysql-bin.000001
- ./mysql-bin.000002
- ./mysql-bin.000003
- ./mysql-bin.000004
- ./mysql-bin.000005
- ./mysql-bin.000006
- ./mysql-bin.000007
- ./mysql-bin.000008
- ./mysql-bin.000009
- ./mysql-bin.000010
数据主从备份其实就是master把binlog发给slave,然后slave在本地执行这些sql语句。
2、master.info
master.info文件存在于slave的var目录,记录了master的信息。
master.info示例:

- 15
- mysql-bin.000105 #master的binlog文件
- 498027148 #偏移值
- 192.168.0.1
- backup
- slavepass
- 3306
- 60
- 0
- 0
3、relay-log.info
relay-log.info文件存在于slave的var目录,记录了slave执行binlog文件情况的信息。
relay-log.info示例:

- ./lab-relay-bin.000050 #slave存放master的binlog的文件
- 210263408 #slave偏移值
- mysql-bin.000105
- 498027148
- 2
从master的mysql-bin.000105发送信息写到lab-relay-bin.000050里,slave再从lab-relay-bin.000050取sql语句执行。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution: 1. Check the eMule settings to make sure you have entered the correct server address and port number; 2. Check the network connection, make sure the computer is connected to the Internet, and reset the router; 3. Check whether the server is online. If your settings are If there is no problem with the network connection, you need to check whether the server is online; 4. Update the eMule version, visit the eMule official website, and download the latest version of the eMule software; 5. Seek help.

As a LINUX user, we often need to install various software and servers on CentOS. This article will introduce in detail how to install fuse and set up a server on CentOS to help you complete the related operations smoothly. CentOS installation fuseFuse is a user space file system framework that allows unprivileged users to access and operate the file system through a customized file system. Installing fuse on CentOS is very simple, just follow the following steps: 1. Open the terminal and Log in as root user. 2. Use the following command to install the fuse package: ```yuminstallfuse3. Confirm the prompts during the installation process and enter `y` to continue. 4. Installation completed

What should I do if the RPC server is unavailable and cannot be accessed on the desktop? In recent years, computers and the Internet have penetrated into every corner of our lives. As a technology for centralized computing and resource sharing, Remote Procedure Call (RPC) plays a vital role in network communication. However, sometimes we may encounter a situation where the RPC server is unavailable, resulting in the inability to enter the desktop. This article will describe some of the possible causes of this problem and provide solutions. First, we need to understand why the RPC server is unavailable. RPC server is a

In network data transmission, IP proxy servers play an important role, helping users hide their real IP addresses, protect privacy, and improve access speeds. In this article, we will introduce the best practice guide on how to build an IP proxy server with PHP and provide specific code examples. What is an IP proxy server? An IP proxy server is an intermediate server located between the user and the target server. It acts as a transfer station between the user and the target server, forwarding the user's requests and responses. By using an IP proxy server

The role of a DHCP relay is to forward received DHCP packets to another DHCP server on the network, even if the two servers are on different subnets. By using a DHCP relay, you can deploy a centralized DHCP server in the network center and use it to dynamically assign IP addresses to all network subnets/VLANs. Dnsmasq is a commonly used DNS and DHCP protocol server that can be configured as a DHCP relay server to help manage dynamic host configurations in the network. In this article, we will show you how to configure dnsmasq as a DHCP relay server. Content Topics: Network Topology Configuring Static IP Addresses on a DHCP Relay D on a Centralized DHCP Server

What should I do if I can’t enter the game when the epic server is offline? This problem must have been encountered by many friends. When this prompt appears, the genuine game cannot be started. This problem is usually caused by interference from the network and security software. So how should it be solved? The editor of this issue will explain I would like to share the solution with you, I hope today’s software tutorial can help you solve the problem. What to do if the epic server cannot enter the game when it is offline: 1. It may be interfered by security software. Close the game platform and security software and then restart. 2. The second is that the network fluctuates too much. Try restarting the router to see if it works. If the conditions are OK, you can try to use the 5g mobile network to operate. 3. Then there may be more

Binary arithmetic is an operation method based on binary numbers. Its basic operations include addition, subtraction, multiplication and division. In addition to basic operations, binary arithmetic also includes logical operations, displacement operations and other operations. Logical operations include AND, OR, NOT and other operations, and displacement operations include left shift and right shift operations. These operations have corresponding rules and operand requirements.

How to install PHPFFmpeg extension on server? Installing the PHPFFmpeg extension on the server can help us process audio and video files in PHP projects and implement functions such as encoding, decoding, editing, and processing of audio and video files. This article will introduce how to install the PHPFFmpeg extension on the server, as well as specific code examples. First, we need to ensure that PHP and FFmpeg are installed on the server. If FFmpeg is not installed, you can follow the steps below to install FFmpe
