mysqli_connect(): (HY000/2002): 无法通过套接字连接到本地 MySQL 服务器 [英] mysqli_connect(): (HY000/2002): Can't connect to local MySQL server through socket

查看:64
本文介绍了mysqli_connect(): (HY000/2002): 无法通过套接字连接到本地 MySQL 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助...我在尝试使用 php 显示数据库时遇到此错误:

I need help bad...I got this error trying to display a database using php:

mysqli_connect(): (HY000/2002): 无法连接到本地 MySQL 服务器通过socket '/Applications/MAMP/tmp/mysql/mysql.sock'(2) 中/Applications/MAMP/htdocs/databases.php 第 7 行 数据库连接失败:无法通过套接字连接到本地 MySQL 服务器'/Applications/MAMP/tmp/mysql/mysql.sock' (2) (2002)

mysqli_connect(): (HY000/2002): Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2) in /Applications/MAMP/htdocs/databases.php on line 7 Database connection failed: Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2) (2002)

我已经尝试修复它好几天了,从有同样问题的人那里阅读了这个论坛,并应用了修复(就像这个 http://tinyurl.com/q4cpxzj)绝对没有运气.

I've been trying to fix it for days, reading through this forum from people with the same issues, and applying the fixes (like this one http://tinyurl.com/q4cpxzj) with absolutely no luck.

这是我尝试过的一些步骤:进入 php.ini 文件并将套接字更改为/tmp/mysql.sock(我也尝试将其更改为/Applications/MAMP/tmp/mysql/mysql.sock 以查看发生了,不好).

Here's some steps I've tried: Going into php.ini file and changing the socket to /tmp/mysql.sock (I also tried changing it to /Applications/MAMP/tmp/mysql/mysql.sock to see what happens, no good).

尝试对/etc/my.cnf 文件执行相同操作.不好.

Tried doing the same to /etc/my.cnf file. No good.

尝试了很多其他的东西,我什至不记得了.

Tried a bunch of other things I can't even remember anymore.

另外,我使用的是 Mac 10.10.2 并且安装了 MAMP.根据 MA​​MP,只有 Apache 正在工作(Apache 为绿灯,MySQL 上没有任何内容),但我已经下载了 MySQL,并且根据 System Pref 它正在运行,尽管由于某种原因它不会让我停止 MySQL 服务器(当我点击停止它会停止然后自己重​​新启动).

Also, I'm using Mac 10.10.2 and I have MAMP installed. According to MAMP, only Apache is working (green light by Apache and nothing on MySQL) but I've downloaded MySQL and according to System Pref it is running, although it won't let me stop MySQL server for some reason (when I click stop it would stop then restart again on its own).

请帮忙...

如果重要,这里是 php:

If it matters, here's the php:

<?php
  // 1. Create a database connection
  $dbhost = "localhost";
  $dbuser = "widget_cms";
  $dbpass = "*********";
  $dbname = "widget_corp";
  $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  // Test if connection succeeded
  if(mysqli_connect_errno()) {
    die("Database connection failed: " . 
         mysqli_connect_error() . 
         " (" . mysqli_connect_errno() . ")"
    );
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
    <head>
        <title>Databases</title>
    </head>
    <body>

    </body>
</html>

<?php
  // 5. Close database connection
  mysqli_close($connection);
?>

推荐答案

刚刚找到解决方案!

我刚刚用 127.0.0.1 替换了 localhost.

I just replaced localhost with 127.0.0.1.

奇怪的是,我想我以前试过这个.也许弄乱配置并重试它现在可以工作了.

Oddly enough, I think I've tried this before. Maybe messing around with the config and retrying it got it working now.

这篇关于mysqli_connect(): (HY000/2002): 无法通过套接字连接到本地 MySQL 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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