11

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.

  • do you really need this part ? `;mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock` – Xavier Barbosa Jan 24 '11 at 19:37
  • i get the error with or without –  Jan 24 '11 at 19:54
  • wanted to say that changing localhost to 127.0.0.1 fixed it (i'm using xampp) – Marin Mar 08 '12 at 05:30
  • Just to add for anyone else that may run into this issue. If you are running a non-standard port like MAMP does (8889) for MySQL you will still run into this issue if you even use 127.0.0.1. Running on the standard port 3306 fixed it. If I find where to change this I'll post back. But I was using CodeIgniter 2 and Doctrine 2, so it's a little more complex. – Michael Jul 19 '13 at 17:31

4 Answers4

13

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.

enter image description here

MAMP PRO 3.x
As stated by Kendrick: enter image description here

Community
  • 1
  • 1
James O
  • 441
  • 5
  • 11
  • 1
    MAMP 3.0 Looks a little different but essentially the same fix. SELECT "Allow network access to MySQL" >> "Only from this mac" – Kendrick Oct 08 '14 at 00:24
7

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.

Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
  • Well, I think both my answer and @Tom's are valid. It's just you need to do some additional configuration to MAMP. You got a "no such file or directory' message because MAMP saves socket in a non default location. – Jakub Zalas Jan 26 '11 at 13:50
  • 1
    nice, using `localhost` instead of 127.0.0.1 did the trick; no need to open up access outside of local as @James suggests – Mark Fox Aug 16 '13 at 05:36
3

I had the same error when attempting to build my tables in Symfony and using MAMP. I fixed the issue by changing my dsn line, in the databases.yml file, to the following:

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

Looks right. Here would be the exact equivalent from a working databases.yml I'm using, in case it's of any use:

dsn: 'mysql:host=localhost;dbname=jobeet'
Tom
  • 30,090
  • 27
  • 90
  • 124
  • changing the host from 127.0.0.1 to localhost, gives me this error No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) –  Jan 25 '11 at 12:28