错误 2003 (HY000):无法连接到“127.0.0.1”上的 MySQL 服务器(111)

发布于 2024-08-10 13:57:28 字数 272 浏览 7 评论 0原文

我使用以下命令:

mysql -u root -h 127.0.0.1 -p

错误消息是:

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

如何修复它?

I use the following command:

mysql -u root -h 127.0.0.1 -p

And the error message is:

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

How can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

枕梦 2024-08-17 13:57:28

如果您使用的是 Ubuntu,则必须使用以下步骤来避免此错误(如果没有启用复制):

  1. 运行命令 vim /etc/mysql/my.cnf
  2. comment bind -address = 127.0.0.1 使用#符号
  3. 重新启动MySQL服务器一次。

在步骤1中,如果在my.cnf文件中找不到bind-address,请在/etc/mysql/mysql.conf.d/中查找mysqld.cnf 文件。

启用 MySQL 复制时的更新

尝试使用文件 my.cnf 中绑定 MySQL 服务器的 IP 地址连接 MySQL 服务器,而不是 localhost > 或127.0.0.1

If you are using Ubuntu, you have to use the following steps to avoid this error (if there is no replication enabled):

  1. run the command vim /etc/mysql/my.cnf
  2. comment bind-address = 127.0.0.1 using the # symbol
  3. restart your MySQL server once.

In Step 1, if you cannot find bind-address in the my.cnf file, look for it in /etc/mysql/mysql.conf.d/mysqld.cnf file.

Update in case of MySQL replication enabled

Try to connect MySQL server on the IP address for which MySQL server is bound in file my.cnf instead of localhost or 127.0.0.1.

梦里梦着梦中梦 2024-08-17 13:57:28

尝试使用 localhost 而不是 127.0.0.1 进行连接或在您的 connection-config 中进行连接。它在 Debian 6.0 (Squeeze) 服务器上对我有用。

Try localhost instead of 127.0.0.1 to connect or in your connection-config. It worked for me on a Debian 6.0 (Squeeze) Server.

自由范儿 2024-08-17 13:57:28

就我而言(远程连接),它有助于关闭服务器上的防火墙:

service iptables stop

In my case (remote connection), it helped turning off the firewall on the server:

service iptables stop
允世 2024-08-17 13:57:28

当您在连接数据库之前忘记启动数据库时,就会发生这种情况

mysql.server start

mysql -u root -p -h 127.0.0.1

This happens when you forget to start the database before connecting to it:

mysql.server start

Then

mysql -u root -p -h 127.0.0.1
落墨 2024-08-17 13:57:28

在 Windows 上,可能会出现此问题,因为您的 MySQL 服务器未安装并运行。

为此,请以管理员身份启动命令提示符并输入命令:

"C:\Program Files (x86)\MySQL\MySQL Server 5.1\bin\mysqld" --install

如果收到“服务已成功安装”消息,则需要启动 MySQL 服务。为此:转到服务窗口
任务管理器服务打开服务)。
搜索 MySQL 并从顶部导航栏启动它。
然后如果尝试打开mysql.exe,它将起作用。

On Windows, this problem may occur because your MySQL server is not installed and running.

To do that, start a command prompt as administrator and enter the command:

"C:\Program Files (x86)\MySQL\MySQL Server 5.1\bin\mysqld" --install

If you get "service successfully installed" message then you need to start the MySQL service. To do that: go to Services window
(Task ManagerServicesOpen Services).
Search for MySQL and start it from the top navigation bar.
Then if trying to open mysql.exe, it will work.

离鸿 2024-08-17 13:57:28

查看 my.cnf 文件。如果它包含 [client] 部分,并且 port 不是真正的监听端口(默认 3306),则必须使用显式参数 连接服务器>-P 3306,例如

mysql -u root -h 127.0.0.1 -p -P 3306

Look at the my.cnf file. If it contains a [client] section, and the port is other than the real listen port (default 3306), you must connect the server with an explicit parameter, -P 3306, e.g.

mysql -u root -h 127.0.0.1 -p -P 3306
请止步禁区 2024-08-17 13:57:28

如果您通过 WSL(Linux 的 Windows 子系统)运行 Ubuntu 并希望连接到主机上的 MySQL 实例,则需要使用主机的 IPv4 地址,例如 192.XXX,而不是127.0.0.1localhost

