当前位置: 首页 > 面试题库 >

使用Docker,我得到一个错误:“ SQLSTATE [HY000] [2002]没有这样的文件或目录”

葛承德
2023-03-14
问题内容

我正在使用Docker创建一个容器来测试我的Mac上基于PHP和MySQL构建的Web应用程序。我的PHP应用是使用无脂肪框架进行MVC和路由构建的。我有两个Dockerfile,一个用于MySQL,一个用于PHP。我已经成功使用了测试Docker应用程序,因此我相信我的映像已正确安装。

错误的主要部分:

Internal Server Error

SQLSTATE[HY000] [2002] No such file or directory

[fatfree/lib/DB/SQL.php:466] PDO->__construct('mysql:host=127.0.0.1;port=3306;dbname=robohome','root','password',array(1002=>'SET NAMES utf8;'))
[fatfree/app/Controllers/Controller.php:24] DB\SQL->__construct('mysql:host=127.0.0.1;port=3306;dbname=robohome','root','password')

请注意,如果我使用127.0.0.1而不是进行连接,则会localhost收到一个稍微不同的错误,该错误表示:SQLSTATE[HY000] [2002] Connection refused

我的PHP Dockerfile:

FROM php:5.6-apache

RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN a2enmod rewrite

我的MySQL Dockerfile:

FROM mysql:5.7

ENV MYSQL_ROOT_PASSWORD password
ENV MYSQL_DATABASE robohome

COPY ./schema.sql /docker-entrypoint-initdb.d/

我的Controller.php文件中的错误提到第24行:

<?php

namespace Controllers;

class Controller
{
    protected $f3;
    protected $db;

    public function __construct()
    {
        $f3 = \Base::instance();
        $this->f3 = $f3;

        $mysqlServerName = $f3->get("MYSQL_SERVERNAME");
        $mysqlDatabseName = $f3->get("MYSQL_DBNAME");

        //$container = \DI\ContainerBuilder::buildDevContainer(); <-Not used currently

        //Below is line 24 referred to in the error
        $db = new \DB\SQL(
            "mysql:host={$mysqlServerName};port=3306;dbname={$mysqlDatabseName}",
            $f3->get("MYSQL_USERNAME"),
            $f3->get("MYSQL_PASSWORD")
        );

        $this->db = $db;
    }

这些MYSQL_*值是从.ini文件中提取的:

MYSQL_SERVERNAME = "localhost" <-This is what I've tried changing to 127.0.0.1
MYSQL_USERNAME = "root"
MYSQL_PASSWORD = "password"
MYSQL_DBNAME = "robohome"

我的Docker撰写文件:

version: '2'

services:
  web:
    build: ./docker/php
    ports:
      - 80:80
    volumes:
      - .:/var/www/html/
    links:
      - db
  db:
    build: ./docker/mysql
    ports:
      - 3306

我通过这样做来运行docker-compose up --build -d。我可以从中得到的输出docker ps是:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
f35066a16586        robohomeweb_mysql   "docker-entrypoint.sh"   3 minutes ago       Up 2 seconds        0.0.0.0:32777->3306/tcp   robohomeweb_mysql_1
86d34eb34583        robohomeweb_php     "apache2-foreground"     3 minutes ago       Up 2 seconds        0.0.0.0:80->80/tcp        robohomeweb_php_1

如果我在前台运行,则会得到以下输出:

Building php
Step 1 : FROM php:5.6-apache
 ---> 8f9b7e57129a
Step 2 : RUN docker-php-ext-install mysqli pdo pdo_mysql
 ---> Using cache
 ---> fadd8f9e7207
Step 3 : RUN a2enmod rewrite
 ---> Using cache
 ---> 9dfed7fdc60f
Successfully built 9dfed7fdc60f
Building mysql
Step 1 : FROM mysql:5.7
 ---> eda6a4884645
Step 2 : ENV MYSQL_ROOT_PASSWORD password
 ---> Using cache
 ---> 759895ac5772
Step 3 : ENV MYSQL_DATABASE robohome
 ---> Using cache
 ---> e926c5ecc088
Step 4 : COPY ./schema.sql /docker-entrypoint-initdb.d/
 ---> Using cache
 ---> cf5d00aa8020
Successfully built cf5d00aa8020
Starting robohomeweb_php_1
Starting robohomeweb_mysql_1
Attaching to robohomeweb_mysql_1, robohomeweb_php_1
php_1    | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3. Set the 'ServerName' directive globally to suppress this message
php_1    | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3. Set the 'ServerName' directive globally to suppress this message
php_1    | [Sun Oct 16 20:21:17.944575 2016] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/5.6.26 configured -- resuming normal operations
php_1    | [Sun Oct 16 20:21:17.946919 2016] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
mysql_1  | 2016-10-16T20:21:18.036272Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
mysql_1  | 2016-10-16T20:21:18.038330Z 0 [Note] mysqld (mysqld 5.7.16) starting as process 1 ...
mysql_1  | 2016-10-16T20:21:18.043331Z 0 [Note] InnoDB: PUNCH HOLE support available
mysql_1  | 2016-10-16T20:21:18.043603Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
mysql_1  | 2016-10-16T20:21:18.043951Z 0 [Note] InnoDB: Uses event mutexes
mysql_1  | 2016-10-16T20:21:18.044077Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
mysql_1  | 2016-10-16T20:21:18.044260Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
mysql_1  | 2016-10-16T20:21:18.044414Z 0 [Note] InnoDB: Using Linux native AIO
mysql_1  | 2016-10-16T20:21:18.045150Z 0 [Note] InnoDB: Number of pools: 1
mysql_1  | 2016-10-16T20:21:18.045620Z 0 [Note] InnoDB: Using CPU crc32 instructions
mysql_1  | 2016-10-16T20:21:18.047629Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
mysql_1  | 2016-10-16T20:21:18.057705Z 0 [Note] InnoDB: Completed initialization of buffer pool
mysql_1  | 2016-10-16T20:21:18.059988Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
mysql_1  | 2016-10-16T20:21:18.074670Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
mysql_1  | 2016-10-16T20:21:18.101209Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
mysql_1  | 2016-10-16T20:21:18.101433Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
mysql_1  | 2016-10-16T20:21:18.354806Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
mysql_1  | 2016-10-16T20:21:18.356928Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
mysql_1  | 2016-10-16T20:21:18.357158Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
mysql_1  | 2016-10-16T20:21:18.358049Z 0 [Note] InnoDB: Waiting for purge to start
mysql_1  | 2016-10-16T20:21:18.412987Z 0 [Note] InnoDB: 5.7.16 started; log sequence number 12179647
mysql_1  | 2016-10-16T20:21:18.414470Z 0 [Note] Plugin 'FEDERATED' is disabled.
mysql_1  | 2016-10-16T20:21:18.421833Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
mysql_1  | 2016-10-16T20:21:18.424144Z 0 [Note] InnoDB: Buffer pool(s) load completed at 161016 20:21:18
mysql_1  | 2016-10-16T20:21:18.425607Z 0 [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key
mysql_1  | 2016-10-16T20:21:18.427018Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
mysql_1  | 2016-10-16T20:21:18.427581Z 0 [Note] IPv6 is available.
mysql_1  | 2016-10-16T20:21:18.427749Z 0 [Note]   - '::' resolves to '::';
mysql_1  | 2016-10-16T20:21:18.428019Z 0 [Note] Server socket created on IP: '::'.
mysql_1  | 2016-10-16T20:21:18.456023Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql_1  | 2016-10-16T20:21:18.456354Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
mysql_1  | 2016-10-16T20:21:18.480237Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql_1  | 2016-10-16T20:21:18.488758Z 0 [Note] Event Scheduler: Loaded 0 events
mysql_1  | 2016-10-16T20:21:18.490880Z 0 [Note] mysqld: ready for connections.
mysql_1  | Version: '5.7.16'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)

从我的研究中,我尝试使用两者进行连接localhost127.0.0.1因为在技术上对它们进行了区别对待。这也可能与尝试通过套接字而不是TCP进行通信有关。理想情况下,我想要一个可以嵌入到Dockerfile中的解决方案,这样我就不必担心记住命令或如何做某事。


问题答案:

正如有人在评论中指出的那样,您提供的docker-compose文件与您的问题非常相关。

对于文档links的搬运工,撰写文件说

链接服务的容器可以使用与别名相同的主机名访问,如果未指定别名,则可以使用服务名。

在您的情况下,数据库容器名为db,因此db从PHP容器解析主机应将您指向MySQL容器。在配置文件中替换localhostdb,应允许PHP容器连接到MySQL。



