MySql5.5半同步复制(Semi-sync Repication)
MySql5.5半同步复制(Semi-syncRepication)..
MySql5.5半同步复制
(Semi-sync Repication)
目 录
作者:贺春旸
半同步复制介绍(Semi-sync Repication)
默认情况下,MySQL5.1/5.5 的复制功能是异步的,这意味着当谈到数据一致性时,主服务器及其从服务器是独立的。异步复制可以提供最佳的性能,因为主服务器在将更新的数据写入它的二进制日志(Binlog)文件中后,无需等待验证更新数据是否已经复制到至少一台拓扑从服务器中,就可以自由处理其它进入的事务处理请求。虽然快,但这也同时带来了很高的风险,香港服务器,如果在主服务器或从服务器端发生故障,会造成主服务器/从服务器数据的不一致,甚至在恢复时造成数据丢失。
MySQL5.5 引入了一种半同步复制功能,该功能可以确保主服务器和访问链中至少一台从服务器之间的数据一致性和冗余。在这种配置结构中,一台主服务器和其许多从服务器都进行了配置,这样在复制拓扑中,至少有一台从服务器在父主服务器进行事务处理前,必须确认更新已经收到并写入了其中继日志(Relay Log)。当出现超时,源主服务器必须暂时切换到异步复制模式重新复制,直到至少有一台设置为半同步复制模式的从服务器及时收到信息。
半同步复制模式必须在主服务器和从服务器端同时启用,否则主服务器默认使用异步复制模式。
半同步复制安装配置
mysql-advanced-5.5.20二进制版,编译好的插件在目录
/usr/local/mysql/lib/plugin下:
[root@vm01 plugin]# pwd
/usr/local/mysql/lib/plugin
[root@vm01 plugin]#
[root@vm01 plugin]# ll -h semisync_*
-rwxr-xr-x 1 mysql mysql 147K 11-23 23:38 semisync_master.so
-rwxr-xr-x 1 mysql mysql 80K 11-23 23:38 semisync_slave.so
在master和slave库首次启动时,安装插件,并开启。
install plugin rpl_semi_sync_master soname 'semisync_master.so';
SET GLOBAL rpl_semi_sync_master_enabled=ON;
install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
SET GLOBAL rpl_semi_sync_slave_enabled =ON;
在初次加载插件后,MySQL会将该插件记录到系统表mysql.plugin中,下次启动时系统则会自动加载该插件了,无需再次执行上面的命令。
在my.cnf配置文件里,加入
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_slave_enabled = 1
这样以后启动MYSQL时会自动开启半同步复制。
参数说明
半同步复制的配置参数较少,在master主库上有4个
rpl_semi_sync_master_enabled = ON(表示在master上已经开启半同步复制模式)
rpl_semi_sync_master_timeout = 10000(默认为10000毫秒,等于10秒,这个参数动态可调,表示主库在某次事务中,如果等待时间超过10秒,那么则降级为异步复制模式,不再等待SLAVE从库。如果主库再次探测到,SLAVE从库恢复了,则会自动再次回到半同步复制模式。)
rpl_semi_sync_master_wait_no_slave(是否允许master每个事务提交后都要等待slave的接收确认信号。默认为on,每一个事务都会等待。如果为off,则slave追赶上后,也不会开启半同步复制模式,需要手工开启。)
rpl_semi_sync_master_trace_level = 32(用于开启半同步复制模式时的调试级别,默认是32)
· The semisynchronous replication debug trace level on the master. Currently, four levels are defined:
o 1 = general level (for example, time function failures)
o 16 = detail level (more verbose information)
o 32 = net wait level (more information about network waits)
o 64 = function level (information about function entry and exit)
在slave从库上有2个
rpl_semi_sync_slave_enabled = ON(表示在slave上已经开启半同步复制模式)
rpl_semi_sync_slave_trace_level = 32(用于开启半同步复制模式时的调试级别,默认是32)
★一个重要的参数relay_log_recovery,mysql5.5的新特性。
这个参数的作用是:当slave从库宕机后,假如Relay-Log损坏了,导致一部分中继日志没有处理,则自动放弃所有未执行的relay-log,并且重新从MASTER上获取日志,这样保证relay-log的完整。默认情况下该功能是关闭的,免备案空间,将relay_log_recovery的值设置为 1时,可在slave从库上开启该功能。
·
Enables automatic relay log recovery immediately following server startup, which means that the replication slave discards all unprocessed relay logs and retrieves them from the replication master. This should be used following a crash on the replication slave to ensure that no possibly corrupted relay logs are processed. The default value is 0 (disabled). This global variable can be changed dynamically, or by starting the slave with the option.
功能测试
1 如何验证半同步复制是否正常工作?
安装完毕后,就要配置主从同步,配置的方法和mysql5.1一样。
首先,香港服务器租用,在master库上,/usr/local/mysql/bin/mysqldump -uroot -p123456 -q --single-transaction --master-data=2 -A > alldata.sql
然后,在slave库上,导入完毕后,
CHANGE MASTER TO MASTER_HOST='192.168.8.22',MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=107;
start slave;
开启同步。
如何验证是否半同步复制模式已经开始工作了呢?
在master主库上
mysql> show status like '%semi%';
+----------------------------------------------------------------------+-------+

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

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

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
