gpt4 book ai didi

php - CakePHP-如何在CakePHP项目中使用套接字连接到Docker的数据库容器

转载 作者:行者123 更新时间:2023-12-02 20:15:50 25 4
gpt4 key购买 nike

我试图通过使用docker而不是MAMP来开发Web应用程序。这是我第一次做这份工作。经过大量的搜索。我终于可以链接到我的数据库容器,并看到标准的CakePHP默认页面全是绿色。

但是,当我尝试在下面使用CakePHP的bake函数时。

bin/cake bake all users

我收到下面的错误。
Exception: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in [CakePhpProjects/cakephptest/vendor/cakephp/cakephp/src/Database/Driver/PDODriverTrait.php, line 48]

但是,当我使用MAMP的环境并在config / app.php的“DataResource”位置添加以下代码时,相同的功能正在工作。
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

那么有什么办法可以解决这个问题呢?

我的DockerFile是这个。
FROM ubuntu
RUN apt-get update \
&& apt-get install -y \
composer \
curl \
php \
php-intl \
php-mbstring \
php-mysql \
unzip \
zip
RUN mkdir /code
WORKDIR /code
ADD . /code/

我的docker-compose.yml文件是这个
version: '2'
services:
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=cakephptest
- MYSQL_USER=root
- MYSQL_PASSWORD=root
web:
build: .
command: cakephptest/bin/cake server -H 0.0.0.0
volumes:
- .:/code
ports:
- "8765:8765"
depends_on:
- db
links:
- db

phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
- PMA_HOST=db
- PMA_USER=root
- PMA_PASSWORD=root
links:
- db
ports:
- 8080:80
volumes:
- /sessions

我的CakePHP的数据库设置是这样
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
/**
* CakePHP will use the default DB port based on the driver selected
* MySQL on MAMP uses port 8889, MAMP users will want to uncomment
* the following line and set the port accordingly
*/
//'port' => 'non_standard_port_number',
'username' => 'root',
'password' => 'root',
'database' => 'caketest',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,

/**
* Set identifier quoting to true if you are using reserved words or
* special characters in your table or column names. Enabling this
* setting will result in queries built using the Query Builder having
* identifiers quoted when creating SQL. It should be noted that this
* decreases performance because each query needs to be traversed and
* manipulated before being executed.
*/
'quoteIdentifiers' => false,

/**
* During development, if using MySQL < 5.6, uncommenting the
* following line could boost the speed at which schema metadata is
* fetched from the database. It can also be set directly with the
* mysql configuration directive 'innodb_stats_on_metadata = 0'
* which is the recommended value in production environments
*/
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],

'url' => env('DATABASE_URL', null),
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
],

最佳答案

您应该使用Docker容器的主机名而不是localhost:

'host' => 'db'

关于php - CakePHP-如何在CakePHP项目中使用套接字连接到Docker的数据库容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43404632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com