无法通过套接字’/tmp/mysql.sock连接到本地MySQL服务器

问题:无法通过套接字’/tmp/mysql.sock连接到本地MySQL服务器

当我在测试套件中尝试连接到本地MySQL服务器时,它失败并显示以下错误:

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

但是,我始终可以通过运行命令行mysql程序连接到MySQL 。A ps aux | grep mysql显示服务器正在运行,并 stat /tmp/mysql.sock确认套接字存在。此外,如果我在except该异常的子句中打开调试器,则可以使用完全相同的参数可靠地进行连接。

这个问题可以相当可靠地重现,但是似乎不是100%,因为每当我遇到一个蓝色月亮时,我的测试套件实际上都运行了而没有遇到此错误。当我尝试使用sudo dtruss它时,它没有复制。

所有的客户端代码都在Python中,尽管我不知道这是如何相关的。

切换为使用主机127.0.0.1会产生错误:

DatabaseError: Can't connect to MySQL server on '127.0.0.1' (61)

When I attempted to connect to a local MySQL server during my test suite, it fails with the error:

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

However, I’m able to at all times, connect to MySQL by running the command line mysql program. A ps aux | grep mysql shows the server is running, and stat /tmp/mysql.sock confirm that the socket exists. Further, if I open a debugger in except clause of that exception, I’m able to reliably connect with the exact same parameters.

This issue reproduces fairly reliably, however it doesn’t appear to be 100%, because every once in a blue moon, my test suite does in fact run without hitting this error. When I attempted to run with sudo dtruss it did not reproduce.

All the client code is in Python, though I can’t figure how that’d be relevant.

Switching to use host 127.0.0.1 produces the error:

DatabaseError: Can't connect to MySQL server on '127.0.0.1' (61)

回答 0

sudo /usr/local/mysql/support-files/mysql.server start 

这对我有用。但是,如果这不起作用,请确保mysqld正在运行并尝试连接。

sudo /usr/local/mysql/support-files/mysql.server start 

This worked for me. However, if this doesnt work then make sure that mysqld is running and try connecting.


回答 1

MySQL手册的相关部分在这里。我首先要完成列出的调试步骤。

另外,请记住,在这种情况下,localhost和127.0.0.1是不同的:

  • 如果host设置为localhost,则使用套接字或管道。
  • 如果将host设置为127.0.0.1,则客户端将被强制使用TCP / IP。

因此,例如,您可以检查数据库是否正在侦听TCP连接vi netstat -nlp。它似乎正在侦听TCP连接,因为您说这mysql -h 127.0.0.1很好。要检查是否可以通过套接字连接到数据库,请使用mysql -h localhost

如果以上方法均无济于事,那么您可能需要发布有关MySQL配置,实例化连接的确切方式等的更多详细信息。

The relevant section of the MySQL manual is here. I’d start by going through the debugging steps listed there.

Also, remember that localhost and 127.0.0.1 are not the same thing in this context:

  • If host is set to localhost, then a socket or pipe is used.
  • If host is set to 127.0.0.1, then the client is forced to use TCP/IP.

So, for example, you can check if your database is listening for TCP connections vi netstat -nlp. It seems likely that it IS listening for TCP connections because you say that mysql -h 127.0.0.1 works just fine. To check if you can connect to your database via sockets, use mysql -h localhost.

If none of this helps, then you probably need to post more details about your MySQL config, exactly how you’re instantiating the connection, etc.


回答 2

对我来说,问题是我没有运行mysql服务器。首先运行服务器,然后执行mysql

$ mysql.server start
$ mysql -h localhost -u root -p

For me the problem was I wasn’t running MySQL Server. Run server first and then execute mysql.

$ mysql.server start
$ mysql -h localhost -u root -p

回答 3

当我的开发人员安装了堆栈管理器(如MAMP)并在非标准位置预先安装了MySQL时,就已经在我的商店中看到这种情况。

在您的终端运行

mysql_config --socket

这将为您提供袜子文件的路径。走那条路,并在您的数据库主机参数中使用它。

您需要做的就是指出您的

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'test',
        'PASSWORD': 'test',
        'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
        'PORT': '',
    },
}

