得到"db:SQLSTATE [HY000] [2002]连接被拒绝".在Mac上使用MAMP报错 [英] getting "db: SQLSTATE[HY000] [2002] Connection refused" error on Mac with MAMP
问题描述
我正在尝试运行此PHP数据库的CLI版本搜索和替换脚本,但我认为这是与Mac OS X和MAMP有关的更一般的MySQL问题.每当我尝试在本地运行CLI脚本时,我都会收到以下错误:
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
我尝试过的事情
- 我能够使用这些设置连接到mysql,没问题,使用
mysql -u root -proot
等... - 将127.0.0.1替换为本地主机会出现相同的错误.
- 我所有的my.cnf文件都是空白.
- Apache和MySQL运行正常.
- 我已在另一台运行MAMP的Mac上成功复制了此问题
我正在使用这个 mysql
:/Applications/MAMP/Library/bin/mysql
I am using this mysql
: /Applications/MAMP/Library/bin/mysql
此 php
:/Applications/MAMP/bin/php/php5.3.28/bin/php
And this php
: /Applications/MAMP/bin/php/php5.3.28/bin/php
有人有什么想法吗?谢谢!
Anybody any ideas? Thanks!
修改
以下是显示脚本如何连接到MySQL的源代码: https://github.com/interconnectit/Search-Replace-DB/blob/master/srdb.cli.php
Here is the source code showing how the script connects to MySQL: https://github.com/interconnectit/Search-Replace-DB/blob/master/srdb.cli.php
依次导入:
https://github.com/interconnectit/Search-Replace-DB/blob/master/srdb.class.php
推荐答案
正如我在评论中所述,很有可能您没有运行您认为正在运行的PHP二进制文件.即使MAMP php二进制文件位于您的路径中, srdb.cli.php
中的shebang行也会读取#!/usr/bin/php
,这指向Apple-提供了php二进制文件.
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.
因此,如果您使用MAMP php二进制文件的完整路径调用脚本,则应避免该问题:
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
另一种解决方案可能是将shebang行替换为:
Another solution might be to replace the shebang line with:
#!/usr/bin/env php
仅当MAMP二进制文件位于$ code的/usr/bin
前面时,此选项才有效.但是,使用#!/usr/bin/env php
可以确保,即使通过 ./srdb.cli.php
或使用 php srdb.cli.php
.
This works only if the MAMP binary is in your $PATH in front of /usr/bin
. Using #!/usr/bin/env php
ensures 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
.
这篇关于得到"db:SQLSTATE [HY000] [2002]连接被拒绝".在Mac上使用MAMP报错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!