树莓派-ubuntu-环境下-mariadb-部署

1、下载树莓派 Ubuntu Server 20.04.3 LTS

https://cn.ubuntu.com/download/raspberry-pi

2、使用 balenaEtcher 制作系统到闪存

https://www.balena.io/etcher/

3、设置 wifi

参考官网步骤:https://ubuntu.com/tutorials/how-to-install-ubuntu-on-your-raspberry-pi#3-wifi-or-ethernet

启动系统前,在闪存找到 network-config 文件,移除注释,按需设置。例如:

1
2
3
4
5
6
7
wifis:
wlan0:
dhcp4: true
optional: true
access-points:
"home network":
password: "123456789"

4、启动系统

  • 默认用户名:ubuntu
  • 默认密码:ubuntu

5、安装 ssh

1
2
sudo apt-get update
sudo apt-get install openssh-server

执行 apt-get 若出现 Could not get lock… 等类似提示,如下:

1
2
3
E: Could not get lock /var/lib/dpkg/lock. It is held by process 34781 (dpkg)
N: Be aware that removing the lock file is not a solution and may break your system.
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

可通过 ps afx|grep apt 命令查看进程,使用 kill -9 <进程编号> 停止占用进程,再执行安装命令即可。

注:不必删除 lock 文件。

通过 sudo systemctl status ssh 查看服务状态是否正常,如下所示:

1
2
3
4
5
6
7
8
9
10
11
ubuntu@ubuntu:~$ sudo systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-02-11 03:48:39 UTC; 1h 43min ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 45984 (sshd)
Tasks: 1 (limit: 4435)
CGroup: /system.slice/ssh.service
└─45984 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups

通过 ssh ubuntu@xxx.xxx.xxx.xxx 连接即可。

6、安装 mariadb

参考官网步骤,采用阿里云镜像

https://mariadb.org/download/?t=repo-config&d=20.04+%22focal%22&v=10.6&r_m=aliyun

依次执行如下命令:

1
2
3
4
5
sudo apt-get install software-properties-common dirmngr apt-transport-https
sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el,s390x] https://mirrors.aliyun.com/mariadb/repo/10.6/ubuntu focal main'
sudo apt update
sudo apt install mariadb-server

通过 sudo systemctl status mariadb.service 查看服务状态,正常工作如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ubuntu@ubuntu:~$ sudo systemctl status mariadb.service

