如何搭建 MySQL 高可用高性能集群

时间:2022-11-23 20:07:56

MySQL NDB Cluster 是什么

MySQL NDB Cluster 是 MySQL 的一个高可用、高冗余版本,适用于分布式计算环境。
文档链接

搭建集群的前置工作

至少准备 3 台服务器,一台作为管理服务器,两台作为数据服务器和 SQL 服务器,当然有更多的服务器会更好。

管理服务器mgm:192.168.0.105
数据服务器ndb1:192.168.0.106
数据服务器ndb2:192.168.0.104
sql服务器:192.168.0.106
sql服务器:192.168.0.104

开始部署集群

首先下载 MySQL NDB Cluster二进制文件,解压缩后开始下面的步骤。

部署管理服务器

更新系统

  1. apt update -y && apt upgrade -y && apt install libncurses5 -y

复制 ndb_mgm 和 ndb_mgmd 到管理服务器

  1. scp ./mysql-cluster-gpl-7.6.17-linux-glibc2.12-x86_64/bin/ndb_mgm* mgm@192.168.0.105:/home/mgm

在管理服务器复制 ndb_mgm 和 ndb_mgmd 到/usr/local/bin 文件夹

  1. cp -rfv /home/mgm/ndb_mgm* /usr/local/bin

赋予 ndb_mgm 和 ndb_mgmd 可执行权限

  1. chmod +x /usr/local/bin/ndb_mgm*

添加配置文件

  1. mkdir /var/lib/mysql-cluster
  2. vi /var/lib/mysql-cluster/config.ini

config.ini

  1. [ndbd default]
  2. # Options affecting ndbd processes on all data nodes:
  3. NoOfReplicas=2 # Number of fragment replicas
  4. DataMemory=98M # How much memory to allocate for data storage
  5.  
  6. [ndb_mgmd]
  7. # Management process options:
  8. HostName=192.168.0.105 # Hostname or IP address of management node
  9. NodeId=1 # Node ID for this Management node
  10. DataDir=/var/lib/mysql-cluster # Directory for management node log files
  11.  
  12. [ndbd]
  13. # Options for data node "A":
  14. # (one [ndbd] section per data node)
  15. HostName=192.168.0.104 # Hostname or IP address
  16. NodeId=2 # Node ID for this data node
  17. DataDir=/data/mysql-cluster/data # Directory for this data node's data files
  18.  
  19. [ndbd]
  20. # Options for data node "B”:
  21. # (one [ndbd] section per data node)
  22. HostName=192.168.0.106 # Hostname or IP address
  23. NodeId=3 # Node ID for this data node
  24. DataDir=/data/mysql-cluster/data # Directory for this data node's data files
  25.  
  26. [mysqld]
  27. # SQL node options:
  28. HostName=192.168.0.104 # Hostname or IP address
  29. # (additional mysqld connections can be
  30. # specified for this node for various
  31. # purposes such as running ndb_restore)
  32.  
  33. [mysqld]
  34. # SQL node options:
  35. HostName=192.168.0.106 # Hostname or IP address
  36. # (additional mysqld connections can be
  37. # specified for this node for various
  38. # purposes such as running ndb_restore)

开启防火墙,集群管理服务默认使用 1186 端口

  1. ufw allow 22
  2. ufw allow 1186
  3. ufw enable

初始化并启动管理服务器

  1. cd /usr/local/bin/
  2. ndb_mgmd --initial --configdir=/var/lib/mysql-cluster -f /var/lib/mysql-cluster/config.ini --ndb-nodeid=1

当出现以下结果的时候,表示管理服务器已经启动成功了

  1. root@mgm:/usr/local/bin# ndb_mgmd --initial --configdir=/var/lib/mysql-cluster -f /var/lib/mysql-cluster/config.ini --ndb-nodeid=1
  2. MySQL Cluster Management Server mysql-5.7.33 ndb-7.6.17

