MySQL连接在本地主机上被拒绝 [英] Mysql connection refused on localhost

查看:292
本文介绍了MySQL连接在本地主机上被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到El Capitan后,我在连接MySql时遇到问题.我有一个名为index.php的基本PHP文件,其中包含以下代码:

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:

<?php
  $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

我不明白这里出了什么问题.我应该提到的是,升级之前,我在优胜美地也遇到了这个问题.当我擦拭计算机进行全新安装并不得不再次设置Apache和PHP配置时,问题开始了.我认为其中某些文件一定没有.

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.

编辑 我也尝试在连接中使用localhost而不是127.0.0.1.将该错误稍微更改为:

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

推荐答案

尝试使用localhost而不是127.0.0.1,并确保您使用的用户名/密码正确并且在mysql服务器上存在.还要检查mysqld是否正在运行,以及您的防火墙是否允许mysql数据库通过.

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.

编辑您的php.ini文件,并确保在Windows上启用扩展名为extension = php_mysqli.dll或在Linux上启用extension = php_mysqli.so.

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

在此处尝试答案: PHP-MYSQL-测试数据库服务器

您必须检查以下两项设置才能使其正常运行(假设您已经安装了MySQL服务器):

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

在您的PHP配置中检查mysql.default_socket的值.

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

在[mysqld]标题下检查MySQL配置文件中的套接字值.

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.

这篇关于MySQL连接在本地主机上被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