$ mysql -u <user> -h 127.0.0.1 -p -P 3306

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
$ mysql -u <user> -h 192.X.X.X -p -P 3306

Welcome to the MySQL monitor...Server version: 8.0.26 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

要检查您的 IPv4 地址,请转至设置 -> 网络 $ Internet -> 属性 -> IPv4 地址

我在为 apache airflow 配置 MySQL URI 时遇到了相同的错误:

mysql+mysqlconnector://<user>:<password>@localhost:3306/<database>

(mysql.connector.errors.DatabaseError) 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (111)

mysql+mysqlconnector://<user>:<password>@127.0.0.1:3306/<database>

(mysql.connector.errors.DatabaseError) 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)

修复了将 URI 配置为:

mysql+mysqlconnector://<user>:<password>@192.X.X.X:3306/<database>

If you are running Ubuntu via WSL (Windows Subsystem for Linux) and wish to connect to the MySQL instance on the host machine, you will need to use the host machine's IPv4 address e.g. 192.X.X.X, not 127.0.0.1 or localhost.

$ mysql -u <user> -h 127.0.0.1 -p -P 3306

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
$ mysql -u <user> -h 192.X.X.X -p -P 3306

Welcome to the MySQL monitor...Server version: 8.0.26 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

To check your IPv4 address, go to Settings -> Network $ Internet -> Properties -> IPv4 address.

I got the same error configuring MySQL URI for apache airflow as:

mysql+mysqlconnector://<user>:<password>@localhost:3306/<database>

(mysql.connector.errors.DatabaseError) 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (111)

or

mysql+mysqlconnector://<user>:<password>@127.0.0.1:3306/<database>

(mysql.connector.errors.DatabaseError) 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)

Fixed the error configuring the URI as:

mysql+mysqlconnector://<user>:<password>@192.X.X.X:3306/<database>
南…巷孤猫 2024-08-17 13:57:28

最快的方法,特别是如果您只想进行完整性检查或获取一些数据,是通过运行以下命令来使用容器 shell:
docker exec -it <容器名称>嘘。然后,您可以运行mysql -p来访问MySQL shell。


笔记:
查找容器名称的一种方法是运行 docker ps

Quickest way, especially if you just want to do a sanity check or grab some data, is to use the container shell by running the following command:
docker exec -it <container_name> sh. Then, you can run mysql -p to access MySQL shell.


Notes:
One way for finding a container name is by running docker ps

千柳 2024-08-17 13:57:28

如果您在这里看到相同的错误消息并且您使用 Docker - 尝试使用数据库服务的名称作为主机。

services:
  app:
    blablabla
  db:
    blablabla

我的意思是,而不是:

127.0.0.1 or localhost

使用:

db

If you are here with the same error message and you use Docker - try to use the name of the database service as a host.

services:
  app:
    blablabla
  db:
    blablabla

I mean, instead of:

127.0.0.1 or localhost

use:

db
鹿! 2024-08-17 13:57:28

如果您已尝试以上所有方法但仍然无效。检查防火墙。

我试图从 Windows 计算机连接到安装在具有 CentOs7 的 VirtualBox 计算机上的 MySql Server 5.7.32。我已经尝试了一切,但它不起作用,即使我可以 ping 通机器,但我无法连接到 MySql 服务器。

在 CentOs7 上,检查防火墙的命令为:

步骤 1:检查防火墙服务的状态:

systemctl status firewallid.service

步骤 2:禁用防火墙服务

systemctl stop firewallid.service

If you have tried all of the above and it still did not work. Check firewall.

I was trying to connect from a windows machine to a MySql Server 5.7.32 installed on a VirtualBox machine with CentOs7. I have tried everything and it was not working, even though i could ping the machine i couldn't connect to the MySql Server.

On CentOs7 the commands to check the firewall are:

Step 1: Check the status of your firewall service:

systemctl status firewallid.service

Step 2: Disable the firewall service

systemctl stop firewallid.service
清晰传感 2024-08-17 13:57:28

您需要在 MySQL 配置文件(my.inimy.cnf)中将绑定地址参数更改为 127.0.0.1 或使用其中定义的参数。

如果这不起作用,您应该检查 MySQL 服务是否确实正在运行。

