一、A、B双主模型的实现条件:1.开启二进制日志2.开启中继日志3.解决自动增长列的问题如果A服务器上自动增长的列编号有一个35,此时还没有同步到B服务器上,在B
一、A、B双主模型的实现条件:
1. 开启二进制日志
2. 开启中继日志
3. 解决自动增长列的问题
如果A服务器上自动增长的列编号有一个35,此时还没有同步到B服务器上,在B服务器上插入一条数据,,编号也是35。当同步A的35到B服务器上来的话,必然产生数据丢失。
解决办法:
让在A上插入的行的自动增长都为奇数,让B服务器上的自动增长都为偶数。这样就解决了自动增长的问题。
假设A是一台生产环境中的数据库,现在想添加B服务器,实现双主模型。
二、步骤:
1. 在A、B服务器上创建具有复制权限的帐号
2. 在A、B服务器上修改配置文件(开启二进制日志、中继日志等)
3. 将A服务器上存在的数据文件导入到B服务器中
注意:导入数据的时候,先关闭B服务器的二进制日志。
4. 让B先成为slave,再让A成为slave
5. 测试
三、开始配置
1. 创建授权用户
mysql> grant replication slave on *.* to 'slave'@'192.168.2.96' identified by '12345'; Query OK, 0 rows affected (0.00 sec) #A服务器 mysql> grant replication slave on *.* to 'slave'@'192.168.2.93' identified by '12345'; Query OK, 0 rows affected (0.00 sec) #B服务器2. 编辑配置文件
[root@oracle ~]# vim /etc/my.cnf log-bin=mysql-bin #开启二进制日志 server-id=1 relay-log=mysql-relay-bin #开启中继日志 log_slave_updates = on #从服务器将时间记录到二进制日志中 auto_increment_increment=2 #自动增长的步长 auto_increment_offset=1 #自动增长的起始数值 #A服务器的配置[root@node2 ~]# vim /etc/my.cnf server-id=2 #log-bin=mysql-bin #log_slave_updates = on auto_increment_increment=2 auto_increment_offset=2 #B服务器的配置,先关闭二进制日志重启服务
[root@oracle ~]# service mysqld restart Shutting down MySQL.. [ OK ] Starting MySQL. [ OK ]创建测试用的表
mysql> select * from info; +-----+-------+-----+ | sid | name | age | +-----+-------+-----+ | 1 | zhang | 23 | | 2 | li | 12 | | 3 | cheng | 34 | | 4 | wang | 22 | | 5 | chen | 44 | +-----+-------+-----+ 5 rows in set (0.00 sec) #在A服务器上创建测试用的表3. 将表导入到B服务器上
[root@oracle ~]# mysqldump --databases data --lock-all-tables --master-data=2 > /root/dump.sql [root@oracle ~]# scp /root/dump.sql root@192.168.2.96:/root/ #A服务器上dump+拷贝 [root@node2 ~]# mysql4. 让B服务器先成为slave
mysql> change master to master_host='192.168.2.93',master_user='slave',master_password='12345',master_port=3306,MASTER_LOG_FILE='mysql-bin.000007', MASTER_LOG_POS=1068; #MASTER_LOG_FILE和MASTER_LOG_POS在dump.sql中有记录[root@node2 ~]# vim /etc/my.cnf #将刚刚注释掉的参数生效 log-bin=mysql-bin log_slave_updates = on[root@node2 ~]# service mysqld restart Shutting down MySQL.. [确定] Starting MySQL. [确定]再让A服务器成为slave
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 120 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) #在B上查看二进制日志信息 mysql> change master to master_host='192.168.2.96',master_user='slave',master_password='12345',master_port=3306,MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=120; mysql> start slave;查看A、B服务器的状态:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.96 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 120 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes #A服务器上的状态mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.93 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000008 Read_Master_Log_Pos: 120 Relay_Log_File: mysql-relay-bin.000003 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000008 Slave_IO_Running: Yes Slave_SQL_Running: Yes #在B服务器上查看状态5. 测试:
mysql> insert into info(name,age) values('sun',25),('ding',29); #在A服务器上插入2行数据 mysql> select * from info; +-----+-------+-----+ | sid | name | age | +-----+-------+-----+ | 1 | zhang | 23 | | 2 | li | 12 | | 3 | cheng | 34 | | 4 | wang | 22 | | 5 | chen | 44 | | 7 | sun | 25 | | 9 | ding | 29 | +-----+-------+-----+ 7 rows in set (0.00 sec) #在B服务器上查看的结果这样双主模型就已经实现了。
本文出自 “My favorite technology” 博客,请务必保留此出处

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

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.

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

WebStorm Mac version
Useful JavaScript development tools