我们再执行 ndb_mgm 命令,可以查看当前集群的状态

  1. root@mgm:/usr/local/bin# ndb_mgm
  2. -- NDB Cluster -- Management Client --
  3. ndb_mgm> show
  4. Connected to Management Server at: localhost:1186
  5. Cluster Configuration
  6. ---------------------
  7. [ndbd(NDB)] 2 node(s)
  8. id=2 (not connected, accepting connect from 192.168.0.104)
  9. id=3 (not connected, accepting connect from 192.168.0.106)
  10.  
  11. [ndb_mgmd(MGM)] 1 node(s)
  12. id=1 @192.168.0.105 (mysql-5.7.33 ndb-7.6.17)
  13.  
  14. [mysqld(API)] 2 node(s)
  15. id=4 (not connected, accepting connect from 192.168.0.104)
  16. id=5 (not connected, accepting connect from 192.168.0.106)

部署数据服务器

在所有数据服务器上执行以下操作

更新系统

  1. apt update -y && apt upgrade -y && apt install libncurses5 -y

开启防火墙

  1. ufw allow 22
  2. ufw allow 2202
  3. ufw enable

复制 ndbd 和 ndbmtd 到数据服务器

  1. #复制到192.168.0.106
  2. scp ./mysql-cluster-gpl-7.6.17-linux-glibc2.12-x86_64/bin/ndbd ndb1@192.168.0.106:/home/ndb1
  3. scp ./mysql-cluster-gpl-7.6.17-linux-glibc2.12-x86_64/bin/ndbmtd ndb1@192.168.0.106:/home/ndb1
  4.  
  5. #复制到192.168.0.104
  6. scp ./mysql-cluster-gpl-7.6.17-linux-glibc2.12-x86_64/bin/ndbd ndb2@192.168.0.104:/home/ndb2
  7. scp ./mysql-cluster-gpl-7.6.17-linux-glibc2.12-x86_64/bin/ndbmtd ndb2@192.168.0.104:/home/ndb2

在管理服务器复制 ndbd 和 ndbmtd 到/usr/local/bin 文件夹

  1. #192.168.0.106
  2. cp -rfv /home/ndb1/ndbd /usr/local/bin
  3. cp -rfv /home/ndb1/ndbmtd /usr/local/bin
  4.  
  5. #192.168.0.104
  6. cp -rfv /home/ndb2/ndbd /usr/local/bin
  7. cp -rfv /home/ndb2/ndbmtd /usr/local/bin

赋予 ndbd 可执行权限

  1. chmod +x /usr/local/bin/ndbd
  2. chmod +x /usr/local/bin/ndbmtd

在/etc下加入my.cnf文件

  1. vi /etc/my.cnf

my.cnf文件

  1. [mysqld]
  2. # Options for mysqld process:
  3. ndbcluster # run NDB storage engine
  4.  
  5. [mysql_cluster]
  6. # Options for NDB Cluster processes:
  7. ndb-connectstring=192.168.0.105 # location of management server

创建数据保存的目录,必须与管理服务配置的路径一致

  1. mkdir -p /data/mysql-cluster/data

启动数据服务

  1. root@ndb1:/usr/local/bin# ndbd
  2. 2021-06-20 08:10:23 [ndbd] INFO -- Angel connected to '192.168.0.105:1186'
  3. 2021-06-20 08:10:23 [ndbd] INFO -- Angel allocated nodeid: 3

回到集群管理服务器查看集群状态,此时可以看到数据服务已经连接成功

  1. root@mgm:/usr/local/bin# ndb_mgm
  2. -- NDB Cluster -- Management Client --
  3. ndb_mgm> show
  4. Connected to Management Server at: localhost:1186
  5. Cluster Configuration
  6. ---------------------
  7. [ndbd(NDB)] 2 node(s)
  8. id=2 (not connected, accepting connect from 192.168.0.104)
  9. id=3 @192.168.0.106 (mysql-5.7.33 ndb-7.6.17, starting, Nodegroup: 0)
  10.  
  11. [ndb_mgmd(MGM)] 1 node(s)
  12. id=1 @192.168.0.105 (mysql-5.7.33 ndb-7.6.17)
  13.  
  14. [mysqld(API)] 2 node(s)
  15. id=4 (not connected, accepting connect from 192.168.0.104)
  16. id=5 (not connected, accepting connect from 192.168.0.106)

