laravel Laradock - SQLSTATE[HY 000] [2002]连接被拒绝(SQL:选择count(*)作为'users'的聚合,其中'email'=

xyhw6mcr  于 2023-02-10  发布在  其他
关注(0)|答案(4)|浏览(139)

**当我在设置laradock后尝试迁移时,我收到此错误:**我按照主文档设置了laradock,一切正常,但当我从浏览器创建新用户时,一切都不正常。

Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known (SQL: select * from information_schema.tables where table_schema = cd_eloquent and table_name = migrations and table_type = 'BASE TABLE')

  at /Users/cristian/development/laravel/cd_eloquent/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673|

  Exception trace:

  1   PDOException::("PDO::__construct(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known")
      /Users/cristian/development/laravel/cd_eloquent/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=mysql;port=3306;dbname=cd_eloquent", "cristian", "cristian", [])
      /Users/cristian/development/laravel/cd_eloquent/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

.env主文档后面的示例.env使用DB_HOST=mysql

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=cd_eloquent
DB_USERNAME=cristian
DB_PASSWORD=cristian

如果我更改了.env我更改了DB_HOST=127.0.0.1

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=cd_eloquent
DB_USERNAME=cristian
DB_PASSWORD=cristian

然后我得到了所有的迁移只适用于127.0.0.1不适用于mysql

cd_eloquent git:(master) ✗ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.04 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.05 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.08 seconds)

当我尝试注册时,出现此错误

如果我更改我的.env

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=cd_eloquent
DB_USERNAME=cristian
DB_PASSWORD=cristian

我得到这个错误

"这是怎么了"

拉腊多克环境

### MYSQL #################################################

MYSQL_VERSION=5.7
MYSQL_DATABASE=default
MYSQL_USER=default
MYSQL_PASSWORD=secret
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d

拉拉多克遗址

站点示例

server {

    listen 80;
    listen [::]:80;

    # For https
    # listen 443 ssl;
    # listen [::]:443 ssl ipv6only=on;
    # ssl_certificate /etc/nginx/ssl/default.crt;
    # ssl_certificate_key /etc/nginx/ssl/default.key;

    server_name cd_eloquent.test;
    root /var/www/cd_eloquent/public;
    index index.php index.html index.htm;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }

    error_log /var/log/nginx/laravel_error.log;
    access_log /var/log/nginx/laravel_access.log;
}

示例phpmyadmin

运行的容器示例

坞站合成ps

另一个例子

DB_CONNECTION=mysql
DB_HOST=laradock_mysql
DB_PORT=3306
DB_DATABASE=cd_eloquent
DB_USERNAME=cristian

tvokkenx

tvokkenx1#

从你laradock工作区容器重试php artisan migrate:fresh,它将工作。例如:一米一米一x一米二米一x

htzpubme

htzpubme2#

在laradock上,如果使用mariadb而不是mysql,则使用mariadb作为DB_HOST。
DB_HOST=mariadb

bihw5rsg

bihw5rsg3#

有时您必须更改.env上的DB_HOST。在我的情况下,IP被更改,迁移在我的计算机上运行,但不在具有另一个IP的容器中运行。

zujrkrfu

zujrkrfu4#

在laradock上,每个服务都有自己的容器,因此localhost或127.0.0.1指的是没有mysql的工作区容器,请尝试:

DB_HOST=mysql

这将使用Docker的内部名称服务器Map正确的内部IP地址。

相关问题