search
HomeDatabaseMysql TutorialMySQL双向复制简单配置步骤

MySQL从3.23版本起就已经内置了复制(Replication)的功能,我们可以配置成master-slave模式,也可以配置为master-master模式。方法

MySQL从3.23版本起就已经内置了复制(Replication)的功能,我们可以配置成master-slave模式,也可以配置为master-master模式。方法很简单,这里就master-master的配置简单的记录下来,我这里测试环境是mysql 5.1.31,一个是自己用源代码编译的,一个是用的xampp1.7里自带的。

首先找到两个版本相同的数据库服务器,最好是版本相同,或者差别太大(一个是3.x,一个是5.x,那估计没戏)。假设数据库的配置文件是/etc/my.cnf,我们假定A机的IP地址是192.168.0.1,B机的IP地址是192.168.0.2


我们在A机上给出一个授权用户,允许B机的某个账号拥有来获取要复制数据库的权利,这里假设针对所有的数据库都有复制的权利

mysql>grant file,replication slave on *.* to replicate@192.168.0.2 identified by ‘password’;

这样我们就创建了一个replicate的账号,密码是password。同样的方法在B机上也给A机一个授权

mysql>grant file,replication slave on *.* to replicate@192.168.0.1 identified by ‘password’;

接下来就是要修改配置文件了,首先打开A机的/etc/my.cnf文件,,在[mysqld]一段里,增加或者修改下面这些变量

server-id=1
log-bin=mysql-bin

master-host=192.168.0.2
master-user=replicate
master-password=password

保存退出。
同样的方法在B机上也做一遍,类似如下:

server-id=2
log-bin=mysql-bin

master-host=192.168.0.1
master-user=replicate
master-password=password

要注意,server-id千万别冲突了,当然,如果冲突了,数据库启动的时候会有报错的。

做完配置修改后,在重启数据库之前,我们还有做一件事请,那就是原始数据的同步,首先停止数据库,然后用A机的数据库全部覆盖B机的数据库(反之也行),总之,在重启之前,保证你需要同步的数据库应该是一致的(怎么说也得在同一个起跑线上嘛)。

好了,现在你可以重启你的数据库了。

你可以通过show master status; show slave status这样的SQL语句来查看对应的状态了。

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息。

本文链接:

linux

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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

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!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor