Introduced
First understand the MySQL master and slave replication is what, we know that MySQL master-slave replication is based on Binlog replication, and the default MySQL master-slave replication method, in fact, asynchronous replication.
The main library does not really care about whether to pull the data from the library or not, nor do you care if the data is written into the hard drive storage, anyway, the data is lost, let the library run slowly, and actually this does not affect the main library any use of the situation.
Careful people will find that, in this case, if the main library temporarily hung, Binlog has not been transferred, even if the cluster is not guaranteed to say that the data consistency after the hang, because you can not exclude others in the main library is normal submission, and from the library no data situation.
Then in a certain bit of time, someone proposed the concept of synchronous replication, meaning that, as long as the library is not written, the main library will not return to completion, this time even if the main library is hung, this is only an unfinished transaction, from the library will not be recorded, to ensure data consistency.
It seems that data consistency is solved, but, the delay of each transaction increases, if it is a large transaction, it is more miserable, it depends on the network bandwidth can be able to live, otherwise the delay is lower, performance is worse.
Finally, after the mysql5.5, the semi-synchronization is put forward, meaning that each transaction replication, at least one guarantee from the library has received the Binlog, the main library returns the transaction complete, but does not need to ignore whether the library is written to the hard disk.
Although the delay is still there, but compared to the full synchronization is still much better, for the high data consistency requirements, sacrificing performance in exchange for a certain value, is inevitable.
Light to see the text is inevitably some virtual, you can look at the following picture to roughly understand, 650) this.width=650; "Src=" http://s2.51cto.com/wyfs02/M01/84/57/ Wkiom1en0fgdahftaadneqww5tu365.png-wh_500x0-wm_3-wmp_4-s_4286764192.png "title=" 20160121150458159.png "alt=" Wkiom1en0fgdahftaadneqww5tu365.png-wh_50 "/>
Everybody, keep an eye on the numbers.
The first part, the client submits the SQL statement,
The second part, submitted to the storage engine to resolve and process data modification,
In the third part, the transaction is finished in the main library, in the pending state, waiting for the final completion,
Fourth, at the same time submitted to each from the library, waiting to complete,
Fifth, return the status from the library to the main library, the tag is complete,
Sixth, return the client, the entire transaction is complete.
Looks good, master-slave data consistency has been greatly improved, but the delay is also unambiguous, is to sacrifice performance to improve the consistency of the problem.
However, I have to be responsible for the sentence, in the extreme case, still may throw some data, so regular decision from the data check or is necessary, such as the use of PT tools and so on, as for why, this does not imitate let us think about, and for large business, semi-synchronous is also relatively weak situation, performance loss is large.
Installation use
below to see how to install the use of semi-synchronous, most of the MySQL itself is not preloaded with semi-synchronized components, need to install additional, but the general MySQL package will bring the so file, so as long as the manual loading can be used.
First check if there's no outfit.
Mysql> Show plugins;
Find out if there are semisync letters, there is is installed, no is not installed.
Find MySQL Plugin directory location
Mysql> Show variables like ' plugin_dir '; +---------------+------------------------------------+| variable_name | Value |+---------------+------------------------------------+| Plugin_dir | /usr/local/mysql/lib/mysql/plugin/|+---------------+------------------------------------+1 row in Set (0.00 sec)
To see if a catalog file exists
[[email protected] ~]# ll /usr/local/mysql/lib/mysql/plugin/Total dosage 25328-rwxr-xr-x 1 root root 16916 3 Month 4 01:18 adt_ Null.so-rwxr-xr-x 1 root root 183009 3 Month 4 01:18 audit_log.so-rwxr-xr-x 1 root root 44095 3 Month 4 01:18 auth_pam_compat.so-rwxr-xr-x 1 root root 45929 3 Month 4 01:18 auth_pam.so-rwxr-xr-x 1 root root 27406 3 Month 4 01:18 auth.so-rwxr-xr-x 1 root root 13607 3 Month 4 01:18 auth_socket.so-rwxr-xr-x 1 root Root 26229 3 Month 4 01:18 auth_test_plugin.so-rw-r--r-- 1 root root 227 3 Month 3 21:27 daemon_example.inidrwxr-xr-x 2 root root 4096 3 Month 4 01:23 Debug-rwxr-xr-x 1 root root 4702357 3 Month 4 01:20 Dialog.so-rwxr-xr-x 1 root root 2212305 3 Month 4 01:18 Handlersocket.so-rwxr-xr-x 1 root root 14950125 3 Month 4 01:22 ha_tokudb.so-rwxr-xr-x 1 root root 834597 3 Month 4 01:18 innodb_engine.so-rwxr-xr-x 1 root root 39169 3 Month 4 01:18 libdaemon_example.so-rwxr-xr-x 1 root root 27048 3 Month 4 01:18 libfnv1a_udf.so-rwxr-xr-x 1 root Root 27089 3 Month  &NBSp; 4 01:18 libfnv_udf.so-rwxr-xr-x 1 root root 779519 3 Month 4 01:18 libmemcached.so-rwxr-xr-x 1 root root 28683 3 Month 4 01:18 libmurmur_udf.so-rwxr-xr-x 1 root root 17898 3 Month 4 01:18 mypluglib.so-rwxr-xr-x 1 Root root 12224 3 Month 4 01:18 mysql_no_ Login.so-rwxr-xr-x 1 root root 18574 3 Month 4 01:18 qa_auth_client.so-rwxr-xr-x 1 root root 27059 3 month 4 01:18 qa_auth_interface.so-rwxr-xr-x 1 root root 13877 3 Month 4 01:18 qa_auth_server.so-rwxr-xr-x 1 root root 395960 3 Month 4 01:18 query_response_time.so-rwxr-xr-x 1 root root 56417 3 Month 4 01:18 scalability_metrics.so-rwxr-xr-x 1 root root 537609 3 Month 4 01:18 semisync_master.so-rwxr-xr-x 1 Root root 289069 3 Month 4 01:18 semisync_slave.so-rwxr-xr-x 1 root root 357058 3 Month 4 01:18 tokudb_ Backup.so-rwxr-xr-x 1 root root 188402 3 Month 4 01:18 validate_password.so
See the following two, is the semi-synchronous components, one is the main library components, one from the library components, you can either load or only one.
Semisync_master.sosemisync_slave.so
The following begins the formal installation:
Executing on the main library
mysql> Install plugin rpl_semi_sync_master soname ' semisync_master.so ';mysql> show plugins; see this, it proves successful. Rpl_semi _sync_master | ACTIVE | REPLICATION | semisync_master.so | Gpl
And then in the run from the library
mysql> Install plugin rpl_semi_sync_slave soname ' semisync_slave.so ';mysql> show plugins; see this, it proves successful. Rpl_semi_ Sync_slave | ACTIVE | REPLICATION | semisync_slave.so | Gpl
When the installation is over, it's ready to start.
The main library is simple, just set up a half-sync to start it.
mysql> Set Global rpl_semi_sync_master_enabled = on;
Of course, you can also write to the configuration file, but write to the configuration file to restart to take effect, but also to reload the component, so this is not interesting.
Then to the slave library, the slave will restart the IO thread to take effect, otherwise the data will be copied asynchronously.
mysql> Set Global rpl_semi_sync_slave_enabled = on;mysql> stop slave io_thread;mysql> start slave io_thread;
Also you can write to the configuration file, I will not say more, anyway this is started.
Below to verify the success, the following is the main library information, because a lot of information from the library is not.
mysql> show status like '%rpl_semi_sync% '; +-------------------------------------------- +---------+| variable_name | value |+--------------------------------------------+---------+| rpl_semi_sync_master_ Clients | 1 | | Rpl_semi_sync_master_net_avg_wait_time | 746 | | rpl_semi_sync_master_net_wait_time | 3788497 | | Rpl_semi_sync_master_net_waits | 5077 | | rpl_semi_sync_master_no_times | 0 | | Rpl_semi_sync_master_no_tx | 0 | | Rpl_semi_sync_master_status | on | | Rpl_semi_sync_master_timefunc_failures | 0 | | Rpl_semi_sync_master_tx_avg_wait_time | 614 | | Rpl_semi_sync_master_tx_wait_time | 2797020 | | Rpl_semi_sync_master_tx_waits | 4552 | | rpl_semi_sync_master_wait_pos_backtraverse | 0 | | Rpl_semi_sync_master_wait_sessions | 0 | | Rpl_semi_sync_master_yes_tx | 5077 | | Rpl_semi_sync_slave_status | off |+-------------------------------------------- +---------+15 rows in set (0.00 sec)
Explain a few important values.
Rpl_semi_sync_master_status Whether the semi-sync is enabled.
Rpl_semi_sync_master_clients Semi-synchronous mode slave How many of them are there altogether.
Rpl_semi_sync_master_no_tx The number of failed transactions sent to slave.
Rpl_semi_sync_master_yes_tx The number of successful transactions sent to slave.
After reading the above analysis, probably you also know that my state is normal. Other delay categories, we have to look at the actual situation.
Let's see what we can set up. Parameters for semi-synchronous
mysql> show variables like '%rpl% '; +------------------------------------+----------+| Variable_name | value |+------------------------------ ------+----------+| rpl_semi_sync_master_enabled | on | | rpl_semi_sync_master_timeout | 10000 | | rpl_semi_sync_master_trace_level | 32 | | rpl_semi_sync_master_wait_no_slave | on | | rpl_semi_sync_slave_enabled | OFF | | rpl_semi_sync_slave_trace_level | 32 | | rpl_stop_slave_timeout | 31536000 |+------------------------------------+----------+7 rows in set (0.00  SEC)
Also take a look at parsing:
Rpl_semi_sync_master_timeout Master waits for the slave response time in milliseconds, the default value is 10 seconds, exceeds this time, slave is unresponsive, and the environment schema is automatically converted to asynchronous replication
Rpl_semi_sync_master_trace_level monitoring level, total 4 levels (1,16,32,64).
Whether Rpl_semi_sync_master_wait_no_slave allows master to wait for slave's receipt signal after each thing is committed. The default is on, each transaction will wait, if slave, when the slave catch up with the master log, you can automatically switch to semi-synchronous mode, if it is off, then slave catch up, will not be used in a semi-synchronous way to replicate, need to manually configure.
Rpl_stop_slave_timeout control the execution time of the stop slave, when replaying a large transaction, the sudden execution of the stop slave, the command stop slave will be executed for a long time, this time may produce deadlock or blocking, severely affect performance, MySQL 5 .6 You can control the execution time of the stop slave by using the Rpl_stop_slave_timeout parameter
There is no doubt that these parameters can be changed by configuration file or direct set, as long as you think this is appropriate for your needs, for example, we will set the response time to 1 seconds.
Finally, let's see what the same command does in the library.
mysql> show status like '%rpl_semi_sync% '; +-------------------------------------------- +-------+| variable_name | value |+--------------------------------------------+-------+| rpl_semi_sync_master_clients | 0 | | Rpl_semi_sync_master_net_avg_wait_time | 0 | | Rpl_semi_sync_master_net_wait_time | 0 | | Rpl_semi_sync_master_net_waits | 0 | | Rpl_semi_sync_master_no_times | 0 | | Rpl_semi_sync_master_no_tx | 0 | | Rpl_semi_sync_master_status | off | | Rpl_semi_sync_master_timefunc_failures | 0 | | Rpl_semi_sync_master_tx_avg_wait_time | 0 | | rpl_semi_sync_master_tx_wait_time | 0 | | Rpl_semi_sync_master_tx_waits | 0 | | rpl_semi_sync_master_wait_pos_backtraverse | 0 | | Rpl_semi_sync_master_wait_sessions | 0 | | Rpl_semi_sync_master_yes_tx | 0 | | Rpl_semi_sync_slave_status | on |+--------------------------------------------+------ -+15 rows in set (0.00 SEC)
As I just said, a lot of information is basically not, only the last line is not the same, but also only to prove the normal connection from the library.
Look at this again.
mysql> show variables like '%rpl% '; +------------------------------------+----------+| Variable_name | value |+------------------------------ ------+----------+| rpl_semi_sync_master_enabled | off | | rpl_semi_sync_master_timeout | 10000 | | rpl_semi_sync_master_trace_level | 32 | | rpl_semi_sync_master_wait_no_slave | on | | rpl_semi_sync_slave_enabled | ON | | rpl_semi_sync_slave_trace_level | 32 | | rpl_stop_slave_timeout | 31536000 |+------------------------------------+----------+7 rows in set (0.00  SEC)
is similar, purely is the state is not the same.
On the method of Mysql5.6 semi-synchronous master-Slave copying