search
HomeDatabaseMysql TutorialMySQL的半同步复制_MySQL

1>半同步复制的出现:

 

默认情况下,复制是异步的,就是客户端提交事务给主库,主库将事务写入到存储引擎和binlog中后会立即返回给客户端告诉其事务执行成功。如果此时该事务还未来得及复制到从库上,如果主库在此时发生崩溃或者服务器宕机,会导致主从切换,此时客户端访问新选举的主库时,就会看不到刚提交的数据。

 

2>半同步复制的原理:

 

mysql5.5开始通过插件的方式支持半同步复制,主库执行完客户端提交的事务后不会立即返回给客户端,而是等待至少一个从库接收到该事务后才返回给客户端。

 

半同步复制是全同步和异步同步的一个折中方案。与异步复制相比,半同步复制提高数据的安全性---可以保证至少两个地方存在该事务,同时也增加了一定的延迟(至少是一个tcp/ip往返的时间),最好在低延迟的网络中使用,比如局域网。

MySQL的半同步复制_MySQL

3>半同步复制的安装配置

 

须满足以下条件:

 

MySQL5.5或者以上版本

 

has_dynamic_loading系统变量设置为YES

 

复制已经配置并且在运行。

 

半同步复制是以插件形式实现的。所以讲semisync_master*文件复制到主库的plugins文件夹中,讲semisync_slave*文件复制到所有从库的plugins文件夹中。

 

接下来分别在主库和从库上执行INSTALL PLUGIN命令加载插件。

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME "semisync_master.so";

 

从库上执行

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME "semisync_slave.so";

 

查看插件的安装情况:

mysql>SHOW PLUGINS;

 

主从都开启半同步插件:

 

   主库上开启半同步插件:

   mysql>SET GLOBAL rpl_semi_sync_master_enabled = 1;

   mysql>SET GLOBAL rpl_semi_sync_master_timeout = N;//单位是毫秒

 

   在从库上开启半同步插件:

   mysql>SET GLOBAL rpl_semi_sync_slave_enabled = 1;

 

rpl_semi_sync_master_timeout表示主库等待从库相应的超市时间,如果在时间内没有收到从库相应,复制切换到异步模式。

 

停止slave IO 线程,然后重新开启该线程。以使slave IO 线程从新连接到主库,并以半同步方式在主库注册。

 

在从库上执行以下命令重启slave IO线程:

mysql>STOP SLAVE IO_THREAD;

mysql>START SLAVE IO_THREAD;

 

注意点:

 

在mysql启动前将全局变量写入到配置文件中。以避免忘记设置使半同步不生效。

 

主库配置文件

 [mysqld]

 rpl_semi_sync_master_enabled = 1

 rpl_semi_sync_master_timeout=10000 #10秒

 

从库配置文件

 [mysqld]

 rpl_semi_sync_slave_enabled = 1

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
How to solve the problem of mysql cannot open shared libraryHow to solve the problem of mysql cannot open shared libraryMar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

Reduce the use of MySQL memory in DockerReduce the use of MySQL memory in DockerMar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How do you alter a table in MySQL using the ALTER TABLE statement?How do you alter a table in MySQL using the ALTER TABLE statement?Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin)Run MySQl in Linux (with/without podman container with phpmyadmin)Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overviewWhat is SQLite? Comprehensive overviewMar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

How do I configure SSL/TLS encryption for MySQL connections?How do I configure SSL/TLS encryption for MySQL connections?Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Running multiple MySQL versions on MacOS: A step-by-step guideRunning multiple MySQL versions on MacOS: A step-by-step guideMar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.