注意

which mysql_config如果您以某种方式在计算机上安装了多个mysql服务器实例,则也可以运行,您可能连接了错误的实例。

I’ve seen this happen at my shop when my devs have a stack manager like MAMP installed that comes preconfigured with MySQL installed in a non standard place.

at your terminal run

mysql_config --socket

that will give you your path to the sock file. take that path and use it in your DATABASES HOST paramater.

What you need to do is point your

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'test',
        'PASSWORD': 'test',
        'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
        'PORT': '',
    },
}

NOTE

also run which mysql_config if you somehow have multiple instances of mysql server installed on the machine you may be connecting to the wrong one.


回答 4

我只是将HOSTfrom 更改为localhost127.0.0.1并且效果很好:

# settings.py of Django project
...

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'username',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '',
},
...

I just changed the HOST from localhost to 127.0.0.1 and it works fine:

# settings.py of Django project
...

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'username',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '',
},
...

回答 5

何时,如果您在Mac OSx中丢失了守护进程mysql,但在其他路径中(例如在private / var中存在),请执行以下命令

1)

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

2)使用以下命令重新启动与mysql的连接:

mysql -u username -p -h host databasename

也适用于mariadb

When, if you lose your daemon mysql in mac OSx but is present in other path for exemple in private/var do the following command

1)

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

2) restart your connexion to mysql with :

mysql -u username -p -h host databasename

works also for mariadb


回答 6

在终端中运行以下cmd

/ usr / local / mysql / bin / mysqld_safe

然后重新启动机器以生效。有用!!

Run the below cmd in terminal

/usr/local/mysql/bin/mysqld_safe

Then restart the machine to take effect. It works!!


回答 7

使用lsof命令检查mysql进程的打开文件数。

增加打开文件的限制,然后再次运行。

Check number of open files for the mysql process using lsof command.

Increase the open files limit and run again.


回答 8

在尝试了其中一些解决方案但没有成功之后,这对我有用:

  1. 重启系统
  2. mysql.server启动
  3. 成功!

After attempting a few of these solutions and not having any success, this is what worked for me:

  1. Restart system
  2. mysql.server start
  3. Success!

回答 9

这可能是以下问题之一。

  1. 错误的mysql锁定。解决方案:您必须通过以下方式找出正确的mysql套接字,

mysqladmin -p变量| grep套接字

然后将其放在您的数据库连接代码中:

pymysql.connect(db='db', user='user', passwd='pwd', unix_socket="/tmp/mysql.sock")

/tmp/mysql.sock是grep返回的

2.不正确的mysql端口解决方案:您必须找出正确的mysql端口:

mysqladmin -p variables | grep port

然后在您的代码中:

pymysql.connect(db='db', user='user', passwd='pwd', host='localhost', port=3306)

3306是从grep返回的端口

我认为第一种选择可以解决您的问题。

This may be one of following problems.

  1. Incorrect mysql lock. solution: You have to find out the correct mysql socket by,

mysqladmin -p variables | grep socket

and then put it in your db connection code:

pymysql.connect(db='db', user='user', passwd='pwd', unix_socket="/tmp/mysql.sock")

/tmp/mysql.sock is the returned from grep

2.Incorrect mysql port solution: You have to find out the correct mysql port:

mysqladmin -p variables | grep port

and then in your code:

pymysql.connect(db='db', user='user', passwd='pwd', host='localhost', port=3306)

3306 is the port returned from the grep

I think first option will resolve your problem.


回答 10

对于通过自制软件从5.7升级到8.0的用户,此错误很可能是由于升级未完成引起的。就我而言,mysql.server start出现以下错误:

错误!服务器退出而不更新PID文件

然后,我通过检查了日志文件cat /usr/local/var/mysql/YOURS.err | tail -n 50

InnoDB:不支持崩溃后升级。

如果您在同一条船上,请首先mysql@5.7通过自制程序安装,停止服务器,然后再次启动8.0系统。

brew install mysql@5.7

/usr/local/opt/mysql@5.7/bin/mysql.server start
/usr/local/opt/mysql@5.7/bin/mysql.server stop

