解决“没有这样的文件或目录”的问题运行 `php app/console 学说:schema:create` 时

发布于 2024-11-13 11:27:01 字数 1663 浏览 2 评论 0原文

我是 Symfony2 (beta4) 和 Doctrine 的新手,当我尝试通过命令行创建数据库模式时遇到问题。

错误如下:

$ php app/console doctrine:schema:create

Creating database schema...

[PDOException]                                    
SQLSTATE[HY000] [2002] No such file or directory  

[ErrorException]                                                                                          
Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) 
in /Applications/MAMP/htdocs/sf-test-2/vendor/doctrine-dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php line 36

mysql 数据库设置已正确插入到 config/parameters.ini 文件中。

这是 config.yml 中的 Doctrine 配置

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

和实体(我只做了一个来测试它)

<?php
// src/Acme/NewsBundle/Entity/Article.php
namespace Acme\NewsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="articles")
 */
class Article
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
protected $id;

/**
 * @ORM\Column(type="string", length="255")
 */
protected $title;

/**
 * @ORM\Column(type="text")
 */
protected $body;

/**
 * @ORM\Column(type="string", length="255")
 */
protected $author;

/**
 * @ORM\Column(type="date")
 */
protected $date;
}
?>

I am new to Symfony2 (beta4) and Doctrine and am having issues when i try to create the DB schema via command line.

Here's the error:

$ php app/console doctrine:schema:create

Creating database schema...

[PDOException]                                    
SQLSTATE[HY000] [2002] No such file or directory  

[ErrorException]                                                                                          
Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) 
in /Applications/MAMP/htdocs/sf-test-2/vendor/doctrine-dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php line 36

The mysql database settings are correctly inserted in the config/parameters.ini file.

And here's the Doctrine configuration in config.yml

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

And the entity (i made only one to test it)

<?php
// src/Acme/NewsBundle/Entity/Article.php
namespace Acme\NewsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="articles")
 */
class Article
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
protected $id;

/**
 * @ORM\Column(type="string", length="255")
 */
protected $title;

/**
 * @ORM\Column(type="text")
 */
protected $body;

/**
 * @ORM\Column(type="string", length="255")
 */
protected $author;

/**
 * @ORM\Column(type="date")
 */
protected $date;
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

半城柳色半声笛 2024-11-20 11:27:01

就在今天,我遇到了类似的情况(但在其他情况下,我试图从数据库创建实体)。

我解决了这个问题,只需将parameters.ini文件中的database_host“localhost”修改为“127.0.0.1”

我认为我的 Mysql 实例仅通过 TCP 而不是套接字运行,因为当使用 database_host="localhost" 时它会失败。

Just today I had a similar situation (but in other context, I was trying to create entities from db).

I solved it simply modifying the database_host from "localhost" to "127.0.0.1" in the parameters.ini file.

I think my Mysql instance is running only via TCP and not socket and because this when use database_host="localhost" it fails.

岛歌少女 2024-11-20 11:27:01

尝试将此行添加

unix_socket: /tmp/mysql.sock

到您的 config.yml 文件中 >学说> dbal 就在密码行之后。

Try to add this line

unix_socket: /tmp/mysql.sock

into your config.yml file > doctrine > dbal just after the password line.

不知在何时 2024-11-20 11:27:01

我改变了'主机'=> “本地主机”到“主机”=> app/config/database.php 上的“127.0.0.1”一切正常

I changed 'host'=> 'localhost' to 'host'=> '127.0.0.1' on app/config/database.php and everything is working correctly

生生漫 2024-11-20 11:27:01

我按照这个小教程修复了它: http://andreys.info/blog/2007-11-07/configuring-terminal-to-work-with-mamp-mysql-on-leopard

[编辑]:
我修改了正确的 php.ini,现在一切正常。

现在我收到以下错误:

[Exception]                                                                                            
 DateTime::__construct(): It is not safe to rely on the system's timezone settings.
 You are *required* to use the date.timezone setting or the date_default_timezone_set() function.
 In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.
 We selected 'Europe/Berlin' for 'CEST/2.0/DST' instead 

这是 php.ini 中的 date.timezone 配置

date.timezone = "Europe/Paris"

我会尝试自己弄清楚,但如果你们中有人知道怎么做要修复它,请毫不犹豫地对此发表评论。谢谢!

I fixed it by following this small tutorial: http://andreys.info/blog/2007-11-07/configuring-terminal-to-work-with-mamp-mysql-on-leopard

[EDIT]:
I modified the right php.ini and everything's working fine now.

Now I get the following error:

[Exception]                                                                                            
 DateTime::__construct(): It is not safe to rely on the system's timezone settings.
 You are *required* to use the date.timezone setting or the date_default_timezone_set() function.
 In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.
 We selected 'Europe/Berlin' for 'CEST/2.0/DST' instead 

Here's the date.timezone config in php.ini

date.timezone = "Europe/Paris"

I'll try to figure it out myself but if any of you know how to fix it don't hesitate to comment on this. Thanks!

懵少女 2024-11-20 11:27:01

对我来说,问题是 PHP.INI 中的 mysql.default_socket 设置默认的位置与 MySQL 实际放置该文件的位置不同。

我没有编辑配置文件,而是在 PHP 正在查找的位置创建了一个别名,该别名连接到真正的 mysql.sock。

只需运行这两个命令(无需重新启动):

mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock

The problem for me was the mysql.default_socket setting in PHP.INI defaults to a different location than where MySQL actually puts that file.

Instead of editing the config files, I created an alias in the location that PHP is looking that connects to the real mysql.sock.

Just run these two commands (no restart needed):

mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock
久伴你 2024-11-20 11:27:01

有时您可能会得到 mysql.lock 位置路径的多种解决方案。但我认为最好的解决方案是使用 php phpinfo() 函数查看 mysql 的位置路径。

注意:如果您不熟悉 phpinfo(),那么只需创建一个文件,给出任意名称并包含该文件的内容,如下所示

<?php
phpinfo();
?>

运行该文件并获取 mysql.lock 文件的路径。我认为这会有帮助。

Sometimes you may got several solution for location path of mysql.lock. But I think the best solution is just see the location path of mysql using php phpinfo() function.

Note: if you are not familar with phpinfo(), then just create a file give any name and include content of that file like this

<?php
phpinfo();
?>

run this file and get the path of mysql.lock file. I think it will help.

痴者 2024-11-20 11:27:01

值得检查一下 Mysql 是否也在运行。

Worth checking to see if Mysql is running too.

ι不睡觉的鱼゛ 2024-11-20 11:27:01

为时已晚,但希望它也能帮助别人。

我正在使用 Vagrant

只需将 localhost 更改为 127.0.0.1 并将端口更改为 3333

效果很好。

Too late but hope it can help someone as well.

I am using Vagrant

Just change localhost to 127.0.0.1 AND change the port to 3333

worked perfectly.

白况 2024-11-20 11:27:01

我想只要重新启动 mysql 和 apache,这对我来说就可以了。

I think just restart the mysql, and apache, that's work for me.

心碎的声音 2024-11-20 11:27:01

重新启动 mysql 对我有用:

sudo /etc/init.d/mysql restart

Restarting mysql worked for me:

sudo /etc/init.d/mysql restart

萌面超妹 2024-11-20 11:27:01

在部署时的 symfony 3.2.9 上有同样的问题“SQLSTATE[HY000] [2002] No such file or directory”

我通过将parameters.yml文件上的database_host值从“localhost”更改为我的服务器IP来修复它,

但我有尝试通过 SSH 运行命令行时出现另一个错误消息:

[Doctrine\DBAL\Exception\ConnectionException]
驱动程序中发生异常: SQLSTATE[HY000] [2002] 连接被拒绝

我最终通过在 Doctrine 配置部分的 config.yml 文件中添加这两行来修复它:

unix_socket: /var/lib/mysql/mysql.sock
server_version: '5.5'

我所有的最终 Doctrine 配置都是这样的:

doctrine:
    dbal:
        driver: pdo_mysql
        host: '%database_host%'
        port: '%database_port%'
        dbname: '%database_name%'
        user: '%database_user%'
        password: '%database_password%'
        unix_socket: /var/lib/mysql/mysql.sock
        server_version: '5.5'
        charset: UTF8

希望这有帮助

Had the same issue "SQLSTATE[HY000] [2002] No such file or directory" on symfony 3.2.9 on deployment

I fixed it by changing the database_host value from "localhost" to my server IP on my parameters.yml file

but I had this another error message when trying to run commandline by SSH:

[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused

I finaly fix it by adding these 2 lines on my config.yml file in Doctrine configuration section :

unix_socket: /var/lib/mysql/mysql.sock
server_version: '5.5'

all my final doctrine configuration is like this:

doctrine:
    dbal:
        driver: pdo_mysql
        host: '%database_host%'
        port: '%database_port%'
        dbname: '%database_name%'
        user: '%database_user%'
        password: '%database_password%'
        unix_socket: /var/lib/mysql/mysql.sock
        server_version: '5.5'
        charset: UTF8

hopefully this helps

丘比特射中我 2024-11-20 11:27:01

Laravel .env 添加 DB_SOCKET 以及 mysql.sock 的路径

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root
DB_PASSWORD=mypassword

DB_SOCKET=/Users/username/Library/Containers/MAMP/mysql/tmp/mysql.sock

或在 PDO 中添加 unix_socket

PDO('mysql:host=localhost;port=3306;unix_socket=/Users/username/Library/Containers/MAMP/mysql/tmp/mysql.sock', root, mypassword);

Laravel .env add DB_SOCKET with path to mysql.sock

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root
DB_PASSWORD=mypassword

DB_SOCKET=/Users/username/Library/Containers/MAMP/mysql/tmp/mysql.sock

or add unix_socket in PDO

PDO('mysql:host=localhost;port=3306;unix_socket=/Users/username/Library/Containers/MAMP/mysql/tmp/mysql.sock', root, mypassword);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文