Introduction to MySQL two-way backup method (with code)
This article brings you an introduction to the MySQL two-way backup method (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
MySQL two-way backup is also called master-master backup, that is, both MySQL services are Master, and any one of them is the Slave of another service.
Preparation
Server
MySQL Server | Version | IP Address |
---|---|---|
masterA | 5.6.41 | 192.168.1.201 |
5.6.41 | 192.168.1.202 |
The backup MySQL server version should be kept consistent as much as possible. Different versions may have binary log formats. Not compatible.
Specific operation
AttentionDuring the operation, pay attention to the consistency of the data on both sides! ! ! masterA configurationmy.cnf[mysqld] # 服务器唯一标识 server-id=1 # 二进制日志文件名 log-bin=mysql-bin # 需要备份的数据库,多个数据库用 , 分隔 binlog-do-db=piumnl # 需要复制的数据库,多个数据库用 , 分隔 replicate-do-db=piumnl # 中继日志文件名 relay_log=mysqld-relay-bin # 手动启动同步服务,避免突然宕机导致的数据日志不同步 skip-slave-start=ON # 互为主从需要加入这一行 log-slave-updates=ON # 禁用符号链接,防止安全风险,可不加 symbolic-links=0 # 可不加 # resolve - [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0 master-info-repository=table relay-log-info-repository=table relay-log-recovery=1 # 可不加 # 禁用 dns 解析,会使授权时使用的域名无效 skip-host-cache skip-name-resolve sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLESmasterB configurationmy.cnf
# 不再解释各个配置项 [mysqld] server-id=2 log-bin=mysql-bin binlog-do-db=piumnl replicate-do-db=piumnl relay_log=mysql-relay-bin skip-slave-start=ON log-slave-updates=ON symbolic-links=0 # resolve - [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0 master-info-repository=table relay-log-info-repository=table relay-log-recovery=1 skip-host-cache skip-name-resolve sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLESCreate backup usermasterA & masterB both need to create a backup user:
create user 'rep'@'%' identified by 'rep'; # 创建一个账户 grant replication slave on *.* to 'rep'@'%'; # 授予该账户对任意数据库任意表的主从备份权限Note:
- MySQL under Linux is closed for the
- root@%
user
grant_privPermissions, so if it is a remote login, authorization failure will occur
The backup user account and password here may not be consistent, here to simplify the operation, use the same account and password
show master status\G; # 此处需要关注 File 和 Position 值Enable backup
stop slave; # master_log_file 就是第一步操作的 File 值 # master_log_pos 就是第一步操作的 Position 值 change master to master_host=<master_hostname>, master_user=<rep_username>, master_port=<master_port>, master_password=<rep_password>, master_log_file='mysql-log.000003', master_log_pos=154; start slave;</rep_password></master_port></rep_username></master_hostname>View the results
show slave status\G; # 查看最重要的两项,两个都必须为 Yes ,有一个为 No 都要去查看错误日志文件,看看什么地方存在问题 # Slave_IO_Running: Yes # Slave_SQL_Running: YesmasterB
Reverse the operation of masterA
TestInsert data into masterA and masterB respectively, and check whether the expected data appears on the other server in time ProblemMySQL Slave Failed to Open the Relay LogThis should be a problem with the relay log. You can try the following operationsstop slave; flush logs; start slave;Got fatal error 1236 from master when reading data from binary logWhen pulling the log from the main library, it is found that the first file in the mysql_bin.index file of the main library does not exist.
# 进行如下操作重置 # 如果二进制日志或中继日志有其他作用,请勿进行如下操作 reset master; reset slave; flush logs;
The above is the detailed content of Introduction to MySQL two-way backup method (with code). For more information, please follow other related articles on the PHP Chinese website!

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
