1.安裝 mariadb
# 1. 安裝
[root@mysql1 ~] yum -y install mariadb-server
[root@mysql1 ~]# systemctl start mariadb
[root@mysql1 ~]# systemctl enable mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@mysql1 ~]# systemctl status mariadb
● mariadb.service - MariaDB 10.5 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: disabled)
Active: active (running) since Mon 2023-11-06 21:13:26 CST; 19s ago
Docs: man:mariadbd(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 25550 (mariadbd)
Status: "Taking your SQL requests now..."
Tasks: 13 (limit: 11125)
Memory: 78.1M
CPU: 698ms
CGroup: /system.slice/mariadb.service
└─25550 /usr/libexec/mariadbd --basedir=/usr
Nov 06 21:13:25 mysql1 mariadbd[25550]: 2023-11-06 21:13:25 0 [Note] /usr/libexec/mariadbd (mysqld 10.5.16-MariaDB) starting as process 25550 ...
Nov 06 21:13:26 mysql1 systemd[1]: Started MariaDB 10.5 database server.
# 2. 檢視埠
[root@mysql1 ~]# netstat -antulp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 596/sshd: /usr/sbin
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 10.10.10.102:22 10.10.10.1:37344 ESTABLISHED 3994/sshd: root [pr
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::3306 :::* LISTEN 25550/mariadbd
udp 0 0 10.10.10.102:68 10.10.10.1:67 ESTABLISHED 538/NetworkManager
udp 0 0 0.0.0.0:111 0.0.0.0:* 1/systemd
udp 0 0 127.0.0.1:323 0.0.0.0:* 552/chronyd
udp6 0 0 :::111 :::* 1/systemd
udp6 0 0 ::1:323 :::* 552/chronyd
[root@mysql1 ~]#
[root@mysql1 ~]#
[root@mysql1 ~]# netstat -antulp | grep mariadb
tcp6 0 0 :::3306 :::* LISTEN 25550/mariadbd
# 3. 更改埠為12122
[root@mysql1 ~]# cat /etc/my.cnf.d/mariadb-server.cnf | grep -A5 -B5 port
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld/mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld]
port=12122
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
[root@mysql1 ~]# cat /etc/my.cnf.d/mariadb-server.cnf | grep -A5 -B5 port
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld/mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld]
port=12122
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
[root@mysql1 ~]#
[root@mysql1 ~]# systemctl restart mariadb.service
[root@mysql1 ~]#
[root@mysql1 ~]# netstat -antulp | grep mariadb
tcp6 0 0 :::12122 :::* LISTEN 25838/mariadbd
# 4. 資料庫設定
[root@mysql1 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.5.16-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# 建立blog資料庫
MariaDB [(none)]> CREATE DATABASE blog;
# 設定字符集
MariaDB [(none)]> ALTER DATABASE blog CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
# 建立使用者設定密碼
MariaDB [(none)]> CREATE USER 'ckh'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.007 sec)
# 使用者訪問表 授權
MariaDB [(none)]> GRANT ALL ON blog.* TO 'ckh'@'%';
Query OK, 0 rows affected (0.005 sec)
# 重新整理許可權
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)
# 以該使用者登入檢視許可權
[root@mysql1 ~]# mysql -uckh -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.5.16-MariaDB MariaDB Server
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 |
+--------------------+
| blog |
| information_schema |
+--------------------+
2 rows in set (0.001 sec)
# 5. 複製我的原始碼到mysql1(提前安裝go) scp 命令
[root@kvm ~]# ls
createVM.sh Desktop Documents Downloads Music p01_bk-p01_bkApi.zip Pictures Public Templates tmp Videos
[root@kvm ~]# scp p01_bk-p01_bkApi.zip root@10.10.10.78:/root
Warning: Permanently added '10.10.10.78' (ED25519) to the list of known hosts.
root@10.10.10.78\s password:
p01_bk-p01_bkApi.zip
# mysql1 上安裝解壓zip包的命令
[root@mysql1 ~]# yum -y install unzip
[root@mysql1 ~]# unzip p01_bk-p01_bkApi.zip
[root@mysql1 ~]# ls
install_go.sh p01_bk-p01_bkApi p01_bk-p01_bkApi.zip
[root@mysql1 ~]# cd p01_bk-p01_bkApi/
[root@mysql1 p01_bk-p01_bkApi]#
[root@mysql1 p01_bk-p01_bkApi]# ls
conf controllers go.mod go.sum lastupdate.tmp LICENSE main.go models package-lock.json README.md routers scripts tests word
# 安裝golang api 執行依賴包
[root@mysql1 p01_bk-p01_bkApi]# go mod vendor
# 建立日誌目錄
[root@mysql1 p01_bk-p01_bkApi]# mkdir /root/p01_bk-p01_bkApi/log
# 啟動 api 專案
[root@mysql1 p01_bk-p01_bkApi]# bee run # 要看到 http://:8092
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v2.1.0
2023/11/06 23:05:23 WARN ▶ 0001 Running application outside of GOPATH
2023/11/06 23:05:23 INFO ▶ 0002 Using 'p01_bk-p01_bkApi' as 'appname'
2023/11/06 23:05:23 INFO ▶ 0003 Initializing watcher...
2023/11/06 23:05:24 SUCCESS ▶ 0004 Built Successfully!
2023/11/06 23:05:24 INFO ▶ 0005 Restarting 'p01_bk-p01_bkApi'...
2023/11/06 23:05:24 SUCCESS ▶ 0006 './p01_bk-p01_bkApi' is running...
2023/11/06 23:05:24.196 [I] [parser.go:85] /root/p01_bk-p01_bkApi/controllers no changed
2023/11/06 23:05:24.197 [I] [server.go:241] http server Running on http://:8092
# client1 上 獲取資料
[root@client1 ~]# curl http://10.10.10.102:8092/v1/code/labels?token=wgnowehgiowGW66OEHGO.WEfwe12
# 生成可執行程式,放到後臺執行
[root@mysql1 p01_bk-p01_bkApi]# ls
conf controllers go.mod go.sum lastupdate.tmp LICENSE log main.go models package-lock.json README.md routers scripts tests vendor word
[root@mysql1 p01_bk-p01_bkApi]#
[root@mysql1 p01_bk-p01_bkApi]# go build
[root@mysql1 p01_bk-p01_bkApi]#
[root@mysql1 p01_bk-p01_bkApi]# ls
conf controllers go.mod go.sum lastupdate.tmp LICENSE log main.go models p01_bkApi package-lock.json README.md routers scripts tests vendor word
[root@mysql1 p01_bk-p01_bkApi]#
[root@mysql1 p01_bk-p01_bkApi]# chmod +x p01_bkApi
[root@mysql1 p01_bk-p01_bkApi]#
[root@mysql1 p01_bk-p01_bkApi]# ./p01_bkApi
2023/11/06 23:23:35.731 [I] [parser.go:85] /root/p01_bk-p01_bkApi/controllers no changed
2023/11/06 23:23:35.731 [I] [server.go:241] http server Running on http://:8092
2023/11/06 23:23:40.670 [D] [router.go:955] | 10.10.10.78| 200 | 4.435923ms| match| GET /v1/code/labels r:/v1/code/labels
^C
[root@mysql1 p01_bk-p01_bkApi]# ls
conf controllers go.mod go.sum lastupdate.tmp LICENSE log main.go models p01_bkApi package-lock.json README.md routers scripts tests vendor word
[root@mysql1 p01_bk-p01_bkApi]#
[root@mysql1 p01_bk-p01_bkApi]# ./p01_bkApi &
[1] 27616
[root@mysql1 p01_bk-p01_bkApi]# 2023/11/06 23:24:10.540 [I] [parser.go:85] /root/p01_bk-p01_bkApi/controllers no changed
2023/11/06 23:24:10.540 [I] [server.go:241] http server Running on http://:8092
[root@mysql1 p01_bk-p01_bkApi]#
[root@mysql1 p01_bk-p01_bkApi]#
[root@mysql1 p01_bk-p01_bkApi]# ls
conf controllers go.mod go.sum lastupdate.tmp LICENSE log main.go models p01_bkApi package-lock.json README.md routers scripts tests vendor word
[root@mysql1 p01_bk-p01_bkApi]# ls
conf controllers go.mod go.sum lastupdate.tmp LICENSE log main.go models p01_bkApi package-lock.json README.md routers scripts tests vendor word
[root@mysql1 p01_bk-p01_bkApi]# ps -ef | grep Api
root 27616 26451 0 23:24 pts/0 00:00:00 ./p01_bkApi
root 27624 26451 0 23:24 pts/0 00:00:00 grep --color=auto Api
[root@mysql1 p01_bk-p01_bkApi]# 2023/11/06 23:24:40.386 [D] [router.go:955] | 10.10.10.78| 200 | 3.613273ms| match| GET /v1/code/labels r:/v1/code/labels
# 但是日誌輸出還是列印到前臺了
[root@mysql1 p01_bk-p01_bkApi]# ls2023/11/06 23:27:52.144 [D] [router.go:955] | 10.10.10.78| 200 | 4.848498ms| match| GET /v1/code/labels r:/v1/code/labels
2023/11/06 23:27:52.728 [D] [router.go:955] | 10.10.10.78| 200 | 2.428822ms| match| GET /v1/code/labels r:/v1/code/labels
2023/11/06 23:27:53.309 [D] [router.go:955] | 10.10.10.78| 200 | 4.407138ms| match| GET /v1/code/labels r:/v1/code/labels
2023/11/06 23:27:53.866 [D] [router.go:955] | 10.10.10.78| 200 | 4.231196ms| match| GET /v1/code/labels r:/v1/code/labels
^C
# 使用nohup
[root@mysql1 p01_bk-p01_bkApi]# ps -aux | grep Api
root 27616 0.0 1.4 1096940 26488 pts/0 Sl 23:24 0:00 ./p01_bkApi
root 27632 0.0 0.1 6408 2176 pts/0 S+ 23:29 0:00 grep --color=auto Api
[root@mysql1 p01_bk-p01_bkApi]#
[root@mysql1 p01_bk-p01_bkApi]# kill -9 27616
[root@mysql1 p01_bk-p01_bkApi]#
[1]+ Killed ./p01_bkApi
[root@mysql1 p01_bk-p01_bkApi]#
[root@mysql1 p01_bk-p01_bkApi]# ls
conf controllers go.mod go.sum lastupdate.tmp LICENSE log main.go models p01_bkApi package-lock.json README.md routers scripts tests vendor word
[root@mysql1 p01_bk-p01_bkApi]# nohup ./p01_bkApi &
[1] 27636
[root@mysql1 p01_bk-p01_bkApi]# nohup: ignoring input and appending output to 'nohup.out'
[root@mysql1 p01_bk-p01_bkApi]# ls
conf controllers go.mod go.sum lastupdate.tmp LICENSE log main.go models nohup.out p01_bkApi package-lock.json README.md routers scripts tests vendor word
[root@mysql1 p01_bk-p01_bkApi]# cat nohup.out
2023/11/06 23:30:05.988 [I] [parser.go:85] /root/p01_bk-p01_bkApi/controllers no changed
2023/11/06 23:30:05.988 [I] [server.go:241] http server Running on http://:8092
[root@mysql1 p01_bk-p01_bkApi]#
[root@mysql1 p01_bk-p01_bkApi]# cat nohup.out
2023/11/06 23:30:05.988 [I] [parser.go:85] /root/p01_bk-p01_bkApi/controllers no changed
2023/11/06 23:30:05.988 [I] [server.go:241] http server Running on http://:8092
2023/11/06 23:30:33.162 [D] [router.go:955] | 10.10.10.78| 200 | 7.940736ms| match| GET /v1/code/labels r:/v1/code/labels
2023/11/06 23:30:33.680 [D] [router.go:955] | 10.10.10.78| 200 | 2.550892ms| match| GET /v1/code/labels r:/v1/code/labels
2023/11/06 23:30:34.216 [D] [router.go:955] | 10.10.10.78| 200 | 3.955831ms| match| GET /v1/code/labels r:/v1/code/labels
2023/11/06 23:30:34.720 [D] [router.go:955] | 10.10.10.78| 200 | 5.085136ms| match| GET /v1/code/labels r:/v1/code/labels
# 這兩個服務都不能掛掉
[root@mysql1 p01_bk-p01_bkApi]# ps -ef | grep Api
root 27636 26451 0 23:30 pts/0 00:00:00 ./p01_bkApi
root 27661 26451 0 23:36 pts/0 00:00:00 grep --color=auto Api
[root@mysql1 p01_bk-p01_bkApi]#
[root@mysql1 p01_bk-p01_bkApi]# systemctl status mariadb.service
● mariadb.service - MariaDB 10.5 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: disabled)
Active: active (running) since Mon 2023-11-06 22:29:30 CST; 1h 7min ago
Docs: man:mariadbd(8)
https://mariadb.com/kb/en/library/systemd/
Process: 26224 ExecStartPre=/usr/libexec/mariadb-check-socket (code=exited, status=0/SUCCESS)
Process: 26247 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/SUCCESS)
Process: 26299 ExecStartPost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/SUCCESS)
Main PID: 26282 (mariadbd)
Status: "Taking your SQL requests now..."
Tasks: 12 (limit: 11125)
Memory: 79.7M
CPU: 2.814s
CGroup: /system.slice/mariadb.service
└─26282 /usr/libexec/mariadbd --basedir=/usr
Nov 06 22:29:29 mysql1 systemd[1]: Starting MariaDB 10.5 database server...
Nov 06 22:29:30 mysql1 mariadb-prepare-db-dir[26247]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.
Nov 06 22:29:30 mysql1 mariadb-prepare-db-dir[26247]: If this is not the case, make sure the /var/lib/mysql is empty before running mariadb-prepare-db-dir.
Nov 06 22:29:30 mysql1 mariadbd[26282]: 2023-11-06 22:29:30 0 [Note] /usr/libexec/mariadbd (mysqld 10.5.16-MariaDB) starting as process 26282 ...
Nov 06 22:29:30 mysql1 systemd[1]: Started MariaDB 10.5 database server.
# 配置開機自啟
[root@kvm2 ~]# cat /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
bash /root/MyScripts/runApi.sh
[root@kvm2 ~]#
[root@kvm2 ~]#
[root@kvm2 ~]# cat /root/MyScripts/runApi.sh
#!/bin/bash
sleep 30
cd /root/p01_bk-p01_bkApi
chmod +x p01_bk-p01_bkApi
nohup ./p01_bk-p01_bkApi &