使用MAMP在Mac OS中通过终端连接到MYSQLi [英] Connecting to MYSQLi through Terminal in Mac OS using MAMP
问题描述
我正在尝试通过Mac中的Terminal运行我的PHP脚本.当我尝试运行脚本时,出现此错误:
I am trying to run my PHP script via Terminal in Mac. When I try to run the script, I am getting this error:
警告:mysqli_connect():(HY000/2002):没有这样的文件或目录
Warning: mysqli_connect(): (HY000/2002): No such file or directory
这是我尝试连接的方式:
This is how I am trying to connect:
mysqli_connect('localhost','root','root','my_db');
mysqli_connect('localhost','root','root','my_db');
我也尝试使用主机:127.0.0.1而不是localhost进行连接,然后出现此错误:
I also tried to connect using the host: 127.0.0.1 instead of localhost and I then got this error:
警告:mysqli_connect():(HY000/2002):连接被拒绝
Warning: mysqli_connect(): (HY000/2002): Connection refused
从我在网上阅读的内容来看,它可能与mysql套接字有关,但是我试图使它起作用的任何尝试都无济于事.
From what I've read online it might have something to do with mysql socket, but whatever I try to do to get this to work doesn't help.
我什至尝试使用以下内容作为主机进行连接:
I even tried to connect using the following as my host:
:/Applications/MAMP/tmp/mysql/mysql.sock
:/Applications/MAMP/tmp/mysql/mysql.sock
:/var/mysql/mysql.sock
:/var/mysql/mysql.sock
您知道什么是问题/解决方案吗?
Any idea what can be the problem/solution?
推荐答案
出现了同样的问题.这就是我所做的.
Had the same issue. Here's what I did.
-
打开终端(/Applications/Utilities/terminal.app)
Open Terminal (/Applications/Utilities/terminal.app)
使用您喜欢的文本编辑器来创建/编辑~/.bash_profile
.例如,如果您使用的是vim,则键入vim ~/.bash_profile
.我使用TextMate,所以我输入了mate ~/.bash_profile
.
Use your favorite text editor to create/edit ~/.bash_profile
. For instance, if you're using vim, type vim ~/.bash_profile
. I use TextMate, so I typed mate ~/.bash_profile
.
添加以下行:
export MAMP_SQL=/Applications/MAMP/Library/bin
export MAMP_PHP=/Applications/MAMP/bin/php/php5.5.10/bin
export PATH="$MAMP_SQL:$MAMP_PHP:$PATH"
这些行确保找到(并因此使用)的MySQL和PHP的第一个版本是MAMP使用的版本. 注意::确保php5.5.10与MAMP使用的版本匹配.默认情况下,您可以对照http://localhost:8888/MAMP/index.php?page=phpinfo&language=English
进行检查. 保存文件 (我不必保存此文件,但总是有人抱怨它不起作用).
These lines ensure that the first versions of MySQL and PHP that are found (and therefore used) are the versions used by MAMP. NB: Make sure that php5.5.10 matches the version MAMP is using. You can check this against http://localhost:8888/MAMP/index.php?page=phpinfo&language=English
by default. SAVE THE FILE (I shouldn't have to save this, but invariably someone complains that it doesn't work).
关闭终端窗口,然后重新打开.这将重新启动外壳程序,该外壳程序将加载您的.bash_profile
脚本.
Close your terminal window and reopen. This restarts the shell, which will load your .bash_profile
script.
键入which php
.您应该得到类似于/Applications/MAMP/bin/php/php5.5.10/bin/php
Type which php
. You should get something akin to /Applications/MAMP/bin/php/php5.5.10/bin/php
键入which mysql
.您应该得到类似于/Applications/MAMP/Library/bin/mysql
Type which mysql
. You should get something akin to /Applications/MAMP/Library/bin/mysql
如果由于某种原因您得到的答案不同于5和6的答案,请返回并检查您的.bash_profile;您只是忽视了正确保存它的机会.
If for any reason you get something different than the responses from 5 and 6, go back and check your .bash_profile; chances are you just neglected to save it properly.
这篇关于使用MAMP在Mac OS中通过终端连接到MYSQLi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!