一、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” 博客,请务必保留此出处

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.