然后,

mysql.server start

这将使您的MySQL(8.0)再次正常工作。

To those who upgraded from 5.7 to 8.0 via homebrew, this error is likely caused by the upgrade not being complete. In my case, mysql.server start got me the following error:

ERROR! The server quit without updating PID file

I then checked the log file via cat /usr/local/var/mysql/YOURS.err | tail -n 50, and found the following:

InnoDB: Upgrade after a crash is not supported.

If you are on the same boat, first install mysql@5.7 via homebrew, stop the server, and then start the 8.0 system again.

brew install mysql@5.7

/usr/local/opt/mysql@5.7/bin/mysql.server start
/usr/local/opt/mysql@5.7/bin/mysql.server stop

Then,

mysql.server start

This would get your MySQL (8.0) working again.


回答 11

我想我前一段时间也看到了相同的行为,但记不清细节了。
在我们的案例中,问题在于测试运行程序相对于所需的第一次数据库交互(例如,通过在settings.py或某个__init__.py中导入模块)初始化数据库连接的时刻。我将尝试挖掘更多信息,但这可能已经为您解决了。

I think i saw this same behavior some time ago, but can’t remember the details.
In our case, the problem was the moment the testrunner initialises database connections relative to first database interaction required, for instance, by import of a module in settings.py or some __init__.py. I’ll try to digg up some more info, but this might already ring a bell for your case.


回答 12

确保您的/ etc / hosts包含127.0.0.1 localhost其中,并且应该工作正常

Make sure your /etc/hosts has 127.0.0.1 localhost in it and it should work fine


回答 13

我对此有两个偷偷摸摸的猜想

主题1

调查无法访问/tmp/mysql.sock文件的可能性。设置MySQL数据库时,通常将套接字文件站点放入/var/lib/mysql。如果您以身份登录到mysql root@localhost,则您的OS会话需要访问该/tmp文件夹。确保/tmp在操作系统中具有正确的访问权限。另外,请确保sudo用户始终可以读取文件/tmp

主题2

通过访问MySQL 127.0.0.1如果您不注意,则可能会引起一些混乱。怎么样?

在命令行中,如果使用来连接到MySQL 127.0.0.1,则可能需要指定TCP / IP协议。

mysql -uroot -p -h127.0.0.1 --protocol=tcp

或尝试使用DNS名称

mysql -uroot -p -hDNSNAME

这将绕过以身份登录root@localhost,但请确保已root@'127.0.0.1'定义。

下次连接到MySQL时,运行以下命令:

SELECT USER(),CURRENT_USER();

这给你什么?

如果这些函数返回的值相同,则说明您正在按预期进行连接和身份验证。如果值不同,则可能需要创建相应的user root@127.0.0.1

I have two sneaky conjectures on this one

CONJECTURE #1

Look into the possibility of not being able to access the /tmp/mysql.sock file. When I setup MySQL databases, I normally let the socket file site in /var/lib/mysql. If you login to mysql as root@localhost, your OS session needs access to the /tmp folder. Make sure /tmp has the correct access rights in the OS. Also, make sure the sudo user can always read file in /tmp.

CONJECTURE #2

Accessing mysql via 127.0.0.1 can cause some confusion if you are not paying attention. How?

From the command line, if you connect to MySQL with 127.0.0.1, you may need to specify the TCP/IP protocol.

mysql -uroot -p -h127.0.0.1 --protocol=tcp

or try the DNS name

mysql -uroot -p -hDNSNAME

This will bypass logging in as root@localhost, but make sure you have root@'127.0.0.1' defined.

Next time you connect to MySQL, run this:

SELECT USER(),CURRENT_USER();

What does this give you?

  • USER() reports how you attempted to authenticate in MySQL
  • CURRENT_USER() reports how you were allowed to authenticate in MySQL

If these functions return with the same values, then you are connecting and authenticating as expected. If the values are different, you may need to create the corresponding user root@127.0.0.1.


回答 14

遇到了同样的问题。原来mysqld已经停止运行了(我在Mac OSX上)。我重新启动它,错误消失了。

