两mysql数据库同步实现异步备份方法_MySQL
bitsCN.com
1.服务器状态
服务器A:192.168.1.1
服务器B:192.168.1.2
2.创建同步用户
主机域A:192.168.1.2 用户名A:sync_a 密码A:aaa
主机域B:192.168.1.1 用户名B:sync_b 密码B:bbb
至少分配以下权限grant replication slave
3.执行flush privileges
4.停止MySQL
5.配置my.cnf(my.ini)
服务器A 服务器B
user = mysql教程
log-bin = mysql-bin
server-id = 1
binlog-do-db = test
binlog-ignore-db = mysql
replicate-do-db = test
replicate-ignore-db = mysql
log-slave-updates
slave-skip-errors = all
sync_binlog = 1 user = mysql
log-bin = mysql-bin
server-id = 2
binlog-do-db = test
binlog-ignore-db = mysql
replicate-do-db = test
replicate-ignore-db = mysql
log-slave-updates
slave-skip-errors = all
sync_binlog = 1
server-id必须唯一
binlog-do-db和replicate-do-db表示需要同步的数据库教程
binlog-ignore-db和replicate-ignore-db表示不需要同步的数据库
请不要加入以下命令,该命令并不会解决uid跳号的问题,恰恰相反以下两行命令才是导致uid跳号的罪魁祸首
auto_increment_increment = 2
auto_increment_offset = 1
6.重新启动MySQL
7.进入MySQL控制台
服务器A:
show master status G
flush tables with read lock;
服务器B:
show master status G
flush tables with read lock;
同时记录下两台服务器的File和Position,此处假设:
A: File: mysql-bin.000001
Position: 001
B: File: mysql-bin.000002
Position: 002
服务器A:
change master to
-> master_host='192.168.1.2',
-> master_user='sync_b',
-> master_password='bbb',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=002;
服务器B:
change master to
-> master_host='192.168.1.1',
-> master_user='sync_a',
-> master_password='aaa',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=001;
此处填写的数据完全相反
8.执行show processlist G查看是否同步成功
方法二
两台服务器
192.168.1.1(A)
192.168.1.2(B)
先保证这mysql的版本是一致的,参考http://dev.mysql.com/doc/refman/5.1/zh/replication.html#replication-implementation-details,否则复制中的异常情况很折腾人。
1.在两台mysql上创建用户,设置权限
A上添加:
#grant replication slave,replication client,reload,super on *.* to 'sync_user'@'192.168.1.2' identified by '123456' with grant option;//用于B访问
B上:
#grant replication slave,replication client,reload,super on *.* to 'sync_user'@'192.168.1.1' identified by '123456' with grant option;//用于A访问
执行 #flush privileges; 更新数据库使用户生效。
2.在/etc/my.cnf上进行相关配置
A B
server-id = 1
master-host =192.168.1.2
master-user =sync_user
master-pass =123456
master-port =3306
master-connect-retry=60
replicate-do-db =db1
replicate-do-db =db2
replicate-ignore-db=mysql server-id = 2
master-host =192.168.1.1
master-user =sync_user
master-pass =123456
master-port =3306
master-connect-retry=60
replicate-do-db =db1
replicate-do-db =db2
replicate-ignore-db=mysql
注意
1.server_id必须为唯一.
2.如果想要同时同步多个库,添加多行replicate-do-db,每行指定一个数据库。不能使用replicate-do-db=db1,db2的形式
3.replicate-ignore-db:指定不进行同步的数据库。
保存后,重启mysql
#mysqladmin -u root -p shutdown
#mysqld_safe --user=mysql
3.把两台服务器上需要同步的数据库进行拷贝,保证这两台数据库初始状态一致。
4.进行双向同步
双向同步就是把单向同步反过来在做一遍,但一定要注意操作的顺序,这是成功的关键
step1.在A上mysql shell中执行
#show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000054 | 35 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
记录下 mysql-bin.000054,和35
step2.在B上执行:
#stop slave;//停止同步
#
CHANGE MASTER TO MASTER_HOST='192.168.1.1', MASTER_PORT=3306, MASTER_USER='sync_user', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000054', MASTER_LOG_POS=35;
#start slave;//开始同步
step3,执行show slave statusG;如显示如下内容,表示同步设置成功。
Slave_IO_State: Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
step4:上一步没有问题。则在B上继续执行show master status;
#show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 6854 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
step5:在A上执行
#stop slave;//停止同步
#
CHANGE MASTER TO MASTER_HOST='192.168.1.2', MASTER_PORT=3306, MASTER_USER='sync_user', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=6854;
#start slave;//开始同步
step6:执行show slave statusG;如显示如下内容,表示同步设置成功。
Slave_IO_State: Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如上述没有啥问题。到此双向同步设置完成。
方法三
一、准备服务器
由于MySQL不同版本之间的(二进制日志)binlog格式可能会不一样,因此最好的搭配组合是Master的MySQL版本和Slave的版本相同或者更低,Master的版本肯定不能高于Slave版本。
more.. | less.. | 本文中,我们假设主服务器(以下简称Master)和从服务器(以下简称Slave)的版本都是5.0.27,操作系统是RedHat Linux 9。
假设同步Master的主机名为:master(IP:192.168.1.123),Slave主机名为:slave(IP:192.168.1.124),2个MySQL的basedir目录都是/usr/local/mysql,datadir都是:/var/lib/mysql。
二、设置同步服务器
1、设置同步Master
修改 my.cnf 文件,在
# Replication Master Server (default)
# binary logging is required for replication
添加如下内容:
#log-bin=/var/log/mysql/updatelog
server-id = 1
binlog-do-db=discuz
binlog-ignore-db=mysql
重启MySQL,创建一个MySQL帐号为同步专用
# /usr/local/mysql/bin/mysql -u root -p
mysql> GRANT REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
如果想要在Slave上有权限执行 "LOAD TABLE FROM MASTER" 或 "LOAD DATA FROM MASTER" 语句的话,必须授予全局的 FILE 和 SELECT 权限:
mysql>GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> FLUSH PRIVILEGES ;
2、设置同步Slave
修改my.cnf文件,添加
server-id = 2
master-host = 192.168.1.123
master-user = back
master-password = back
master-port = 3306
replicate-ignore-db=mysql
replicate-do-db=discuz
重启MySQL
3、启动同步
在主服务器master MySQL命令符下:
# /usr/local/mysql/bin/mysql -u root -p
mysql> show master status;
显示(当然这个是我机器的情况,你的不可能跟我一样哈,只是个例子):
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000009 | 98 | discuz | mysql |
+------------------+----------+-------------------+------------------+
在从服务器master MySQL命令符下:
# /usr/local/mysql/bin/mysql -u root -p
mysql> slave stop;
mysql> change master to master_host='192.168.1.123', master_user='back', master_password='back', master_log_file='mysql-bin.000009', master_log_pos=98;
mysql> slave start;
用show slave statusG;看一下从服务器的同步情况
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果都是yes,那代表已经在同步
往表里面写点数据测试一下看是否同步成功,如果不成功,绝对不是你的RP问题,再检查一下操作步骤!
4、设置双向同步
修改slave服务器的my.cnf,添加
log-bin=/var/log/mysql/updatelog
binlog-do-db=discuz
binlog-ignore-db=mysql
重启MySQL,创建一个MySQL帐号为同步专用
mysql> GRANT REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> FLUSH PRIVILEGES ;
修改master服务器的my.cnf,添加
master-host = 192.168.1.124
master-user = back
master-password = back
master-port = 3306
replicate-ignore-db=mysql
replicate-do-db=discuz
重启MySQL
在主服务器slave MySQL命令符下:
show master status;
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000013 | 98 | discuz | mysql |
+------------------+----------+-------------------+------------------+
在服务器A MySQL命令符下:
mysql> slave stop;
mysql> change master to master_host='192.168.1.124', master_user='back', master_password='back', master_log_file='mysql-bin.000013', master_log_pos=98;
mysql> slave start;
其实也就是A->B单向同步的反向操作!双向同步,就这么简单啦!
提示:如果修改了主服务器的配置,记得删除从服务器上的master.info文件。否则从服务器使用的还是老配置,可能会导致错误。
-----------------------------------------------------------------------------------
注意:关于要复制多个数据库时,binlog-do-db和replicate-do-db选项的设置,网上很多人说是用半角逗号分隔,经过测试,这样的说法是错误的,MySQL官方文档也明确指出,如果要备份多个数据库,只要重复设置相应选项就可以了。
比如:
binlog-do-db=a
binlog-do-db=b
replicate-do-db=a
replicate-do-db=b

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