在另一台服务器(192.168.0.104)重复 4、5、6、7 步骤的操作,结果可看到

  1. root@ndb2:/usr/local/bin# ndbd
  2. 2021-06-20 08:20:10 [ndbd] INFO -- Angel connected to '192.168.0.105:1186'
  3. 2021-06-20 08:20:10 [ndbd] INFO -- Angel allocated nodeid: 2

回到集群管理服务器查看集群状态,此时可以看到所有数据服务已经连接成功

  1. root@mgm:/usr/local/bin# ndb_mgm
  2. -- NDB Cluster -- Management Client --
  3. ndb_mgm> show
  4. Connected to Management Server at: localhost:1186
  5. Cluster Configuration
  6. ---------------------
  7. [ndbd(NDB)] 2 node(s)
  8. id=2 @192.168.0.104 (mysql-5.7.33 ndb-7.6.17, Nodegroup: 0, *)
  9. id=3 @192.168.0.106 (mysql-5.7.33 ndb-7.6.17, Nodegroup: 0)
  10.  
  11. [ndb_mgmd(MGM)] 1 node(s)
  12. id=1 @192.168.0.105 (mysql-5.7.33 ndb-7.6.17)
  13.  
  14. [mysqld(API)] 2 node(s)
  15. id=4 (not connected, accepting connect from 192.168.0.104)
  16. id=5 (not connected, accepting connect from 192.168.0.106)
  17. 在目录/data/mysql/data下面可以看到数据服务已经产生了数据
  18. root@ndb1:~# ls /data/mysql/data/
  19. ndb_3_fs ndb_3_out.log ndb_3.pid

部署 SQL 服务

复制 MySQL 到SQL服务器

  1. scp ./mysql-cluster-gpl-7.6.17-linux-glibc2.12-x86_64.tar.gz ndb2@192.168.0.104:/home/ndb2
  2. scp ./mysql-cluster-gpl-7.6.17-linux-glibc2.12-x86_64.tar.gz ndb1@192.168.0.106:/home/ndb1

解压缩 MySQL, 然后复制到/usr/local目录

  1. tar -zxvf mysql-cluster-gpl-7.6.17-linux-glibc2.12-x86_64.tar.gz
  2. cp -rfv mysql-cluster-gpl-7.6.17-linux-glibc2.12-x86_64 /usr/local/
  3. ln -snf /usr/local/mysql-cluster-gpl-7.6.17-linux-glibc2.12-x86_64 /usr/local/mysql
  4.  
  5. cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql.server
  6. export PATH=$PATH:/usr/local/mysql/bin
  7. source /etc/profile

开启防火墙

  1. ufw allow 22
  2. ufw allow 3306
  3. ufw enable

创建 MySQL 数据存放的目录

  1. mkdir -p /data/mysql/data
  2. mkdir -p /data/mysql/run
  3. mkdir -p /var/log/mysql

创建 mysql 用户,创建相关目录

  1. groupadd mysql
  2. useradd -r -g mysql -s /bin/false mysql
  3. chown mysql:mysql /data/mysql/data
  4. chmod 750 /data/mysql/data
  5.  
  6. chown mysql:mysql /data/mysql/run
  7. chmod 750 /data/mysql/run
  8.  
  9. chown mysql:mysql /var/log/mysql
  10. chmod 750 /var/log/mysql

创建 MySQL 配置文件

  1. mkdir -p /etc/mysql
  2. vi /etc/mysql/my.cnf
  3. my.cnf
  4. [mysqld]
  5. # Options for mysqld process:
  6. ndbcluster # run NDB storage engine
  7.  
  8. pid-file = /data/mysql/run/mysqld.pid
  9. socket = /data/mysql/run/mysqld.sock
  10. datadir = /data/mysql/data
  11. # log-error = /var/log/mysql/error.log
  12. # By default we only accept connections from localhost
  13. bind-address = 192.168.0.106
  14. # Disabling symbolic-links is recommended to prevent assorted security risks
  15. symbolic-links = 0
  16.  
  17. [mysql_cluster]
  18. # Options for NDB Cluster processes:
  19. ndb-connectstring = 192.168.0.105 # location of management server
  20.  
  21. [client]
  22. socket = /data/mysql/run/mysqld.sock

