###### tags: `server` # 伺服器 ## MariaDB Cluster: Galera * 關閉GUI介面(節省資源,不然一次開三台主機會爆掉) sudo systemctl isolate multi-user.target * 開啟GUI介面 sudo systemctl isolate graphical.target ### 1. 準備三台主機 server1 192.168.31.174 server2 192.168.31.245 server3 192.168.31.86 ### 2. <a href="https://www.digitalocean.com/community/tutorials/how-to-install-mariadb-on-rocky-linux-9">各自安裝MariaDB</a> * 安裝MariaDB ``` 1. 安裝mariadb $ sudo dnf install mariadb-server 2. 啟動mariadb $ sudo systemctl start mariadb 3.確認mariadb狀態 $ sudo systemctl status mariadb 4. 設為開機啟動 $ sudo systemctl enable mariadb ``` * 新增使用者: 先以root身分登入,新增使用者並給予權限 ``` 1. $ mysql -u root 2. $ CREATE USER 'my_user'@'localhost' IDENTIFIED BY 'my_password'; 3. $ GRANT ALL PRIVILEGES ON *.* TO 'my_user'@'localhost'; 4. $ flush privileges; ``` ### 3. 各自安裝MariaDB Galera $ sudo dnf -y install mariadb-server-galera ### 4. 照著參考網址做吧! * <a href="https://techviewleo.com/install-mariadb-galera-cluster-on-rocky-linux-with-proxysql/">參考網址1</a> * <a href="https://www.server-world.info/en/note?os=CentOS_Stream_9&p=mariadb&f=5">參考網址2</a> ### 5. 補充遇到問題 database mariadb is probably initialized in /var/lib/mysql already, nothing is done. if is not the case, make sure the /var/lib/mysql isempty before running mariadb-prepare-db-dir 原因: mysql已在此目錄下初始化過,導致這次初始化失敗 解決方法: 解1. 清空/var/lib/mysql 解2. 把cluster初始資料夾位置轉移 解3. mysqld_safe --skip-grant-tables --skup-networking&(不建議) 搞了好久結果是我設定檔字拼錯 ## MQTT Server: Mosquitto * ### <a href="https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-centos-7">centos系統上方法安裝</a>(建議用) * ### 另外找到的方法-用snap ### 1. <a href="https://snapcraft.io/docs/installing-snap-on-rocky">安裝snap</a> 1. 因為rocky本身並沒有snap,也不能直接用yum或dnf來安裝 解決方法: snap可以在rocky的Extra Packages for Enterprise Linux(EPEL) 存儲庫中找到並安裝。 $ sudo dnf install epel-release $ sudo dnf upgrade 2. 安裝並啟動snap $ sudo yum install snapd $ sudo systemctl enable --now snapd.socket $ sudo ln -s /var/lib/snapd/snap /snap ### 2. <a href="https://mosquitto.org/download/">安裝mosquitto</a> $ sudo snap install mosquitto ### 3. 啟動mosquitto 通常啟動服務都是用指令 $ sudo systemctl start "service", 不過這次卻遇到unit not found的問題,因此改用: $ sudo snap run mosquitto 若出現ipv4 or ipv6 port 1883 Address already in use,代表1883 port已被其他服務(有可能是自己)占用 -> 用netstat -ntpl | grep 1883 確認PID後Kill該process 再重新執行 $ sudo snap run mosquitto *注意: 若該process就是mosquitto,要先 $ sudo snap stop mosquitto ### 4. 測試mosquitto訂閱功能 * [本地測試](https://ithelp.ithome.com.tw/articles/10217726) * 遠端測試(匿名登入) ``` 1. 準備2台安裝好mosquitto的虛擬機server1 server2 2. 使用要接收訊息的server1,並cd到/var/snap/mosquitto/common (centos方法的的設定檔為/etc/mosquitto/mosquitto.conf) 3. 複製目錄下的mosquitto_example.conf到同目錄,名稱為mosquitto.conf $ sudo cp ./mosquitto_example.conf ./mosquitto.conf 4. 去掉allow_annoymous的註解並在後方加上true 5. 在listener加上port和ip 6. 重啟mosquitto 7. 將server1設為訂閱者,A為頻道名 $ mosquitto_sub -h {server1 ip} -t A 8. 用傳送訊息的server2,將server2設為發送者,在A頻道傳送訊息 $ mosquitto_pub -h {server1 ip} -t A -m "test message" 9. 正確的話server1會收到server2傳來的訊息 ``` ### 5. 補充遇到問題 * 可以使用centos上安裝mosquitto的方法在rocky上安裝,因為這兩者都是基於Red Hat Enterprise Linux (RHEL) 的 Linux發行版,所以有許多相似之處 * 使用snap方法安裝的mosquitto不會被系統偵測到,且設定檔案位置也不同,之後可能會有別的問題,因此不建議使用 --- #### *下列範例皆使用以centos方法安裝的mosquitto操作 ### 6. 更改為需要密碼登入 ``` 1. 新增使用帳號及密碼 $ sudo mosquitto_passwd -c /etc/mosquitto/passwd youraccount 2. 刪除原本的設定檔 $ sudo rm /etc/mosquitto/mosquitto.conf 3. 新增新設定檔,內容如下 $ sudo vi /etc/mosquitto/mosquitto.conf listener 1883 #要記得加這行,否則設定會失效 allow_anonymous false password_file /etc/mosquitto/passwd 4. 重啟mosquitto $ sudo systemctl restart mosquitto 5. 測試是否生效,成功會出現以下錯誤訊息 $ mosquitto_pub -h localhost -t "test" -m "hello world" 報錯: Output Connection Refused: not authorised. Error: The connection was refused. 6. 在新視窗啟動訂閱者,並以帳密登入 $ mosquitto_sub -h localhost -t "test" -m "hello world" -u "youraccount" -P "yourpassword" 7. 在原本視窗啟動發送者,並以帳密登入 $ mosquitto_pub -h localhost -t "test" -m "hello world" -u "youraccount" -P "yourpassword" ``` ## Kafka * [參考網址1](https://www.atlantic.net/vps-hosting/how-to-install-apache-kafka-on-rocky-linux-8) * [參考網址2](https://www.digitalocean.com/community/tutorials/how-to-install-apache-kafka-on-centos-7) ### 1. 安裝java-openjdk ``` 1. 安裝java $ sudo dnf install java-11-openjdk 2. 輸入以下指令確認安裝成功 $ java -version ``` ### 2. 新增使用者kafka ``` 1. 新增使用者 $ sudo useradd kafka -m 2. 設定密碼 $ sudo passwd kafka 3. 將kafka加到wheel中,確保擁有安裝相關依賴檔的權限 $ sudo usermod -aG wheel kafka 4. 登入kafka $ su -l kafka ``` ### 3. 下載&解壓kafka的檔案 ``` 1. 新增目錄來放下載檔案 $ mkdir ~/Downloads $ cd ~/Downlaods 2. 下載kafka檔案 $ wget https://archive.apache.org/dist/kafka/3.0.0/kafka_2.13-3.0.0.tgz 3. 新增kafka目錄來放解壓縮的檔案 $ mkdir ~/kafka $ cd ~/kafka 4. 把/Downloads中的檔案解壓縮到/kafka $ tar xzf ~/Downloads/kafka_2.13-3.0.0.tgz --strip 1 ``` ### 4. 修改kafka server ``` 在下列檔案最後面加上 delete.topic.enable = true $ vi ~/kafka/config/server.properties ``` ### 5. 建立systemd檔案來啟動kafka * #### 新增zookeeper.service ``` 建立zookeeper.service $ sudo vi /etc/systemd/system/zookeeper.service 內容如下: ``` ![](https://i.imgur.com/VV5rcc6.png) * #### 新增kafka.service ``` 建立kafka.service $ sudo vi /etc/systemd/system/kafka.service 內容如下: ``` ![](https://i.imgur.com/uxjvm7x.png) ### 6. 啟動zookeeper和kafka ``` 1. 先重載設定 $ sudo systemctl daemon-reload 2. 啟動zookeeper並設為開機啟動 $ sudo systemctl start zookeeper $ sudo systemctl enable zookeeper 3. 啟動kafka並設為開機啟動 $ sudo systemctl start kafka $ sudo systemctl enable kafka 4. 檢查兩者是否都正常運作 $ sudo systemctl status zookeeper kafka ``` ### 7. 用kafka創建topic ``` 1. 輸入以下指令創建topic $ cd ~/kafka/bin $ ./kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic topic1 ``` * #### 注意這邊要打--bootstrap-server而不是--zookeeper,因為[zookeeper](https://stackoverflow.com/questions/53428903/zookeeper-is-not-a-recognized-option-when-executing-kafka-console-consumer-sh)已被棄用 ``` 2. 確認topic1被成功創建,正確會輸出topic1 $ ./kafka-topics.sh --list --bootstrap-server localhost:9092 3. 使用producer傳輸event 新增event1,並輸入內容 $ ./kafka-console-producer.sh --broker-list localhost:9092 --topic event1 >Hi 87, this is my first event 4. producer會得到以下輸出: [2021-10-22 07:58:05,318] WARN [Producer clientId=console-producer] Error while fetching metadata with correlation id 3 : {event1=LEADER_NOT_AVAILABLE (org.apache.kafka.clients.NetworkClient) 5. 使用consumer接收event $ ./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic event1 --from-beginning 6. consumer會得到以下輸出: Hi 87, this is my first event `````` ### 8. 補充遇到問題 * 一開始主要都是參照<a href="https://www.digitalocean.com/community/tutorials/how-to-install-apache-kafka-on-centos-7">參考網址2</a>的方法實作,原本在設定zookeeper.service的ExecStart時,是沒有加最前面的/bin/sh,這造成啟動zookeeper時出現PermissionDenied的問題。最初以為是檔案權限問題,但卻沒有用,後來找了好久也找不到原因,最後是在參照<a href="https://www.atlantic.net/vps-hosting/how-to-install-apache-kafka-on-rocky-linux-8">參考網址1</a>的設定後把/bin/sh加到最前面問題就解決了! ## redis * [redis安裝教學](https://www.golinuxcloud.com/install-redis-server-rocky-linux-9/) * [redis使用教學](https://ithelp.ithome.com.tw/articles/10105731) ### 1. 安裝&啟動redis ``` 1. 安裝redis $ sudo dnf -y install redis 2. 設為開機啟動 $ sudo systemctl enable redis 3. 啟動redis $ sudo systemctl start redis 4. 確認redis狀態 $ sudo systemctl status redis ``` ### 2. 更改redis設定檔 * #### 修改/etc/redis/redis.conf的部分內容如下: ``` #bind 127.0.0.1 -::1 bind 0.0.0.0 #開啟遠端訪問 requirepass test@12345 #帳號@密碼 #pidfile /var/run/redis_6379.pid pidfile /var/run/redis/redis-server.pid #新增pid檔案 #appendonly no appendonly yes ``` ### 3. 重啟redis ``` 1. 重新啟動redis $ sudo systemctl restart redis 2. 確認redis狀態 $ sudo systemctl status redis ``` ### 4. 測試redis ``` 1. 進入redis-cli $ redis-cli 2. 這時還未登入帳號,如果下指令會出現(error) NOAUTH Authentication required的錯誤 3. 登入剛才創建的帳號(出現OK就代表登入成功) $ auth test@12345 4. 切換資料庫(預設是0) $ select 1 5. 新增key:value $ set dog fengdog >OK 6. 查詢key $ get dog >"fengdog" 7. 列出所有key $ keys * >1) "dog" ``` ### 5. 遠端連線redis * #### 用另一台虛擬機telnet連線到剛才裝好redis的虛擬機 * #### 已裝好redis的虛擬機的ip: 192.168.31.245 ``` 1. 用telent連線過去 $ telnet 192.168.31.245 6379 #因為redis服務是在6379port上執行 2. 這時可能會無法成功連線,原因可能是防火牆的設定 3. 防火牆開啟6379port $ sudo firewall-cmd --add-port=6379/tcp --permanent 4. 套用防火牆設定 $ sudo firewall-cmd --reload 5. 重新用telnet連線 $ telnet 192.168.31.245:6379 ``` * #### 出現以下畫面代表遠端連線成功 ![](https://i.imgur.com/5pnTzz5.png)