无法通过 socket '/tmp/mysql.sock' 连接到本地 MySQL 服务器 (2)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22436028/
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
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
提问by user3344382
I have installed MySQL server and trying to connect to it, but getting the error:
我已经安装了 MySQL 服务器并尝试连接到它,但出现错误:
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
I have checked my /tmp directory and there is no mysql.sock. I can't find mysql.sock anywhere. I read that it might be in
我检查了我的 /tmp 目录,没有 mysql.sock。我在任何地方都找不到 mysql.sock。我读到它可能在
/var/lib/mysql/mysql.sock
But I checked there as well and there is even no mysql directory, only some postfix thing inside /lib. Could anyone help me with this problem?
但是我也检查了那里,甚至没有 mysql 目录,只有 /lib 中的一些后缀。有人能帮我解决这个问题吗?
回答by Casey Robinson
Try to start the MySQL server:
尝试启动 MySQL 服务器:
mysql.server start
mysql.server start
回答by keren
I got the same question after updating OS X Yosemite, well the solution is quite simple, check system preference -> mysql, the status was STOP. Just restart it and it works fine on my mac now.
更新 OS X Yosemite 后我遇到了同样的问题,解决方案很简单,检查系统偏好 -> mysql,状态为 STOP。只需重新启动它,它现在在我的 Mac 上运行良好。
回答by Bryan
For MAMP
对于 MAMP
ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
UPDATE: Every time my computer restarts I have to enter this command, so I created a shortcut.
更新:每次我的电脑重新启动时,我都必须输入这个命令,所以我创建了一个快捷方式。
Do the following in terminal type:
在终端类型中执行以下操作:
~: vi ~/.profile
Add
添加
alias ...='source ~/.profile'
alias sockit='sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock'
Save.
节省。
In terminal type:
在终端类型中:
~: ...
to source the .profile config.
~: ...
获取 .profile 配置。
Now in terminal you can just type
现在在终端你可以输入
~: sockit
回答by Nikesh Suwal
Following command resolved my issue:
以下命令解决了我的问题:
sudo chown -R _mysql:mysql /usr/local/var/mysql
sudo mysql.server start
回答by Hos Mercury
After trying all solutions it worked only for me after specifying the host
在尝试了所有解决方案后,它在指定主机后仅对我有用
mysql -u root -p -h127.0.0.1
when asking for password
询问密码时
Enter password:
press enter
按回车
and it will work , if everything is ok as above .
如果一切正常,它将起作用。
回答by Krishnadas PC
After struggling for hours the only thing which worked was
经过几个小时的努力,唯一有效的是
sudo mysql.server start
Then do a secure installation with
然后使用以下命令进行安全安装
mysql_secure_installation
Then connect to the db via
然后通过连接到数据库
mysql -uroot -p
Mysql is installed via homebrew and the version is
Mysql是通过homebrew安装的,版本是
Server version: 5.7.21 Homebrew
Specifying the version might be helpful as the solution may be different based upon the version.
指定版本可能会有所帮助,因为解决方案可能因版本而异。
回答by Rahul
In your mysql config file, which is present in /etc/my.cnf
make the below changes and then restart mysqld
dameon process
在您的 mysql 配置文件中,/etc/my.cnf
进行以下更改,然后重新启动mysqld
dameon 进程
[client]
socket=/var/lib/mysql/mysql.sock
As well check this related thread
也检查这个相关的线程
Can't connect to local MySQL server through socket '/tmp/mysql.sock
回答by Umesh
Following resolved my issue:
以下解决了我的问题:
Check where is your MySQL server is listning to: netstat -nlp If it is listning to TCP then use 127.0.0.1 while connecting to DB instead of "localhost"
检查您的 MySQL 服务器在哪里列出: netstat -nlp 如果它正在列出 TCP,则在连接到 DB 时使用 127.0.0.1 而不是“localhost”
Check MySQL doc here
在此处检查 MySQL 文档
回答by vasanth vasu
Try this it worked for me.
试试这个它对我有用。
sudo /usr/local/mysql/support-files/mysql.server start
须藤/usr/local/mysql/support-files/mysql.server 启动
回答by Cairo
If you are using XAMPP in Mac OS X and have installed MySQL with Homebrew you may have this problem. In XAMPP manager window go to Manage Servers and select MySQL, then click configure and open the configuration file, there you have the socket file path, put the path in your MySQL host config and it should work.
如果您在 Mac OS X 中使用 XAMPP 并且安装了带有 Homebrew 的 MySQL,您可能会遇到这个问题。在 XAMPP 管理器窗口转到管理服务器并选择 MySQL,然后单击配置并打开配置文件,您有套接字文件路径,将路径放入您的 MySQL 主机配置中,它应该可以工作。
It's something like this:
它是这样的:
...
[client]
#password = your_password
port = 3306
socket = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
...
then, for instance in Django:
然后,例如在 Django 中:
...
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "database_name",
"USER": "user",
"PASSWORD": "password",
"HOST": "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock",
"PORT": "",
}
}
...
Hope this helps.
希望这可以帮助。