1.架構確認
mysql cluster 基本架構如下
MySQL data nodes (ndbd) : 為 SQL Node 資料交換角色,同時會進行相互同步抄寫
Cluster Manager (ndb_mgmd) : 負責整個Cluster 集群中各個節點的管理工作,
MySQL server/client (mysqld) : 主要為接收/傳遞對外接口角色,由 AP Service 連結
2.建立主機
198.168.1.61 ndb_mgmd
198.168.1.62 ndbd01
198.168.1.63 ndbd02
198.168.1.64 mysqld01
如遇到需要下載部分請按照需求版本更動
這裡的 OS 跟 DB 的版本分別是
OS: ubuntu 20.04
mysql-cluster: 8.0.28
可由這邊查詢連結
https://dev.mysql.com/downloads/cluster/
3.安裝 ndb_mgmd
#下載 ndb_mgmd wget https://dev.mysql.com/get/Downloads/MySQL-Cluster-8.0/mysql-cluster-community-management-server_8.0.28-1ubuntu20.04_amd64.deb #安裝 ndb_mgmd sudo dpkg -i mysql-cluster-community-management-server_8.0.28-1ubuntu20.04_amd64.deb #ndb_mgmd 設定 sudo mkdir /var/lib/mysql-cluster sudo vi /var/lib/mysql-cluster/config.ini
[ndbd default] #data 備分數量 NoOfReplicas=2 [ndb_mgmd] # ndb_mgmd 參數設定 hostname=192.168.1.61 datadir=/var/lib/mysql-cluster [ndbd] #node 的 IP 或是 hostname hostname=192.168.1.62 #這個 Node 的 ID NodeId=2 #該主機資料夾位置 datadir=/usr/local/mysql/data [ndbd] #node 的 IP 或是 hostname hostname=192.168.1.63 #這個 Node 的 ID NodeId=3 #該主機資料夾位置 datadir=/usr/local/mysql/data [mysqld] # mysqld 參數設定 hostname=192.168.1.64
測試啟動 ndb_mgmd
sudo ndb_mgmd -f /var/lib/mysql-cluster/config.ini #服務啟動成功 MySQL Cluster Management Server mysql-8.0.28 ndb-8.0.28 2022-02-18 07:22:10 [MgmtSrvr] INFO -- The default config directory '/usr/mysql-cluster' does not exist. Trying to create it... 2022-02-18 07:22:10 [MgmtSrvr] INFO -- Sucessfully created config directory
但是如果每次開機都需要手動啟動一次那就很麻煩,所以把這個東西寫進 service 程序裡面
sudo vi /etc/systemd/system/ndb_mgmd.service
[Unit] Description=MySQL NDB Cluster Management Server After=network.target auditd.service [Service] Type=forking ExecStart=/usr/sbin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure [Install] WantedBy=multi-user.target
#重讀 service 配置 sudo systemctl daemon-reload #將剛剛設定的服務 enable sudo systemctl enable ndb_mgmd #重開機後確認是否有順利執行 sudo systemctl status ndb_mgmd #service 正在運作中 ● ndb_mgmd.service - MySQL NDB Cluster Management Server Loaded: loaded (/etc/systemd/system/ndb_mgmd.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-02-18 07:50:01 UTC; 23s ago Main PID: 866 (ndb_mgmd) Tasks: 12 (limit: 2274) Memory: 7.6M CGroup: /system.slice/ndb_mgmd.service └─866 /usr/sbin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini Feb 18 07:50:01 ndb systemd[1]: Starting MySQL NDB Cluster Management Server... Feb 18 07:50:01 ndb ndb_mgmd[795]: MySQL Cluster Management Server mysql-8.0.28 ndb-8.0.28 Feb 18 07:50:01 ndb systemd[1]: Started MySQL NDB Cluster Management Server.
4.安裝 data node
#安裝 libclass-methodmaker-perl sudo apt update && sudo apt install -y libclass-methodmaker-perl #下載 data node wget https://dev.mysql.com/get/Downloads/MySQL-Cluster-8.0/mysql-cluster-community-data-node_8.0.28-1ubuntu20.04_amd64.deb #安裝 data node sudo dpkg -i mysql-cluster-community-data-node_8.0.28-1ubuntu20.04_amd64.deb #建立資料庫目錄 sudo mkdir -p /usr/local/mysql/data #建立設定檔 sudo vi /etc/my.cnf
[mysql_cluster] #設定 ndb_mgmd 的位置 ndb-connectstring=192.168.1.61
測試啟動 node
sudo ndbd #node 成功連上了 ndb_mgmd 2022-02-18 08:44:29 [ndbd] INFO -- Angel connected to '192.168.1.61:1186' 2022-02-18 08:44:29 [ndbd] INFO -- Angel allocated nodeid: 2
將 node 寫成 service
sudo vi /etc/systemd/system/ndbd.service
[Unit] Description=MySQL NDB Data Node Daemon After=network.target auditd.service [Service] Type=forking ExecStart=/usr/sbin/ndbd ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure [Install] WantedBy=multi-user.target
#重讀 service 配置 sudo systemctl daemon-reload #將剛剛設定的服務 enable sudo systemctl enable ndbd #重開機後確認是否有順利執行 sudo systemctl status ndbd #service 正在運作中 ● ndbd.service - MySQL NDB Data Node Daemon Loaded: loaded (/etc/systemd/system/ndbd.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-02-18 09:51:25 UTC; 1min 42s ago Main PID: 868 (ndbd) Tasks: 46 (limit: 2274) Memory: 784.7M CGroup: /system.slice/ndbd.service ├─868 /usr/sbin/ndbd └─869 /usr/sbin/ndbd Feb 18 09:51:24 ndbd01 systemd[1]: Starting MySQL NDB Data Node Daemon... Feb 18 09:51:25 ndbd01 ndbd[797]: 2022-02-18 09:51:25 [ndbd] INFO -- Angel connected to '192.168.1.61:1186' Feb 18 09:51:25 ndbd01 ndbd[797]: 2022-02-18 09:51:25 [ndbd] INFO -- Angel allocated nodeid: 2 Feb 18 09:51:25 ndbd01 systemd[1]: Started MySQL NDB Data Node Daemon.
5.安裝 mysqld
ubuntu apt install 裡面雖然有 mysql server/client 但是並不支援 MySQL Cluster engine NDB,所以要另外安裝
#安裝 libaio1 libmecab2 sudo apt update && sudo apt -y install libaio1 libmecab2 libnuma1 psmisc #下載 DEB Bundle wget https://dev.mysql.com/get/Downloads/MySQL-Cluster-8.0/mysql-cluster_8.0.28-1ubuntu20.04_amd64.deb-bundle.tar mkdir DEB tar -xvf mysql-cluster_8.0.28-1ubuntu20.04_amd64.deb-bundle.tar -C DEB/ cd DEB #安裝需求套件 sudo dpkg -i mysql-common_8.0.28-1ubuntu20.04_amd64.deb sudo dpkg -i mysql-cluster-community-client-plugins_8.0.28-1ubuntu20.04_amd64.deb sudo dpkg -i mysql-cluster-community-client-core_8.0.28-1ubuntu20.04_amd64.deb sudo dpkg -i mysql-cluster-community-client_8.0.28-1ubuntu20.04_amd64.deb sudo dpkg -i mysql-client_8.0.28-1ubuntu20.04_amd64.deb sudo dpkg -i mysql-cluster-community-server-core_8.0.28-1ubuntu20.04_amd64.deb sudo dpkg -i mysql-cluster-community-server_8.0.28-1ubuntu20.04_amd64.deb sudo dpkg -i mysql-server_8.0.28-1ubuntu20.04_amd64.deb #設定 cnf 檔案 sudo vi /etc/mysql/my.cnf
[mysqld] #啟動 NDB storage engine ndbcluster [mysql_cluster] #設定 ndb_mgmd 的位置 ndb-connectstring=192.168.1.61
重新啟動 mysql server
sudo systemctl restart mysql
6. 確認 MySQL Cluster 啟動狀況
登入 mysql
mysql -u root -p #登入後確認已經是 MySQL Cluster Serve 了 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.28-cluster MySQL Cluster Community Server - GPL Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
登入後查詢 ENGINE 狀態
SHOW ENGINE NDB STATUS \G
*************************** 1. row *************************** Type: ndbclus Name: connection Status: cluster_node_id=4, connected_host=192.168.1.61, connected_port=1186, number_of_data_nodes=2, number_of_ready_data_nodes=2, connect_count=0 *************************** 2. row *************************** ... #確認已經有連上了 ndb_mgmd
確認 ndb_mgm 啟動狀況
ndb_mgm
#進入管理介面 -- NDB Cluster -- Management Client -- ndb_mgm>
查看目前狀態
SHOW
Connected to Management Server at: 192.168.1.61:1186 Cluster Configuration --------------------- [ndbd(NDB)] 2 node(s) id=2 @192.168.1.62 (mysql-8.0.28 ndb-8.0.28, Nodegroup: 0, *) id=3 @192.168.1.63 (mysql-8.0.28 ndb-8.0.28, Nodegroup: 0) [ndb_mgmd(MGM)] 1 node(s) id=1 @192.168.1.61 (mysql-8.0.28 ndb-8.0.28) [mysqld(API)] 1 node(s) id=4 @192.168.1.64 (mysql-8.0.28 ndb-8.0.28) #目前列出所有主機的資訊
後記
NoOfReplicas 這個參數很有趣,他並不是以 node 數量來決定的,而是影響到了 node group 的數量計算方式如下