Saltern
Saltern

Reputation: 1383

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

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

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

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

Upvotes: 17

Views: 39479

Answers (9)

HVossi92
HVossi92

Reputation: 21

In my case I have my setup running in Docker. I had the same error message and had to run the migrate command inside docker.

docker exec -it <container ID> bash
cd app
php yii migrate

Upvotes: 0

Coyas
Coyas

Reputation: 73

Using yii2, my solution is to comment codes in common/main-local.php For some reason, yii2 try to get main-local.php in production instead of main.php in the folder common, but when is commented it works (get DB configs on common/main.php

<?php
// common/main-local.php
return [
 /*  'components' => [
       'db' => [
           'class' => 'yii\db\Connection',
           'dsn' => 'mysql:host=172.17.0.2;dbname=ememariadb',
           'username' => 'dbsenha',
           'password' => 'dbUser',
           'charset' => 'utf8',
       ],
   ],*/
];

Upvotes: 0

Gireesh
Gireesh

Reputation: 498

For PHP 7.2.24-0ubuntu0.18.04.3,

  • Get mysql socket path
  • add socket info to the database config of yii2

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

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

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.

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

    <?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

Upvotes: 1

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

  1. change localhost to 127.0.0.1
  2. remain as localhost and define mysql port even default port been used (localhost:3306)

Upvotes: 1

Olamide226
Olamide226

Reputation: 488

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

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

Upvotes: 1

sirjay
sirjay

Reputation: 1766

For MAMP users solution is

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

Upvotes: 8

qiaoer
qiaoer

Reputation: 11

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.

Upvotes: 1

vijay nathji
vijay nathji

Reputation: 1638

Changing "localhost" to "127.0.0.1" as your host

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

Upvotes: 68

Deepak swain
Deepak swain

Reputation: 3700

Hope this answer will help You:

Change the Host name from localhost to 127.0.0.1

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

Now you run php yii migrate .

Hope, It will successfully create the tables in Database

Upvotes: 3

Related Questions