我发现mysqld该链接在很大程度上没有运行: http //dev.mysql.com/doc/refman/5.6/en/can-not-connect-to-server.html

注意第一个技巧!

Had this same problem. Turned out mysqld had stopped running (I’m on Mac OSX). I restarted it and the error went away.

I figured out that mysqld was not running largely because of this link: http://dev.mysql.com/doc/refman/5.6/en/can-not-connect-to-server.html

Notice the first tip!


回答 15

如果出现如下错误:

django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

然后只要找到您的mysqld.sock文件位置并将其添加到“主机”即可。

就像我在Linux上使用xampp一样,所以我的mysqld.sock文件在另一个位置。因此它不适用于“ /var/run/mysqld/mysqld.sock

DATABASES = {

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'asd',
        'USER' : 'root',
        'PASSWORD' : '',
        'HOST' : '/opt/lampp/var/mysql/mysql.sock',
        'PORT' : ''
    }
}

if you get an error like below :

django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

Then just find your mysqld.sock file location and add it to “HOST”.

Like i am using xampp on linux so my mysqld.sock file is in another location. so it is not working for ‘/var/run/mysqld/mysqld.sock

DATABASES = {

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'asd',
        'USER' : 'root',
        'PASSWORD' : '',
        'HOST' : '/opt/lampp/var/mysql/mysql.sock',
        'PORT' : ''
    }
}

回答 16

如果my.cnf中的设置不正确,请检查您的mysql是否未达到最大连接数,或是否未处于某种引导循环中。

使用ps aux | grep mysql检查PID是否正在更改。

Check that your mysql has not reached maximum connections, or is not in some sort of booting loop as happens quite often if the settings are incorrect in my.cnf.

Use ps aux | grep mysql to check if the PID is changing.


回答 17

在网上环顾四周时间太长,无济于事。尝试从命令行键入mysql提示符后,我继续收到此消息:

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

这是因为我的本地mysql服务器不再运行。为了重新启动服务器,我导航到

shell> cd /user/local/bin

我的mysql.server所在的位置。在这里,只需键入:

shell> mysql.server start

这将重新启动本地mysql服务器。

从那里您可以根据需要重置root密码。

mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass')
->                   WHERE User='root';
mysql> FLUSH PRIVILEGES;

Looked around online too long not to contribute. After trying to type in the mysql prompt from the command line, I was continuing to receive this message:

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

This was due to the fact that my local mysql server was no longer running. In order to restart the server, I navigated to

shell> cd /user/local/bin

where my mysql.server was located. From here, simply type:

shell> mysql.server start

This will relaunch the local mysql server.

From there you can reset the root password if need be..

mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass')
->                   WHERE User='root';
mysql> FLUSH PRIVILEGES;

回答 18

我必须首先找到所有进程ID,以杀死mysql的所有实例:

ps aux | grep MySQL的

然后杀死他们:

杀死-9 {pid}

然后:

mysql.server启动

为我工作。

I had to kill off all instances of mysql by first finding all the process IDs:

ps aux | grep mysql

And then killing them off:

kill -9 {pid}

Then:

mysql.server start

Worked for me.


回答 19

套接字位于/ tmp中。在Unix系统上,由于/ tmp上的模式和所有权,这可能会引起一些问题。但是,只要您告诉我们可以正常使用mysql连接,我想这对您的系统来说不是问题。首要检查应该是将mysql.sock放置在一个更中性的目录中。

问题“随机”发生(或并非每次都发生),这一事实让我认为这可能是服务器问题。

  • 您的/ tmp是位于标准磁盘上,还是位于奇异的挂载上(如RAM中)?

  • / tmp是空的吗?

  • iotop遇到问题时,是否表明您有问题?

The socket is located in /tmp. On Unix system, due to modes & ownerships on /tmp, this could cause some problem. But, as long as you tell us that you CAN use your mysql connexion normally, I guess it is not a problem on your system. A primal check should be to relocate mysql.sock in a more neutral directory.

The fact that the problem occurs “randomly” (or not every time) let me think that it could be a server problem.

  • Is your /tmp located on a standard disk, or on an exotic mount (like in the RAM) ?

  • Is your /tmp empty ?

  • Does iotopshow you something wrong when you encounter the problem ?


回答 20

在“管理数据库连接”对话框中配置数据库连接。选择“标准(TCP / IP)”作为连接方法。

有关更多详细信息,请参见此页面 http://dev.mysql.com/doc/workbench/en/wb-manage-db-connections.html

根据另一页,即使您指定localhost,也会使用套接字文件。

如果您未指定主机名或指定特殊主机名localhost,则使用Unix套接字文件。

它还显示了如何通过运行以下命令来检查服务器:

如果mysqld进程正在运行,则可以通过尝试以下命令进行检查。您的设置中的端口号或Unix套接字文件名可能不同。host_ip代表运行服务器的计算机的IP地址。

shell> mysqladmin version 
shell> mysqladmin variables 
shell> mysqladmin -h `hostname` version variables 
shell> mysqladmin -h `hostname` --port=3306 version 
shell> mysqladmin -h host_ip version 
shell> mysqladmin --protocol=SOCKET --socket=/tmp/mysql.sock version

Configure your DB connection in the ‘Manage DB Connections dialog. Select ‘Standard (TCP/IP)’ as connection method.

See this page for more details http://dev.mysql.com/doc/workbench/en/wb-manage-db-connections.html

According to this other page a socket file is used even if you specify localhost.

A Unix socket file is used if you do not specify a host name or if you specify the special host name localhost.

It also shows how to check on your server by running these commands:

If a mysqld process is running, you can check it by trying the following commands. The port number or Unix socket file name might be different in your setup. host_ip represents the IP address of the machine where the server is running.

shell> mysqladmin version 
shell> mysqladmin variables 
shell> mysqladmin -h `hostname` version variables 
shell> mysqladmin -h `hostname` --port=3306 version 
shell> mysqladmin -h host_ip version 
shell> mysqladmin --protocol=SOCKET --socket=/tmp/mysql.sock version

回答 21

在ubuntu14.04中,您可以执行此操作以解决此问题。

zack@zack:~/pycodes/python-scraping/chapter5$ **mysqladmin -p variables|grep socket**
Enter password: 
| socket                                            | ***/var/run/mysqld/mysqld.sock***                                                                                            |
zack@zack:~/pycodes/python-scraping/chapter5$***ln -s  /var/run/mysqld/mysqld.sock /tmp/mysql.sock***
zack@zack:~/pycodes/python-scraping/chapter5$ ll /tmp/mysql.sock 
lrwxrwxrwx 1 zack zack 27 11 29 13:08 /tmp/mysql.sock -> /var/run/mysqld/mysqld.sock=

in ubuntu14.04 you can do this to slove this problem.

zack@zack:~/pycodes/python-scraping/chapter5$ **mysqladmin -p variables|grep socket**
Enter password: 
| socket                                            | ***/var/run/mysqld/mysqld.sock***                                                                                            |
zack@zack:~/pycodes/python-scraping/chapter5$***ln -s  /var/run/mysqld/mysqld.sock /tmp/mysql.sock***
zack@zack:~/pycodes/python-scraping/chapter5$ ll /tmp/mysql.sock 
lrwxrwxrwx 1 zack zack 27 11月 29 13:08 /tmp/mysql.sock -> /var/run/mysqld/mysqld.sock=

回答 22

对我来说,我确定mysqld已启动,并且命令行mysql可以正常工作。但是httpd服务器显示了问题(无法通过套接字连接到mysql)。

我使用mysqld_safe&启动了该服务。

最后,我发现当我使用服务mysqld start启动mysqld服务时,出现了问题(selinux权限问题),当我修复了selinux问题,并使用“ service mysqld start”启动mysqld时,httpd连接问题消失了。但是,当我使用mysqld_safe&启动mysqld时,可以使用mysqld。(mysql客户端可以正常工作)。但是与httpd连接时仍然存在问题。

For me, I’m sure mysqld is started, and command line mysql can work properly. But the httpd server show the issue(can’t connect to mysql through socket).

I started the service with mysqld_safe&.

finally, I found when I start the mysqld service with service mysqld start, there are issues(selinux permission issue), and when I fix the selinux issue, and start the mysqld with “service mysqld start”, the httpd connection issue disappear. But when I start the mysqld with mysqld_safe&, mysqld can be worked. (mysql client can work properly). But there are still issue when connect with httpd.


回答 23

如果与套接字相关,请阅读此文件

/etc/mysql/my.cnf

并查看什么是标准插座位置。就像这样的一行:

socket = /var/run/mysqld/mysqld.sock

现在为您的shell创建一个别名,例如:

alias mysql="mysql --socket=/var/run/mysqld/mysqld.sock"

这样,您就不需要root特权。

If it’s socket related read this file

/etc/mysql/my.cnf

and see what is the standard socket location. It’s a line like:

socket = /var/run/mysqld/mysqld.sock

now create an alias for your shell like:

alias mysql="mysql --socket=/var/run/mysqld/mysqld.sock"

This way you don’t need root privileges.


回答 24

只需尝试运行 mysqld

在Mac上,这对我不起作用。如果不起作用/usr/local/var/mysql/<your_name>.err,请尝试查看详细的错误日志。

Simply try to run mysqld.

This was what was not working for me on mac. If it doesn’t work try go to /usr/local/var/mysql/<your_name>.err to see detailed error logs.


回答 25

# shell script ,ignore the first 
$ $(dirname `which mysql`)\/mysql.server start

可能会有帮助。

# shell script ,ignore the first 
$ $(dirname `which mysql`)\/mysql.server start

May be helpful.


回答 26

使用通过Homebrew安装的适用于MySQL 8.0.19的MacOS Mojave 10.14.6

  • sudo find / -name my.cnf
  • 文件位于 /usr/local/etc/my.cnf

工作了一段时间,然后最终返回了错误。卸载MySQL的Homebrew版本并直接从.dmg文件安装此处

从那时起,我们一直很高兴地建立联系。

Using MacOS Mojave 10.14.6 for MySQL 8.0.19 installed via Homebrew

  • Ran sudo find / -name my.cnf
  • File found at /usr/local/etc/my.cnf

Worked for a time then eventually the error returned. Uninstalled the Homebrew version of MySQL and installed the .dmg file directly from here

Happily connecting since then.


回答 27

就我而言,帮助编辑文件/etc/mysql/mysql.conf.d/mysqld.cnf并替换以下行:

socket      = /var/run/mysqld/mysqld.sock

socket      = /tmp/mysql.sock

然后,我重新启动服务器,它工作正常。有趣的是,如果我将线路恢复原状并重新启动,它仍然可以工作。

In my case what helped was to edit the file /etc/mysql/mysql.conf.d/mysqld.cnfand replace the line:

socket      = /var/run/mysqld/mysqld.sock

with

socket      = /tmp/mysql.sock

Then I restarted the server and it worked fine. The funny thing is that if I put back the line as it was before and restarted it still worked..


回答 28

我最近也遇到过类似的问题。经历了许多答案。我按照以下步骤操作。

  1. 更改/etc/my.cnf中的套接字路径(因为我反复出现/tmp/mysql.sock错误)引用以更改套接字路径
  2. 运行mysqld_safe以重新启动服务器,因为这是在出现错误时重新启动的推荐方法。引用mysqld_safe

I had faced similar problem recently. Went through many answers. I got it working by following steps.

  1. change the socket path in /etc/my.cnf (as i was repeatedly getting error with /tmp/mysql.sock ) reference to change the socket path
  2. run mysqld_safe to restart the server as it is the recommended way to restart in case of errors. reference to mysqld_safe

回答 29

对我而言,mysql服务器未运行。因此,我通过启动了mysql服务器

mysql.server start

然后

mysql_secure_installation

保护服务器,现在我可以通过访问MySQL服务器

sudo mysql -uroot -p

For me, the mysql server was not running. So, i started the mysql server through

mysql.server start

then

mysql_secure_installation

to secure the server and now I can visit the MySQL server through

sudo mysql -uroot -p