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

16
我正在按照这个教程在Google云平台上搭建Wordpress网站: https://googlecloudplatform.github.io/appengine-php-wordpress-starter-project/ 以下是需要注意的事项:
  • 我使用的是OSX 10.10.3操作系统的Mac电脑。
  • 我已经安装了适用于Google App Engine软件的PHP SDK。
现在,我正在尝试在我的Mac电脑上安装MySQL服务器。我在此处下载了Mac OS X 10.9 (x86, 64-bit)压缩TAR归档文件: http://dev.mysql.com/downloads/mysql/ 如同教程中所述,我正在终端中输入以下命令行:
/Users/myuser/Downloads/mysql-5.6.24-osx10.9-x86_64/bin/mysql/mysql -u root -p mypassword

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

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


这是在我从 Time Machine 备份中恢复了一个新的 Mac Mini 之后发生的。我不得不卸载 mysql@5.7 并重新安装它才能让它正常工作。虽然有点麻烦,但相对来说还是比较轻松的,因为我的所有配置都被保留了下来。 - Joshua Pinter
11个回答

29

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

mysqld stop
mysql.server start

我遇到了完全相同的问题,使用上述命令解决了它。


谢谢!我安装了Valet+,它停止了服务器,不得不手动启动,感谢您指出这一点。 - Asadullah

7

OSX 10.13.2 High Sierra
mariadb 10.2.12

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

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

为了解决问题,我进行了以下操作:

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

然后我尝试了:

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

并且:

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

解决方案:

~$ 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! 
~$ 

我在自动启停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

经过100个地方的尝试,我终于找到了正确的方法。在我的情况下,我同时安装了Maria DB和MAMP,但它们一直互相冲突。当我按照您上面写的方法操作时,我的mysql似乎无法在CLI中连接,也无法在Sql workbench中连接。请问您能否帮我解决这个问题?它一直在使用Maria DB,而我的核心mysql可以在网站中正常工作,但我无法在CLI中访问它。 - Umar
@Umar,1)$ which mysql.server 返回什么?2)$ which mysql 返回什么?3)~$ ruby -e 'puts ENV["PATH"].split ":"' 返回什么? - 7stud
我遇到了一个错误,提示目录不为空。通过输入“$ which mysql”,我意识到我的mysql文件路径是“/usr/local/OPT/mysql@5.7/”。非常感谢这个提示!我一直在尝试解决这个问题已经两天了。 - Deniz Öztürk

6

在采取任何激进措施之前,请尝试使用回环地址127.0.0.1而不是默认的localhost进行连接。

mysql -h 127.0.0.1 -u root -p

如果您没有指定-h,则默认使用名称为localhost的名称连接到命名管道而不是TCP/IP。如果未启用命名管道,则会出现此错误消息。


6
错误提示: Mac 终端 ERROR 2002 (HY000): 无法通过套接字 '/tmp/mysql.sock' (2) 连接到本地 MySQL 服务器 我在我的MAC + MAMP(pro)设置中是这么解决的: sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock 这将从 /tmp/mysql.sock 创建一个符号链接到 MAMP mysql.sock
现在重新启动 MAMP,然后该错误就不会再次出现了。

1
对于XAMPP使用,请执行以下命令: sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock /tmp/mysql.sock - megamaiku

5

这是一个常见的错误,您可以按照以下步骤进行修复:

enter image description here

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

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

谢谢您的回答。我已经执行了这两行代码,但是我仍然得到了错误提示:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)。 - Robbert
1
尝试这个:sudo /usr/local/mysql/support-files/mysql.server restart - Diogo Cunha
我通过以下命令进行了重启:sudo /usr/local/mysql/support-files/mysql.server restart。但是,如果我尝试安装mysql,现在会出现ERROR 1045 (28000):访问被拒绝,用户'root'@'localhost'(使用密码:YES)。 - Robbert
但如果您不是命令行专家,如果您转到Mac设置,则必须在那里找到MySQL配置。 - Diogo Cunha
让我们在聊天中继续这个讨论 - Robbert
显示剩余3条评论

3

如果您通过Homebrew安装了Mysql,只需运行下面的命令即可:

brew services start mysql

该命令将会很有帮助。


2

一旦安装了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)

您只需要启动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>

2

如果您在从MySQL8降级到MySQL5.7(通过Homebrew)后在MacOSX Catalina上遇到此问题,我已经通过以下方式解决:

警告:在执行以下命令之前,请备份任何数据库

rm -rf /usr/local/var/mysql
rm /usr/local/etc/my.cnf
brew postinstall mysql@5.7 -v
mysqld

1
你可以尝试切换mysql的版本。
以下是在Mac上使用HomeBrew的说明。
首先列出所有mysql的版本:
$ brew list --versions mysql 

切换到旧版本:

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

当我升级到mysql 8.*时,我遇到了同样的问题。 - Đỗ Ngọc Hoan

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

我尝试了你的解决方案,但是没有起作用。这是我所做的让它工作的方法:brew services start mariadb - Niyongabo Eric

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接