无法通过套接字连接到本地MySQL服务器

无法通过套接字连接到本地MySQL服务器

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

问题描述

我需要糟糕的帮助...尝试使用php显示数据库时出现此错误:

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

好几天以来,我一直在尝试修复该问题,从遇到同样问题的人那里通读本论坛,并应用这些修复程序(例如 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.根据MAMP的说法,只有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服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 12:28