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

时间:2015-05-07 11:50:00

标签: mysql macos google-app-engine terminal localhost

我正在按照本教程在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

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

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

11 个答案:

答案 0 :(得分:21)

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

mysqld stop
mysql.server start

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

答案 1 :(得分:5)

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矛盾的:

  

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

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

  2.   
  3. 使用mysqld_safe启动脚本

  4.   
  5. 使用mysql.server启动脚本

  6.         

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

         

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

         

    mysql.server作为标准的SysV样式的init脚本。就这样你   使用带有start和stop参数的脚本,如下所示:

    mysql.server start
    mysql.server stop
    

答案 2 :(得分:4)

错误: 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,然后不应再发生错误。

答案 3 :(得分:3)

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

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

答案 4 :(得分:1)

如果您通过Homebrew安装了Mysql,只需运行以下命令,这将有所帮助。

  

brew服务启动mysql

答案 5 :(得分:1)

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

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

首先列出mysql的所有版本:

$ brew list --versions mysql 

切换到旧版本:

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

答案 6 :(得分:1)

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

答案 7 :(得分:1)

如果您在通过 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

答案 8 :(得分:0)

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

感谢所有能够“eclairer ma lanterne!”的人。

答案 9 :(得分:0)

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

答案 10 :(得分:0)

在进行任何大动作之前,请尝试使用环回地址127.0.0.1而不是默认的localhost进行连接。

mysql -h 127.0.0.1 -u root -p

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