简体   繁体   English

MySQL连接在本地主机上被拒绝

[英]Mysql connection refused on localhost

After an upgrade to El Capitan I'm having issues connecting to MySql. 升级到El Capitan后,我在连接MySql时遇到问题。 I have a basic PHP file called index.php with the following code: 我有一个名为index.php的基本PHP文件,其中包含以下代码:

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

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. 当我擦除计算机进行全新安装并不得不再次设置Apache和PHP配置时,问题开始了。 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 . 编辑我也尝试在连接中使用localhost而不是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. 尝试使用localhost而不是127.0.0.1,并确保您使用的用户名/密码正确并且在mysql服务器上存在。 Also check mysqld is running and that your firewall is allowing the mysql database through. 还要检查mysqld是否正在运行,以及防火墙是否允许mysql数据库通过。

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.ini文件,并确保在Windows上启用扩展名为extension = php_mysqli.dll或在Linux上启用extension = php_mysqli.so。

Try the answer here: PHP - MYSQL - test database server 在此处尝试答案: PHP-MYSQL-测试数据库服务器

There are two settings you have to check for this to work (assuming you have MySQL server installed of course): 您必须检查以下两项设置才能使其正常工作(假设您已经安装了MySQL服务器):

Check the value of mysql.default_socket in your PHP configuration. 在您的PHP配置中检查mysql.default_socket的值。

Check the value of socket in your MySQL configuration file under the [mysqld] heading. 在[mysqld]标题下检查MySQL配置文件中的socket值。

Those values have to be identical; 这些值必须相同。 if they're not, change one to match the other and restart respective service. 如果不是,请更改其中一个以匹配另一个并重新启动相应的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM