无法通过套接字自制软件连接到本地 MySQL 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15016376/
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:39:47  来源:igfitidea点击:

Can't connect to local MySQL server through socket homebrew

mysqlhomebrew

提问by socketman

I recently tried installing MySQL with homebrew (brew install mysql) and when I try to run it I get the following error:

我最近尝试使用自制软件 ( brew install mysql)安装 MySQL,当我尝试运行它时,出现以下错误:

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)

There is no /tmp/mysql.socknor a /var/lib/mysql.sock.

没有/tmp/mysql.sock也没有/var/lib/mysql.sock

I've searched and haven't found any mysql.sockfile.

我已经搜索过,但没有找到任何mysql.sock文件。

How can I fix this?

我怎样才能解决这个问题?

回答by AAGD

When you got the server running via

当您通过以下方式运行服务器时

mysql.server start

mysql.server start

you should see the socket in /tmp/mysql.sock. However, the system seems to expect it in /var/mysql/mysql.sock. To fix this, you have to create a symlink in /var/mysql:

您应该在/tmp/mysql.sock 中看到套接字。但是,系统似乎希望它在/var/mysql/mysql.sock 中。要解决此问题,您必须在/var/mysql 中创建一个符号链接:

sudo mkdir /var/mysql

sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

sudo mkdir /var/mysql

sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

This solved it for me. Now my phpMyAdmin works happily with localhostand127.0.0.1.

这为我解决了它。现在我的 phpMyAdmin 可以与localhost127.0.0.1一起愉快地工作。

Credit goes to Henry

归功于亨利

回答by jesuis

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 Royal.O

I had some directories left from another mysql(8.0) installation, that were not removed.

我从另一个 mysql(8.0) 安装中留下了一些目录,这些目录没有被删除。

I solved this by doing the following:

我通过执行以下操作解决了这个问题:

First uninstall mysql

首先卸载mysql

brew uninstall [email protected]

Delete the folders/files that were not removed

删除未删除的文件夹/文件

rm -rf /usr/local/var/mysql
rm /usr/local/etc/my.cnf

Reinstall mysql and link it

重新安装mysql并链接

brew install [email protected]
brew link --force [email protected]

Enable and start the service

启用并启动服务

brew services start [email protected]

回答by Iswanto San

Try to connect using "127.0.0.1" instead "localhost".

尝试使用“127.0.0.1”而不是“localhost”进行连接。

回答by Berk

1) If you are able to see "mysql stopped" when you run below command;

1)如果您在运行以下命令时能够看到“mysql已停止”;

brew services list

2) and if you are able to start mysql with below command;

2)如果您能够使用以下命令启动mysql;

mysql server start

this means; mysql is able to start manually, but it doesn't start automatically when the operating system is started. Adding mysql to services will fix this problem. To do so, you can run below command;

这意味着; mysql 可以手动启动,但在操作系统启动时不会自动启动。将 mysql 添加到服务将解决此问题。为此,您可以运行以下命令;

brew services start mysql

After that, you may restart your operating system and try connecting to mysql to see if it started automatically. I did the same and stop receiving below error;

之后,您可以重新启动操作系统并尝试连接到 mysql 以查看它是否自动启动。我做了同样的事情并停止收到以下错误;

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)

I hope this helps.

我希望这有帮助。

回答by Tyler Curtis Jowers

The file /tmp/mysql.sockis probably a Named-Pipe, since it's in a temporary folder. A named pipe is a Special-File that never gets permanently stored.

该文件/tmp/mysql.sock可能是一个命名管道,因为它位于一个临时文件夹中。命名管道是一个永远不会被永久存储的特殊文件。

If we make two programs, and we want one program to send a message to another program, we could create a text file. We have one program write something in the text file and the other program read what our other program wrote. That's what a pipe is, except it doesn't write the file to our computer hard disk, IE doesn't permanently store the file (like we do when we create a file and save it.)

如果我们制作两个程序,并且我们希望一个程序向另一个程序发送消息,我们可以创建一个文本文件。我们有一个程序在文本文件中写一些东西,另一个程序读取我们另一个程序写的东西。这就是管道,除了它不会将文件写入我们的计算机硬盘之外,IE 不会永久存储文件(就像我们创建文件并保存时所做的那样。)

A Socket is the exact same as a Pipe. The difference is that Sockets are usually used over a network -- between computers. A Socket sends information to another computer, or receives information from another computer. Both Pipes and Sockets use a temporary file to share so that they can 'communicate'.

套接字与管道完全相同。不同之处在于套接字通常在网络上使用——在计算机之间。Socket 向另一台计算机发送信息,或从另一台计算机接收信息。管道和套接字都使用临时文件进行共享,以便它们可以“通信”。

It's difficult to discern which one MySql is using in this case. Doesn't matter though.

在这种情况下很难辨别 MySql 使用的是哪一个。不过没关系。

The command mysql.server startshould get the 'server' (program) running its infinite loop that will create that special-file and wait for changes (listenfor writes).

该命令mysql.server start应该让“服务器”(程序)运行其无限循环,该循环将创建该特殊文件并等待更改(listen用于写入)。

