2

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

JP Lew
  • 4,121
  • 2
  • 32
  • 45
  • How do you access MySQL via PHP? Can you post the code? – Khanh Tran May 26 '14 at 04:15
  • In srcb.class.php, add this code after line 383: print $connection_type."\n\n"; . Then, on line 414 ($connection = @mysql_connect(...)), remove @ sign. Final run your script again and see what happens. – Khanh Tran May 26 '14 at 08:43
  • This could be a problem with PHP and MAMP using different socket loactions. The answer to the following question could be helpful: http://stackoverflow.com/questions/7461360/doctrine-2-command-line-tool-mamp-and-mysql-sock – z80crew May 26 '14 at 09:21
  • And another idea: Are you really sure, you're running the MAMP provided php binary? The shebang line in `srdb.cli.php` reads `#!/usr/bin/php`and that points to the Apple-provided php binary. – z80crew May 26 '14 at 10:36
  • @z80crew: oh dear, that was the answer. I thought I was using MAMP's php, but you're right, the shebang line was defaulting to the system's PHP. Please provide that as an answer and I'll upvote it. Thanks a lot. – JP Lew May 26 '14 at 15:33

3 Answers3

10

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.

z80crew
  • 1,150
  • 1
  • 11
  • 20
  • thanks for the extra info. I don't have the luxury of being able to edit the shebang line, because this script is hosted in a public repo and I download it again each time I want to use it. The previous workaround I was using was to issue a `sed` command to replace the shebang line with one of my choosing. Your solution is much simpler however, I didn't realize you could execute PHP scripts like this: `path/to/my/php script.php ...`. Very useful. – JP Lew May 26 '14 at 17:40
  • THANKS BROTHER ! – fdrv Apr 24 '19 at 11:27
1

Stop mysql :

sudo service mysql stop

And then start it :

sudo service mysql start

It resolved the problem

Mahak Choudhary
  • 1,286
  • 1
  • 16
  • 13
0

To add onto z80crew's brilliant solution, for anyone else unfamiliar/uncomfortable with altering path variables, specifying the full location paths for both the MAMP php binary and the search-replace-db script in the cli script provided by interconnect solved the problem for me. I put the strings to search for and replace with in quotes. I also increased the php timeout limit in wp-config.php with: set_time_limit(3000);

I was consistent with the server name between the options passed to the script and what's in my wp-config.php file (using localhost in wp-config, using localhost in the script as well)

/Applications/MAMP/bin/php/php7.4.2/bin/php /Applications/MAMP/htdocs/test/Search-Replace-DB-master/srdb.cli.php -h localhost -u root -proot --port 8889 -n test -s "http://olddomain.com" -r "http://localhost:8888/test" -v true

user1574371
  • 75
  • 1
  • 1
  • 9