feat: add more
This commit is contained in:
13
src/mariadb-galera/.env.example
Normal file
13
src/mariadb-galera/.env.example
Normal file
@@ -0,0 +1,13 @@
|
||||
# MariaDB version (must support Galera)
|
||||
MARIADB_VERSION="11.7.2"
|
||||
|
||||
# MariaDB root password
|
||||
MARIADB_ROOT_PASSWORD="galera"
|
||||
|
||||
# Galera cluster configuration
|
||||
MARIADB_GALERA_CLUSTER_NAME="galera_cluster"
|
||||
|
||||
# Port overrides for each node
|
||||
MARIADB_PORT_1_OVERRIDE=3306
|
||||
MARIADB_PORT_2_OVERRIDE=3307
|
||||
MARIADB_PORT_3_OVERRIDE=3308
|
||||
89
src/mariadb-galera/README.md
Normal file
89
src/mariadb-galera/README.md
Normal file
@@ -0,0 +1,89 @@
|
||||
# MariaDB Galera Cluster
|
||||
|
||||
[English](./README.md) | [中文](./README.zh.md)
|
||||
|
||||
This service deploys a 3-node MariaDB Galera Cluster for high availability and synchronous multi-master replication.
|
||||
|
||||
## Services
|
||||
|
||||
- `mariadb-galera-1`: First MariaDB Galera node (bootstrap node).
|
||||
- `mariadb-galera-2`: Second MariaDB Galera node.
|
||||
- `mariadb-galera-3`: Third MariaDB Galera node.
|
||||
|
||||
## Environment Variables
|
||||
|
||||
| Variable Name | Description | Default Value |
|
||||
| --------------------------- | --------------------- | ---------------- |
|
||||
| MARIADB_VERSION | MariaDB image version | `11.7.2` |
|
||||
| MARIADB_ROOT_PASSWORD | Root user password | `galera` |
|
||||
| MARIADB_GALERA_CLUSTER_NAME | Galera cluster name | `galera_cluster` |
|
||||
| MARIADB_PORT_1_OVERRIDE | Node 1 port | `3306` |
|
||||
| MARIADB_PORT_2_OVERRIDE | Node 2 port | `3307` |
|
||||
| MARIADB_PORT_3_OVERRIDE | Node 3 port | `3308` |
|
||||
|
||||
Please modify the `.env` file as needed for your use case.
|
||||
|
||||
## Volumes
|
||||
|
||||
- `mariadb_galera_1_data`: Node 1 data storage.
|
||||
- `mariadb_galera_2_data`: Node 2 data storage.
|
||||
- `mariadb_galera_3_data`: Node 3 data storage.
|
||||
|
||||
## Usage
|
||||
|
||||
### Start the Cluster
|
||||
|
||||
```bash
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
The first node (mariadb-galera-1) bootstraps the cluster with `--wsrep-new-cluster`. The other nodes join automatically.
|
||||
|
||||
### Connect to the Cluster
|
||||
|
||||
Connect to any node:
|
||||
|
||||
```bash
|
||||
mysql -h 127.0.0.1 -P 3306 -u root -p
|
||||
```
|
||||
|
||||
Or:
|
||||
|
||||
```bash
|
||||
mysql -h 127.0.0.1 -P 3307 -u root -p
|
||||
mysql -h 127.0.0.1 -P 3308 -u root -p
|
||||
```
|
||||
|
||||
### Check Cluster Status
|
||||
|
||||
```sql
|
||||
SHOW STATUS LIKE 'wsrep_cluster_size';
|
||||
SHOW STATUS LIKE 'wsrep_local_state_comment';
|
||||
```
|
||||
|
||||
The `wsrep_cluster_size` should be 3, and `wsrep_local_state_comment` should show "Synced".
|
||||
|
||||
## Features
|
||||
|
||||
- **Multi-Master Replication**: All nodes accept writes simultaneously
|
||||
- **Synchronous Replication**: Data is replicated to all nodes before commit
|
||||
- **Automatic Failover**: If one node fails, the cluster continues operating
|
||||
- **High Availability**: No single point of failure with 3 nodes
|
||||
- **Read/Write on Any Node**: Connect to any node for read and write operations
|
||||
|
||||
## Notes
|
||||
|
||||
- The first node (mariadb-galera-1) must start first as the bootstrap node
|
||||
- All nodes must be able to communicate with each other
|
||||
- For production, consider adding more nodes (5, 7, etc.) for better fault tolerance
|
||||
- Use an odd number of nodes to avoid split-brain scenarios
|
||||
- Change the default root password for production use
|
||||
- The cluster uses `rsync` for State Snapshot Transfer (SST)
|
||||
|
||||
## Scaling
|
||||
|
||||
To add more nodes, add new service definitions following the pattern of nodes 2 and 3, and update the `wsrep_cluster_address` to include all nodes.
|
||||
|
||||
## License
|
||||
|
||||
MariaDB is licensed under the GPL v2.
|
||||
89
src/mariadb-galera/README.zh.md
Normal file
89
src/mariadb-galera/README.zh.md
Normal file
@@ -0,0 +1,89 @@
|
||||
# MariaDB Galera 集群
|
||||
|
||||
[English](./README.md) | [中文](./README.zh.md)
|
||||
|
||||
此服务用于部署 3 节点 MariaDB Galera 集群,提供高可用性和同步多主复制。
|
||||
|
||||
## 服务
|
||||
|
||||
- `mariadb-galera-1`: 第一个 MariaDB Galera 节点(引导节点)。
|
||||
- `mariadb-galera-2`: 第二个 MariaDB Galera 节点。
|
||||
- `mariadb-galera-3`: 第三个 MariaDB Galera 节点。
|
||||
|
||||
## 环境变量
|
||||
|
||||
| 变量名 | 说明 | 默认值 |
|
||||
| --------------------------- | ---------------- | ---------------- |
|
||||
| MARIADB_VERSION | MariaDB 镜像版本 | `11.7.2` |
|
||||
| MARIADB_ROOT_PASSWORD | root 用户密码 | `galera` |
|
||||
| MARIADB_GALERA_CLUSTER_NAME | Galera 集群名称 | `galera_cluster` |
|
||||
| MARIADB_PORT_1_OVERRIDE | 节点 1 端口 | `3306` |
|
||||
| MARIADB_PORT_2_OVERRIDE | 节点 2 端口 | `3307` |
|
||||
| MARIADB_PORT_3_OVERRIDE | 节点 3 端口 | `3308` |
|
||||
|
||||
请根据实际需求修改 `.env` 文件。
|
||||
|
||||
## 卷
|
||||
|
||||
- `mariadb_galera_1_data`: 节点 1 数据存储。
|
||||
- `mariadb_galera_2_data`: 节点 2 数据存储。
|
||||
- `mariadb_galera_3_data`: 节点 3 数据存储。
|
||||
|
||||
## 使用方法
|
||||
|
||||
### 启动集群
|
||||
|
||||
```bash
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
第一个节点 (mariadb-galera-1) 使用 `--wsrep-new-cluster` 引导集群。其他节点自动加入。
|
||||
|
||||
### 连接到集群
|
||||
|
||||
连接到任何节点:
|
||||
|
||||
```bash
|
||||
mysql -h 127.0.0.1 -P 3306 -u root -p
|
||||
```
|
||||
|
||||
或者:
|
||||
|
||||
```bash
|
||||
mysql -h 127.0.0.1 -P 3307 -u root -p
|
||||
mysql -h 127.0.0.1 -P 3308 -u root -p
|
||||
```
|
||||
|
||||
### 检查集群状态
|
||||
|
||||
```sql
|
||||
SHOW STATUS LIKE 'wsrep_cluster_size';
|
||||
SHOW STATUS LIKE 'wsrep_local_state_comment';
|
||||
```
|
||||
|
||||
`wsrep_cluster_size` 应该为 3,`wsrep_local_state_comment` 应显示 "Synced"。
|
||||
|
||||
## 功能
|
||||
|
||||
- **多主复制**: 所有节点同时接受写入
|
||||
- **同步复制**: 数据在提交前复制到所有节点
|
||||
- **自动故障转移**: 如果一个节点失败,集群继续运行
|
||||
- **高可用性**: 3 个节点无单点故障
|
||||
- **任意节点读写**: 连接到任何节点进行读写操作
|
||||
|
||||
## 注意事项
|
||||
|
||||
- 第一个节点 (mariadb-galera-1) 必须首先启动作为引导节点
|
||||
- 所有节点必须能够相互通信
|
||||
- 对于生产环境,考虑添加更多节点(5、7 等)以获得更好的容错能力
|
||||
- 使用奇数个节点以避免脑裂场景
|
||||
- 生产环境请更改默认 root 密码
|
||||
- 集群使用 `rsync` 进行状态快照传输(SST)
|
||||
|
||||
## 扩展
|
||||
|
||||
要添加更多节点,请按照节点 2 和 3 的模式添加新的服务定义,并更新 `wsrep_cluster_address` 以包含所有节点。
|
||||
|
||||
## 许可证
|
||||
|
||||
MariaDB 使用 GPL v2 许可证授权。
|
||||
119
src/mariadb-galera/docker-compose.yaml
Normal file
119
src/mariadb-galera/docker-compose.yaml
Normal file
@@ -0,0 +1,119 @@
|
||||
x-default: &default
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- &localtime /etc/localtime:/etc/localtime:ro
|
||||
- &timezone /etc/timezone:/etc/timezone:ro
|
||||
logging:
|
||||
driver: json-file
|
||||
options:
|
||||
max-size: 100m
|
||||
|
||||
x-mariadb-galera: &mariadb-galera
|
||||
<<: *default
|
||||
image: mariadb:${MARIADB_VERSION:-11.7.2}
|
||||
environment: &galera-env
|
||||
MARIADB_ROOT_PASSWORD: ${MARIADB_ROOT_PASSWORD:-galera}
|
||||
MARIADB_GALERA_CLUSTER_NAME: ${MARIADB_GALERA_CLUSTER_NAME:-galera_cluster}
|
||||
MARIADB_GALERA_CLUSTER_ADDRESS: gcomm://mariadb-galera-1,mariadb-galera-2,mariadb-galera-3
|
||||
command:
|
||||
- --wsrep-new-cluster
|
||||
- --wsrep_node_address=${WSREP_NODE_ADDRESS}
|
||||
- --wsrep_cluster_name=${MARIADB_GALERA_CLUSTER_NAME:-galera_cluster}
|
||||
- --wsrep_cluster_address=gcomm://mariadb-galera-1,mariadb-galera-2,mariadb-galera-3
|
||||
- --wsrep_sst_method=rsync
|
||||
- --wsrep_on=ON
|
||||
- --wsrep_provider=/usr/lib/galera/libgalera_smm.so
|
||||
- --binlog_format=row
|
||||
- --default_storage_engine=InnoDB
|
||||
- --innodb_autoinc_lock_mode=2
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '2.0'
|
||||
memory: 2G
|
||||
reservations:
|
||||
cpus: '1.0'
|
||||
memory: 1G
|
||||
|
||||
services:
|
||||
mariadb-galera-1:
|
||||
<<: *mariadb-galera
|
||||
container_name: mariadb-galera-1
|
||||
hostname: mariadb-galera-1
|
||||
ports:
|
||||
- "${MARIADB_PORT_1_OVERRIDE:-3306}:3306"
|
||||
environment:
|
||||
<<: *galera-env
|
||||
WSREP_NODE_ADDRESS: mariadb-galera-1
|
||||
command:
|
||||
- --wsrep-new-cluster
|
||||
- --wsrep_node_address=mariadb-galera-1
|
||||
- --wsrep_cluster_name=${MARIADB_GALERA_CLUSTER_NAME:-galera_cluster}
|
||||
- --wsrep_cluster_address=gcomm://mariadb-galera-1,mariadb-galera-2,mariadb-galera-3
|
||||
- --wsrep_sst_method=rsync
|
||||
- --wsrep_on=ON
|
||||
- --wsrep_provider=/usr/lib/galera/libgalera_smm.so
|
||||
- --binlog_format=row
|
||||
- --default_storage_engine=InnoDB
|
||||
- --innodb_autoinc_lock_mode=2
|
||||
volumes:
|
||||
- *localtime
|
||||
- *timezone
|
||||
- mariadb_galera_1_data:/var/lib/mysql
|
||||
|
||||
mariadb-galera-2:
|
||||
<<: *mariadb-galera
|
||||
container_name: mariadb-galera-2
|
||||
hostname: mariadb-galera-2
|
||||
ports:
|
||||
- "${MARIADB_PORT_2_OVERRIDE:-3307}:3306"
|
||||
environment:
|
||||
<<: *galera-env
|
||||
WSREP_NODE_ADDRESS: mariadb-galera-2
|
||||
command:
|
||||
- --wsrep_node_address=mariadb-galera-2
|
||||
- --wsrep_cluster_name=${MARIADB_GALERA_CLUSTER_NAME:-galera_cluster}
|
||||
- --wsrep_cluster_address=gcomm://mariadb-galera-1,mariadb-galera-2,mariadb-galera-3
|
||||
- --wsrep_sst_method=rsync
|
||||
- --wsrep_on=ON
|
||||
- --wsrep_provider=/usr/lib/galera/libgalera_smm.so
|
||||
- --binlog_format=row
|
||||
- --default_storage_engine=InnoDB
|
||||
- --innodb_autoinc_lock_mode=2
|
||||
volumes:
|
||||
- *localtime
|
||||
- *timezone
|
||||
- mariadb_galera_2_data:/var/lib/mysql
|
||||
depends_on:
|
||||
- mariadb-galera-1
|
||||
|
||||
mariadb-galera-3:
|
||||
<<: *mariadb-galera
|
||||
container_name: mariadb-galera-3
|
||||
hostname: mariadb-galera-3
|
||||
ports:
|
||||
- "${MARIADB_PORT_3_OVERRIDE:-3308}:3306"
|
||||
environment:
|
||||
<<: *galera-env
|
||||
WSREP_NODE_ADDRESS: mariadb-galera-3
|
||||
command:
|
||||
- --wsrep_node_address=mariadb-galera-3
|
||||
- --wsrep_cluster_name=${MARIADB_GALERA_CLUSTER_NAME:-galera_cluster}
|
||||
- --wsrep_cluster_address=gcomm://mariadb-galera-1,mariadb-galera-2,mariadb-galera-3
|
||||
- --wsrep_sst_method=rsync
|
||||
- --wsrep_on=ON
|
||||
- --wsrep_provider=/usr/lib/galera/libgalera_smm.so
|
||||
- --binlog_format=row
|
||||
- --default_storage_engine=InnoDB
|
||||
- --innodb_autoinc_lock_mode=2
|
||||
volumes:
|
||||
- *localtime
|
||||
- *timezone
|
||||
- mariadb_galera_3_data:/var/lib/mysql
|
||||
depends_on:
|
||||
- mariadb-galera-1
|
||||
|
||||
volumes:
|
||||
mariadb_galera_1_data:
|
||||
mariadb_galera_2_data:
|
||||
mariadb_galera_3_data:
|
||||
Reference in New Issue
Block a user