ubuntu

Ubuntu MySQL集群搭建步骤是什么

小樊
36
2025-04-29 14:55:03
栏目: 云计算

在Ubuntu上搭建MySQL集群涉及多个步骤,包括环境准备、软件安装、配置、测试等。以下是详细的搭建步骤:

前提条件

搭建过程

  1. 修改配置文件

    • 服务器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
      
  2. 创建复制用户

    在两个服务器上的MySQL命令行执行以下SQL命令来创建一个专门用于复制的用户:

    GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%' IDENTIFIED BY 'strong_password';
    FLUSH PRIVILEGES;
    
  3. 锁定表并获取二进制日志位置

    在服务器A上执行:

    FLUSH TABLES WITH READ LOCK;
    SHOW MASTER STATUS;
    

    记录下返回的File和Position值。在服务器B上也执行同样的步骤,但记录服务器B的值时不要忘记先在服务器A上执行UNLOCK TABLES;命令解锁表。

  4. 配置复制

    在服务器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的数据。

  5. 启动复制

    在两台服务器上分别执行:

    START SLAVE;
    
  6. 检查复制状态

    在两台服务器上执行以下命令检查复制状态:

    SHOW SLAVE STATUS \G;
    

    确认以下两个字段的值为Yes:

    • Slave_IO_Running: Yes
    • Slave_SQL_Running: Yes

请注意,以上步骤仅适用于双主复制集群的搭建。如果需要搭建更高可用性的集群,如Galera Cluster或MySQL Cluster,请参考官方文档进行配置。

0
看了该问题的人还看了