使用 docker-compose.yml 文件 docker 内的 Mura CMS 数据库连接问题

Mura CMS database connectivity issue inside docker using docker-compose.yml file

我正在尝试在我的本地计算机上使用 Docker 设置 Mura CMS。 MYSQL 数据库似乎无法正常工作。

我正在使用 docker-compose up 命令。我一直在尝试以下示例: https://github.com/blueriver/docker-muracms/tree/master/examples/blueriver-muracms-mysql

version: '3.3'

services:

  muracms:
    image: blueriver/muracms:latest
    depends_on:
      - svc_muradb
    environment:
      MURA_ADMIN_USERNAME: admin
      MURA_ADMIN_PASSWORD: 5trongP@55w0rd
      MURA_ADMINEMAIL: youremail@domain.com
      MURA_APPRELOADKEY: appreload
      MURA_DATASOURCE: dsn_muracms
      MURA_DATABASE: muradb
      MURA_DBTYPE: mysql
      MURA_DBUSERNAME: root
      MURA_DBPASSWORD: 5trongP@55w0rd 
      MURA_DBHOST: svc_muradb
      MURA_DBPORT: 3306
      MURA_SITEIDINURLS: "false"
      MURA_INDEXFILEINURLS: "true"
      MURA_TESTBOX: "true"
      MURA_USESSL: "false"
    volumes:
      - ./www/modules:/var/www/modules
      - ./www/plugins:/var/www/plugins
      - ./www/sites:/var/www/sites
      - ./www/themes:/var/www/themes
    ports:
      - "8888:8888"

  # DB
  svc_muradb:
    image: mysql:latest
    environment:
      MYSQL_DATABASE: muradb
      MYSQL_ROOT_PASSWORD: 5trongP@55w0rd
    volumes:
      - vol_muradb:/var/lib/mysql
    ports:
      - "5001:3306"

# Mounts
volumes:
  vol_muradb:

浏览localhost:8888时看到如下错误信息:

500 Error
Could not create connection to database server.
Code:0
Type:database
lucee.runtime.exp.DatabaseException: Could not create connection to database server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)

这是因为当muracms启动时,svc_muradb还没有完成它的初始化。

depends_on只是保证svc_muradb先启动,然后muracms启动。但是启动过程svc_muradb是异步的。所以在 svc_muradb 准备好之前,你的 muracms 很可能已经想要连接数据库了。

此问题记录在 official guide 中,建议您为 svc_muradb 命令编写一个包装器。

像下面这样,我只是举个例子:

docker-compose.yml:

version: "2"
services:
  web:
    build: .
    ports:
      - "80:8000"
    depends_on:
      - "db"
    command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
  db:
    image: postgres

wait-for-it.sh:

#!/bin/sh
# wait-for-postgres.sh

set -e

host=""
shift
cmd="$@"

until PGPASSWORD=$POSTGRES_PASSWORD psql -h "$host" -U "postgres" -c '\q'; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

>&2 echo "Postgres is up - executing command"
exec $cmd 

使用上述解决方法,Web 容器将 运行 wait-for-it.sh,首先它会尝试 link 其依赖容器 db:5432,只有在确认后才能 link 到数据库 psql client,然后它将 运行 真正的命令 python app.py.

对于你来说,你需要改变mysql client test,然后一切就OK了。