初始化MySQL

  1. /usr/local/mysql/bin/mysqld --defaults-file=/etc/mysql/my.cnf --initialize --user=mysql --basedir=/usr/local/mysql

记录下 MySQL 初始化生成的 root 用户密码 sF#Hy,IuT6d#

  1. root@ndb1:~# /usr/local/mysql/bin/mysqld --defaults-file=/etc/mysql/my.cnf --initialize --user=mysql --basedir=/usr/local/mysql
  2. 2021-06-20T12:23:26.874302Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
  3. 2021-06-20T12:23:27.102146Z 0 [Warning] InnoDB: New log files created, LSN=45790
  4. 2021-06-20T12:23:27.145317Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
  5. 2021-06-20T12:23:27.154405Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 50a15854-d1c2-11eb-9792-000c29681e23.
  6. 2021-06-20T12:23:27.155927Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
  7. 2021-06-20T12:23:28.339372Z 0 [Warning] CA certificate ca.pem is self signed.
  8. 2021-06-20T12:23:28.624534Z 1 [Note] A temporary password is generated for root@localhost: sF#Hy,IuT6d#

启动MySQL

  1. /usr/local/mysql/bin/mysqld_safe --user=mysql &

修改 root 用户密码

  1. mysqladmin -uroot -p'sF#Hy,IuT6d#' password '123456'

回到集群管理服务器查看集群状态,此时可以看到有一个 SQL 服务已经连接上了

  1. root@mgm:/usr/local/bin# ndb_mgm
  2. -- NDB Cluster -- Management Client --
  3. ndb_mgm> show
  4. Connected to Management Server at: localhost:1186
  5. Cluster Configuration
  6. ---------------------
  7. [ndbd(NDB)] 2 node(s)
  8. id=2 @192.168.0.104 (mysql-5.7.33 ndb-7.6.17, Nodegroup: 0, *)
  9. id=3 @192.168.0.106 (mysql-5.7.33 ndb-7.6.17, Nodegroup: 0)
  10.  
  11. [ndb_mgmd(MGM)] 1 node(s)
  12. id=1 @192.168.0.105 (mysql-5.7.33 ndb-7.6.17)
  13.  
  14. [mysqld(API)] 2 node(s)
  15. id=4 (not connected, accepting connect from 192.168.0.104)
  16. id=5 @192.168.0.106 (mysql-5.7.33 ndb-7.6.17)

在另一台服务器(192.168.0.104)部署 SQL 服务,回到集群管理服务器查看集群状态,此时可以看到所有 SQL 服务已经连接成功

  1. root@mgm:/usr/local/bin# ndb_mgm
  2. -- NDB Cluster -- Management Client --
  3. ndb_mgm> show
  4. Cluster Configuration
  5. ---------------------
  6. [ndbd(NDB)] 2 node(s)
  7. id=2 @192.168.0.104 (mysql-5.7.33 ndb-7.6.17, Nodegroup: 0, *)
  8. id=3 @192.168.0.106 (mysql-5.7.33 ndb-7.6.17, Nodegroup: 0)
  9.  
  10. [ndb_mgmd(MGM)] 1 node(s)
  11. id=1 @192.168.0.105 (mysql-5.7.33 ndb-7.6.17)
  12.  
  13. [mysqld(API)] 2 node(s)
  14. id=4 @192.168.0.104 (mysql-5.7.33 ndb-7.6.17)
  15. id=5 @192.168.0.106 (mysql-5.7.33 ndb-7.6.17)

所有集群服务部署完毕,我们来测试一下集群是否真的部署成功

