search
HomeDatabaseMysql TutorialMysql主主复制架构配置_MySQL

bitsCN.com

MySQL主主复制结构区别于主从复制结构。在主主复制结构中,两台服务器的任
何一台上面的数据库存发生了改变都会同步到另一台服务器上,这样两台服务器
互为主从,并且都能向外提供服务。 这就比使用主从复制具有更好的性能。
接下来我将使用两个同样的服务器来实现这个效果:
具体Mysql的安装我就省略了,在上一篇的Mysql的主从架构的配置中有详细介绍
server1_mysql:192.168.1.108
server2_mysql: 192.168.1.110
拓扑结构:
server1_mysql-------server2_mysql

1.创建用户并授权
server1:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'server2'@'192.168.1.110'
IDENTIFIED BY 'server2';
server2:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'server1'@'192.168.1.108'
IDENTIFIED BY 'server1';

2.修改Mysql的主配置文件
server1:
 [mysqld]
 server-id = 10
 log-bin = mysql-bin
 replicate-do-db = mydb
 auto-increment-increment = 2   //每次增长2
 auto-increment-offset = 1  //设置自动增长的字段的偏移量,即初始值为2
启动Mysql服务:
# service mysqld restart
server2:
 [mysqld]
 server-id = 20
 log-bin = mysql-bin
 replicate-do-db = mydb
 auto-increment-increment = 2  //每次增长2
 auto-increment-offset = 2 //设置自动增长的字段的偏移量,即初始值为2
启动Mysql服务:
# service mysqld restart
注:二都只有server-id不同和 auto-increment- offset不同
auto-increment-increment的值应设为整个结构中服务器的总数,本案例用到两
台服务器,所以值设为2。
3.重新启动两个服务器
# service mysqld restart
4.为了让两个数据库一样,我们备份其中一个数据库,然后在另一个数据库上恢
复,这样是两个数据库一开始都是一样的。
在server1上操作:
# mysqldump --databases luowei > /tmp/luowei.sql
在server2上操作:
创建一个与mydb同名的空数据库
# mysql
    > CREATE DATABASE mydb;
    >/q
# scp 192.168.1.108:/tmp/mydb.sql  ./
# mysql -uroot -p mydb 5.然后两个服务器相互通告二进制日志的位置并启动复制功能:
在server1上:
# mysql
   > CHANGE MASTER TO
   > MASTER_HOST='192.168.1.110',
   > MASTER_USER='server2',
   > MASTER_PASSWORD='server2';
mysql > START SLAVE;
在server2上:
# mysql
   > CHANGE MASTER TO
   > MASTER_HOST='192.168.1.108',
   > MASTER_USER='server1',
   > MASTER_PASSWORD='server1';
mysql > START SLAVE;
6.查看,并验证:
分别在两个数据库服务器上查看
mysql > START SLAVE;
然后查看数据库和表,你会发现内容是一样的,这样就是整个主主Mysql的架构
的配置过程。
 
 
 
 
作者“IT梦-齐-分享”

bitsCN.com
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

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.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

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

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

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.

MySQL: How to Add a User RemotelyMySQL: How to Add a User RemotelyMay 12, 2025 am 12:10 AM

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

The Ultimate Guide to MySQL String Data Types: Efficient Data StorageThe Ultimate Guide to MySQL String Data Types: Efficient Data StorageMay 12, 2025 am 12:05 AM

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

MySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMay 11, 2025 am 12:13 AM

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.

MySQL: Should I use root user for my product?MySQL: Should I use root user for my product?May 11, 2025 am 12:11 AM

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

MySQL String Data Types Explained: Choosing the Right Type for Your DataMySQL String Data Types Explained: Choosing the Right Type for Your DataMay 11, 2025 am 12:10 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

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.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft