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

问题描述 投票:0回答:12

我正在按照本教程在 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 位)压缩的 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'连接到本地MySQL服务器(2)

mysql macos google-app-engine terminal localhost
12个回答
29
投票

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

mysqld stop
mysql.server start

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


8
投票

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 的最佳说明却在 自动启动和停止 MariaDB:

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

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

  2. 使用mysqld_safe启动脚本

  3. 使用mysql.server启动脚本

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

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

mysql.server 作为标准 SysV 风格的初始化脚本工作。因此你 使用带有开始和停止参数的脚本,如下所示:

mysql.server start
mysql.server stop

6
投票

错误:

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

这会创建从 /tmp/mysql.sock 到 MAMP mysql.sock 的符号链接

现在重新启动 MAMP,错误就不会再出现了。


6
投票

在做任何激烈的事情之前,请尝试使用环回地址

127.0.0.1
而不是默认的
localhost
进行连接。

mysql -h 127.0.0.1 -u root -p

名称

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


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

3
投票

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

brew服务启动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
投票

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

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

rm -rf /usr/local/var/mysql
rm /usr/local/etc/my.cnf
brew postinstall [email protected] -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

1
投票

这对我有用,只需删除文件即可

$ rm /tmp/mysql.sock
然后
$ brew services mariadb restart


0
投票

这是一步一步。找到 pid 并杀死 -9 $PID...

za-2:ec2 za$ mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
za-2:ec2 za$ mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
za-2:ec2 za$ mysql.server status
 SUCCESS! MySQL running (6858)
za-2:ec2 za$ mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
za-2:ec2 za$ mysql.server stop
 ERROR! MySQL server PID file could not be found!
za-2:ec2 za$ mysql.server start
Starting MySQL
.2024-04-06T17:58:09.6NZ mysqld_safe A mysqld process already exists
 SUCCESS! 
za-2:ec2 za$ mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
za-2:ec2 za$ ps aux | grep mysqld
za                7827  12.7  4.2 34720068 350988 s002  R     1:59PM   0:00.64 /usr/local/Cellar/mysql/8.0.28/bin/mysqld --basedir=/usr/local/Cellar/mysql/8.0.28 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql/8.0.28/lib/plugin --log-error=za-2.local.err --pid-file=/usr/local/var/mysql/za-2.local.pid
za                6535   0.0  0.0 34151004   1812 s002  S     1:55PM   0:00.64 /bin/sh /usr/local/Cellar/mysql/8.0.28/bin/mysqld_safe --datadir=/usr/local/var/mysql --pid-file=/usr/local/var/mysql/za-2.local.pid
za                7830   0.0  0.0 34122828    836 s002  S+    1:59PM   0:00.00 grep mysqld
za-2:ec2 za$ kill -9  6535
za-2:ec2 za$ ps aux | grep mysqld
za                8028   0.0  0.0 34130912    700 s002  R+    1:59PM   0:00.00 grep mysqld
za-2:ec2 za$ mysql.server start
Starting MySQL
 SUCCESS! 
za-2:ec2 za$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28 Homebrew

-1
投票

是的,它也适合我...... 但我不明白:在两个 php.ini 文件conf(MAMP 的 apache 和 php)中,套接字路径都很好:

socket=/Applications/MAMP/tmp/mysql/mysql.sock
那么为什么还要寻找
/tmp/mysql.sock
???

感谢所有能够“闪电泡芙妈妈灯笼!”的人

© www.soinside.com 2019 - 2024. All rights reserved.