简体   繁体   English

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

[英]PDO connection error when using symfony and MAMP

Getting an PDO error when trying to do php symfony doctrine:insert-sql 尝试执行php symfony doctrine:insert-sql时出现PDO错误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 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. 使用“root”作为密码进行mysql -u root -p jobeet可以访问,所以没问题。 And yes, the mysql that I run is MAMP's. 是的,我运行的mysql是MAMP的。

Thanks for any help. 谢谢你的帮助。

MAMP PRO 2.x MAMP PRO 2.x
I was able to solve this and many similar issues by simply unchecking "Allow local access only" in the MySQL prefs on the MAMP control panel. 我只需在MAMP控制面板上的MySQL prefs中取消选中“仅允许本地访问”即可解决此问题和许多类似问题。

在此输入图像描述

MAMP PRO 3.x MAMP PRO 3.x
As stated by Kendrick : 正如肯德里克所说: 在此输入图像描述

MAMP by default doesn't allow TCP connections. 默认情况下,MAMP不允许TCP连接。 You can either turn it on or use sockets. 您可以打开它或使用套接字。

Changing your dsn as @Tom suggests should fix your issues. 改变你的dsn @Tom建议应该解决你的问题。 Weird as it is but using localhost instead of 127.0.0.1 makes that mysql connects through sockets. 虽然很奇怪但是使用localhost而不是127.0.0.1使得mysql通过套接字连接。

http://dev.mysql.com/doc/refman/5.0/en/connecting.html : 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. 在Unix上,MySQL程序特别对待主机名localhost,其方式可能与您期望的与其他基于网络的程序相比有所不同。 For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. 对于与localhost的连接,MySQL程序尝试使用Unix套接字文件连接到本地服务器。 This occurs even if a --port or -P option is given to specify a port number. 即使给出--port或-P选项指定端口号,也会发生这种情况。 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. 要确保客户端与本地服务器建立TCP / IP连接,请使用--host或-h指定主机名值127.0.0.1,或本地服务器的IP地址或名称。 You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. 您还可以使用--protocol = TCP选项显式指定连接协议,即使对于localhost也是如此。

I had the same error when attempting to build my tables in Symfony and using MAMP. 尝试在Symfony中构建表并使用MAMP时,我遇到了同样的错误。 I fixed the issue by changing my dsn line, in the databases.yml file, to the following: 我通过将databases.yml文件中的dsn行更改为以下内容来解决此问题:

dsn: 'mysql:host=localhost;dbname=jobeet;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock'

Looks right. 看起来不错。 Here would be the exact equivalent from a working databases.yml I'm using, in case it's of any use: 这将是我正在使用的工作databases.yml的完全等价物,万一它有任何用途:

dsn: 'mysql:host=localhost;dbname=jobeet'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM