运行bake命令时SQLSTATE HY000 2002

时间:2015-06-19 15:07:26

标签: cakephp-3.0

我在运行bake命令时遇到问题。我认为它与mysql有关,但我没有在Stackoverflow上找到任何解决此错误的方法。 这是我的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,

请帮忙,我得到的错误是:

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}

6 个答案:

答案 0 :(得分:8)

问题解决了!! 我应该添加的唯一内容是.bash_profile中的这一行使其工作:

var sha = new HMACSHA256(key)
var sha = new HMACSHA384(key)
var sha = new HMACSHA512(key)

答案 1 :(得分:8)

首先,让我们找出我们正在运行的PHP版本(了解它是否是默认版本)。

为此,在终端内,点击此命令:

which php

这应该输出Mac OS X预装的默认PHP安装路径,默认情况下必须是(假设您之前没有更改过):

/usr/bin/php

现在,我们只需要将它交换到安装了MAMP的PHP,它位于/Applications/MAMP/bin/php/php7.0.0/bin(MAMP 3.5)

为此,我们需要编辑.bash_profile并将MAMP版本的PHP添加到PATH变量中。 请按照以下简单步骤操作:

在终端内,运行vim ~/.bash_profile

键入i,然后将以下内容粘贴到文件顶部:

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

按ESC键,键入:wq,然后按Enter键

在终端中,运行source ~/.bash_profile

在终端中,再次输入which php并查找更新后的字符串。如果一切都成功,它应该输出MAMP PHP安装的新路径。

如果它没有输出正确的路径,请尝试关闭终端窗口(完全退出)并再次打开,它应该应用更改(简称重启)。

答案 2 :(得分:6)

我认为这是最好的方法:

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

在数据库配置中添加

答案 3 :(得分:2)

'host' => 'localhost',更改为'host' => '127.0.0.1',可以为我解决带有“例外”的问题。

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

我在Ampps上奔跑。

答案 4 :(得分:1)

我是一个初学者,在CakePHP教程中通过在MAC上使用MAMP遇到了这个问题。

首先,我使用以下命令在MAMP中找到了php:

find /Applications/mampstack-7.1.21-0/ -type f -name php

这应该输出您的目录…。我的情况是:
/Applications/mampstack-7.1.21-0//php/bin/php

然后我发现.bash_profile并使用以下命令将其打开:

touch ~/.bash_profile; open ~/.bash_profile

它将使用TextEdit打开它。然后在此文件中,我编辑了第一个命令获得的路径,并使用sbin删除了多余的路径:

export PATH="/Applications/mampstack-7.1.21-0/php/bin/:$PATH"

在终端命令中:

grep php ~/.bash_profile

然后我关闭所有终端,然后对我有用。

非常感谢您的第一条评论,这对于解决此问题很有帮助。

答案 5 :(得分:-2)

可以测试

 'default' => [
        'className' => 'Cake\Database\Connection',

        'driver' => 'Cake\Database\Driver\Mysql',

        'persistent' => false,

        'host' => 'localhost',

        'username' => 'root',

        'password' => '',

        'database' => 'laboiterose',

        'encoding' => 'utf8',

        'timezone' => 'UTC',

        'cacheMetadata' => true,

        'quoteIdentifiers' => false,

   ],