After that, a common issue might be that the MySql program doesn't have permission to create a file on your machine, so you might have to give it root privileges

之后,一个常见的问题可能是 MySql 程序没有权限在您的机器上创建文件,因此您可能必须为其授予 root 权限

sudo mysql.server start

回答by GnrlBzik

After installing macos mojave, had to wipe mysql folder under /usr/local/var/mysqland then reinstall via brew install mysqlotherwise permission related things would come up all over the place.

安装 macos mojave 后,必须擦除下面的 mysql 文件夹/usr/local/var/mysql,然后通过brew install mysql其他权限重新安装,否则会到处出现与权限相关的事情。

回答by dingalapadum

Since I spent quite some time trying to solve this and always came back to this page when looking for this error, I'll leave my solution here hoping that somebody saves the time I've lost. Although in my case I am using mariadb rather than MySql, you might still be able to adapt this solution to your needs.

由于我花了相当多的时间试图解决这个问题,并且在查找此错误时总是返回此页面,因此我将解决方案留在这里,希望有人能节省我失去的时间。尽管就我而言,我使用的是 mariadb 而不是 MySql,但您仍然可以根据自己的需要调整此解决方案。

My problem

我的问题

is the same, but my setup is a bit different (mariadb instead of mysql):

是一样的,但我的设置有点不同(mariadb 而不是 mysql):

Installed mariadb with homebrew

用自制软件安装 mariadb

$ brew install mariadb

Started the daemon

启动守护进程

$ brew services start mariadb

Tried to connect and got the above mentioned error

尝试连接并收到上述错误

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

My solution

我的解决方案

find out which my.cnffiles are used by mysql(as suggested in this comment):

找出my.cnf使用的文件mysql(如本评论中所建议):

$ mysql --verbose --help | grep my.cnf
/usr/local/etc/my.cnf ~/.my.cnf
                        order of preference, my.cnf, $MYSQL_TCP_PORT,

check where the Unix socket file is running (almost as described here):

检查Unix套接字文件运行的位置(几乎为描述这里):

$ netstat -ln | grep mariadb
.... /usr/local/mariadb/data/mariadb.sock

(you might want to grep mysqlinstead of mariadb)

(你可能想要grep mysql代替 mariadb)

Add the socket file you found to ~/.my.cnf(create the file if necessary)(assuming ~/.my.cnfwas listed when running the mysql --verbose ...-command from above):

添加您找到的套接字文件~/.my.cnf(如有必要,请创建该文件)(假设~/.my.cnfmysql --verbose ...从上面运行-command时已列出):

[client]
socket = /usr/local/mariadb/data/mariadb.sock

Restart your mariadb:

重启你的 mariadb:

$ brew services restart mariadb

After this I could run mysql and got:

在此之后,我可以运行 mysql 并得到:

$ mysql -uroot
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

So I run the command with superuser privileges instead and after entering my password I got:

所以我用超级用户权限运行命令,输入密码后我得到:

$ sudo mysql -uroot
MariaDB [(none)]>


Notes:

笔记:

  1. I'm not quite sure about the groups where you have to add the socket, first I had it [client-server] but then I figured [client] should be enough. So I changed it and it still works.

  2. When running mariadb_config | grep socketI get: --socket [/tmp/mysql.sock]which is a bit confusing since it seems that /usr/local/mariadb/data/mariadb.sockis the actual place (at least on my machine)

  3. I wonder where I can configure the /usr/local/mariadb/data/mariadb.sockto actually be /tmp/mysql.sockso I can use the default settings instead of having to edit my .my.cnf(but I'm too tired now to figure that out...)

  4. At some point I also did things mentioned in other answers before coming up with this.

  1. 我不太确定您必须添加套接字的组,首先我拥有它 [client-server] 但后来我认为 [client] 应该足够了。所以我改变了它,它仍然有效。

  2. 运行时mariadb_config | grep socket我得到: --socket [/tmp/mysql.sock]这有点令人困惑,因为它似乎/usr/local/mariadb/data/mariadb.sock是实际位置(至少在我的机器上)

  3. 我想知道我可以在哪里配置它/usr/local/mariadb/data/mariadb.sock/tmp/mysql.sock这样我就可以使用默认设置而不必编辑我的.my.cnf(但我现在太累了,无法弄清楚......)

  4. 在某些时候,在提出这个之前,我还做了其他答案中提到的事情。

回答by gouravtiwari21

I got the same error and this is what helped me:

我遇到了同样的错误,这对我有帮助:

$ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
$launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
$mysql -uroot
mysql>

回答by Kelley Robinson

You'll need to run mysql_install_db- easiest way is if you're in the install directory:

您需要运行mysql_install_db- 最简单的方法是如果您在安装目录中:

$ cd /usr/local/Cellar/mysql/<version>/ 
$ mysql_install_db

Alternatively, you can feed mysql_install_dba basedirparameter like the following:

另外,你可以养活mysql_install_db一个basedir类似以下参数:

$ mysql_install_db --basedir="$(brew --prefix mysql)"