MySQL主主互备结构是基于mysql增量日志基础上的,区别于主从复制结构.在主主复制结构中,两台服务器的任何一台上面的数据库存发生
MySQL主主互备结构是基于mysql增量日志基础上的,区别于主从复制结构.
在主主复制结构中,两台服务器的任何一台上面的数据库存发生了改变都会同步到另一台服务器上,这个改变是基于sql语句的改变,如果删除系统数据库源文件或删除后新创建同名MYSQL表实现同步则无效。这样两台服务器互为主从,并且都能向外提供服务,这就比使用主从复制具有更好的性能.
接下来我将使用两个相同的环境来实现这个效果:
系统环境:CentOS6.3
数据库: mysql-5.6.10
安装配置mysql(略)
1.主1 mysql配置:(192.168.7.201)
删除之前遗留bin日志
# rm -rf /usr/local/mysql/log/bin*
修改mysql配置文件:
# vi /etc/my.cnf
添加:
-----------------
# Replication Master Server 1
# bin日志路径
log-bin = /usr/local/mysql/log/bin.log
# 服务器ID号
server-id = 1
# 忽略mysql数据库复制
binlog-ignore-db=mysql
# 每次增长2
auto-increment-increment = 2
# 设置自动增长的字段的偏移量,即初始值为2
auto-increment-offset = 1
------------
# mysql -u root -p123456
在主1上为主2添加同步帐号
> grant replication slave on *.* to 'slave'@'192.168.7.249' identified by '123456';
将主1的数据库表全部备份,并发送到主2服务器上。
# /usr/local/mysql/bin/mysqldump -u root -p123456 --opt --skip-lock-tables --flush-logs --all-database > /root/allbak.sql
# cd ~
# scp allbak.sql root@192.168.7.249:/root
重启服务
# service mysqld restart
# mysql -u root -p123456;
配置连接到主2服务器(注意这步一定要在主2添加账户后执行)
> stop slave;
> change master to master_host='192.168.7.249',master_user='slave',master_password='123456';
> start slave;
2.主2 mysql配置(192.168.7.249)
删除之前遗留bin日志
# rm -rf /usr/local/mysql/log/bin*
修改mysql配置文件:
# vi /etc/my.cnf
添加:
--------------------
# Replication Master Server 2
# bin日志路径
log-bin = /usr/local/mysql/log/bin.log
server-id=2
# 忽略mysql数据库复制
replicate-ignore-db=mysql
# 每次增长2
auto-increment-increment = 2
# 设置自动增长的字段的偏移量,即初始值为2
auto-increment-offset = 2
---------------------
# mysql -u root -p123456
在主2上为主1添加同步帐号
> grant replication slave on *.* to 'slave'@'192.168.7.201' identified by '123456';
恢复主1的数据库到主2
#/usr/local/mysql/bin/mysql -u root -p123456
重启服务
# service mysqld restart
# mysql -u root -p123456;
配置连接到主1服务器
> stop slave;
> change master to master_host='192.168.7.201',master_user='slave',master_password='123456';
> start slave;
最后分别登陆两台服务器mysql后台查看主主备份连接状态
# mysql -u root -p123456;
> show slave status\G;
搜索这三行,如下则主主互备配置成功
Slave_IO_State: Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
当主1的数据库增加,删除,改变数据时,,主2也会同步更新。
当主2的数据库增加,删除,改变数据时,主1也会同步更新。
这样就可以配合keepalived实现数据库双机热备+数据同步互备,大大提高了MYSQL的可靠性和安全性。
Keepalived+MySQL互备

MySQLhandlesconcurrencyusingamixofrow-levelandtable-levellocking,primarilythroughInnoDB'srow-levellocking.ComparedtootherRDBMS,MySQL'sapproachisefficientformanyusecasesbutmayfacechallengeswithdeadlocksandlacksadvancedfeatureslikePostgreSQL'sSerializa

MySQLhandlestransactionseffectivelyusingtheInnoDBengine,supportingACIDpropertiessimilartoPostgreSQLandOracle.1)MySQLusesREPEATABLEREADasthedefaultisolationlevel,whichcanbeadjustedtoREADCOMMITTEDforhigh-trafficscenarios.2)Itoptimizesperformancewithabu

Best practices include: 1) Understanding the data structure and MySQL processing methods, 2) Appropriate indexing, 3) Avoid SELECT*, 4) Using appropriate JOIN types, 5) Use subqueries with caution, 6) Analyzing queries with EXPLAIN, 7) Consider the impact of queries on server resources, 8) Maintain the database regularly. These practices can make MySQL queries not only fast, but also maintainability, scalability and resource efficiency.

MySQLisbetterforspeedandsimplicity,suitableforwebapplications;PostgreSQLexcelsincomplexdatascenarioswithrobustfeatures.MySQLisidealforquickprojectsandread-heavytasks,whilePostgreSQLispreferredforapplicationsrequiringstrictdataintegrityandadvancedSQLf

MySQL processes data replication through three modes: asynchronous, semi-synchronous and group replication. 1) Asynchronous replication performance is high but data may be lost. 2) Semi-synchronous replication improves data security but increases latency. 3) Group replication supports multi-master replication and failover, suitable for high availability requirements.

The EXPLAIN statement can be used to analyze and improve SQL query performance. 1. Execute the EXPLAIN statement to view the query plan. 2. Analyze the output results, pay attention to access type, index usage and JOIN order. 3. Create or adjust indexes based on the analysis results, optimize JOIN operations, and avoid full table scanning to improve query efficiency.

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

The main reasons for slow MySQL query include missing or improper use of indexes, query complexity, excessive data volume and insufficient hardware resources. Optimization suggestions include: 1. Create appropriate indexes; 2. Optimize query statements; 3. Use table partitioning technology; 4. Appropriately upgrade hardware.


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

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