When using Win11 system, sometimes you will encounter a prompt that requires you to enter the administrator username and password. This article will discuss how to deal with this situation. Method 1: 1. Click [Windows Logo], then press [Shift+Restart] to enter safe mode; or enter safe mode this way: click the Start menu and select Settings. Select "Update and Security"; select "Restart Now" in "Recovery"; after restarting and entering the options, select - Troubleshoot - Advanced Options - Startup Settings -&mdash

Wireless networks have become an indispensable part of people's lives in today's digital world. Protecting the security of personal wireless networks is particularly important, however. Setting a strong password is key to ensuring that your WiFi network cannot be hacked by others. To ensure your network security, this article will introduce in detail how to use your mobile phone to change the router WiFi password. 1. Open the router management page - Open the router management page in the mobile browser and enter the router's default IP address. 2. Enter the administrator username and password - To gain access, enter the correct administrator username and password in the login page. 3. Navigate to the wireless settings page - find and click to enter the wireless settings page, in the router management page. 4. Find the current Wi

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

This article will explore how to solve the problem of wrong password, especially the need to be careful when dealing with BitLocker warnings. This warning is triggered when an incorrect password is entered multiple times in BitLocker to unlock the drive. Usually, this warning occurs because the system has a policy that limits incorrect login attempts (usually three login attempts are allowed). In this case, the user will receive an appropriate warning message. The complete warning message is as follows: The password entered is incorrect. Please note that continuously entering incorrect passwords will cause the account to be locked. This is to protect the security of your data. If you need to unlock your account, you will need to use a BitLocker recovery key. The password is incorrect, beware the BitLocker warning you receive when you log in to your computer

