mysql拒绝本地访问,MySQL连接在本地主机上被拒绝

After an upgrade to El Capitan I'm having issues connecting to MySql. I have a basic PHP file called index.php with the following code:

$conn = new mysqli("127.0.0.1", "xxxx", "xxxx");

if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);

echo "Connected successfully";

?>

Now when I visit the page in my browser, I get the following message:

Warning: mysqli::mysqli(): (HY000/2002): Connection refused in /Users/rich/Documents/DESIGN/test/index.php on line 3

Connection failed: Connection refused

I can't understand what's going wrong here. I should mention that I also had this problem on Yosemite before upgrade. The problem began when I wiped my computer to do a fresh install and had to setup Apache and PHP config again. I think something must have been left out of one of the files.

EDIT

I've also tried using localhost in the connection instead of 127.0.0.1. That changes the error slightly to:

Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /Users/rich/Documents/DESIGN/test/index.php on line 4

Connection failed: No such file or directory

解决方案

Try using localhost instead of 127.0.0.1 and also make sure the username/password you are using are correct and exist on the mysql server. Also check mysqld is running and that your firewall is allowing the mysql database through.

Edit your php.ini file and make sure extension extension=php_mysqli.dll is enabled on Windows or extension=php_mysqli.so on Linux.

There are two settings you have to check for this to work (assuming you have MySQL server installed of course):

Check the value of mysql.default_socket in your PHP configuration.

Check the value of socket in your MySQL configuration file under the [mysqld] heading.

Those values have to be identical; if they're not, change one to match the other and restart respective service.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值