웹사이트 검색

Debian 8에서 MySQL용 Percona XtraDB 클러스터를 설치하는 방법


이 페이지에서

  1. Debian 8에서 Percona XtraDB 클러스터 구성
  2. 전제 조건
  3. 1단계. Percona Xtradb 클러스터 설치
  4. 2단계. 첫 번째 노드 구성
  5. 3단계. 두 번째 노드 구성
  6. 4단계. 세 번째 노드 구성
  7. 복제 테스트

이 기사에서는 다중 마스터 복제에서 3개의 노드로 MySQL 데이터베이스 클러스터를 구성하는 방법을 보여줍니다. 다중 마스터 복제는 각 노드의 레코드 쓰기를 허용하므로 노드가 실패해도 아무 일도 없었던 것처럼 다른 노드에서 작업할 수 있습니다.

Percona의 공식 문서는 공식 웹 사이트에서 찾을 수 있습니다.

우선, 왜 우리는 두 개가 아닌 세 개의 노드를 선택할까요? 모든 클러스터에서 노드의 수는 홀수여야 하므로 노드 연결이 끊어지는 경우 가장 높은 서버 그룹에 최신 데이터가 있다고 가정하고 데이터 손실을 방지하기 위해 다운 노드에 복제해야 합니다. 이것은 데이터 복제의 충돌을 해결하는 것과만 관련이 있으며 연결이 끊긴 노드에만 기록된 데이터는 손실되지 않습니다.

이는 올바른 데이터가 있는 노드를 자동으로 선택할 수 없는 스플릿 브레인이라는 상황을 피하기 위해 사용됩니다. 예를 들어 두 노드가 서로 연결이 끊어지고 두 노드에 동일한 레코드가 기록되는 2노드 클러스터를 생각해 보십시오. 다시 온라인 상태가 되면 누가 이기나요? 우리는 모르기 때문에 스플릿 브레인이 발생하고 어떤 레코드가 올바른 레코드인지 수동으로 결정해야 합니다.

클러스터의 어느 부분에 올바른 데이터가 있는지 결정하는 데 필요한 노드 수를 QUORUM이라고 합니다. 이 경우 쿼럼은 2입니다. 따라서 2개의 서버가 항상 서로 연결되어 있어야 합니다. 3개 노드가 모두 다운되는 경우 분할 브레인이 있고 수동으로 부트스트랩 모드로 전환해야 하는 서버를 결정해야 합니다. 이것은 분할 브레인에서 재개할 주 서버가 될 서버를 결정하는 절차입니다.

Debian 8에서 Percona XtraDB 클러스터 구성

