mysql replation_MySQL Replicationation基础

摘要

一、MySQL Replication

介绍MySQL Replication的基本概念,摘自于Mysql官网

二、Replication Configuration

2.1 Basic Steps in Configuration

一、MySQL Replication

1. What is Replicaiton?

For the purposes of this article we define "Replication" as the duplication of data to one or more locations.

These can be co-located within a data center, or geographically distributed.

Replication enables a database to copy or duplicate changes from one physical location or system to another.

note:

Replication is relatively good for scaling reads, which you can direct to a slave, but it's not a good way to scale writes unless you design it right

Attaching many slaves to a master simple causes the writes to be done, once on each slave

Replication is also wasteful with more than a few slaves, because it essentially duplicated a lot of data needlessly.

2. Problems Solved by Replication

note:MySQL's replication is usually not very bandwidth-intensive, and you can stop and start it at will

Load balancing: Distribute read queries across serveral servers, which works very wll for read-intensive applications.

Backups

High availability and failover

Testing MySQL upgrades

3. Replication: Master-Slave

f4f8e38262cd409da08b4d741aafc99b.png

概念简要说明:

Binary log: Binary log (二进制日志) is a Set of files :

Contains all writes and schema changes

!= REDO/Transaction log

Rotated when full (Set max_binlog_size)

Incrementing numbers (defaults, mysql-bin.000001,0000002,0000003...)

Relay Logs are alo binary logs

With 2 Formats: Statement Based(SBR), Row based(RBR, since mysql5.1)

Relay log: 中继日志

Binlog Dump Thread:

To send the binary logcontens to the slave.

A master that has multiple slaves "attached" to it creates one binlog dump thread for each currently connected slave, "with each slave having its own I/O and SQL threads."

Slave I/O Thread:

A START SLAVE statement creates an I/O thread, which connects to the master and asks it to send the updates recorded in its binary logs

Slave SQL Thread:

To read the relay logs that were written by the slave I/0 thread and executes the updates contained in the relay logs

If using multi-threaded slaves, mutiple SQL threads will be created on the slave.

4. Replication Modes and Data Consistency

Asynchronous Replication:By default, MySQL is asynchronus.

Updates are committed to the database on the master and than relayed(重播,转发) to the slave where they are also applied.

The master does not wait for the slave to receive the update, and so is able to continue processing further write operations without as it waits for acknowledgement from the slave.

c896b53bb389f4fba217c422ffa4c20a.png

Semi-Synchronous Replication

Using semi-synchronous replication, a commit is returned to the client only when a slave has received the update, or a timeout occurs.

Therrfore it is assured that the data exists on the master and at least one slave (note that the slave will have received the update but not necessarily applied it when a commit is returned to the master).

5. MySQL Replication workflow

6b6a6940637f90423cf95dce44cdfc3d.png

二、Replication Configuration

2.1 Basic Steps in Replication

step1: Configure one server to be a master

step2: Configure one server to be a slave

step3: Connect the slave to the master

(1) Configure one server to be a master

To configure a server so that it can act as master, ensure the server has an active binary log and a unique server ID.

The Server ID is used to distinguish two servers from each other.

To set up the binary log and Server ID, you have to take the server down and add the log-bin,log-bin-index and server-id options to the my.cnf configuration file.

5eecb2b6cc2ae44f3dca58bf36a5e768.png

notes:

1. It is not necessary to give a name in the log-bin option, The default value is hostname-bin;

2. It is a good idea to create a name that is unique server and not tied to the machine the server is running on, since it can be confusing to work with a series of binlog files that suddenly change name midstream;

3. Each server is identified by a unique server ID;

(2) Creating Replication Accounts

Must create a user account on the master and give it the proper privileges, so the I/O thread can connect as  that user and read the master's binary log

9dd28b88a3e2ea6414e5650943b1130f.png

(3) Configuration the Slave

server ID also

you may also want to consider adding the names of relay log and the relay log index files to the my.cnf file using the options relay-log and relay-log-index

811dbd4c55d96b914f227cddeb5cfb9f.png

(4) Connecting the Master and Slave

Final step: directing the slave to the master so that it knows where to replicate

It also lets you point the slave at a different master  in the future, without stopping the server.

4feac1552d6e3765b7cce396f2a3d91f.png

03a8392f584bf8b7388c59ffbc07c4bc.png

(5) Monitoring Replication

1d8e1e0a15213c21b4ef8d96ed0884ae.png

c8237ce8d3be0757a58faa0c88035359.png

65c0942255b78b8bff780212a697423a.png

(6) Watching Replication in Action

7bfe3a52dcbc150f83e4122f65b7b29c.png

Event_type:

This is the type of the event.

Server_id:

This is the server ID of the server that created the event

Log_name;

The name of the file that stores the event

Pos:

This is the position of the file where the event starts; that is, it's the first byte of the event/

End_log_pos:

This gives the position in the file where the event ends and the next event starts

Info:

This is human-readable text with information about the event.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值