gpt4 book ai didi

php - 建立与CakePHP数据库的连接时出错

转载 作者:行者123 更新时间:2023-12-02 20:40:41 24 4
gpt4 key购买 nike

我想学习CakePHP并立即出现奇怪的错误=)尝试通过控制台创建模型

bin/cake bake model Posts
并得到一个错误
root@5b118a83e609:/var/www/public/cake# bin/cake bake model Posts
One moment while associations are detected.
Exception: Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory
In [/var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver.php, line 140]

2020-10-07 09:23:42 Error: [Cake\Database\Exception\MissingConnectionException] Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory in /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver.php on line 140
Exception Attributes: array (
'driver' => 'Mysql',
'reason' => 'SQLSTATE[HY000] [2002] No such file or directory',
)
Stack Trace:
- /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:178
- /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Schema/SchemaDialect.php:47
- /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:230
- /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Schema/Collection.php:53
- /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Connection.php:399
- /var/www/public/cake/vendor/cakephp/bake/src/Utility/TableScanner.php:65
- /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:1060
- /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:191
- /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:122
- /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:98
- /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:81
- /var/www/public/cake/vendor/cakephp/cakephp/src/Console/BaseCommand.php:179
- /var/www/public/cake/vendor/cakephp/cakephp/src/Console/CommandRunner.php:336
- /var/www/public/cake/vendor/cakephp/cakephp/src/Console/CommandRunner.php:171
- /var/www/public/cake/bin/cake.php:12

Caused by: [PDOException] SQLSTATE[HY000] [2002] No such file or directory in /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver.php on line 132
Stack Trace:
- /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver.php:132
- /var/www/public/cake/vendor/cakephp/cakephp/src/Core/Retry/CommandRetry.php:70
- /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver.php:138
- /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:178
- /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Schema/SchemaDialect.php:47
- /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:230
- /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Schema/Collection.php:53
- /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Connection.php:399
- /var/www/public/cake/vendor/cakephp/bake/src/Utility/TableScanner.php:65
- /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:1060
- /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:191
- /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:122
- /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:98
- /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:81
- /var/www/public/cake/vendor/cakephp/cakephp/src/Console/BaseCommand.php:179
- /var/www/public/cake/vendor/cakephp/cakephp/src/Console/CommandRunner.php:336
- /var/www/public/cake/vendor/cakephp/cakephp/src/Console/CommandRunner.php:171
- /var/www/public/cake/bin/cake.php:12
设置是100%正确: config/app.php
'host' =>  'mariadb',
'port' => 3306,
'username' => 'root',
'password' => 'password',
'database' => 'database',
我正在使用Docker。附近是Yii2,Laravel,Symfony和WordPress。一切正常,没有任何错误,但是CakePHP出于某种原因无法连接到数据库。
如何解决?
附言:
顺便说一句,如果我在浏览器中打开一个新安装的框架,那么“数据库”块中也将出现错误。
CakePHP is NOT able to connect to the database.
Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory
同时,我尝试通过本地PHP函数 mysql_connect直接连接到数据库-一切正常!但是CakePHP不想工作。
我已经尝试过将主机从 mariadb更改为Docker容器的 IP,但这对我没有帮助。
'host' =>  '192.168.32.3',
'port' => 3306,
'username' => 'root',
'password' => 'password',
'database' => 'database',
不工作

最佳答案

您将mariadb用作主机,而不是实际MySQL容器的IP。通常,您可以使用localhost,但是由于您使用的是Docker,因此这是不可能的。确保使用MySQL / MariaDB容器的IP。
如果您不知道如何使用,请使用docker ps,它将显示您所有的containerID。
然后在正确的容器(带有containerID)docker inspect CONTAINERIDGOESHERE | grep "IPAddress"中查找IP。
在数据库连接(而不是localhost)中使用此IP,您已设置好。
所以你的配置看起来像这样:

'host' =>  '172.19.0.2', // This is example IP, replace it with your found IP
'port' => 3306,
'username' => 'root',
'password' => 'password',
'database' => 'database',

关于php - 建立与CakePHP数据库的连接时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64241451/

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