2

干杯人‍♂️</p>

我目前正在为一个朋友开发一个 wordpress 网站。由于我为自己设定了目标,只在本地使用 docker,因此我尝试对我的设置进行 docker 化。该设置基于根的基岩和鼠尾草系统。

由于已经有一个 wordpress docker-compose(由 wordpress 提供),我使用它作为我的 docker-compose 文件的基础,编辑了适当的数据并开始使用它。

我似乎可以访问我的 wp 实例。所以这里没有问题。不幸的是,我无法从我的 wp 容器连接到数据库容器。

我尝试更改 docker-compose 文件中的环境变量、端口/链接,但似乎没有任何效果。

# the compose file
---
  version: "3"

  services:
    wordpress-db:
      image: mariadb:latest
      volumes:
        - wordpress-db-data:/var/lib/mysql
      restart: always
      environment:
        MYSQL_ROOT_PASSWORD: somewordpress
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress
        MYSQL_PASSWORD: wordpress
      container_name: wordpress-db
      ports:
        - '3306:3306'

    wordpress-wp:
      depends_on:
        - wordpress-db
      image: wordpress:latest
      links:
        - wordpress-db
      volumes:
        - ./web:/var/www/html
        - ./config:/var/www/config
        - ./vendor:/var/www/vendor
        - ./.env:/var/www/.env
        - ./composer.json:/var/www/composer.json
        - ./wp-cli.yml:/var/www/wp-cli.yml
      ports:
      - '80:80'
      restart: always
      container_name: wordpress-wp
  volumes:
    wordpress-db-data:
      driver: local
# the env file
DB_NAME=wordpress
DB_USER=wordpress
DB_PASSWORD=wordpress

# Optionally, you can use a data source name (DSN)
# When using a DSN, you can remove the DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST variables
DATABASE_URL=mysql://wordpress:wordpress@localhost:3306/wordpress

# Optional variables
# DB_HOST=
# DB_PREFIX=wp_

WP_ENV=development
WP_HOME=localhost
WP_SITEURL=${WP_HOME}/wp

# Generate your keys here: https://roots.io/salts.html
AUTH_KEY='6&J&.WDf,VGfb!i@2B6TW-294-R|B?dZ1K1b7_Ms<Dbu9a9-Iud<B$*LC1{lU)m!'
SECURE_AUTH_KEY='#MgCVGx.KWV6R*lk:esocM5f#qucQ>N4gq#|`pdgI+ROF<b6|Q8K!.Yu6jXI@I!p'
LOGGED_IN_KEY='#dOrYLz[JlxSwlWgY6oE:0/(6fsxL_x$6amRw?A:i(oaUK]U+W5S!z%CgiA,B[T3'
NONCE_KEY='A8Dl3^6H6p=q1jR/(m1HLR|^k-A}WGiR$Q!bbfu.`|udsNr>s?bMV^L+mWYY?*5Z'
AUTH_SALT='9<(xi|RS.+x0h6z+XbErRKoQlE1wG`X>)!xb5zW1}dZPQ$,+OS[Z_ev2KdZSOjR0'
SECURE_AUTH_SALT='tj(#5`or}_Afs.lZOT@4NdTaNU!s+Y0e=4oWHywgVi9B.U6vo*Y5$s;f`>WwGx}L'
LOGGED_IN_SALT='tw*Wcin=d8T.7w+)$oei:?|clZ1;-hCc||KgI[Zm[P*O+i4YE9_7:|^6_&AnpYyN'
NONCE_SALT='PPjSg+}.N5u&zfex[o[9<3m6dWSQFWeSW00;;B5E(Dn{nLFZ:k)MzVbjsUC5E0|$'
# the folder structure
.
├── CHANGELOG.md
├── LICENSE.md
├── README.md
├── composer.json
├── composer.lock
├── config
│   ├── application.php
│   └── environments
├── docker-compose.dev.yml
├── docker-compose.yml
├── vendor
│   ├── autoload.php
│   ├── bin
│   ├── composer
│   ├── oscarotero
│   ├── phpoption
│   ├── roots
│   ├── squizlabs
│   ├── symfony
│   └── vlucas
├── web
│   ├── app
│   ├── index.php
│   ├── wp
│   └── wp-config.php
└── wp-cli.yml

