MySQL5.5 除了支持内置的异步复制机制,还提供了接口支持半同步复制的机制。MySQL复制默认是异步复制,Master将事件写入binlog,
MySQL Semisynchronous Replication
MySQL5.5 除了支持内置的异步复制机制,还提供了接口支持半同步复制的机制。
异步复制的缺点:
MySQL复制默认是异步复制,Master将事件写入binlog,但并不知道Slave是否或何时已经接收且已处理。在异步复制的机制的情况下,如果Master宕机,事务在Master上已提交,但很可能这些事务没有传到任何的Slave上。假设有Master->Salve故障转移的机制,此时Slave也可能会丢失事务。
半同步复制的概念:
i.
当Slave主机连接到Master时,能够查看其是否处于半同步复制的机制。
ii.
当Master上开启半同步复制的功能时,至少应该有一个Slave开启其功能。此时,一个线程在Master上提交事务将受到阻塞,直到得知一个已开启半同步复制功能的Slave已收到此事务的所有事件,或等待超时。
iii.
当一个事务的事件都已写入其relay-log中且已刷新到磁盘上,Slave才会告知已收到。
iv.
如果等待超时,也就是Master没被告知已收到,此时Master会自动转换为异步复制的机制。当至少一个半同步的Slave赶上了,Master与其Slave自动转换为半同步复制的机制。
v.
半同步复制的功能要在Master,Slave都开启,半同步复制才会起作用;否则,只开启一边,它依然为异步复制。
同步,异步,半同步复制的比较:
同步复制:Master提交事务,直到事务在所有的Slave都已提交,此时才会返回客户端,事务执行完毕。缺点:完成一个事务可能会有很大的延迟。
异步复制:当Slave准备好才会向Master请求binlog。缺点:不能保证一些事件都能够被所有的Slave所接收。
半同步复制:半同步复制工作的机制处于同步和异步之间,Master的事务提交阻塞,只要一个Slave已收到该事务的事件且已记录。它不会等待所有的Slave都告知已收到,且它只是接收,并不用等其完全执行且提交。
半同步复制的控制变量,状态监控变量:
控制变量
Variable Name Variable Scope Dynamic Variable Type Default Effect Host
rpl_semi_sync_master_enabled Global Yes boolean OFF Master
rpl_semi_sync_slave_enabled Global Yes boolean OFF Slave
rpl_semi_sync_master_timeout Global Yes numeric 10000 Master
解释:
rpl_semi_sync_master_enabled是控制Master是否开启半同步,开启或不开启,将其设置为ON或OFF(1or0).
rpl_semi_sync_master_timeout是控制Master等待多长时间被告知Slave已收到,也就是所谓的超时时间。
rpl_semi_sync_slave_enabled是控制Slave是否开启半同步,开启或不开启,将其设置为ON或OFF(1or0)。
监控半同步复制的状态变量(几个常用的)
Rpl_semi_sync_master_clients:查看有多少个开启半同步复制的插件的Slave
Rpl_semi_sync_master_status:查看在Master上半同步复制是否正在运行,其值为ON时,说明Master已启用半同步且已被告知有Slave收到;其值为OFF时,说明Master没启用半同步或是没被告知,,由于timeout等原因。
Rpl_semi_sync_master_no_tx:查看有多少事务没有用半同步复制的机制进行复制。
Rpl_semi_sync_master_yes_tx:查看有多少事务是通过半同步复制机制成功复制。
Rpl_semi_sync_slave_status:查看Slave上半同步复制是否正常运行,其值为ON时,说明Slave正通过半同步复制且Slave I/O正在运行;为OFF时,反之。
说明关于其它的请参阅:
半同步复制的安装,配置
环境要求:
i.
MySQL5.5或以上版本
ii.
在MySQL上安装插件需要数据库支持动态载入。检查是否支持,用如下检测:
mysql> show global variables like 'have_dynamic_loading';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| have_dynamic_loading | YES |
+----------------------+-------+
1 row in set (0.00 sec)
iii.半同步复制是基于复制的环境。也就是说配置半同步复制前,已有复制的环境。
安装:
在Master上执行:
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
各个Slave上执行:
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
如果不清楚Plugin的目录,用如下查找:
mysql> show global variables like 'plugin_dir';
+---------------+----------------------------------+
| Variable_name | Value |
+---------------+----------------------------------+
| plugin_dir | /opt/usr/local/mysql/lib/plugin/ |
+---------------+----------------------------------+
检查Plugin是否已正确安装:
mysql> show plugins;
or
mysql> select * from information_schema.plugins;
配置:
在Master上执行:
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
mysql> SET GLOBAL rpl_semi_sync_master_timeout = N;
在Slave上执行:
mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
说明:
如果在一个正在运行的Slave上开启半同步复制的功能,必须先停止Slave I/O,将其启用半同步后,再开启Slave I/O.
mysql> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;
如果不这样做,Slave还是会以异步的方式进行复制。
正如大家所知,如果不将变量的设置写到配置文件,下次重启数据库,将失效。写入配置文件:
Master上:
[mysqld]
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000 # 1 second
Slave上:
[mysqld]
rpl_semi_sync_slave_enabled=1
总结:半同步复制个人感觉是维持数据完整性,安全性的的一个策略,虽会损失一点性能,还是值得的。配置很简单,关键是理解其工作机制。

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

AI Hentai Generator
Generate AI Hentai for free.

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.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool