使用 symfony 和 MAMP 时出现 PDO 连接错误

2022-01-16 00:00:00 php mysql doctrine symfony1 mamp

尝试执行时收到 PDO 错误 php symfony 学说:插入-sql
我得到的错误:

<上一页>警告:PDO::__construct(): [2002] 连接被拒绝(试图通过 tcp://127.0.0.1:3306 连接)在/Users/johannes/Programmering/PHP/htdocs/symfony/sfprojects/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection.php 在第 470 行

databases.yml

<上一页>全部:教义:类:sfDoctrineDatabase参数:dsn: mysql:host=127.0.0.1;dbname=jobeet;用户名:root密码:root

使用root"作为密码执行 mysql -u root -p jobeet 可以让我访问,所以没有问题.是的,我运行的 mysql 是 MAMP 的.

感谢您的帮助.

解决方案

MAMP 默认不允许 TCP 连接.您可以打开它或使用插座.

按照@Tom 的建议更改您的 dsn 应该可以解决您的问题.虽然很奇怪,但使用 localhost 而不是 127.0.0.1 使得 mysql 通过套接字连接.

http://dev.mysql.com/doc/refman/5.0/en/connecting.html :

<块引用>

在 Unix 上,MySQL 程序处理主机以某种方式特别命名 localhost这可能与您的不同与其他基于网络的期望相比程式.对于连接到localhost,MySQL 程序试图通过使用连接到本地服务器Unix 套接字文件.即使发生这种情况一个 --port 或 -P 选项被赋予指定端口号.以确保客户端建立 TCP/IP 连接到本地服务器,使用 --host 或 -h指定主机名值127.0.0.1,或本地服务器的 IP 地址或名称.你也可以指定连接协议明确地,即使对于本地主机,通过使用 --protocol=TCP 选项.

Getting an PDO error when trying to do php symfony doctrine:insert-sql
The error I get:

Warning: PDO::__construct(): [2002] Connection refused (trying to connect via tcp://127.0.0.1:3306) in /Users/johannes/Programmering/PHP/htdocs/symfony/sfprojects/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection.php on line 470

databases.yml

    all:
    doctrine:
    class: sfDoctrineDatabase
    param:
      dsn: mysql:host=127.0.0.1;dbname=jobeet;
      username: root
      password: root

Doing a mysql -u root -p jobeet with "root" as password gives me access, so no problem there. And yes, the mysql that I run is MAMP's.

Thanks for any help.

解决方案

MAMP by default doesn't allow TCP connections. You can either turn it on or use sockets.

Changing your dsn as @Tom suggests should fix your issues. Weird as it is but using localhost instead of 127.0.0.1 makes that mysql connects through sockets.

http://dev.mysql.com/doc/refman/5.0/en/connecting.html :

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option.

相关文章