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

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

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


当前回答

在我的案例中,这只是删除一个锁文件的问题。

sudo rm -f /tmp/mysql.sock.lock

其他回答

在我的案例中,这只是删除一个锁文件的问题。

sudo rm -f /tmp/mysql.sock.lock

执行命令:brew info mysql

按照说明做。由公式中的描述可知:

Set up databases to run AS YOUR USER ACCOUNT with:
    unset TMPDIR
    mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

To set up base tables in another folder, or use a different user to run
mysqld, view the help for mysql_install_db:
    mysql_install_db --help

and view the MySQL documentation:
  * http://dev.mysql.com/doc/refman/5.5/en/mysql-install-db.html
  * http://dev.mysql.com/doc/refman/5.5/en/default-privileges.html

在我的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连接。

这个问题与/usr/local/var/mysql文件夹访问有关,我删除这个文件夹并重新安装mysql。

用brew卸载mysql: 卸载mysql 执行rm -r /usr/local/var/mysql Brew install mysql@8.0 Mysql -u root

这个解决方案很适合我! 但是你丢失了所有的数据库!警告!

运行sudo chown _mysql /usr/local/var/mysql/*在尝试了所有这些SO答案中的大量选项后终于帮助了我。权限问题可能是由于机器关闭不当引起的。