 类似资料:
  • 问题内容: 我相信我已经将我的(非常基本的)站点成功部署到了fortrabbit,但是一旦我连接到SSH以运行一些命令(例如或),我就会收到一条错误消息: 在某个时候,迁移肯定已经成功了,因为我的表在那里-但这不能解释为什么它现在对我不起作用。 问题答案: 错误消息表明尝试了通过套接字的MySQL连接(不支持)。 在Laravel(工匠)的背景下,您可能想要使用其他/正确的环境。例如:(或任何环境

  • 我相信我已经成功地将我的(非常基本的)站点部署到fortrabbit,但是一旦我连接到SSH运行一些命令(如或),我就会出错留言: 在某种程度上,迁移肯定是有效的,因为我的表就在那里——但这并不能解释为什么它现在对我不起作用。

  • 我已经有一个在我的本地主机与数据库工作的laravel项目。但当我使用GCP应用程序引擎时,我遵循数据库连接的步骤。我已经在云中创建了mysql数据库,并从工作的本地数据库导入了所有sql。当我部署应用程序时,显示SQLSTATE[HY000][2002]下面没有这样的文件或目录是我的应用程序。yaml文件内容

  • 当我尝试使用“php artisan migrate”命令迁移Laravel 5中的表时,出现以下错误: “SQLSTATE[HY000][2002]中没有此类文件或目录…”/供应商/laravel/framework/src/light/Database/Connectors/Connector。菲律宾:47 我的. env文件包含以下默认设置: 我的数据库。php文件将mysql列为默认数据库

  • 问题内容: 我正在尝试在Mac上安装香草论坛,为此,我刚刚从MySQL命令行创建了一个数据库和一个用户: 因此,我尝试使用以下代码进行连接: 但不幸的是,我收到一条错误消息 警告:mysqli_connect():(HY000 / 2002):在第3行的/Users/kramer65/Sites/vanilla/info.php中没有这样的文件或目录无法连接到MySQL:没有这样的文件或目录 知道

  • 我试图在我的Mac上安装vanilla forums,为此我刚从MySQL命令行创建了一个数据库和一个用户: 所以我尝试使用以下代码进行连接: 但不幸的是,我有一个错误说 警告:mysqli_connect():(HY000/2002):第3行/users/kramer65/sites/vanilla/info.php中没有这样的文件或目录无法连接到mysql:没有这样的文件或目录 知道我哪里错了