17

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
nicolascolman
  • 549
  • 4
  • 15
Saltern
  • 1,305
  • 2
  • 16
  • 42

9 Answers9

66

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',
        ],
vijay nathji
  • 1,608
  • 13
  • 23
8

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',
    ],
],
sirjay
  • 1,767
  • 3
  • 32
  • 52
3

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

Deepak swain
  • 3,380
  • 1
  • 30
  • 26
1

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.

legoscia
  • 39,593
  • 22
  • 116
  • 167
qiaoer
  • 11
  • 2
1

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'
rob006
  • 21,383
  • 5
  • 53
  • 74
Olamide226
  • 428
  • 6
  • 12
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)
1

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
Gireesh
  • 468
  • 7
  • 13
0

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',
       ],
   ],*/
];
Coyas
  • 73
  • 1
  • 10
0

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
HVossi92
  • 11
  • 2