前提条件
使用本教程需要满足下面的条件
- 已经安装
Docker
- 连接互联网
开始部署
Halo
博客系统支持多种数据库,这里我采用熟悉的MySql
创建数据目录
mkdir -p /data/demo/mysql
启动数据库容器
docker run -tid --name mysql -v /data/demo/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=HaloDemoPd -e MYSQL_USER=halo -e MYSQL_PASSWORD=HaloDbPd -e MYSQL_DATABASE=halo mysql:8.0.27
MYSQL_ROOT_PASSWORD
: 设置Root
用户登录密码MYSQL_USER
: 设置用户名MYSQL_PASSWORD
: 设置普通用户halo
登录密码MYSQL_DATABASE
: 设置数据库名称
效果如下
root@l:~# docker run -tid --name mysql -v /data/demo/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=HaloDemoPd -e MYSQL_USER=halo -e MYSQL_PASSWORD=HaloDbPd -e MYSQL_DATABASE=halo mysql:8.0.27
Unable to find image 'mysql:8.0.27' locally
8.0.27: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:8.0.27
d6e70342b654e6e784643d47ea59c264a6d88270c7d3dd8c5a45ddae96b60a39
root@l:~#
验证数据库信息
docker exec -ti mysql bash # 进入容器终端
mysql -uhalo -pHaloDbPd -e "show databases;" # 查询普通用户数据库
效果
root@l:~# docker exec -ti mysql bash # 进入容器终端
root@d6e70342b654:/# mysql -uhalo -pHaloDbPd -e "show databases;" # 查询普通用户数据库
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| halo |
| information_schema |
+--------------------+
root@d6e70342b654:/#
可以看到,此时已经创建好用户和数据库,下面看一下登录权限
mysql -uroot -pHaloDemoPd -e " select user,host from mysql.user;"
root@d6e70342b654:/# mysql -uroot -pHaloDemoPd -e " select user,host from mysql.user;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+-----------+
| user | host |
+------------------+-----------+
| halo | % |
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
root@d6e70342b654:/#
从结果可以看到,此时无论是root
还是halo
用户都可以使用任意主机登录,如果对安全有要求的建议修改权限
到这里数据库配置就结束了
启动Halo
容器
由于Halo
使用Mysql数据库
需要通过一个yaml
配置文件实现配置,所以我们需要先配置好配置文件,然后在启动容器的时候把配置文件映射到容器中
配置文件
在启动数据库容器
中,我们映射了/data/demo/mysql
目录,现在我们新建一个/data/demo/halo
的目录作为映射目录
mkdir -p /data/demo/halo
下载配置文件
cd /data/demo/halo
wget https://dl.halo.run/config/application-template.yaml -O application.yaml
效果
root@l:/data/demo# cd /data/demo/halo
root@l:/data/demo/halo# wget https://dl.halo.run/config/application-template.yaml -O application.yaml
--2023-03-10 23:26:26-- https://dl.halo.run/config/application-template.yaml
Resolving dl.halo.run (dl.halo.run)... 104.26.9.31, 172.67.75.92, 104.26.8.31, ...
Connecting to dl.halo.run (dl.halo.run)|104.26.9.31|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 798 [application/x-yaml]
Saving to: 'application.yaml'
application.yaml 100%[=========================================================================================================>] 798 --.-KB/s in 0s
2023-03-10 23:26:32 (14.8 MB/s) - 'application.yaml' saved [798/798]
修改配置文件
- 删除
H2
配置 - 启用
Mysql
配置并修改连接信息
首先查询数据库容器IP地址
root@l:/data/demo/halo# docker inspect mysql | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
然后根据信息修改配置文件
server:
port: 8090
# Response data gzip.
compression:
enabled: false
spring:
datasource:
# MySQL database configuration.
driver-class-name: com.mysql.cj.jdbc.Driver
# 在URL这行设置数据库地址和端口、数据库信息
url: jdbc:mysql://172.17.0.2:3306/halo?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
# 设置数据库登录账号信息
username: halo
password: HaloDbPd
halo:
# Your admin client path is https://your-domain/{admin-path}
admin-path: admin
# memory or level
cache: memory
启动Halo容器
使用下面的命令启动
docker run -it -d --name halo -p 8080:8090 -v /data/demo/halo/:/root/.halo --restart=unless-stopped halohub/halo:1.6.0
效果
root@l:/data/demo/halo# docker run -it -d --name halo -p 8080:8090 -v /data/demo/halo/:/root/.halo --restart=unless-stopped halohub/halo:1.6.0
b5f4b025e512af8ca6f389440d1ca6fa585b4d59d49c0f5def684d0f2e872c8e
root@l:/data/demo/halo# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5f4b025e512 halohub/halo:1.6.0 "/bin/sh -c 'java -X…" 8 seconds ago Up 6 seconds 0.0.0.0:8080->8090/tcp, :::8080->8090/tcp halo
root@l:/data/demo/halo# docker logs halo
2023-03-10 23:28:29.668 INFO 7 --- [ main] run.halo.app.listener.StartedListener : Halo started at http://127.0.0.1:8090
2023-03-10 23:28:29.668 INFO 7 --- [ main] run.halo.app.listener.StartedListener : Halo admin started at http://127.0.0.1:8090/admin
2023-03-10 23:28:29.668 INFO 7 --- [ main] run.halo.app.listener.StartedListener : Halo has started successfully!
此时,就可以通过8080
端口访问服务了
数据库验证
root@d6e70342b654:/# mysql -uhalo -pHaloDbPd -e "use halo;show tables;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------------------+
| Tables_in_halo |
+-----------------------+
| attachments |
| categories |
| comment_black_list |
| comments |
| content_patch_logs |
| contents |
| flyway_schema_history |
| journals |
| links |
| logs |
| menus |
| metas |
| options |
| photos |
| post_categories |
| post_tags |
| posts |
| tags |
| theme_settings |
| users |
+-----------------------+
root@d6e70342b654:/#
此时可以看到,数据库已经产生数据了