● mariadb.service - MariaDB 10.6.5 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor prese>
Drop-In: /etc/systemd/system/mariadb.service.d
└─migrated-from-my.cnf-settings.conf
Active: active (running) since Fri 2022-02-11 03:56:37 UTC; 39min ago
Docs: man:mariadbd(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 48637 (mariadbd)
Status: "Taking your SQL requests now..."
Tasks: 8 (limit: 4435)
CGroup: /system.slice/mariadb.service
└─48637 /usr/sbin/mariadbd

7、修改密码

通过 sudo mariadb -u root -p 登录,不必输入密码,直接回车后修改 root 密码:

1
2
use mysql;
alter user 'root'@'localhost' identified by 'xxxxxx';

可通过 select user, plugin from mysql.user; 查看登陆方式。初始状态为 unix_socket(MariaDB),配置密码后为 mysql_native_password

1
2
3
4
5
6
7
8
9
MariaDB [(none)]> select user, plugin from mysql.user;
+-------------+-----------------------+
| User | plugin |
+-------------+-----------------------+
| mariadb.sys | mysql_native_password |
| root | mysql_native_password |
| mysql | mysql_native_password |
+-------------+-----------------------+
4 rows in set (0.011 sec)

8、允许远程连接

通过 sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf 修改配置,将其中 bind-address = 127.0.0.1 修改为 0.0.0.0 或直接注释掉。

新增远程连接用户 create user <username>@xxx.xxx.xxx.xxx identified by '123456';

9、其他

启动 MariaDB 数据库服务 sudo systemctl start mysql

重启数据库服务 sudo systemctl restart mysql

设置 mysql 随系统服务启动 sudo update-rc.d mysql defaults

撤销随系统服务启动 sudo update-rc.d -f mysql remove

防火墙

磁盘挂载

以机械硬盘为例,已经在 windows 格式化为 exFAT 格式。通过 USB 接入树莓派后,登入系统,通过 sudo fdisk -l 查看当前磁盘状态如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
ubuntu@ubuntu:~$ sudo fdisk -l
Disk /dev/loop0: 48.92 MiB, 51277824 bytes, 100152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop1: 61.98 MiB, 64962560 bytes, 126880 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop2: 28.7 MiB, 29433856 bytes, 57488 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mmcblk0: 29.74 GiB, 31914983424 bytes, 62333952 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xf66f0719

Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 * 2048 526335 524288 256M c W95 FAT32 (LBA)
/dev/mmcblk0p2 526336 62333918 61807583 29.5G 83 Linux

Disk /dev/sda: 298.9 GiB, 320072933376 bytes, 625142448 sectors
Disk model: 00AAJS-00L7A0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 33553920 bytes
Disklabel type: dos
Disk identifier: 0x91676b87

Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 625139711 625137664 298.1G 7 HPFS/NTFS/exFAT

格式化硬盘为 ext4 格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ubuntu@ubuntu:~$ sudo mkfs -t ext4 /dev/sda
mke2fs 1.45.5 (07-Jan-2020)
Found a dos partition table in /dev/sda
Proceed anyway? (y,N) y
Creating filesystem with 78142806 4k blocks and 19537920 inodes
Filesystem UUID: bfff555b-254e-403a-880e-260601f7c1e5
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616

Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

通过以下命令建立目录

1
2
mkdir /mnt/disk
chmod 777 /mnt/disk

挂载硬盘

1
mount /dev/sda /mnt/disk

查询 uuid

1
2
3
4
5
6
7
ubuntu@ubuntu:~$ sudo blkid
/dev/mmcblk0p1: LABEL_FATBOOT="system-boot" LABEL="system-boot" UUID="5496-E6C8" TYPE="vfat" PARTUUID="f66f0719-01"
/dev/mmcblk0p2: LABEL="writable" UUID="675ba907-3741-428c-afa4-c00f1b649e3c" TYPE="ext4" PARTUUID="f66f0719-02"
/dev/loop0: TYPE="squashfs"
/dev/loop1: TYPE="squashfs"
/dev/loop2: TYPE="squashfs"
/dev/sda: UUID="bfff555b-254e-403a-880e-260601f7c1e5" TYPE="ext4"

通过 sudo vim /etc/fstab 在末尾添加下列内容,两者选其一:

1
2
3
UUID=bfff555b-254e-403a-880e-260601f7c1e5 /mnt/disk ext4 defaults 0 0

/dev/sda /mnt/disk ext4 defaults 0 0

第一个数字:0 表示开机不检查磁盘,1 表示开机检查磁盘;
第二个数字:0 表示交换分区,1 代表启动分区(Linux),2 表示普通分区

其他补充:
进入 /mnt/disk 目录,其中包含

如果你运行 fsck 命令(文件系统检查和修复命令),它也许会找到一些数据碎片,这些文件碎片在硬盘中并没有引用。特别的,fsck 也许能找到看起来是完整的文件,但是在系统中没有名字-一个 inode 但是不对应文件名。这个数据仍然占用硬盘空间,但是并不能通过正常方式访问。

     lost+found目录的文件通常是未链接的文件(名字已经被删除),但是这些文件还被一些进程使用(数据没有删除),在突然关机时(内核panic或者突然断电)出现。这些文件系统会自动删除。

    当因为软件或者硬件出现错误,导致文件系统不一致,也有可能把有问题的文件放到lost+found目录。它提供了恢复丢失文件的一种方法。



    如果你不小心删除了lost+found目录,不能使用mkdir命令创建lost+found目录,应该使用mklost+found命令创建它。

$ cd /
$ sudo mklost+found

磁盘容量及分区状况(不能查看未挂载分区)

df -Th

磁盘容量及分区状况(可以查看未挂载分区)

sudo fdisk -l
sudo lsblk -f

/lib 目录大小

du -sh /lib

/lib 子目录大小

du -sh /lib/*

转移 mariadb 数据

执行 sudo systemctl stop mariadb.service 停止服务

新建 /mnt/disk/mariadbdata

通过 sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf 修改 datadir 如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[mysqld]

#
# * Basic Settings
#

user = mysql
pid-file = /run/mysqld/mysqld.pid
basedir = /usr
#datadir = /var/lib/mysql
datadir = /mnt/disk/mariadbdata
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
lc-messages = en_US
skip-external-locking

执行 sudo cp -r /var/lib/mysql/* /mnt/disk/mariadbdata/ 复制旧数据到新位置。

修改目录权限和组

sudo chown -R mysql:mysql /mnt/disk/mariadbdata

注意:若需要保存旧目录数据,应复制文件后再修改所有者,否则无法启动服务。

执行 sudo systemctl start mariadb.service 重启服务

其他

如果将数据转移到 /home 目录需要修改 /lib/systemd/system/mariadb.serviceProtectHome=false

1
2
# Prevent accessing /home, /root and /run/user
ProtectHome=true

The MariaDB/MySQL tools read configuration files in the following order:

0. “/etc/mysql/my.cnf” symlinks to this file, reason why all the rest is read.

1. “/etc/mysql/mariadb.cnf” (this file) to set global defaults,

2. “/etc/mysql/conf.d/*.cnf” to set global options.

3. “/etc/mysql/mariadb.conf.d/*.cnf” to set MariaDB-only options.

4. “~/.my.cnf” to set user-specific options.

50-client.cnf 50-mysqld_safe.cnf 60-galera.cnf
50-mysql-clients.cnf 50-server.cnf 99-enable-encryption.cnf.preset

部署 java11

参考 https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/generic-linux-install.html

1
2
3
4
5
wget -O- https://apt.corretto.aws/corretto.key | sudo apt-key add -

sudo add-apt-repository 'deb https://apt.corretto.aws stable main'

sudo apt-get update; sudo apt-get install -y java-11-amazon-corretto-jdk

安装路径

1
/usr/lib/jvm/java-11-amazon-corretto/bin/