简体   繁体   English

如何从同一台 wordpress 服务器(运行在同一台服务器上的 docker 容器)访问运行在 docker 容器中的数据库?

[英]How to access the database running in a docker container from the same wordpress server(docker container running in same server)?

I have a WordPress server and it's trying to connect the database which is running as a docker container locally.我有一个 WordPress 服务器,它试图连接在本地作为 docker 容器运行的数据库。 But while accessing localhost:80 getting "Error establishing a database connection" error但是在访问 localhost:80 时出现“错误建立数据库连接”错误

i have a database container running我有一个正在运行的数据库容器

CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                 NAMES
9f9fea1b744e   mysql:5.7   "docker-entrypoint.s…"   47 minutes ago   Up 47 minutes   3306/tcp, 33060/tcp   my_mysql

And am trying to configure it in WordPress file /var/www/html/wp-config.php like this我试图在 WordPress 文件 /var/www/html/wp-config.php 中像这样配置它

define( 'DB_NAME', 'wordpress' );^M
^M
/** MySQL database username */^M
define( 'DB_USER', 'root' );^M
^M
/** MySQL database password */^M
define( 'DB_PASSWORD', '****' );^M
^M
/** MySQL hostname */^M
define( 'DB_HOST', 'mysql:3306' );^M

Both WP and DB in a Container容器中的 WP 和 DB

With the following docker-compose.yml file you'll have Wordpress and Database running each in its own docker container:使用以下 docker-compose.yml 文件,您将在自己的 docker 容器中运行 Wordpress 和数据库:

version: '3.1'

services:

  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8181:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - ./wp:/var/www/html

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql
    ports:
      - "8086:3306"

volumes:
  db:

For the WP in the Container your database is running on host 'db' and on port '3306'.对于容器中的 WP,您的数据库在主机“db”和端口“3306”上运行。

WP on the Host and DB in the Container Host 上的 WP 和容器中的 DB

To access the Database running in the above container from a WP running on the host, your database is running on host 'localhost' and on port '8086'要从主机上运行的 WP 访问在上述容器中运行的数据库,您的数据库在主机“localhost”和端口“8086”上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM