错误 2003 (HY000):无法连接到“127.0.0.1”上的 MySQL 服务器(111)
我使用以下命令:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
如果您使用的是 Ubuntu,则必须使用以下步骤来避免此错误(如果没有启用复制):
vim /etc/mysql/my.cnf
bind -address = 127.0.0.1
使用#符号在步骤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):
vim /etc/mysql/my.cnf
bind-address = 127.0.0.1
using the # symbolIn Step 1, if you cannot find
bind-address
in themy.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.
尝试使用 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.就我而言(远程连接),它有助于关闭服务器上的防火墙:
In my case (remote connection), it helped turning off the firewall on the server:
当您在连接数据库之前忘记启动数据库时,就会发生这种情况
:
This happens when you forget to start the database before connecting to it:
Then
在 Windows 上,可能会出现此问题,因为您的 MySQL 服务器未安装并运行。
为此,请以管理员身份启动命令提示符并输入命令:
如果收到“服务已成功安装”消息,则需要启动 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:
If you get "service successfully installed" message then you need to start the MySQL service. To do that: go to Services window
(Task Manager → Services → Open Services).
Search for MySQL and start it from the top navigation bar.
Then if trying to open mysql.exe, it will work.
查看
my.cnf
文件。如果它包含[client]
部分,并且port
不是真正的监听端口(默认 3306),则必须使用显式参数连接服务器>-P 3306
,例如Look at the
my.cnf
file. If it contains a[client]
section, and theport
is other than the real listen port (default 3306), you must connect the server with an explicit parameter,-P 3306
, e.g.如果您通过 WSL(Linux 的 Windows 子系统)运行
Ubuntu
并希望连接到主机上的 MySQL 实例,则需要使用主机的 IPv4 地址,例如192.XXX
,而不是127.0.0.1
或localhost
。要检查您的 IPv4 地址,请转至
设置
->网络 $ Internet
->属性
->IPv4 地址
。我在为
apache airflow
配置 MySQL URI 时遇到了相同的错误:或
修复了将 URI 配置为:
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
, not127.0.0.1
orlocalhost
.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:or
Fixed the error configuring the URI as:
最快的方法,特别是如果您只想进行完整性检查或获取一些数据,是通过运行以下命令来使用容器 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 runmysql -p
to access MySQL shell.Notes:
One way for finding a container name is by running
docker ps
如果您在这里看到相同的错误消息并且您使用 Docker - 尝试使用数据库服务的名称作为主机。
我的意思是,而不是:
使用:
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.
I mean, instead of:
use:
如果您已尝试以上所有方法但仍然无效。检查防火墙。
我试图从 Windows 计算机连接到安装在具有 CentOs7 的 VirtualBox 计算机上的 MySql Server 5.7.32。我已经尝试了一切,但它不起作用,即使我可以 ping 通机器,但我无法连接到 MySql 服务器。
在 CentOs7 上,检查防火墙的命令为:
步骤 1:检查防火墙服务的状态:
步骤 2:禁用防火墙服务
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:
Step 2: Disable the firewall service
您需要在 MySQL 配置文件(my.ini 或 my.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.
如果您在非默认端口上运行,您可以尝试使用
--port=
,前提是--skip-networking
不已启用。
In case you are running on a non-default port, you may try using
--port=<port num>
provided--skip-networking
isnot enabled.
我也面临着同样的问题。
以下内容帮助我解决了问题
转到控制面板 → 管理工具 → 服务。在其中您肯定会看到 MySQL 服务:右键单击并说启动(强制启动)。
I was also facing the same issue.
The following helped me fix the problem
Go to Control Panel → Administrative Tools → Services. Inside this you will most certainly see the MySQL service: right click and say start (force start).
我只是遇到这个问题...在 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.
检查端口 3306 是否开放(更多信息此处) :
如果您收到类似以下内容:
则打开端口 3306:
或者如果您使用 UFW。
检查:netstat -lnp | grep mysql 你应该得到这样的结果:
Check if it is open port 3306 (more here):
If you receive something like:
then open port 3306:
Or
sudo ufw allow 3306
if you use UFW.Check:
netstat -lnp | grep mysql
you should get something like this:当我尝试连接到 Docker 容器中的 MySQL 服务器时,我遇到了这个问题,
我按照以下步骤修复了。
I had this problem when I tried to connect to the MySQL server in the Docker container
I fixed by following the steps below.
当数据库存储容量耗尽时连接到 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.
就我而言,使用 Centos Linux 和 MYSQL 8.0.26。我通过从防火墙打开端口来修复它。如果 [3306] 是 MySQL 端口并且 [public] 是活动区域(默认配置),则可以运行以下命令
另外,我查找了
bind-address=127.0.0.1
但没有在任何地方进行配置在配置中。/etc/my.cnf
/etc/my.cnf.d/*
In my case, with
Centos Linux
andMYSQL 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)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/*
对于 Docker 用户 - 当尝试使用
mysql -u root -h 127.0.0.1 -p
连接到本地 SQL 数据库服务器并且您的数据库正在 Docker 容器中运行时,请确保MySQL 服务已启动并正在运行(使用 docker ps 进行验证,并检查您是否位于正确的端口)。如果容器关闭,您将收到连接错误。最佳实践是在计算机上的
/etc/hosts
中设置 IP 地址:并通过
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 usingdocker 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: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.
确保您的密码不包含“@”字符。例如,如果您的密码是 Jane@123,则会显示以下错误。
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.
我在重新安装时更改了安装目录,并且它有效。
I changed the installation directory on re-install, and it worked.
我刚刚重新启动了 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 thennet start MySQl
.On Ubuntu (Linux):
sudo service start mysql
请确保您的 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