이 가이드에서는 Debian 8 서버에 3개의 Percona XtraDB 클러스터 노드를 설치하고 구성하는 방법을 설명합니다. 여기서는 Percona 저장소의 패키지를 사용합니다.

  • 서버 1
    • 호스트 이름: mysql1.local.vm\n
    • IP 주소: 192.168.152.100

    • 호스트 이름: mysql2.local.vm\n
    • IP 주소: 192.168.152.110

    • 호스트 이름: mysql3.local.vm\n
    • IP 주소: 192.168.152.120

    각 호스트에서 /etc/hosts 파일을 다음과 같이 수정하여 DNS가 올바르게 작동하도록 합니다.

    127.0.0.1 localhost
    192.168.152.100 mysql1.local.vm mysql1
    192.168.152.110 mysql2.local.vm mysql2
    192.168.152.120 mysql3.local.vm mysql3

    # The following lines are desirable for IPv6 capable hosts
    ::1 localhost ip6-localhost ip6-loopback
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters

    전제 조건

    이 자습서에서 설명하는 절차에는 다음과 같은 최소 서버 설정이 필요합니다.

    • 3개 노드 모두 데비안 8이 있으므로 이 가이드를 따를 것을 권장합니다. https://linux-console.net/tutorial/debian-8-jessie-minimal-server/\n

    1단계. Percona Xtradb 클러스터 설치하기

    모든 노드에서 다음 명령을 루트로 실행합니다.

    wget https://repo.percona.com/apt/percona-release_0.1-4.$(lsb_release -sc)_all.deb
    dpkg -i percona-release_0.1-4.$(lsb_release -sc)_all.deb
    apt-get update
    apt-get -y install percona-xtradb-cluster-57

    분명히 선택하려는 mysql 비밀번호를 입력하십시오.

    패키지가 설치되면 mysqld가 자동으로 시작됩니다. /etc/init.d/mysql stop을 사용하여 세 노드 모두에서 mysqld를 중지합니다.

    2단계. 첫 번째 노드 구성

    클러스터를 부트스트랩할 수 있도록 개별 노드를 구성해야 합니다. 클러스터 부트스트랩에 대한 자세한 내용은 클러스터 부트스트랩을 참조하세요.

    1. Make sure to add these lines to configuration file /etc/mysql/my.cnf for the first node (mysql1.local.vm) at the end of [mysqld] section:

      [mysqld]

      ... # Path to Galera library
      wsrep_provider=/usr/lib/libgalera_smm.so

      # Cluster connection URL contains the IPs of node#1, node#2 and node#3
      wsrep_cluster_address=gcomm://192.168.152.100,192.168.152.110,192.168.152.120

      # In order for Galera to work correctly binlog format should be ROW
      binlog_format=ROW

      # MyISAM storage engine has only experimental support
      default_storage_engine=InnoDB

      # This InnoDB autoincrement locking mode is a requirement for Galera
      innodb_autoinc_lock_mode=2

      # Node #1 address
      wsrep_node_address=192.168.152.100

      # SST method
      wsrep_sst_method=xtrabackup-v2

      # Cluster name
      wsrep_cluster_name=my_ubuntu_cluster

      # Authentication for SST method
      wsrep_sst_auth="sstuser:PASSW0RD"

      Pay attention to the password you setup there in my case "PASSW0RD".

    2. Start the first node with the following command:

      :~# /etc/init.d/mysql bootstrap-pxc
      

      This command will start the first node and bootstrap the cluster, you will see something like this if all is ok:

      :~#
    3. After the first node has been started, connect to mysql with classic mysql -p command, then cluster status can be checked executing the query show status like 'wsrep%';as in the example below:

    4. mysql> show status like 'wsrep%';
      +----------------------------+--------------------------------------+
      | Variable_name              | Value                                |
      +----------------------------+--------------------------------------+
      | wsrep_local_state_uuid     | 0251a27c-8a19-11e6-905b-f3f13b0ddc5b |
      ...
      | wsrep_local_state          | 4                                    |
      | wsrep_local_state_comment  | Synced                               |
      ...
      | wsrep_cluster_size         | 1                                    |
      | wsrep_cluster_status       | Primary                              |
      | wsrep_connected            | ON                                   |
      ...
      | wsrep_ready                | ON                                   |
      +----------------------------+--------------------------------------+
      59 rows in set (0.00 sec)
      

    이 출력은 클러스터가 성공적으로 부트스트랩되었음을 보여줍니다.

    권한을 수행하려면:

    mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'PASSW0RD';
    mysql> GRANT PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
    mysql> FLUSH PRIVILEGES;
    

    3단계. 두 번째 노드 구성

    1. Append the following lines to the configuration file /etc/mysql/my.cnf on the second node (mysql2.local.vm), so that it contains the following data:

      [mysqld]

      ... # Path to Galera library
      wsrep_provider=/usr/lib/libgalera_smm.so

      # Cluster connection URL contains the IPs of node#1, node#2 and node#3
      wsrep_cluster_address=gcomm://192.168.152.100,192.168.152.110,192.168.152.120

      # In order for Galera to work correctly binlog format should be ROW
      binlog_format=ROW

      # MyISAM storage engine has only experimental support
      default_storage_engine=InnoDB

      # This InnoDB autoincrement locking mode is a requirement for Galera
      innodb_autoinc_lock_mode=2

      # Node #2 address
      wsrep_node_address=192.168.152.110

      # SST method
      wsrep_sst_method=xtrabackup-v2

      # Cluster name
      wsrep_cluster_name=my_ubuntu_cluster

      # Authentication for SST method
      wsrep_sst_auth="sstuser:PASSW0RD"
    2. Start the second node with the following command (attention this time as you can see is not in boostrap mode!!):

      :~# /etc/init.d/mysql start
      
    3. After the server has been started, it should receive SST automatically. Cluster status can now be checked on both nodes. The following is an example of the status from the second node (mysql2.local.vm):

    4. mysql> show status like 'wsrep%';
      +----------------------------+--------------------------------------+
      | Variable_name              | Value                                |
      +----------------------------+--------------------------------------+
      | wsrep_local_state_uuid     | 0251a27c-8a19-11e6-905b-f3f13b0ddc5b |
      ...
      | wsrep_local_state          | 4                                    |
      | wsrep_local_state_comment  | Synced                               |
      ...
      | wsrep_cluster_size         | 2                                    |
      | wsrep_cluster_status       | Primary                              |
      | wsrep_connected            | ON                                   |
      ...
      | wsrep_ready                | ON                                   |
      +----------------------------+--------------------------------------+
      40 rows in set (0.01 sec)
      

      This output shows that the new node has been successfully added to the cluster. Note the variable wsrep_cluster_size wich has become 2, instead of one of the first query we made.

    4단계. 세 번째 노드 구성

    1. Append the following lines to the configuration file /etc/mysql/my.cnf on the second node (mysql3.local.vm), so it contains the following configuration:

      [mysqld]

      ... # Path to Galera library
      wsrep_provider=/usr/lib/libgalera_smm.so

      # Cluster connection URL contains the IPs of node#1, node#2 and node#3
      wsrep_cluster_address=gcomm://192.168.152.100,192.168.152.110,192.168.152.120

      # In order for Galera to work correctly binlog format should be ROW
      binlog_format=ROW

      # MyISAM storage engine has only experimental support
      default_storage_engine=InnoDB

      # This InnoDB autoincrement locking mode is a requirement for Galera
      innodb_autoinc_lock_mode=2

      # Node #2 address
      wsrep_node_address=192.168.152.120

      # SST method
      wsrep_sst_method=xtrabackup-v2

      # Cluster name
      wsrep_cluster_name=my_ubuntu_cluster

      # Authentication for SST method
      wsrep_sst_auth="sstuser:PASSW0RD"
    2. Start the third node with the following command:

      :~# /etc/init.d/mysql start
      
    3. After the server has been started, it should receive SST automatically. Cluster status can be checked on all nodes. The following is an example of status from the third node (mysql3.local.vm):

      mysql> show status like 'wsrep%';
      +----------------------------+--------------------------------------+
      | Variable_name              | Value                                |
      +----------------------------+--------------------------------------+
      | wsrep_local_state_uuid     | 0251a27c-8a19-11e6-905b-f3f13b0ddc5b |
      ...
      | wsrep_local_state          | 4                                    |
      | wsrep_local_state_comment  | Synced                               |
      ...
      | wsrep_cluster_size         | 3                                    |
      | wsrep_cluster_status       | Primary                              |
      | wsrep_connected            | ON                                   |
      ...
      | wsrep_ready                | ON                                   |
      +----------------------------+--------------------------------------+
      40 rows in set (0.01 sec)
      

      This output confirms that the third node has joined the cluster. Again take a look at wsrep_cluster_size wich now has become 3, instead of 2.

    몇 가지 문제가 발생하면 /var/log/syslog를 살펴보고 문제가 없는지 확인하십시오.

    Oct 4 12:16:13 mysql3 mysql[2767]: Starting MySQL (Percona XtraDB Cluster) database server: mysqld . . .State transfer in progress, setting sleep higher: mysqld . ..
    Oct 4 12:16:13 mysql3 systemd[1]: Started LSB: Start and stop the mysql (Percona XtraDB Cluster) daemon.
    Oct 4 12:17:01 mysql3 CRON[3731]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)

    이 예에서는 모든 것이 잘 진행되었으며 상태 전송이 진행 중인 것을 볼 수 있습니다. 즉, 데이터가 노드로 전송될 것입니다.

    복제 테스트

    복제를 테스트하기 위해 두 번째 노드에 새 데이터베이스를 만들고 세 번째 노드에 해당 데이터베이스에 대한 테이블을 만들고 첫 번째 노드의 테이블에 일부 레코드를 추가합니다.

    1. Create a new database on the second node:

      mysql@mysql2> CREATE DATABASE percona;
      Query OK, 1 row affected (0.01 sec)
      
    2. Create a table on the third node:

      mysql@mysql3> USE percona;
      Database changed
      
      mysql@pxc3> CREATE TABLE example (node_id INT PRIMARY KEY, node_name VARCHAR(30));
      Query OK, 0 rows affected (0.05 sec)
      
    3. Insert records on the first node:

      mysql@mysql1> INSERT INTO percona.example VALUES (1, 'percona1');
      Query OK, 1 row affected (0.02 sec)
      
    4. Retrieve all the rows from that table on the second node:

      mysql@mysql2> SELECT * FROM percona.example;
      +---------+-----------+
      | node_id | node_name |
      +---------+-----------+
      |       1 | percona1  |
      +---------+-----------+
      1 row in set (0.00 sec)
      

    애플리케이션이 항상 클러스터에 도달할 수 있도록 세 개의 노드 앞에 로드 밸런서를 추가할 수 있습니다.