You need to change the bind-address parameter to 127.0.0.1 in the MySQL configuration file (my.ini or my.cnf) or use the one that is defined there.

If that doesn't work you should check that the MySQL service is actually running.

紧拥背影 2024-08-17 13:57:28

如果您在非默认端口上运行,您可以尝试使用 --port= ,前提是 --skip-networking已启用。

In case you are running on a non-default port, you may try using --port=<port num> provided --skip-networking is not enabled.

久而酒知 2024-08-17 13:57:28

我也面临着同样的问题。

以下内容帮助我解决了问题

转到控制面板管理工具服务。在其中您肯定会看到 MySQL 服务:右键单击并说启动(强制启动)。

I was also facing the same issue.

The following helped me fix the problem

Go to Control PanelAdministrative ToolsServices. Inside this you will most certainly see the MySQL service: right click and say start (force start).

熊抱啵儿 2024-08-17 13:57:28

我只是遇到这个问题...在 Windows 7 和 WAMP 中运行服务器...阅读此内容之后。

我发现防病毒防火墙导致了这个问题。

I just have this problem... running in Windows 7 and WAMP server ... after reading this.

I found that Antivirus Firewall had caused the problem.

落墨 2024-08-17 13:57:28

检查端口 3306 是否开放(更多信息此处) :

nmap -p3306 127.0.0.1

如果您收到类似以下内容:

Starting Nmap 7.92 ( https://nmap.org ) at 2022-01-07 11:55 CET
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000082s latency).

PORT     STATE  SERVICE
3306/tcp closed mysql

Nmap done: 1 IP address (1 host up) scanned in 0.08 seconds

则打开端口 3306:

sudo iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT

或者如果您使用 UFW

检查:netstat -lnp | grep mysql 你应该得到这样的结果:

cp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      2048/mysqld
tcp6       0      0 :::33060                :::*                    LISTEN      2048/mysqld
unix  2      [ ACC ]     STREAM     LISTENING     514961   2048/mysqld          /var/run/mysqld/mysqld.sock
unix  2      [ ACC ]     STREAM     LISTENING     514987   2048/mysqld          /var/run/mysqld/mysqlx.sock

Check if it is open port 3306 (more here):

nmap -p3306 127.0.0.1

If you receive something like:

Starting Nmap 7.92 ( https://nmap.org ) at 2022-01-07 11:55 CET
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000082s latency).

PORT     STATE  SERVICE
3306/tcp closed mysql

Nmap done: 1 IP address (1 host up) scanned in 0.08 seconds

then open port 3306:

sudo iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT

Or sudo ufw allow 3306 if you use UFW.

Check: netstat -lnp | grep mysql you should get something like this:

cp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      2048/mysqld
tcp6       0      0 :::33060                :::*                    LISTEN      2048/mysqld
unix  2      [ ACC ]     STREAM     LISTENING     514961   2048/mysqld          /var/run/mysqld/mysqld.sock
unix  2      [ ACC ]     STREAM     LISTENING     514987   2048/mysqld          /var/run/mysqld/mysqlx.sock
遮了一弯 2024-08-17 13:57:28

当我尝试连接到 Docker 容器中的 MySQL 服务器时,我遇到了这个问题,

我按照以下步骤修复了

  1. 进入容器内部:
docker exec -it mysql bash
  1. 运行以下命令:
echo "bind-address           = 0.0.0.0" >> /etc/mysql/conf.d/mysql.cnf 
  1. 从容器中退出
exit
  1. 重新启动容器
docker restart mysql
  1. 进入容器内部
docker exec -it mysql bash
  1. 连接到 MySQL 服务器,然后输入密码
mysql -u root -p

I had this problem when I tried to connect to the MySQL server in the Docker container

I fixed by following the steps below.

  1. Go inside the container:
docker exec -it mysql bash
  1. Run this command:
echo "bind-address           = 0.0.0.0" >> /etc/mysql/conf.d/mysql.cnf 
  1. Exit from the container
exit
  1. Restart the container
docker restart mysql
  1. Go inside the container
docker exec -it mysql bash
  1. Connect to the MySQL server, and enter your password
mysql -u root -p
淤浪 2024-08-17 13:57:28

