antony@notes:~/suse$ cat "Install-and-Setup-MySQL-on-SLE-HAE.md"
Install and Setup MySQL on SLE HAE
Preface
本篇文章會主要會介紹,如何在 SUSE Linux Enterprise HAE 上安裝和設定 MySQL,並將 MySQL 創建為 HAE 中的 Cluster Resource ,目標是模擬 MySQL 服務所在的機器完全故障時,如何實現 MySQL 服務會在故障時自動移轉,來繼續提供服務。
可以透過點擊以下目錄,選擇想看的內容,跳轉至特定章節
環境準備
- 機器 3 台,兩台是 HAE 叢集,另外一台在叢集外部提供 Share Storage 的服務
安裝和設定 NFS Server
ssh連線至提供 Share Storage 服務的機器- 安裝 NFS Server
$ sudo zypper in -y yast2-nfs-server- 啟動和設定 NFS Server
$ sudo yast2 nfs_server- 設定啟動 NFS Server

設定好後
Next
- 選擇
[Add Directory],新增要共享的目錄,輸入/var/lib/mysql

- 將權限調整為
rw

設定好後
OK
- 確認設定都正確後,選
[Finish]

- 建立 MySQL 使用者
$ /usr/sbin/useradd -r -c MySQL database admin -d /var/lib/mysql -U -u 60 mysql -s /usr/sbin/nologin- 更改
/var/lib/mysql的目錄擁有者和群組為mysql
$ sudo chown -R mysql:mysql /var/lib/mysql安裝和設定 NFS Client
- 在兩台 SLE HAE 叢集的 Node 上安裝 NFS Client
- 執行以下命令安裝 NFS Client
$ sudo zypper in -y yast2-nfs-client- 設定 NFS Client
$ sudo yast2 nfs點選 Add 新增 NFS Server 的資訊
- NFS Server Hostname:
192.168.11.173 - Remote Directory:
/var/lib/mysql - Mount Point (local):
/var/lib/mysql
設定好後,點選 OK

確認資訊都正確後,點選 OK

