Mac 终端 ERROR 2002 (HY000): 无法通过 socket '/tmp/mysql.sock' 连接到本地 MySQL 服务器 (2)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30100222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:59:10  来源:igfitidea点击:

Mac terminal ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

mysqlmacosgoogle-app-engineterminallocalhost

提问by Robbert

I am following this tutorial to setup a Wordpress website on Google cloud: https://googlecloudplatform.github.io/appengine-php-wordpress-starter-project/

我正在按照本教程在 Google 云上设置 Wordpress 网站:https: //googlecloudplatform.github.io/appengine-php-wordpress-starter-project/

  • I am working on a Mac with OSX 10.10.3.
  • I have installed the PHP SDK for Google App Engine software.
  • 我正在使用 OSX 10.10.3 的 Mac 上工作。
  • 我已经为 Google App Engine 软件安装了 PHP SDK。

Now I am trying to install MySQL server on my mac. I have downloaded the Mac OS X 10.9 (x86, 64-bit), Compressed TAR Archive here: http://dev.mysql.com/downloads/mysql/

现在我正在尝试在我的 Mac 上安装 MySQL 服务器。我已经在此处下载了 Mac OS X 10.9(x86,64 位),压缩的 TAR 存档:http: //dev.mysql.com/downloads/mysql/

As the tutorial says, I am command the following line in my Terminal:

正如教程所说,我在终端中命令以下行:

/Users/myuser/Downloads/mysql-5.6.24-osx10.9-x86_64/bin/mysql/mysql -u root -p mypassword

First the terminal asked for my password, and when I enter this the following error occurs:

首先终端要求我输入密码,当我输入时出现以下错误:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

ERROR 2002 (HY000): 无法通过 socket '/tmp/mysql.sock' 连接到本地 MySQL 服务器 (2)

采纳答案by Diogo Cunha

It's a common error , you can fix it like this

这是一个常见的错误,你可以像这样修复它

enter image description here

在此处输入图片说明

You can remove root password with this command sequence:

您可以使用以下命令序列删除 root 密码:

$ mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("") where User='root';
mysql> flush privileges;
mysql> quit

回答by Prakhar Agarwal

Looks like Mysql server has not started.

看起来Mysql服务器还没有启动。

mysqld stop
mysql.server start

Had exactly the same issue, used the above command to fix it.

有完全相同的问题,使用上面的命令来修复它。

回答by desloovere_j

The error: Mac terminal ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

错误: Mac terminal ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

How I solved this on my MAC + MAMP (pro) setup:

我是如何在我的 MAC + MAMP(专业版)设置上解决这个问题的:

sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

Which creates a symlink from /tmp/mysql.sock to the MAMP mysql.sock

它创建了一个从 /tmp/mysql.sock 到 MAMP mysql.sock 的符号链接

Now restart MAMP and then the error should not occur again.

现在重新启动 MAMP,然后错误不应再次发生。

回答by 7stud

OSX 10.13.2 High Sierra
mariadb 10.2.12

OSX 10.13.2 High Sierra
mariadb 10.2.12

I got the exact same error when I tried to use mariadb, which I installed with homebrew. The first thing I did after installing was:

当我尝试使用mariadb我用自制软件安装的时,我遇到了完全相同的错误。安装后我做的第一件事是:

$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through 
socket '/tmp/mysql.sock' (2)

To troubleshoot, I did:

为了排除故障,我做了:

~$ which mysql
/usr/local/mysql/bin/mysql

and then I tried:

然后我试过:

~$ mysql -u 7stud -p test
 Enter password:
 ERROR 2002 (HY000): Can't connect to local MySQL server 
 through socket '/tmp/mysql.sock' (2)

and:

和:

~$ mysql -u -p
ERROR 2002 (HY000): Can't connect to local MySQL server 
through socket '/tmp/mysql.sock' (2) 

The solution:

解决方案

~$ mysql.server start
Starting MySQL
.180127 00:24:48 mysqld_safe Logging to '/usr/local/var/mysql/MyMBP.home.err'.
180127 00:24:48 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
 SUCCESS! 

~$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.5.5-10.2.12-MariaDB Homebrew

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Okay, let's go:

好,我们走:

mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)

mysql> CREATE DATABASE my_db;
Query OK, 1 row affected (0.00 sec)

mysql> use my_db;
Database changed

mysql> show tables;
Empty set (0.01 sec)