当数据库存储容量耗尽时连接到 AWS 上的 RDS 时会发生这种情况。您必须增加可用的存储容量才能解决此问题。

This happens when connecting to RDS on AWS when the database has run out of storage capacity. You have to increase the amount of storage capacity available to fix this.

三生一梦 2024-08-17 13:57:28

就我而言,使用 Centos Linux 和 MYSQL 8.0.26。我通过从防火墙打开端口来修复它。如果 [3306] 是 MySQL 端口并且 [public] 是活动区域(默认配置),则可以运行以下命令

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

另外,我查找了 bind-address=127.0.0.1 但没有在任何地方进行配置在配置中。 /etc/my.cnf /etc/my.cnf.d/*

In my case, with Centos Linux and MYSQL 8.0.26. I fixed it by opening the port from the firewall. You can run the below command if [3306] is the MySQL port and [public] is the active zone (default configuration)

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

Also, I looked for bind-address=127.0.0.1 but did not configure anywhere in the configuration./etc/my.cnf /etc/my.cnf.d/*

小糖芽 2024-08-17 13:57:28

对于 Docker 用户 - 当尝试使用 mysql -u root -h 127.0.0.1 -p 连接到本地 SQL 数据库服务器并且您的数据库正在 Docker 容器中运行时,请确保MySQL 服务已启动并正在运行(使用 docker ps 进行验证,并检查您是否位于正确的端口)。如果容器关闭,您将收到连接错误。

最佳实践是在计算机上的 /etc/hosts 中设置 IP 地址:

127.0.0.1 db.local

并通过 mysql -u root -h db.local -p 运行它。

如果这一切都正常并且工作正常(或者如果它正在工作但突然停止了),您应该检查您的容器是否空间不足。在这种情况下,MySQL 无法授权和创建连接。症状是端口处于活动状态,一切看起来都很好,但如果您尝试连接,连接要么被取消,要么被拒绝。

For Docker users - When trying to connect to the local SQL database server using mysql -u root -h 127.0.0.1 -p and your database is running in a Docker container, make sure the MySQL service is up and running (verify using docker ps and also check that you are in the right port as well). If the container is down you'll get connection error.

The best practice is to set the IP addresses in /etc/hosts on your machine:

127.0.0.1 db.local

And running it by mysql -u root -h db.local -p.

If all this is ok and working (or in case it was working and sudenly it stopped), you should check if your container is not out of space. In this case MySQL isn't able to authorize and create connection. The symptom is that port is active, everything looks great, but if you try to connect, the connection is either cancelled, or refused.

呆° 2024-08-17 13:57:28

确保您的密码不包含“@”字符。例如,如果您的密码是 Jane@123,则会显示以下错误。

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '123@localhost' ([Errno -2] Name or service not known)")

Make sure that, your password doesn't contain the '@' character. As an example, If your password is Jane@123, then the following error will be displayed.

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '123@localhost' ([Errno -2] Name or service not known)")
柏林苍穹下 2024-08-17 13:57:28

我在重新安装时更改了安装目录,并且它有效。

I changed the installation directory on re-install, and it worked.

百善笑为先 2024-08-17 13:57:28

我刚刚重新启动了 MySQL 服务器,问题就解决了。

在 Windows 上,net stop MySQL,然后 net start MySQl

Ubuntu (Linux) 上:sudo service start mysql

I just restarted my MySQL server and the problem was solved.

On Windows, net stop MySQL, and then net start MySQl.

On Ubuntu (Linux): sudo service start mysql

空‖城人不在 2024-08-17 13:57:28

请确保您的 MySQL 服务器正在本地主机上运行。

在 Linux 上

检查 MySQL 服务器是否正在运行:

sudo service mysql status

要运行 MySQL 服务器:

sudo service mysql start

在 Windows 上< /strong>

检查 MySQL 服务器是否正在运行:

net start

如果 MySQL 不在列表中,则必须启动/运行 MySQL。

运行 MySQL 服务器:

net start mysql

Please make sure your MySQL server is running on localhost.

On Linux

To check if MySQL server is running:

sudo service mysql status

To run MySQL server:

sudo service mysql start

On Windows

To check if MySQL server is running:

net start

If MySQL is not in list, you have to start/run MySQL.

To run MySQL server:

net start mysql

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文