0%

Docker安装nextcloud并使用postgresql数据库

  1. 安装nextcloud
  2. 安装nextcloud + postgresql
  3. 更换数据库

安装nextcloud

docker命令

1
docker run -d --name _nextcloud -v ./data:/var/www/html -p 8000:80 nextcloud

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
version: "3"
services:
nextcloud:
container_name: _nextcloud
image: nextcloud
ports:
- 8000:80
volumes:
- ./data:/var/www/html
restart: always

安装nextcloud + postgresql

docker-compose.yml

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
version: "3"
services:
psql:
image: postgres
container_name: _postgres
restart: always
environment:
- POSTGRES_PASSWORD=nextcloud
- POSTGRES_DB=nextcloud
volumes:
- ./postgres:/var/lib/postgresql/data
networks:
- default
nextcloud:
depends_on:
- psql
container_name: _nextcloud
image: nextcloud
ports:
- 8000:80
volumes:
- ./data:/var/www/html
restart: always
links:
- psql
networks:
- default
  • 将二者放在同一个网段default中,可以通过link在nextcloud中,直接访问postgres容器的端口,从而无需用到宿主机的端口。通过以上配置文件运行两个容器后,进入nextcloud页面配置postgresql数据库时,直接在host中输入psql,就相等于输入了<ip>:<port>

  • 使用默认的数据库用户名为postgres,数据库名为postgres。postgresql的docker容器创建时,会添加一个名为postgres的系统用户作为管理员,该用户名也作为数据库的管理员账户,一般不做修改

  • 可以通过添加环境变量POSTGRES_DB创建一个自定义名称的数据库作为基础数据库

image-20221026150747877

在nextcloud创建管理员账号时,指定PostgreSQL数据库后,可能会出现如下页面提示:

image-20221026150212094

此时,打开/config/config.php,看到

image-20221026150616919

dbuserdbpassword和我们输入的不一样,将其改正后,刷新页面,可以成功进入页面了。

更换数据库

Converting database type — Nextcloud latest Administration Manual latest documentation

nextcloud可以使用三种数据库:MySQL/PostgreSQL/SQLite,在代码内置标识符分别为mysql/pgsql/sqlite3

标识符位于以下文件中:\lib\private\DB\ConnectionFactory.php

切换数据库的命令为:

1
php occ db:convert-type --password="<password>" [--port=<port>] --clear-schema --all-apps <db-type> <username> <hostname> <database>

使用nextcloud的Docker容器执行:

1
docker exec nextcloud php occ db:convert-type --password="nextclout" --clear-schema --all-apps pgsql postgres psql postgres