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

时间:2019-05-15 01:20:34

标签: mysql docker docker-compose mura

我正在尝试在本地计算机上使用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)

1 个答案:

答案 0 :(得分:0)

这是因为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="$1"
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,首先它将尝试链接其依赖项容器db:5432,只有在确认可以使用psql client链接到数据库之后,然后它将运行真实的命令python app.py

就您而言,您需要进行与mysql client test相关的更改,然后一切正常。