Docker wordpress / mysql因“连接被拒绝”而失败

时间:2019-06-14 14:13:45

标签: mysql wordpress docker

我正在尝试设置环境来构建wordpress网站,但docker-compose失败,并重新连接。

我发送了2天多的时间试图弄清楚,但徒劳地浏览了网络。我尝试了栈溢出可用的解决方案,但仍然徒劳。

这是我的docker-compose

version: '3.7'

services:
  wordpress:
    depends_on:
      - db
    #links:
     # - db:mysql
    image: wordpress
    container_name: wp-web
    ports:
      - 7000:80
     # - 443:443
    restart: unless-stopped
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: banana
      WORDPRESS_DB_PASSWORD: banana
      WORDPRESS_DB_NAME: banana
    working_dir: /var/www/html
    volumes:
      - ./wp-content:/var/www/html/wp-content
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    # networks:
      # - wp-back

  db:
    image: mysql:5.7
    container_name: db_server
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - ./db_data:/home/abelmbula/Documents/dockerapp/lib/mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: banana
      MYSQL_DATABASE: banana
      MYSQL_USER: banana
      MYSQL_PASSWORD: banana
    ports:
     - 8889:3306
    # networks:
     # - wp-back

volumes:
 db_data:

我把这个放了出去

Starting db_server ... done
Recreating wp-web  ... done
Attaching to db_server, wp-web
db_server    | Initializing database
db_server    | 2019-06-14T13:32:53.477264Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_server    | 2019-06-14T13:32:53.479113Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
db_server    | 2019-06-14T13:32:53.479146Z 0 [ERROR] Aborting
db_server    | 
wp-web       | [14-Jun-2019 13:33:03 UTC] PHP Warning:  mysqli::__construct(): (HY000/2002): Connection refused in Standard input code on line 22
wp-web       | 
wp-web       | MySQL Connection Error: (2002) Connection refused
db_server exited with code 1

我上面的文件有问题吗?

2 个答案:

答案 0 :(得分:0)

这是一个最小的示例:

version: '3.7'
services:
  wordpress:
    depends_on:
    - mysql
    image: wordpress
    container_name: wordpress
    hostname: wordpress
    domainname: example.com
    ports:
    - 8080:80
    restart: unless-stopped
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_USER: banana
      WORDPRESS_DB_PASSWORD: banana
      WORDPRESS_DB_NAME: banana
    working_dir: /var/www/html
    volumes:
    - wp_content:/var/www/html/wp-content
    - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
  mysql:
    image: mysql:5.7
    container_name: mysql
    hostname: mysql
    domainname: example.com
    command: --default-authentication-plugin=mysql_native_password
    volumes:
    - db_data:/var/lib/mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: banana
      MYSQL_DATABASE: banana
      MYSQL_USER: banana
      MYSQL_PASSWORD: banana
    expose:
    - 3306
volumes:
  db_data:
  wp_content:

此示例使用db_data中定义的volumes卷来存储数据库文件(默认情况下,主机操作系统上docker中存在/var/lib/docker/volumes卷),它为docker volume内容添加名为wordpress的文件,它不会将数据库端口发布到主机,而是将其公开在docker网络内部,以便wordpress服务可以访问它并为服务设置示例hostnamedomainname条目。

答案 1 :(得分:0)

我也遇到了同样的问题,我尝试了两种对我有用的方法,也许对您也有用!

1-在docker-compose.yml文件中为wordpress和db添加此标志重新启动:除非已停止 2-让MySQL首先启动以使WordPress与数据库连接。

NodePort

希望对您有用