Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLSTATE HY000 2002 while running bake command

Tags:

cakephp-3.0

I have problem running the bake commands. i think it is related to mysql but i didn't find any solution to this error on Stackoverflow. This is my app.php:

'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',

            //'port' => 'nonstandard_port_number',
            'username' => 'root',
            'password' => 'root',
            'database' => 'laboiterose',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'cacheMetadata' => true,

Please help, the error i get, is:

Exception: SQLSTATE[HY000] [2002] No such file or directory in [/Applications/MAMP/htdocs/my_db/vendor/cakephp/cakephp/src/Database/Driver/PDODriverTrait.php, line 48]
2015-06-19 14:35:48 Error: [PDOException] SQLSTATE[HY000] [2002] No such file or directory
Stack Trace:
0 /Applications/MAMP/htdocs/my_db/vendor/cakephp/cakephp/src/Database/Driver/PDODriverTrait.php(48): PDO->__construct('mysql:host=loca...', 'root', 'root', Array)
1 /Applications/MAMP/htdocs/my_db/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php(89): Cake\Database\Driver\Mysql->_connect('mysql:host=loca...', Array)
2 /Applications/MAMP/htdocs/my_db/vendor/cakephp/cakephp/src/Database/Schema/BaseSchema.php(46): Cake\Database\Driver\Mysql->connect()
3 /Applications/MAMP/htdocs/my_db/vendor/cakephp/cakephp/src/Database/Dialect/MysqlDialectTrait.php(62): Cake\Database\Schema\BaseSchema->__construct(Object(Cake\Database\Driver\Mysql))
4 /Applications/MAMP/htdocs/my_db/vendor/cakephp/cakephp/src/Database/Schema/Collection.php(52): Cake\Database\Driver\Mysql->schemaDialect()
5 /Applications/MAMP/htdocs/my_db/vendor/cakephp/cakephp/src/Database/Schema/CachedCollection.php(44): Cake\Database\Schema\Collection->__construct(Object(Cake\Database\Connection))
6 /Applications/MAMP/htdocs/my_db/vendor/cakephp/cakephp/src/Database/Connection.php(319): Cake\Database\Schema\CachedCollection->__construct(Object(Cake\Database\Connection), true)
7 /Applications/MAMP/htdocs/my_db/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(806): Cake\Database\Connection->schemaCollection()
8 /Applications/MAMP/htdocs/my_db/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(782): Bake\Shell\Task\ModelTask->_getAllTables()
9 /Applications/MAMP/htdocs/my_db/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(186): Bake\Shell\Task\ModelTask->listAll()
10 /Applications/MAMP/htdocs/my_db/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(109): Bake\Shell\Task\ModelTask->getAssociations(Object(Cake\ORM\Table))
11 /Applications/MAMP/htdocs/my_db/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(95): Bake\Shell\Task\ModelTask->bake('Users')
12 /Applications/MAMP/htdocs/my_db/vendor/cakephp/bake/src/Shell/BakeShell.php(241): Bake\Shell\Task\ModelTask->main('Users')
13 [internal function]: Bake\Shell\BakeShell->all('users')
14 /Applications/MAMP/htdocs/my_db/vendor/cakephp/cakephp/src/Console/Shell.php(380): call_user_func_array(Array, Array)
15 /Applications/MAMP/htdocs/my_db/vendor/cakephp/cakephp/src/Console/ShellDispatcher.php(204): Cake\Console\Shell->runCommand(Array, true)
16 /Applications/MAMP/htdocs/my_db/vendor/cakephp/cakephp/src/Console/ShellDispatcher.php(175): Cake\Console\ShellDispatcher->_dispatch()
17 /Applications/MAMP/htdocs/my_db/vendor/cakephp/cakephp/src/Console/ShellDispatcher.php(126): Cake\Console\ShellDispatcher->dispatch()
18 /Applications/MAMP/htdocs/my_db/bin/cake.php(33): Cake\Console\ShellDispatcher::run(Array)
19 {main}
like image 479
Mireil Avatar asked Jun 19 '15 15:06

Mireil


4 Answers

I think this is the best way:

'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock',

add this in your database config

like image 135
Navegante Avatar answered Nov 09 '22 12:11

Navegante


First, Lets find out what version of PHP we're running (To find out if it's the default version).

To do that, Within the terminal, Fire this command:

which php

This should output the path to the default PHP install which comes preinstalled by Mac OS X, by default it has to be (Assuming you've not changed it before):

/usr/bin/php

Now, We just need to swap this over to the PHP that is installed with MAMP, which is located at /Applications/MAMP/bin/php/php7.0.0/bin (MAMP 3.5)

To do this, We need to edit the .bash_profile and add the MAMP version of PHP to the PATH variable. Follow these simple steps:

Within the Terminal, run vim ~/.bash_profile

Type i and then paste the following at the top of the file:

export PATH=/Applications/MAMP/bin/php/php5.4.10/bin:$PATH

Hit ESC, Type :wq, and hit Enter

In Terminal, run source ~/.bash_profile

In Terminal, type in which php again and look for the updated string. If everything was successful, It should output the new path to MAMP PHP install.

In case it doesn't output the correct path, try closing the terminal window (exit fully) and open again, it should apply the changes (Restart in short).

like image 24
6754534367 Avatar answered Nov 09 '22 14:11

6754534367


Problem solved!! The only thing i should add is this line in .bash_profile to make it work:

export PATH=/Applications/MAMP/bin/php/php5.6.10/bin:$PATH
like image 7
Mireil Avatar answered Nov 09 '22 13:11

Mireil


Changing 'host' => 'localhost', to 'host' => '127.0.0.1', resolve the issue with Exception for me.

 'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => '127.0.0.1',

I'm running on the Ampps.

like image 3
redrom Avatar answered Nov 09 '22 14:11

redrom