在 192.168.0.106 的 MySQL 上创建数据库和表

  1. CREATE DATABASE `wechat`;
  2. CREATE TABLE wechat.user (
  3. Column1 varchar(100) NULL,
  4. Column2 varchar(100) NULL
  5. )
  6. ENGINE=ndbcluster
  7. DEFAULT CHARSET=utf8mb4
  8. COLLATE=utf8mb4_general_ci;
  9. 插入数据并查看
  10. mysql> show databases;
  11. +--------------------+
  12. | Database |
  13. +--------------------+
  14. | information_schema |
  15. | mysql |
  16. | ndbinfo |
  17. | performance_schema |
  18. | sys |
  19. | wechat |
  20. +--------------------+
  21. 6 rows in set (0.00 sec)
  22.  
  23. mysql> select * from wechat.user;
  24. Empty set (0.02 sec)
  25.  
  26. mysql> insert wechat.user (Column1, column2) value ('1', '2');
  27. Query OK, 1 row affected (0.01 sec)
  28.  
  29. mysql> select * from wechat.user;
  30. +---------+---------+
  31. | Column1 | Column2 |
  32. +---------+---------+
  33. | 1 | 2 |
  34. +---------+---------+
  35. 1 row in set (0.00 sec)

在另一个 SQL 服务器查询,结果是成功的

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | ndbinfo |
  8. | performance_schema |
  9. | sys |
  10. | wechat |
  11. +--------------------+
  12. 6 rows in set (0.00 sec)
  13.  
  14. mysql> select * from wechat.user;
  15. Empty set (0.07 sec)
  16.  
  17. mysql> select * from wechat.user;
  18. +---------+---------+
  19. | Column1 | Column2 |
  20. +---------+---------+
  21. | 1 | 2 |
  22. +---------+---------+
  23. 1 row in set (0.00 sec)

现在我们把其中一个数据节点关掉,在管理服务器我们看到 ndbd已经关闭一个了

  1. root@mgm:/usr/local/bin# ndb_mgm
  2. -- NDB Cluster -- Management Client --
  3. ndb_mgm> show
  4. Connected to Management Server at: localhost:1186
  5. Cluster Configuration
  6. ---------------------
  7. [ndbd(NDB)] 2 node(s)
  8. id=2 @192.168.0.104 (mysql-5.7.33 ndb-7.6.17, Nodegroup: 0, *)
  9. id=3 (not connected, accepting connect from 192.168.0.106)
  10.  
  11. [ndb_mgmd(MGM)] 1 node(s)
  12. id=1 @192.168.0.105 (mysql-5.7.33 ndb-7.6.17)
  13.  
  14. [mysqld(API)] 2 node(s)
  15. id=4 @192.168.0.104 (mysql-5.7.33 ndb-7.6.17)
  16. id=5 @192.168.0.106 (mysql-5.7.33 ndb-7.6.17)

写入一笔数据

  1. mysql> select * from wechat.user;
  2. +---------+---------+
  3. | Column1 | Column2 |
  4. +---------+---------+
  5. | 1 | 2 |
  6. +---------+---------+
  7. 1 row in set (0.01 sec)
  8.  
  9. mysql> insert into wechat.user (Column1, column2) value ('3', '4');
  10. Query OK, 1 row affected (0.00 sec)
  11.  
  12. mysql> select * from wechat.user;
  13. +---------+---------+
  14. | Column1 | Column2 |
  15. +---------+---------+
  16. | 3 | 4 |
  17. | 1 | 2 |
  18. +---------+---------+
  19. 2 rows in set (0.00 sec)

在另一台 SQL 服务器查询,结果还是一致的

  1. mysql> select * from wechat.user;
  2. +---------+---------+
  3. | Column1 | Column2 |
  4. +---------+---------+
  5. | 3 | 4 |
  6. | 1 | 2 |
  7. +---------+---------+
  8. 2 rows in set (0.00 sec)

