我在Mac OS X Mountain Lion上安装了MySQL,但当我尝试MySQL -u root时,我得到了以下错误:

错误2002 (HY000):无法通过套接字/tmp/ MySQL连接到本地MySQL服务器。袜子”(2)

这个错误意味着什么?我该怎么解决呢?


当前回答

在做了几个小时之后,对我有用的是 进入/etc/mysql/,编辑my.cnf文件。添加以下内容

[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock

其他回答

警告:此方法将删除/usr/local/var/mysql文件夹中的所有数据库

我在Homebrew上安装了MySQL,唯一解决这个问题的方法就是重新安装MySQL。

在我公司的笔记本电脑上,我没有通过Homebrew卸载MySQL的权限:

$ brew uninstall mysql --ignore-dependencies
Uninstalling /usr/local/Cellar/mysql/8.0.12... (255 files, 233.0MB)
Error: Permission denied @ dir_s_rmdir - /usr/local/Cellar/mysql/8.0.12

所以,我手动删除并重新安装MySQL:

$ sudo rm -rf /usr/local/Cellar/mysql
$ brew cleanup
$ sudo rm -rf /usr/local/var/mysql
$ brew install mysql

这奏效了!

这可能是因为MySQL已经安装但还没有运行。

要验证它正在运行,打开活动监视器,在“所有进程”下,搜索并验证你看到了进程“mysqld”。

你可以通过安装“MySQL.prefPane”来启动它。

以下是帮助我的完整教程: http://obscuredclarity.blogspot.in/2009/08/install-mysql-on-mac-os-x.html

我找到了解决问题的办法。这确实是因为我的MySQL服务器没有运行。

这是由于MySQL没有正确设置在我的机器上,因此不能运行。

为了解决这个问题,我使用了一个在Mac OSX Mountain Lion上安装MySQL的脚本,该脚本必须安装丢失的文件。

链接如下:http://code.macminivault.com/

重要提示:该脚本将根密码设置为随机生成的字符串,保存在Desktop上,因此请注意不要删除该文件并记录密码。它还会在您的系统首选项中安装MySQL管理器。我也不确定是否删除任何现有的数据库,所以要小心。

在做了几个小时之后,对我有用的是 进入/etc/mysql/,编辑my.cnf文件。添加以下内容

[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock

在我的mac m1 macOS Monterey上安装MySQL后,使用brew安装MySQL,我得到了以下信息:

[System] [MY-013169] [Server] /opt/homebrew/Cellar/mysql/8.0.27_1/bin/mysqld (mysqld 8.0.27) initializing of server in progress as process 3624

[ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.

[ERROR] [MY-013236] [Server] The designated data directory /opt/homebrew/var/mysql/ is unusable. You can remove all files that the server added to it.

[ERROR] [MY-010119] [Server] Aborting

[System] [MY-010910] [Server] /opt/homebrew/Cellar/mysql/8.0.27_1/bin/mysqld: Shutdown complete (mysqld 8.0.27)  Homebrew.

还有这个警告:

Warning: The post-install step did not complete successfully You can
try again using:   brew postinstall mysql

之后,我尝试使用brew services启动MySQL,我得到了这个错误:

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

我设法修复它使用:

> rm -rf ~/opt/homebrew/var/mysql/
> brew postinstall mysql

现在您可以使用mysql -uroot或mysql -uroot -p连接。