mysql互为主从配置(双主模型)
一、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” 博客,请务必保留此出处

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

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.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

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

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.