我们再关闭 192.168.0.106 SQL服务

  1. root@mgm:/usr/local/bin# ndb_mgm
  2. -- NDB Cluster -- Management Client --
  3. ndb_mgm> show
  4. Connected to Management Server at: localhost:1186
  5. Cluster Configuration
  6. ---------------------
  7. [ndbd(NDB)] 2 node(s)
  8. id=2 @192.168.0.104 (mysql-5.7.33 ndb-7.6.17, Nodegroup: 0, *)
  9. id=3 (not connected, accepting connect from 192.168.0.106)
  10.  
  11. [ndb_mgmd(MGM)] 1 node(s)
  12. id=1 @192.168.0.105 (mysql-5.7.33 ndb-7.6.17)
  13.  
  14. [mysqld(API)] 2 node(s)
  15. id=4 @192.168.0.104 (mysql-5.7.33 ndb-7.6.17)
  16. id=5 (not connected, accepting connect from 192.168.0.106)

在 192.168.0.104 的 SQL 服务写入一笔数据

  1. mysql> insert into wechat.user (Column1, column2) value ('5', '6');
  2. Query OK, 1 row affected (0.00 sec)
  3.  
  4. mysql> select * from wechat.user;
  5. +---------+---------+
  6. | Column1 | Column2 |
  7. +---------+---------+
  8. | 5 | 6 |
  9. | 3 | 4 |
  10. | 1 | 2 |
  11. +---------+---------+
  12. 3 rows in set (0.00 sec)

启动 192.168.0.106 的数据服务和SQL服务

  1. root@mgm:/usr/local/bin# ndb_mgm
  2. -- NDB Cluster -- Management Client --
  3. ndb_mgm> show
  4. Connected to Management Server at: localhost:1186
  5. Cluster Configuration
  6. ---------------------
  7. [ndbd(NDB)] 2 node(s)
  8. id=2 @192.168.0.104 (mysql-5.7.33 ndb-7.6.17, Nodegroup: 0, *)
  9. id=3 @192.168.0.106 (mysql-5.7.33 ndb-7.6.17, Nodegroup: 0)
  10.  
  11. [ndb_mgmd(MGM)] 1 node(s)
  12. id=1 @192.168.0.105 (mysql-5.7.33 ndb-7.6.17)
  13.  
  14. [mysqld(API)] 2 node(s)
  15. id=4 @192.168.0.104 (mysql-5.7.33 ndb-7.6.17)
  16. id=5 @192.168.0.106 (mysql-5.7.33 ndb-7.6.17)

在 192.168.0.106 查询数据库发现,发生故障期间产生的数据已经同步了过来

  1. root@ndb1:~# mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 4
  5. Server version: 5.7.33-ndb-7.6.17-cluster-gpl MySQL Cluster Community Server (GPL)
  6.  
  7. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  8.  
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12.  
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14.  
  15. mysql> select * from wechat.user;
  16. +---------+---------+
  17. | Column1 | Column2 |
  18. +---------+---------+
  19. | 1 | 2 |
  20. | 5 | 6 |
  21. | 3 | 4 |
  22. +---------+---------+
  23. 3 rows in set (0.08 sec)

数据库集群部署成功了,总结一下集群的注意事项

  1. 创建表的时候,需要设置ENGINE=ndbcluster,具体请看上面的建表脚本。
  2. 每个 SQL 服务需要创建一样的用户密码
  3. 管理服务器不能全部发生故障,否则集群数据库操作失败。
  4. 数据服务器不能全部发生故障,否则集群数据库操作失败。
  5. SQL 服务器发生故障期间建立的数据库,在恢复后不会自动同步新建数据库过来,需要手动在故障恢复后的服务器上创建同名数据库,之后数据才会自动同步过来。
  6. 只要管理服务器和数据服务器越多,故障发生时,才能保证数据安全的写入,才不会导致数据库系统不可用。
  7. SQL 服务器越多,把数据库访问的请求通过负载均衡服务分摊到各个 SQL 服务器,才能承受更多的并发量。
  8. 集群启动必须按照以下顺序依次启动,管理服务->数据服务->SQL服务。

以上就是如何搭建 MySQL 高可用高性能集群的详细内容,更多关于搭建 MySQL 高可用高性能集群的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/huangmingji/p/14909174.html