翼度科技»论坛 编程开发 mysql 查看内容

MySQL 8.0 Reference Manual(读书笔记90节--Replication)

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
1.
The following options also have an impact on the source:
• For the greatest possible durability and consistency in a replication setup using InnoDB with transactions, you should use innodb_flush_log_at_trx_commit=1 and sync_binlog=1 in the source's my.cnf file.
• Ensure that the skip_networking system variable is not enabled on the source. If networking has been disabled, the replica cannot communicate with the source and replication fails.
2.The default server_id value is 1.
The default server_id value from MySQL 8.0 is 1.Note that if a value of 0 (which was the default in earlier releases) was set previously for the server ID, you must restart the server to initialize the source with your new nonzero server ID.
Note that a value of 0 for the server ID prevents a replica from connecting to a source. If that server ID value (which was the default in earlier releases) was set previously, you must restart the server to initialize the replica with your new nonzero server ID. Otherwise【ˈʌðərwaɪz 否则;另;不然;在其他方面;亦;除此以外;】, a server restart is not needed when you change the server ID, unless you make other configuration changes that require it. For example, if binary logging was disabled on the server and you want it enabled for your replica, a server restart is required to enable this.
3.级联复制
Binary logging is enabled by default on all servers. A replica is not required to have binary logging enabled for replication to take place. However, binary logging on a replica means that the replica's binary log can be used for data backups and crash recovery. Replicas that have binary logging enabled can also be used as part of a more complex replication topology. For example, you might want to set up replication servers using this chained arrangement:
  1. A -> B -> C
复制代码
Here, A serves as the source for the replica B, and B serves as the source for the replica C. For this to work, B must be both a source and a replica. Updates received from A must be logged by B to its binary log, in order to be passed on to C. In addition to binary logging, this replication topology requires the system variable log_replica_updates (from MySQL 8.0.26) or log_slave_updates (before MySQL 8.0.26) to be enabled. With replica updates enabled, the replica writes updates that are received from a source and performed by the replica's SQL thread to the replica's own binary log. The log_replica_updates or log_slave_updates system variable is enabled by default.
If you need to disable binary logging or replica update logging on a replica, you can do this by specifying the --skip-log-bin and --log-replica-updates=OFF or --log-slave-updates=OFF options for the replica. If you decide to re-enable these features on the replica, remove the relevant options and restart the server.
4.Creating a Data Snapshot Using mysqldump
By default, if GTIDs are in use on the source (gtid_mode=ON), mysqldump includes the GTIDs from the gtid_executed set on the source in the dump output to add them to the gtid_purged set on the replica. If you are dumping only specific databases or tables, it is important to note that the value that is included by mysqldump includes the GTIDs of all transactions in the gtid_executed set on the source, even those that changed suppressed【səˈprest 被抑制的;(病)症状不显明的;被删去的;】 parts of the database, or other databases on the server that were not included in the partial【ˈpɑːrʃl 部分的,不完全的;偏的;偏向一方的,偏袒的,不公平的;偏爱的,癖好的;】 dump. Check the description for mysqldump's --set-gtid-purged option to find the outcome of the default behavior for the MySQL Server versions you are using, and how to change the behavior if this outcome is not suitable for your situation.
  1. /usr/local/mysql/bin/mysqldump --master-data=2 -u用户 -p密码 --databases 指定数据库 --set-gtid-purged=off --single-transaction -R --triggers > /指定路径/tmp_xxxxx.sql
复制代码
 否则,会遇到如下错误
  1. ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
复制代码
 5.注意scheduled events
