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

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

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

现在我正在尝试在我的 mac 上安装 MySQL 服务器。我已经在此处下载了 Mac OS X 10.9(x86,64 位)压缩 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

首先终端询问我的密码,当我输入密码时出现以下错误:

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

这是一个常见错误,您可以这样修复它

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

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

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

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

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

现在重启MAMP,错误应该不会再出现了。

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

谢谢大家"eclairer ma lanterne !"

看起来 Mysql 服务器尚未启动。

mysqld stop
mysql.server start

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

OSX 10.13.2 High Sierra
mariadb 10.2.12

当我尝试使用 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>

好的,开始吧:

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 的最佳说明自相矛盾地位于 Starting and Stopping MariaDB Automatically:

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

如果您通过 Homebrew 安装了 Mysql,只需在命令下方 运行,那将会很有帮助。

brew services start mysql

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

以下是在 Mac.

上使用 HomeBrew 的说明

首先列出mysql的所有版本:

$ brew list --versions mysql 

切换到旧版本:

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

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

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

进行连接
mysql -h 127.0.0.1 -u root -p

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

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>

如果您在通过 Homebrew 从 MySQL8 降级到 MySQL5.7 后在 MacOSX Catalina 上遇到此问题,我设法通过以下方法修复了它:

警告:在继续以下命令之前备份所有数据库

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