zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

MySQL远程连接报错2003-cant connection to mysql server on ‘IP’(10061 unknown error)

mysqlserverOn连接IP 报错 远程 Error
2023-09-14 09:13:14 时间

       MySQL远程连接报错2003-cant connection to mysql server on ‘IP’(10061 unknown error)的解决

首先,得说明一下,本文讨论的是远程连接MySQL数据库。

其次,该报错的意思是未知错误导致无法远程连接MySQL。

该报错的解决也很简单,总共从四个方面着手解决即可。

一,检查MySQL服务是否正常启动

如果mysql的server服务并没有启动,那么,你任何的客户端都是别想使用服务,自然谈不到连接了,不管是远程还是本地连接。

二,如果服务是正常的,那么需要检查配置文件。

查看配置文件内是否有bind-address选项,该选项的值如果为127.0.0.1,那么,将该值更改为本机IP或者0.0.0.0或者注释该行。

例如,我这个mysql的配置文件内容如下,其中的bind-address值是127.0.0.1,那么,远程连接的时候将会报错。

root@3b5e59d2d7e0:/# cat -n /etc/mysql/my.cnf
     1	# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     2	#
     3	# This program is free software; you can redistribute it and/or modify
     4	# it under the terms of the GNU General Public License as published by
     5	# the Free Software Foundation; version 2 of the License.
     6	#
     7	# This program is distributed in the hope that it will be useful,
     8	# but WITHOUT ANY WARRANTY; without even the implied warranty of
     9	# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10	# GNU General Public License for more details.
    11	#
    12	# You should have received a copy of the GNU General Public License
    13	# along with this program; if not, write to the Free Software
    14	# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
    15	
    16	#
    17	# The MySQL  Server configuration file.
    18	#
    19	# For explanations see
    20	# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
    21	
    22	[mysqld]
    23	pid-file        = /var/run/mysqld/mysqld.pid
    24	socket          = /var/run/mysqld/mysqld.sock
    25	datadir         = /var/lib/mysql
    26	secure-file-priv= NULL
    27	
    28	bind-address=127.0.0.1
    29	# Custom config should go here
    30	!includedir /etc/mysql/conf.d/

将28行注释或者更改为0.0.0.0, 或者注释掉这一行,重启MySQL服务,即可远程连接了。

三,检查配置文件,server所开放的端口

例如,本文的第一张图,很明显,我是写了一个错误的端口号3301,实际上,我使用的是默认端口3306

 

密码错误是报这个错哦:

四,MySQL数据库服务并没有给root或者别的用户开放远程连接的权限。

如果登陆MySQL服务器后,执行 select user,host from user;查询的结果如下的话,那么会报上图的错,不是2003哦:

mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.01 sec)

 开启远程连接:

mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

或者这样开启:

mysql> grant all privileges on *.* to root@'%' with grant option;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

 

 

总结:

2003错误的解决,首先,是看MySQL的服务是否正常,ps命令查看进程等等方式确认服务正常即可,其次,是看配置文件内的绑定IP选项是否绑定了localhost或者127.0.0.1回环地址,再次,查看服务所启动的端口是否和远程连接时使用的端口一致。