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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15450091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:57:35  来源:igfitidea点击:

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

mysqlmacos

提问by shrewdbeans

I installed MySQL on Mac OS X Mountain Lion with homebrew install mysql, but when I tried mysql -u rootI got the following error:

我在 Mac OS X Mountain Lion 上安装了 MySQL homebrew install mysql,但是当我尝试时mysql -u root出现以下错误:

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

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

What does this error mean? How can I fix it?

这个错误是什么意思?我该如何解决?

采纳答案by shammerw0w

It's probably because MySQL is installed but not yet running.

这可能是因为 MySQL 已安装但尚未运行。

To verify that it's running, open up Activity Monitor and under "All Processes", search and verify you see the process "mysqld".

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

You can start it by installing "MySQL.prefPane".

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

Here is the complete tutorial which helped me: http://obscuredclarity.blogspot.in/2009/08/install-mysql-on-mac-os-x.html

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

回答by Md Mazedul Islam Khan

You'll need to start MySQL before you can use the mysqlcommand on your terminal. To do this, run brew services start mysql. By default, brew installs the MySQL database without a root password. To secure it run: mysql_secure_installation.

您需要先启动 MySQL,然后才能mysql在终端上使用该命令。为此,请运行brew services start mysql. 默认情况下,brew 在没有 root 密码的情况下安装 MySQL 数据库。为了确保它运行:mysql_secure_installation.

To connect run: mysql -uroot. rootis the username name here.

连接运行:mysql -urootroot是这里的用户名。

回答by Nirojan Selvanathan

This happened after the homebrew install and occurs due to permission issues. The following commands fixed the issue.

这发生在自制软件安装之后,并且由于权限问题而发生。以下命令修复了该问题。

sudo chown -R _mysql:mysql /usr/local/var/mysql

sudo mysql.server start

回答by Gaurav Agarwal

Run: brew info mysql

跑: brew info mysql

And follow the instructions. From the description in the formula:

并按照说明进行操作。从公式中的描述来看:

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

Hope this helps.

希望这可以帮助。

回答by Ithar

As others have pointed out this is because MySQL is installed but the service isn't running. There are many ways to start the MySQL service and what worked for me is the below.

正如其他人指出的那样,这是因为 MySQL 已安装但服务未运行。有很多方法可以启动 MySQL 服务,下面是对我有用的方法。

To start the service:

启动服务:

  1. Go to "System Preference"
  2. At the bottom pane there should be MySql icon.
  3. Double click that to launch the 'MySQL Server Status' and press the button 'Start MySQL Server'
  1. 转到“系统偏好设置”
  2. 在底部窗格中应该有 MySql 图标。
  3. 双击它以启动“MySQL 服务器状态”,然后按“启动 MySQL 服务器”按钮

My env:

我的环境:

Mac Yosemite 10.10.3

Mac 优胜美地 10.10.3

Installed Package: /Volumes/mysql-advanced-5.6.24-osx10.8-x86_64

安装包:/Volumes/mysql-advanced-5.6.24-osx10.8-x86_64

回答by mondieki

Solutions revolve around:

解决方案围绕:

  • changing MySQL's permissions

    sudo chown -R _mysql:mysql /usr/local/var/mysql
    
  • Starting a MySQL process

    sudo mysql.server start
    
  • 更改 MySQL 的权限

    sudo chown -R _mysql:mysql /usr/local/var/mysql
    
  • 启动 MySQL 进程

    sudo mysql.server start
    

Just to add on a lot of great and useful answers that have been provided hereand from many different posts, try specifying the hostif the above commands did not resolve this issue for you, i.e

只是为了添加这里和许多不同帖子中提供的许多很棒和有用的答案,如果上述命令没有为您解决此问题,请尝试指定主机,即

mysql -u root -p h127.0.0.1

回答by yndolok

Warning - this method will remove all of your databases in the /usr/local/var/mysqlfolder

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

I had MySQL installed with Homebrew, and the only thing that fixed this for me was re-installing MySQL.

我用 Homebrew 安装了 MySQL,唯一能解决这个问题的是重新安装 MySQL。

On my company laptop, I didn't have permission to uninstall MySQL from my computer via Homebrew:

在我公司的笔记本电脑上,我无权通过 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

So instead, I removed and reinstalled MySQL manually:

因此,我手动删除并重新安装了 MySQL:

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

And that worked!

那奏效了!

回答by ggentzke

Below I'm including the latest instructions from brew install mysqlso newer searches for this issue can benefit:

下面我包含了最新的说明,brew install mysql因此对这个问题的较新搜索可以受益:

$ brew install mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-5.6.26.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring mysql-5.6.26.yosemite.bottle.1.tar.gz

To connect:
    mysql -uroot

To have launchd start mysql at login:
  ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
  mysql.server start

In my case I loaded mysql now via launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plistand was then able to launch $ mysqland be on my way.

就我而言,我现在通过加载 mysql launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist,然后就可以启动$ mysql并在路上了。

I hope this helps recent troubleshooters!

我希望这有助于最近的疑难解答!

回答by Prakhar Agarwal

Looks like your mysql server is not started. I usually run the stop command and then start it again:

看起来你的 mysql 服务器没有启动。我通常运行停止命令,然后再次启动它:

mysqld stop
mysql.server start

Same error, and this works for me.

同样的错误,这对我有用。

回答by pmventura

This fixed my issue when I restarted the mysql service. Just run:

当我重新启动 mysql 服务时,这解决了我的问题。赶紧跑:

brew services start mysql