cakephp bake SQLSTATE[HY000] [2002] No such file or directory
Asked Answered
A

4

5

I followed the CakePHP tutorial and till now everything worked fine(newly added articles are shown in the db). But now that I tried to "bin/cake bake model users" I get the following error message: SQLSTATE[HY000] [2002] No such file or directory in [/opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Database/Driver.php, line 92]. I found this solution here: SQLSTATE HY000 2002 while running bake command . But this does not seem to work on linux(Ubuntu). Any help would be appreciated.

Full Error:

Exception: SQLSTATE[HY000] [2002] No such file or directory in [/opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Database/Driver.php, line 92] 2018-12-06 11:28:03 Error: [PDOException] SQLSTATE[HY000] [2002] No such file or directory in /opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Database/Driver.php on line 92 Stack Trace:
#0 /opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Database/Driver.php(92): PDO->__construct('mysql:host=loca...', 'root', '', Array)
#1 /opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php(106): Cake\Database\Driver->_connect('mysql:host=loca...', Array)
#2 /opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Database/Schema/BaseSchema.php(45): Cake\Database\Driver\Mysql->connect()
#3 /opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Database/Dialect/MysqlDialectTrait.php(63): Cake\Database\Schema\BaseSchema->__construct(Object(Cake\Database\Driver\Mysql))
#4 /opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Database/Schema/Collection.php(52): Cake\Database\Driver\Mysql->schemaDialect()
#5 /opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Database/Schema/CachedCollection.php(42): Cake\Database\Schema\Collection->__construct(Object(Cake\Database\Connection))
#6 /opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Database/Connection.php(391): Cake\Database\Schema\CachedCollection->__construct(Object(Cake\Database\Connection), true)
#7 /opt/lampp/htdocs/MyApp/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(1052): Cake\Database\Connection->getSchemaCollection()
#8 /opt/lampp/htdocs/MyApp/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(1006): Bake\Shell\Task\ModelTask->_getAllTables()
#9 /opt/lampp/htdocs/MyApp/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(213): Bake\Shell\Task\ModelTask->listAll()
#10 /opt/lampp/htdocs/MyApp/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(131): Bake\Shell\Task\ModelTask->getAssociations(Object(Cake\ORM\Table))
#11 /opt/lampp/htdocs/MyApp/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(114): Bake\Shell\Task\ModelTask->getTableContext(Object(Cake\ORM\Table), 'users', 'Users')
#12 /opt/lampp/htdocs/MyApp/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(101): Bake\Shell\Task\ModelTask->bake('Users')
#13 /opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Console/Shell.php(532): Bake\Shell\Task\ModelTask->main('users')
#14 /opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Console/Shell.php(525): Cake\Console\Shell->runCommand(Array, false, Array)
#15 /opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Console/CommandRunner.php(342): Cake\Console\Shell->runCommand(Array, true)
#16 /opt/lampp/htdocs/MyApp/vendor/cakephp/cakephp/src/Console/CommandRunner.php(164): Cake\Console\CommandRunner->runShell(Object(Bake\Shell\BakeShell), Array)
#17 /opt/lampp/htdocs/MyApp/bin/cake.php(12): Cake\Console\CommandRunner->run(

Array)

18 {main}

Anorexia answered 6/12, 2018 at 11:40 Comment(3)
Have you installed Cakephp using composer?Suet
It seems like if you remove the vendor directory and perform a composer install the problem will be fixedTutty
I just had the same issue, with CakePHP 3.8 in a Xampp stack on Linux. Cause was the wrong version of PHP being run (my system-wide install of PHP at /usr/bin/php). Solution: added the path /opt/lampp/bin to my PATH and all was well. :)Fallonfallout
D
11

On my case the problem was fixed by changing app/config.php, simply change datasource host from "localhost" to "127.0.0.1"

    'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => '127.0.0.1',...
Domesticate answered 21/5, 2020 at 16:38 Comment(0)
O
1

Follow the below points

  1. Your cake-php isn't connected to any local database, visit pagesController from browser to see if its connected to database,if not you need to configure in config/app.php with database name and credential.
  2. You are in cake-php project directory, try to hit the same "bin\cake bake model users" in windows ,for linux use "bin/cake bake model users".

Reply to this thread if issues not resolved

Odaniel answered 9/12, 2018 at 13:22 Comment(0)
A
-1

Change following crediantiles:

config/app_local.php

    'Datasources' => [
        'default' => [
            'host' => '127.0.0.1',  //change this from localhost to 127.0.0.1
            'username' => 'root',  //change user name
            'password' => '',  //change password
            'database' => 'db_cake'  //change Database name
    ],
Aggrandize answered 19/4 at 17:19 Comment(1)
That's exactly said in the existing answer. Please do not add answers that duplicate existingHissing
A
-2

Try changing datasource host from localhost to 127.0.0.1

Aslam answered 12/2, 2023 at 9:17 Comment(1)
This is effectively duplicating an existing answer. If there's something missing from an answer, please propose an edit, otherwise please use votes/comments if you agree with it or it helped you :).Cultivator

© 2022 - 2024 — McMap. All rights reserved.