debian

Debian MariaDB集群搭建怎么做

小樊
45
2025-09-30 04:35:01
栏目: 云计算

Prerequisites
Before starting, ensure you have:

Step 1: Prepare All Nodes
Update packages and install dependencies:

sudo apt update && sudo apt upgrade -y
sudo apt install -y rsync galera-4 mariadb-server

Secure the installation:

sudo mysql_secure_installation

Follow prompts to set a root password, remove anonymous users, disable remote root login, and delete the test database.

Step 2: Configure the First Node (Primary)
Create a Galera config file (/etc/mysql/conf.d/galera.cnf) on the first node:

sudo nano /etc/mysql/conf.d/galera.cnf

Add the following (replace IPs with your node addresses):

[mysqld]
# Basic settings
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
query_cache_size=0
query_cache_type=0
bind-address=0.0.0.0
skip-name-resolve

# Galera provider (mandatory)
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so

# Cluster configuration (name must be identical across nodes)
wsrep_cluster_name="my_galera_cluster"
wsrep_cluster_address="gcomm://192.168.1.11,192.168.1.12,192.168.1.13"

# Node identification
wsrep_node_address="192.168.1.11"  # IP of this node
wsrep_node_name="node1"           # Unique name for this node

# SST (State Snapshot Transfer) method (choose one: rsync, xtrabackup, mariabackup)
wsrep_sst_method=rsync

Save and exit. This config enables Galera replication and defines the cluster topology.

Step 3: Initialize the Cluster
Stop MariaDB on all nodes:

sudo systemctl stop mariadb

Only on the first node, start it with the --wsrep-new-cluster flag to bootstrap the cluster:

sudo systemctl start mariadb --wsrep-new-cluster

Verify the first node is running:

sudo mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size';"

You should see wsrep_cluster_size: 1 (indicating the primary node is up).

Step 4: Add Remaining Nodes
On each additional node (e.g., node2, node3), edit /etc/mysql/conf.d/galera.cnf with its own IP and name:

[mysqld]
# ... (same basic settings as node1) ...

wsrep_cluster_name="my_galera_cluster"
wsrep_cluster_address="gcomm://192.168.1.11,192.168.1.12,192.168.1.13"

wsrep_node_address="192.168.1.12"  # IP of this node
wsrep_node_name="node2"           # Unique name
wsrep_sst_method=rsync

Start MariaDB on each node normally:

sudo systemctl start mariadb

Check cluster status from any node:

sudo mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size';"

The output should show wsrep_cluster_size: 3 (all nodes joined).

Step 5: Validate Replication
On node1, create a test database and table:

sudo mysql -u root -p -e "CREATE DATABASE test_cluster;"
sudo mysql -u root -p -e "USE test_cluster; CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50)); INSERT INTO users (name) VALUES ('Alice');"

On node2 and node3, verify the data was replicated:

sudo mysql -u root -p -e "USE test_cluster; SELECT * FROM users;"

You should see the Alice record on all nodes, confirming successful replication.

Step 6: Secure Remote Access (Optional but Recommended)
Edit the MariaDB config (/etc/mysql/mariadb.conf.d/50-server.cnf) on all nodes to allow remote connections:

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Comment out the bind-address line (or change it to 0.0.0.0):

#bind-address = 127.0.0.1

Restart MariaDB:

sudo systemctl restart mariadb

Grant remote access to the root user (run on any node):

sudo mysql -u root -p -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_strong_password'; FLUSH PRIVILEGES;"

Update your firewall to allow MariaDB (3306) and Galera (4567, 4568, 4444) ports:

sudo ufw allow 3306/tcp
sudo ufw allow 4567/tcp  # Galera sync
sudo ufw allow 4568/tcp  # Incremental state transfer
sudo ufw allow 4444/tcp  # State snapshot transfer

Troubleshooting Tips

For production, consider adding monitoring (e.g., Prometheus + Grafana) and high availability (e.g., HAProxy for load balancing) to your cluster.

0
看了该问题的人还看了