php SQLSTATE[HY000] [2002] yii2 中没有这样的文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36930536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:59:10  来源:igfitidea点击:

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

phpmysqlyii2apache2

提问by Saltern

I use ubuntu 16.04.
PHP Version 7.0.4-7ubuntu2.
Apache/2.4.18 (Ubuntu).
PHP extension: mysqli(in phpmyadmin Written).

我用ubuntu 16.04.
PHP Version 7.0.4-7ubuntu2.
Apache/2.4.18 (Ubuntu).
PHP extension: mysqli(在 phpmyadmin 中写的)。

I got Upgrade my ubuntu from 15.10 to 16.04and I have this error:

我从升级我的 ubuntu15.10 to 16.04并且我有这个错误:

My project correctly run in my server but I can't run that in my os:

我的项目在我的服务器中正确运行,但我无法在我的操作系统中运行它:

Database Exception – yii\db\Exception
SQLSTATE[HY000] [2002] No such file or directory
?
Caused by: PDOException
SQLSTATE[HY000] [2002] No such file or directory

in /var/www/html/iicitySite/vendor/yiisoft/yii2/db/Connection.php at line 579

回答by vijay nathji

Changing "localhost" to "127.0.0.1" as your host

将“localhost”更改为“127.0.0.1”作为您的主机

return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',    
            'dsn' => 'mysql:host=127.0.0.1;dbname=abc',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],

回答by sirjay

For MAMP users solution is

对于 MAMP 用户,解决方案是

'components' => [
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;port=8889;dbname=mydbname;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock',
        'username' => 'myuser',
        'password' => 'mypassword',
        'charset' => 'utf8',
    ],
],

回答by Olamide226

I had this same problem too. Changing localhostdidn't solve my problem. Instead, add your db port like this:

我也有同样的问题。改变localhost并没有解决我的问题。相反,像这样添加你的数据库端口:

'dsn'=>'mysql:host=localhost:3307;dbname=geep'

回答by Deepak swain

Hope this answer will help You:

希望这个回答能帮到你:

Change the Host name from localhostto 127.0.0.1

将主机名从localhost更改 为127.0.0.1

This is inside backend\common\config\main-local.php

这是在 backend\common\config\main-local.php

Now you run php yii migrate.

现在你运行php yii migrate

Hope, It will successfully create the tables in Database

希望,它会成功地在数据库中创建表

回答by qiaoer

if you use mamp,don't use "php" command in MAC OS, but use "php" in mamp, such as /Applications/MAMP/bin/php/php5.6.30/bin/php yii migrate.

如果您使用mamp,请不要在MAC OS 中使用“php”命令,而在mamp 中使用“php”,例如/Applications/MAMP/bin/php/php5.6.30/bin/php yii migrate.

回答by Gireesh Kudipudi

For PHP 7.2.24-0ubuntu0.18.04.3,

对于 PHP 7.2.24-0ubuntu0.18.04.3,

  • Get mysql socket path
  • add socket info to the database config of yii2
  • 获取mysql套接字路径
  • 将套接字信息添加到yii2数据库配置中

To get the socket path login to the mysql and perform following steps

获取socket路径登录mysql并执行以下步骤

  • Open Terminal and perform following steps

    mysql -u root -p
    mysql> show variables like '%sock%';
    +-----------------------------------------+------------------------------------------------------+
    | Variable_name                           | Value                                                |
    +-----------------------------------------+------------------------------------------------------+
    | mysqlx_socket                           | /tmp/mysqlx.sock                                     |
    | performance_schema_max_socket_classes   | 10                                                   |
    | performance_schema_max_socket_instances | -1                                                   |
    | socket                                  | /opt/packages/lampstack-7.3.9-0/mysql/tmp/mysql.sock |
    +-----------------------------------------+------------------------------------------------------+
    4 rows in set (0.00 sec)
    exit
    
  • Next add following config information

    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:unix_socket=/opt/packages/lampstack-7.3.9-0/mysql/tmp/mysql.sock;dbname=basketmantra',
    'username' => 'root',
    'password' => 'root123',
    'charset' => 'utf8',
    
  • 打开终端并执行以下步骤

    mysql -u root -p
    mysql> show variables like '%sock%';
    +-----------------------------------------+------------------------------------------------------+
    | Variable_name                           | Value                                                |
    +-----------------------------------------+------------------------------------------------------+
    | mysqlx_socket                           | /tmp/mysqlx.sock                                     |
    | performance_schema_max_socket_classes   | 10                                                   |
    | performance_schema_max_socket_instances | -1                                                   |
    | socket                                  | /opt/packages/lampstack-7.3.9-0/mysql/tmp/mysql.sock |
    +-----------------------------------------+------------------------------------------------------+
    4 rows in set (0.00 sec)
    exit
    
  • 接下来添加以下配置信息

    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:unix_socket=/opt/packages/lampstack-7.3.9-0/mysql/tmp/mysql.sock;dbname=basketmantra',
    'username' => 'root',
    'password' => 'root123',
    'charset' => 'utf8',
    

I hope this helps

我希望这有帮助

In Some cases you may use httpd or apache or lampp then also make sure to check php and mysql commands in terminal are same as versions web servers are using.

在某些情况下,您可能会使用 httpd 或 apache 或 lampp,然后还要确保检查终端中的 php 和 mysql 命令与 Web 服务器使用的版本相同。

phpinfo() in web server is helpful to find out the versions web server is using

Web 服务器中的 phpinfo() 有助于找出 Web 服务器正在使用的版本

    <?php
        phpinfo();
    ?>

For commands

对于命令

    $ type php
    php is hashed (/usr/bin/php)

    $ type mysql
    mysql is /opt/packages/lampstack-7.3.9-0/mysql/bin/mysql

回答by Hairunizam Mohamed Salleh

I'm running on MAMP environment and works well using 2 solutions above

我在 MAMP 环境中运行并且使用上面的 2 个解决方案运行良好

  1. change localhostto 127.0.0.1
  2. remain as localhost and define mysql port even default port been used (localhost:3306)
  1. 更改localhost127.0.0.1
  2. 保留为 localhost 并定义 mysql 端口,即使使用了默认端口 ( localhost:3306)