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

w8biq8rn  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(152)

我想学习CakePHP和immidiatly得到奇怪的错误=)试图通过控制台创建一个模型

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。附近是Yii 2,Laravel,Symfony和WordPress。所有工作正常,没有任何错误,但CakePHP由于某种原因无法连接到数据库。
如何修复?
附言:
顺便说一句,如果我在浏览器中打开一个新安装的框架,那么在“Database”块中也会出现错误

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',

不工作。

sbtkgmzw

sbtkgmzw1#

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

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

更新的答案:请不要使用IP来连接容器。IP可能会在容器重新创建时更改,因此这是一种不好的做法。请使用容器名称来代替。只需确保将网络行添加到容器和全局中。看起来像这样:

version: "3.1"
services:

  mariadb:
    image: mariadb:10.6
    container_name: mariadb
    working_dir: /application
    volumes:
      - .:/application
    ports:
      - "1027:3306"
    networks:
      - application

  webserver:
    container_name: webserver
    image: nginx:alpine
    working_dir: /application
    volumes:
      - .:/application
    ports:
      - "1025:80"
    networks:
      - application
networks:
  application:

您的DB连接字符串看起来如下所示:根目录:根目录@容器名称b/数据库名称

相关问题