If the replication source server or existing replica that you are copying to create the new replica has any scheduled events, ensure that these are disabled on the new replica before you start it. If an event runs on the new replica that has already run on the source, the duplicated operation causes an error. The Event Scheduler is controlled by the event_scheduler system variable, which defaults to ON from MySQL 8.0, so events that are active on the original server run by default when the new replica starts up. To stop all events from running on the new replica, set the event_scheduler system variable to OFF or DISABLED on the new replica. Alternatively, you can use the ALTER EVENT statement to set individual events to DISABLE or DISABLE ON SLAVE to prevent them from running on the new replica. You can list the events on a server using the SHOW statement or the Information Schema EVENTS table.
6.
GTID assignment distinguishes between client transactions, which are committed on the source, and replicated transactions, which are reproduced on a replica. When a client transaction is committed on the source, it is assigned a new GTID, provided that the transaction was written to the binary log. Client transactions are guaranteed【ɡærənˈtiːd 保证;保障;担保;确保;使必然发生;提供(产品)保修单(免费掉换或修理有问题的产品);】 to have monotonically【mɒnə'tɒnɪklɪ 单调地;单调地,无变化地;】 increasing GTIDs without gaps between the generated numbers. If a client transaction is not written to the binary log (for example, because the transaction was filtered out, or the transaction was read-only), it is not assigned a GTID on the server of origin.
Replicated transactions retain the same GTID that was assigned to the transaction on the server of origin. The GTID is present before the replicated transaction begins to execute, and is persisted【pərˈsɪstɪd 持续存在;保持;维持;顽强地坚持;执著地做】 even if the replicated transaction is not written to the binary log on the replica, or is filtered out on the replica. The MySQL system table mysql.gtid_executed is used to preserve【prɪˈzɜːrv 保存;保护;保留;维护;保鲜;保养;贮存;维持…的原状;使继续存活;】 the assigned GTIDs of all the transactions applied on a MySQL server, except those that are stored in a currently active binary log file.
只会执行一次
The auto-skip function for GTIDs means that a transaction committed on the source can be applied no more than once on the replica, which helps to guarantee consistency. Once a transaction with a given GTID has been committed on a given server, any attempt to execute a subsequent【ˈsʌbsɪkwənt 随后的;之后的;后来的;接后的;】 transaction with the same GTID is ignored by that server. No error is raised, and no statement in the transaction is executed.
7.
If a transaction with a given GTID has started to execute on a server, but has not yet committed or rolled back, any attempt to start a concurrent【kənˈkɜːrənt 同时发生的;同意的,一致的;有相等权力的,同时(实施)的;合作的;协调的;并存的;共点的,会合的;共同(或同时)起作用的;】 transaction on the server with the same GTID blocks. The server neither begins to execute the concurrent transaction nor returns control to the client. Once the first attempt at the transaction commits or rolls back, concurrent sessions that were blocking on the same GTID may proceed. If the first attempt rolled back, one concurrent session proceeds【proʊˈsiːdz , ˈproʊsiːdz 行进;前往;继续做(或从事、进行);接着做;继而做;】 to attempt the transaction,and any other concurrent sessions that were blocking on the same GTID remain blocked. If the first attempt committed, all the concurrent sessions stop being blocked, and auto-skip all the statements of the transaction.
 8.GTID Sets
A GTID set is a set comprising【kəmˈpraɪzɪŋ 包括;包含;组成;构成;由…组成;是(某事物的)组成部分;】 one or more single GTIDs or ranges of GTIDs. GTID sets are used in a MySQL server in several ways. For example, the values stored by the gtid_executed and gtid_purged system variables are GTID sets. The START REPLICA (or before MySQL 8.0.22, START SLAVE) clauses UNTIL SQL_BEFORE_GTIDS and UNTIL SQL_AFTER_GTIDS can be used to make a replica process transactions only up to the first GTID in a GTID set, or stop after the last GTID in a GTID set. The built-in functions GTID_SUBSET() and GTID_SUBTRACT() require GTID sets as input.
A range of GTIDs originating from the same server can be collapsed into a single expression, as shown here:
  1. 3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5
复制代码
The above example represents the first through fifth transactions originating【əˈrɪdʒɪneɪtɪŋ 起源;创建;发明;发端于;发源;创立;】 on the MySQL server whose server_uuid is 3E11FA47-71CA-11E1-9E33-C80AA9429562. Multiple single GTIDs or ranges of GTIDs originating from the same server can also be included in a single expression, with the GTIDs or ranges separated by colons, as in the following example:
  1. 3E11FA47-71CA-11E1-9E33-C80AA9429562:1-3:11:47-49
复制代码
A GTID set can include any combination of single GTIDs and ranges of GTIDs, and it can include GTIDs originating from different servers. This example shows the GTID set stored in the gtid_executed system variable (@@GLOBAL.gtid_executed) of a replica that has applied transactions from more than one source:
  1. 2174B383-5441-11E8-B90A-C80AA9429562:1-3, 24DA167-0C0C-11E8-8442-00059A3C7B00:1-19
复制代码
When GTID sets are returned from server variables, UUIDs are in alphabetical【ˌælfəˈbetɪkl 按字母顺序排列的;按字母(表)顺序的;】 order, and numeric intervals are merged and in ascending order.

来源:https://www.cnblogs.com/xuliuzai/p/18154077
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具