在Debian系統上搭建MySQL集群可以通過多種方式實現,包括使用MySQL Replication(主從復制)、MySQL Cluster等。以下是使用MySQL Replication實現高可用性MySQL集群的步驟:
修改配置文件
服務器A(master1)配置:
[mysqld]
server-id = 1
log-bin = mysql-bin
binlog-do-db = your_database_name
sync_binlog = 1
binlog_format = mixed
relay_log = relay-bin
relay_log_index = relay-bin.index
auto_increment_increment = 2
auto_increment_offset = 1
# 為復制設置監聽IP和端口(如果非默認3306端口)
bind-address = your_server_ip
port = your_port_number
服務器B(master2)配置:
[mysqld]
server-id = 2
log-bin = mysql-bin
binlog-do-db = your_database_name
sync_binlog = 1
binlog_format = mixed
relay_log = relay-bin
relay_log_index = relay-bin.index
auto_increment_increment = 2
auto_increment_offset = 2
# 為復制設置監聽IP和端口(如果非默認3306端口)
bind-address = your_server_ip
port = your_port_number
創建復制用戶在兩個服務器上的MySQL命令行執行以下SQL命令來創建一個專門用于復制的用戶:
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%' IDENTIFIED BY 'strong_password';
FLUSH PRIVILEGES;
確保替換 ‘replication_user’ 為你希望使用的復制用戶名,并設置 ‘strong_password’ 為強密碼。
鎖定表并獲取二進制日志位置在服務器A上執行:
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
記錄下返回的 File 和 Position 值。在服務器B上也執行同樣的步驟,但記錄服務器B的值時不要忘記先在服務器A上執行 UNLOCK TABLES;
命令解鎖表。
配置復制在服務器B上設置復制服務器A的數據:
CHANGE MASTER TO
MASTER_HOST='master1_ip_address',
MASTER_USER='replication_user',
MASTER_PASSWORD='strong_password',
MASTER_LOG_FILE='master1_binlog_file',
MASTER_LOG_POS=master1_binlog_position;
將 ‘master1_ip_address’ 替換為服務器A的IP地址,‘strong_password’ 替換為之前設置的密碼,以及 ‘master1_binlog_file’ 和 master1_binlog_position 分別替換為在服務器A上執行 SHOW MASTER STATUS;
命令后獲取的文件名和位置。在服務器A上執行相同操作來設置服務器B的數據。
啟動復制在兩臺服務器上分別執行:
START SLAVE;
檢查復制狀態在兩臺服務器上執行以下命令檢查復制狀態:
SHOW SLAVE STATUS \G;
確認以下兩個字段的值為 Yes:Slave_IO_Running: Yes
、Slave_SQL_Running: Yes
以上步驟展示了如何在Debian系統上搭建一個基本的MySQL集群。對于更復雜的高可用性和負載均衡需求,可能需要進一步配置和優化。