我希望这真的有效。不幸的是,WordPress(在本地主机上)告诉我,“建立数据库连接时出错”。这是截图: https ://d.pr/i/hZrDUk

这是启动容器时的终端输出:

➜  mb-wp git:(master) ✗ docker-compose -f docker-compose.dev.yml up --force-recreate --build
Creating volume "mb-wp_wordpress-db-data" with local driver
Creating wordpress-db ... done
Creating wordpress-wp ... done
Attaching to wordpress-db, wordpress-wp
wordpress-db    | Initializing database
wordpress-wp    | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 192.168.112.3. Set the 'ServerName' directive globally to suppress this message
wordpress-wp    | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 192.168.112.3. Set the 'ServerName' directive globally to suppress this message
wordpress-wp    | [Mon Jul 08 08:14:42.817813 2019] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.3.6 configured -- resuming normal operations
wordpress-wp    | [Mon Jul 08 08:14:42.817965 2019] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
wordpress-db    | 
wordpress-db    | 
wordpress-db    | PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
wordpress-db    | To do so, start the server, then issue the following commands:
wordpress-db    | 
wordpress-db    | '/usr/bin/mysqladmin' -u root password 'new-password'
wordpress-db    | '/usr/bin/mysqladmin' -u root -h  password 'new-password'
wordpress-db    | 
wordpress-db    | Alternatively you can run:
wordpress-db    | '/usr/bin/mysql_secure_installation'
wordpress-db    | 
wordpress-db    | which will also give you the option of removing the test
wordpress-db    | databases and anonymous user created by default.  This is
wordpress-db    | strongly recommended for production servers.
wordpress-db    | 
wordpress-db    | See the MariaDB Knowledgebase at http://mariadb.com/kb or the
wordpress-db    | MySQL manual for more instructions.
wordpress-db    | 
wordpress-db    | Please report any problems at http://mariadb.org/jira
wordpress-db    | 
wordpress-db    | The latest information about MariaDB is available at http://mariadb.org/.
wordpress-db    | You can find additional information about the MySQL part at:
wordpress-db    | http://dev.mysql.com
wordpress-db    | Consider joining MariaDB's strong and vibrant community:
wordpress-db    | https://mariadb.org/get-involved/
wordpress-db    | 
wordpress-db    | Database initialized
wordpress-db    | MySQL init process in progress...
wordpress-db    | 2019-07-08  8:14:44 0 [Note] mysqld (mysqld 10.4.6-MariaDB-1:10.4.6+maria~bionic) starting as process 106 ...
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Using Linux native AIO
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Uses event mutexes
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Number of pools: 1
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Using SSE2 crc32 instructions
wordpress-db    | 2019-07-08  8:14:44 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Completed initialization of buffer pool
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Creating shared tablespace for temporary tables
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Waiting for purge to start
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: 10.4.6 started; log sequence number 139827; transaction id 21
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
wordpress-db    | 2019-07-08  8:14:44 0 [Note] Plugin 'FEEDBACK' is disabled.
wordpress-db    | 2019-07-08  8:14:44 0 [Note] InnoDB: Buffer pool(s) load completed at 190708  8:14:44
wordpress-db    | 2019-07-08  8:14:44 0 [Warning] 'user' entry 'root@ed060ae69169' ignored in --skip-name-resolve mode.
wordpress-db    | 2019-07-08  8:14:44 0 [Warning] 'user' entry '@ed060ae69169' ignored in --skip-name-resolve mode.
wordpress-db    | 2019-07-08  8:14:44 0 [Warning] 'proxies_priv' entry '@% root@ed060ae69169' ignored in --skip-name-resolve mode.
wordpress-db    | 2019-07-08  8:14:44 0 [Note] Reading of all Master_info entries succeeded
wordpress-db    | 2019-07-08  8:14:44 0 [Note] Added new Master_info '' to hash table
wordpress-db    | 2019-07-08  8:14:44 0 [Note] mysqld: ready for connections.
wordpress-db    | Version: '10.4.6-MariaDB-1:10.4.6+maria~bionic'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  mariadb.org binary distribution
wordpress-db    | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
wordpress-db    | 2019-07-08  8:14:58 10 [Warning] 'proxies_priv' entry '@% root@ed060ae69169' ignored in --skip-name-resolve mode.
wordpress-db    | 
wordpress-db    | 2019-07-08  8:14:58 0 [Note] mysqld (initiated by: unknown): Normal shutdown
wordpress-db    | 2019-07-08  8:14:58 0 [Note] Event Scheduler: Purging the queue. 0 events
wordpress-db    | 2019-07-08  8:14:58 0 [Note] InnoDB: FTS optimize thread exiting.
wordpress-db    | 2019-07-08  8:14:58 0 [Note] InnoDB: Starting shutdown...
wordpress-db    | 2019-07-08  8:14:58 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool
wordpress-db    | 2019-07-08  8:14:58 0 [Note] InnoDB: Buffer pool(s) dump completed at 190708  8:14:58
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Shutdown completed; log sequence number 139836; transaction id 24
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
wordpress-db    | 2019-07-08  8:14:59 0 [Note] mysqld: Shutdown complete
wordpress-db    | 
wordpress-db    | 
wordpress-db    | MySQL init process done. Ready for start up.
wordpress-db    | 
wordpress-db    | 2019-07-08  8:14:59 0 [Note] mysqld (mysqld 10.4.6-MariaDB-1:10.4.6+maria~bionic) starting as process 1 ...
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Using Linux native AIO
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Uses event mutexes
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Number of pools: 1
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Using SSE2 crc32 instructions
wordpress-db    | 2019-07-08  8:14:59 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Completed initialization of buffer pool
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Creating shared tablespace for temporary tables
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: 10.4.6 started; log sequence number 139836; transaction id 21
wordpress-db    | 2019-07-08  8:14:59 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
wordpress-db    | 2019-07-08  8:14:59 0 [Note] Plugin 'FEEDBACK' is disabled.
wordpress-db    | 2019-07-08  8:15:00 0 [Note] Server socket created on IP: '::'.
wordpress-db    | 2019-07-08  8:15:00 0 [Note] InnoDB: Buffer pool(s) load completed at 190708  8:14:59
wordpress-db    | 2019-07-08  8:15:00 0 [Warning] 'proxies_priv' entry '@% root@ed060ae69169' ignored in --skip-name-resolve mode.
wordpress-db    | 2019-07-08  8:15:00 0 [Note] Reading of all Master_info entries succeeded
wordpress-db    | 2019-07-08  8:15:00 0 [Note] Added new Master_info '' to hash table
wordpress-db    | 2019-07-08  8:15:00 0 [Note] mysqld: ready for connections.
wordpress-db    | Version: '10.4.6-MariaDB-1:10.4.6+maria~bionic'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
wordpress-wp    | [Mon Jul 08 08:16:40.801399 2019] [php7:warn] [pid 17] [client 192.168.112.1:47502] PHP Warning:  mysqli_real_connect(): (HY000/2002): No such file or directory in /var/www/html/wp/wp-includes/wp-db.php on line 1612
wordpress-wp    | 192.168.112.1 - - [08/Jul/2019:08:16:40 +0000] "GET / HTTP/1.1" 200 1737 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
4

1 回答 1

2

您没有在配置文件中为 Wordpress 提供正确的数据库 URL:

# Won't work
DATABASE_URL=mysql://wordpress:wordpress@localhost:3306/wordpress

当您提供 Wordpresslocalhost作为 DB 主机时,它会尝试使用 MySQL 本地套接字连接到它,但它在您的容器中不存在,导致此No such file错误。

根据Docker Compose 文档

服务的每个容器都加入默认网络,并且可以被该网络上的其他容器访问,并且可以通过与容器名称相同的主机名被它们发现。

换句话说,您命名的数据库服务wordpress-db可以通过 hostname 从您的 wordpress 容器中访问wordpress-db。您应该在配置中指定:

DATABASE_URL=mysql://wordpress:wordpress@wordpress-db:3306/wordpress

或者

DB_HOST=wordpress-db

您可以通过以下方式检查wordpress-db服务是否可以通过您的wordpress-wp服务访问

docker exec [wordpress-wp-container-name] getent hosts wordpress-db
于 2019-07-08T09:13:51.793 回答