Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting "db: SQLSTATE[HY000] [2002] Connection refused" error on Mac with MAMP

I am trying to run the CLI version of this PHP databse Search and Replace Script, but I think this a more general MySQL problem relating to Mac OS X and MAMP. I receive the following error whenever I attempt to run the CLI script locally:

db: SQLSTATE[HY000] [2002] Connection refused

Here is the command I'm running:

./srdb.cli.php -h 127.0.0.1 -u root -n mydbname -proot -c utf\-8 -s mywebsite.com -r dev.mywebsite.com

What I've tried

  • I am able to connect to mysql using these settings, no problem, using mysql -u root -proot etc...
  • Swapping 127.0.0.1 for localhost gives the same error.
  • All my my.cnf files are blank.
  • Apache and MySQL are running fine.
  • I have succeeded in replicating this problem on another Mac running MAMP

I am using this mysql: /Applications/MAMP/Library/bin/mysql

And this php: /Applications/MAMP/bin/php/php5.3.28/bin/php

Anybody any ideas? Thanks!

Edit

Here is the source code showing how the script connects to MySQL: https://github.com/interconnectit/Search-Replace-DB/blob/master/srdb.cli.php

which in turn imports this:

https://github.com/interconnectit/Search-Replace-DB/blob/master/srdb.class.php

like image 926
JP Lew Avatar asked Dec 14 '25 00:12

JP Lew


2 Answers

As stated in my comment already, chances are that you're not running the PHP binary you thought you were running. Even if the MAMP php binary is in your path, the shebang line in srdb.cli.php reads #!/usr/bin/php and that points to the Apple-provided php binary.

So if you invoke the script with the full path to your MAMP php binary, the problem should be avoided:

/Applications/MAMP/bin/php/php5.3.28/bin/php srdb.cli.php -h 127.0.0.1 -u root -n mydbname -proot -c utf\-8 -s mywebsite.com -r dev.mywebsite.com

Another solution might be to replace the shebang line with:

#!/usr/bin/env php

This works only if the MAMP binary is in your $PATH in front of /usr/bin. Using #!/usr/bin/env phpensures however, that you're always using the same binary no matter if you're invoking the script via ./srdb.cli.php or with php srdb.cli.php.

like image 127
z80crew Avatar answered Dec 15 '25 12:12

z80crew


Stop mysql :

sudo service mysql stop

And then start it :

sudo service mysql start

It resolved the problem

like image 37
Mahak Choudhary Avatar answered Dec 15 '25 14:12

Mahak Choudhary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!