ssh連線至另一台機器做同樣的安裝和設定操作
安裝和設定 MySQL Server 和 Client
在兩台 SLE HAE 叢集的機器上都要安裝 MySQL Server 和 Client 端
連線至第一台機器,執行以下安裝命令
$ sudo zypper in -y mysql mysql-client- 設定任意 IP 都可以連線到 MySQL
$ sudo nano /etc/my.cnf修改檔案內容如下 :
bind-address = 0.0.0.0- 將
bind-address的值設定為0.0.0.0
- 啟動 MySQL
$ sudo systemctl start mariadb- 安全性設定
$ sudo mysql_secure_installation設定內容如下 :
Enter current password for root (enter for none):
(輸入現有root密碼,enter)
Switch to unix_socket authentication [Y/n]
( y,啟用unix_socket 身份驗證)
Change the root password? [Y/n]
( y,更換 root 密碼)
New password: root
Re-enter new password: root
Remove anonymous users? [Y/n]
( Y,移除匿名登入)
Disallow root login remotely? [Y/n]
( Y,移除遠端 root 登入權限)
Remove test database and access to it? [Y/n]
( Y,移除測試資料庫及帳號)
Reload privilege tables now? [Y/n]
( Y,重新載入權限表) - 關閉 MySQL 服務
$ sudo systemctl stop mariadb連線至第二台機器執行以上同樣安裝和設定步驟
在 MySQL 建立使用者,並允許透過任何網路登入
## 登入 MySQL
$ sudo mysql
## 建立 bigred 使用者並且設定最高權限
$ GRANT ALL PRIVILEGES ON *.* TO 'bigred'@'%' IDENTIFIED BY 'bigred' WITH GRANT OPTION;
Query OK, 0 rows affected (0.007 sec)
## 立刻生效
$ FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)
## 檢查
$ SELECT user,host FROM mysql.user;
+-------------+-----------+
| User | Host |
+-------------+-----------+
| bigred | % |
| mariadb.sys | localhost |
| mysql | localhost |
| root | localhost |
+-------------+-----------+
4 rows in set (0.007 sec)
## 離開
$ \q
Bye- 使用 bigred 使用者透過網路登入 MySQL
$ mysql -u bigred -pbigred -h 192.168.11.172 -P 3306將 MySQL 設定為 Cluster Resource
- 執行以下命令將 MySQL 設定為 Cluster Resource
$ sudo crm configure primitive mysql systemd:mariadb op start timeout=100 interval=0 op stop timeout=100 interval=0 op monitor timeout=100 interval=60 meta target-role=Stopped- 透過
crm啟動 MySQL
$ sudo crm resource start mysql- 檢查 MySQL Cluster Resource 的狀態
$ sudo crm status螢幕輸出 :
Status of pacemakerd: 'Pacemaker is running' (last updated 2023-11-29 13:39:04 +08:00)
Cluster Summary:
* Stack: corosync
* Current DC: sle-ha-1 (version 2.1.5+20221208.a3f44794f-150500.4.9-2.1.5+20221208.a3f44794f) - partition with quorum
* Last updated: Wed Nov 29 13:39:05 2023
* Last change: Wed Nov 29 11:03:19 2023 by root via cibadmin on sle-ha-2
* 2 nodes configured
* 3 resource instances configured
Node List:
* Online: [ sle-ha-1 sle-ha-2 ]
Full List of Resources:
* sbd (stonith:external/sbd): Started sle-ha-1
* Resource Group: Group-mysql:
* admin-ip (ocf::heartbeat:IPaddr2): Started sle-ha-1
* mysql (systemd:mariadb): Started sle-ha-1- 將 MySQL 和 VIP 組成群組
$ sudo crm configure group Group-mysql admin-ip mysql meta target-role=Stopped- 將群組資源啟動
$ sudo crm resource start Group-mysql- 透過 VIP 登入 MySQL 測試
$ mysql -u bigred -pbigred -h 192.168.11.170 -P 3306- 建立資料庫、資料表和匯入資料
## 建立 haha Database
> create database haha;
## 進入 Database
> use haha;
## 建 customer 資料表
> CREATE TABLE customer (
ID int NOT NULL,
NAME varchar(100) NOT NULL,
AGE int,
ADDRESS varchar(255),
SALARY DECIMAL(10,2),
PRIMARY KEY (ID)
);
## 新增 7 筆資料
> INSERT INTO customer
VALUES (1,'Ramesh',32,'Ahmedabad','2000.00'),
(2,'Khilan',25,'Delhi','1500.00'),
(3,'kaushik',23,'kota','2000.00'),
(4,'Chaitali',25,'Mumbai','6500.00'),
(5,'Hardik',27,'Bhopal','8500.00'),
(6,'Komal',22,'MP','4500.00'),
(7,'Muffy',24,'Indore','10000.00');
## 顯示 customer 的所有資料
> select * from customer;
+----+----------+------+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+------+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+------+-----------+----------+
7 rows in set (0.001 sec)
## 離開
> \q故障模擬測試
目標
將 MySQL 服務所在的 Node 關閉,並測試 SLE HAE 是否會自動執行服務故障轉移到叢集中健康的 Node 上。
實作
- 由於當前 MySQL 在叢集中的第一台 Node 上,故將第一台 Node 關閉
$ sudo poweroff- 檢視 MySQL 服務與 VIP 這兩個 Cluster Resources 是否被移轉
$ sudo crm status螢幕輸出 :
Status of pacemakerd: 'Pacemaker is running' (last updated 2023-11-29 14:45:47 +08:00)
Cluster Summary:
* Stack: corosync
* Current DC: sle-ha-2 (version 2.1.5+20221208.a3f44794f-150500.4.9-2.1.5+20221208.a3f44794f) - partition with quorum
* Last updated: Wed Nov 29 14:45:48 2023
* Last change: Wed Nov 29 11:03:19 2023 by root via cibadmin on sle-ha-2
* 2 nodes configured
* 3 resource instances configured
Node List:
* Online: [ sle-ha-2 ]
* OFFLINE: [ sle-ha-1 ]
Full List of Resources:
* sbd (stonith:external/sbd): Started sle-ha-2
* Resource Group: Group-mysql:
* admin-ip (ocf::heartbeat:IPaddr2): Started sle-ha-2
* mysql (systemd:mariadb): Started sle-ha-2- 可以看到在 Node List 區塊中,第一台 Node 已離線
- 在 Full List of Resources 區塊中,MySQL 服務與 VIP 已被移轉至第二台 Node 上
- 測試能否透過 VIP 連接 MySQL
$ mysql -u bigred -pbigred -h 192.168.11.170 -P 3306螢幕輸出 :
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.6.12-MariaDB MariaDB package
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>- 檢查資料是否還存在
> show databases;
+--------------------+
| Database |
+--------------------+
| haha |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
> select * from haha.customer;
+----+----------+------+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+------+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+------+-----------+----------+
7 rows in set (0.003 sec)