# 常见 MySQL 集群方案

## 一、常见 MySQL 集群方案

* Repliaction 集群方案
* PXC 集群方案（ Percona XtraDB Cluster

两种集群方案特性如下图：

![](/files/-LfnTL36aSfGJrFtUx_6)

## 二、PXC方案 和 Replication方案对比

**1. 先看看 PXC方案**

![](/files/-LfnTL38iV6z5E8b_7ob)

很明显 PXC方案在任何一个节点写入的数据都会同步到其他节点，数据**双向同步**的（在任何节点上都可以同时读写）。

**2. Replication 集群方案：**

![](/files/-LfnTL3Aa6LZwm89yAdt)

Replication方案只能在Master数据库进行写操作，在Slave数据库进行读操作。如果在Slave数据库中写入数据，Master数据库是不能知道的（**单向同步**的）。

**3. PXC 数据的强一致性**

PXC 采用同步复制，事务在所有集群节点要么同时提交，要么不提交。\
Replication 采用异步复制，无法保证数据的一致性。

* 下面看看 PXC写入操作：

![](/files/-LfnTL3ClPRnvmbgk0lv)

当一个写入请求到达PXC集群中的一个 mysql（node1数据库） 数据库时，node1数据库会将**该写入请求同步给集群中的其他所有数据库，等待所有数据库都成功提交事务后**，node1节点才会将写入成功的结果告诉给 node1的客户端。

> PXC 的强一致性对保存高价值数据时特别重要。

* 在看Replication集群写入操作：

![](/files/-LfnTL3EplrRKYF6IvBO)

当一个写入请求到达 Master数据库时，Master数据库执行写入操作，然后 Master 向客户端返回写入成功，**同时异步的复制写入操作给 Slave数据库，如果异步复制时出现问题，从数据库将无法执行写入操作，而客户端得到的是写入成功。**&#x8FD9;也是**弱一致性**的体现。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docker.shujuwajue.com/mysql-ji-qun/chang-jian-mysql-ji-qun-fang-an.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