mysql> CREATE TABLE people (
    -> id INT(12) not null auto_increment primary key, 
    -> name VARCHAR(40), 
    -> info VARCHAR(100)
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> describe people;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(12)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(40)  | YES  |     | NULL    |                |
| info  | varchar(100) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> INSERT INTO people(name, info) VALUES("Joe", "a b c") ;
Query OK, 1 row affected (0.01 sec)

mysql> select * from people;
+----+------+-------+
| id | name | info  |
+----+------+-------+
|  1 | Joe  | a b c |
+----+------+-------+
1 row in set (0.00 sec)

mysql> INSERT INTO people(name, info) VALUES("Beth", "1 2 3") ;
Query OK, 1 row affected (0.00 sec)

mysql> select * from people;
+----+-------+-------+
| id | name  | info  |
+----+-------+-------+
|  1 | Joe   | a b c |
|  2 | Beth  | 1 2 3 |
+----+-------+-------+
2 rows in set (0.00 sec)

mysql> quit
Bye

~$ mysql.server stop
Shutting down MySQL
. SUCCESS! 
~$ 

The best instructions I found for manually starting and stopping mariadb are paradoxically at Starting and Stopping MariaDB Automatically:

我发现手动启动和停止 mariadb 的最佳说明自相矛盾的是启动和停止 MariaDB 自动

You have the option of starting the mysqld server several different ways:

  1. Run or invoke mysqld itself. An example of doing this is described more in Running MariaDB from the Source Directory.

  2. Use the mysqld_safe startup script

  3. Use the mysql.server startup script

The mysql.server script starts mysqld by first changing to the MariaDB install directory and then calling mysqld_safe. Adding an appropriate user line to the [mysqld] group in your my.cnf file will cause the server to be run as that user.

If you have installed MariaDB to a non-standard location, you may need to edit the mysql.server script to get it to work right.

mysql.server works as a standard SysV-style init script. As such you use the script with start and stop arguments like so:

mysql.server start
mysql.server stop

您可以选择以几种不同的方式启动 mysqld 服务器:

  1. 运行或调用 mysqld 本身。从源目录运行 MariaDB 中详细介绍了执行此操作的示例。

  2. 使用 mysqld_safe 启动脚本

  3. 使用 mysql.server 启动脚本

mysql.server 脚本通过首先更改到 MariaDB 安装目录然后调用 mysqld_safe 来启动 mysqld。将适当的用户行添加到 my.cnf 文件中的 [mysqld] 组将导致服务器以该用户身份运行。

如果您已将 MariaDB 安装到非标准位置,则可能需要编辑 mysql.server 脚本以使其正常工作。

mysql.server 作为标准的 SysV 风格的 init 脚本工作。因此,您可以使用带有 start 和 stop 参数的脚本,如下所示:

mysql.server start
mysql.server stop

回答by Michael L Perry

Before doing anything drastic, try connecting using the loopback address 127.0.0.1instead of the default localhost.

在做任何激烈的事情之前,请尝试使用回送地址127.0.0.1而不是默认的localhost.

mysql -h 127.0.0.1 -u root -p

The name localhost, which would be used by default if you don't specify -h, connects over named pipes rather than TCP/IP. This is the error message you see if named pipes are not enabled.

localhost如果您不指定-h,默认情况下将使用name ,它通过命名管道而不是 TCP/IP 连接。这是您在未启用命名管道时看到的错误消息。

回答by Long Nguyen

If you installed Mysql through Homebrew, just run below command, that will be helpful.

如果你通过 Homebrew 安装了 Mysql,只需运行下面的命令,这会很有帮助。

brew services start mysql

brew 服务启动 mysql

回答by ?? Ng?c Hoan

You could try to switch the version of the mysql.

您可以尝试切换mysql的版本。

Below is the instruction for using HomeBrew on Mac.

以下是在 Mac 上使用 HomeBrew 的说明。

First list all the versions of mysql:

首先列出mysql的所有版本:

$ brew list --versions mysql 

Switch to an older version:

切换到旧版本:

$ brew services stop mysql
$ brew switch mysql 5.7.20
$ brew services start mysql

回答by Agus Sudarmanto

This work for me, just delete the file $ rm /tmp/mysql.sockthen $ brew services mariadb restart

这对我有用,只需删除文件 $ rm /tmp/mysql.sock然后$ brew services mariadb restart

回答by nevosial

Homebrew will not start the mysql server once it is installed and hence you get the error.

Homebrew 安装后不会启动 mysql 服务器,因此您会收到错误消息。

$ brew services list
Name  Status  User Plist
mysql stopped

$ mysql -u root
 ERROR 2002 (HY000): Can't connect to local MySQL server through socket 
 '/tmp/mysql.sock' (2)

$ mysql -uroot
 ERROR 2002 (HY000): Can't connect to local MySQL server through socket 
 '/tmp/mysql.sock' (2)

You just need to start the mysql service and then connect.

你只需要启动mysql服务,然后连接。

$ mysql.server start
Starting MySQL
. SUCCESS!

$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19 Homebrew

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

回答by nina

yes it works for me too.... but I don't understand : in both of php.ini files conf (apache and php of MAMP) path of socket is good : socket=/Applications/MAMP/tmp/mysql/mysql.sockso why still looking for /tmp/mysql.sock???

是的,它也适用于我......但我不明白:在 php.ini 文件中 conf(MAMP 的 apache 和 php)的套接字路径都很好:那socket=/Applications/MAMP/tmp/mysql/mysql.sock为什么还要寻找/tmp/mysql.sock???

Thank's for everyone who can "eclairer ma lanterne !"

感谢所有可以“eclairer ma lanterne”的人!