Wireless networks have become an indispensable part of our lives with the rapid development of the Internet. In order to protect personal information and network security, it is very important to change your wifi password regularly, however. To help you better protect your home network security, this article will introduce you to a detailed tutorial on how to use your mobile phone to change your WiFi password. 1. Understand the importance of WiFi passwords. WiFi passwords are the first line of defense to protect personal information and network security. In the Internet age, understanding its importance can better understand why passwords need to be changed regularly. 2. Confirm that the phone is connected to wifi. First, make sure that the phone is connected to the wifi network whose password you want to change before changing the wifi password. 3. Open the phone’s settings menu and enter the phone’s settings menu.

In the Windows 10 system, the password policy is a set of security rules to ensure that the passwords set by users meet certain strength and complexity requirements. If the system prompts that your password does not meet the password policy requirements, it usually means that your password does not meet the requirements set by Microsoft. standards for complexity, length, or character types, so how can this be avoided? Users can directly find the password policy under the local computer policy to perform operations. Let’s take a look below. Solutions that do not comply with password policy specifications: Change the password length: According to the password policy requirements, we can try to increase the length of the password, such as changing the original 6-digit password to 8-digit or longer. Add special characters: Password policies often require special characters such as @, #, $, etc. I

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

1. Download and install Xiaobai’s one-click system reinstallation tool on another computer, insert an empty USB disk to create a USB boot disk. For specific tutorials, please refer to: 2. Insert the USB boot disk into the computer that needs to change the password to restart, and press Start hotkey. Generally, the startup hotkey is one of F12, F8, F9, F10, and ESC. Then the startup interface appears, select the option of the USB disk and press Enter to enter. 3. Select [1] to start win10x64PE and press Enter to confirm. 4. Select the password modification tool on the desktop and double-click to open it. 5. Then a list of account names appears, select the account that needs to change the password and open it. 6. Click the Change Password command below, enter the new password twice, and then click OK to save the changes. 7. Finally, unplug the USB flash drive and restart the computer. Then it will be normal.
