• 35648

    文章

  • 23

    评论

  • 20

    友链

  • 最近新加了很多技术文章,大家多来逛逛吧~~~~
  • 喜欢这个网站的朋友可以加一下QQ群,我们一起交流技术。

debian下的mysql 5.7无法从windows主机上远程访问:2003/10061错误 can't connection

欢迎来到阿八个人博客网站。本 阿八个人博客 网站提供最新的站长新闻,各种互联网资讯。 喜欢本站的朋友可以收藏本站,或者加QQ:我们大家一起来交流技术! URL链接:https://www.abboke.com/jsh/2019/0824/108288.html 1190000020169077

问题描述

基于 debian 9 的云主机上,按照官方安装说明安装了 mysql 5.7 后,在本地(window 10 环境)使用 navicat 连接此 mysql 报错,报错信息如下:

2003 Can't connect to MySQL server on '我自己的IP地址就不写明了'(10061"Unknown error")

搜寻解决方案

使用谷歌以can't connect 2003 10061 inurl:dev.mysql.com/doc/为关键词进行搜索。这里指定 inurl:dev.mysql.com/doc/ 是因为能从官方文档获得信息尽量从官方获得信息,而为什么能知道这个URL?搜索mysql 官网后在顶部有个大大的导航栏,我们要找相关资料显然要切换到 DOCUMENTATION 选项卡,现在网址就变成了https://dev.mysql.com/doc/

我的搜索结果如图

可以看到,第一个搜索结果讲的是 mysql 8.0 版本的事情,我们使用的是 5.7 版本,先不看。第二个结果是 mysql connector ,这个和我们在说的关联不大,也不看。第三个说的是 Troubleshooting(处理重大问题;解决难题) Problems Connecting to MySQL ,感觉上和我们的问题关系很大,所以先看这个。

传送门

按图索骥

第一点

官方文档的意思是:确认 mysql server 正在运行。

按照官方文档给出的验证方法,确认 mysql server 正在运行。

第二点

“you are trying to connect using a TCP/IP port, named pipe, or Unix socket file different from the one on which the server is listening.” --- 谷歌翻译 ===> "您正尝试使用TCP / IP端口,命名管道或Unix套接字文件进行连接,该文件与服务器正在侦听的文件不同。"

如果看到这里还不明白的话,看到接下来的一句话应该能大概理解这一点想要说什么了。

"To correct this when you invoke a client program, specify a --port option to indicate the proper port number" --- 谷歌翻译 ===> "要在调用客户端程序时更正此问题,请指定--port选项以指示正确的端口号"

这也就是说确认我们的端口是否是正确的。

那我们检查 mysql server 是否在监听 3306 端口,这里官方文档没有给出指令,又是“高手写的文档“——高手总是不写小白觉得必要的但在自己脑子里自然而然的东西。

使用netstat -nlp | grep 3306,这个指令如果不了解的话需要学习netstat|grep,自己用搜索引擎搜索就好,网上资料还是很多的。简单说明的话,这个指令前半段查看当前服务器的网络状态,然后通过|把前半段查到的内容作为参数传给后半段,后半段从内容中筛出有3306的那一行。

结果如下
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 8635/mysqld

这说明有一个 mysqld 在监听 3306 端口,只要我们在 navicat 中连接时设置的端口为3306,就说明这一步没有问题了。

第三点

第三点说, mysql server 在启动时会有一些参数对其进行配置,要注意是否启用了 --skip-networking`参数,或者--bind-address=127.0.0.1这样的参数,这些参数会导致 mysql server 只接收本地的访问请求,而拒绝外网的。

实际上,从第二点最后我们看到127.0.0.1:3306和这里的--bind-address=127.0.0.1,有经验的话应该就已经明白,就是配置参数有问题了。那么该如何修改配置参数呢?这里官方文档提供了这两个重要参数的说明文档的超链接,实际上时前往同一个页面的。

参数说明文档

进入参数说明文档后直接翻到顶层,因为我们的目的有两个:一:查看我们的 mysql server 是否真的配置了这种参数导致我们的问题;二:如果是的话,或者说来都来了,看看应该如何配置参数。所以我们不必细究这个参数,而是直接回到顶上看看有没有关于设置参数的说明。

回到顶部,看第一段

When you start the mysqld server, you can specify program options using any of the methods described in Section 4.2.2, “Specifying Program Options”. The most common methods are to provide options in an option file or on the command line. However, in most cases it is desirable to make sure that the server uses the same options each time it runs. The best way to ensure this is to list them in an option file. See Section 4.2.2.2, “Using Option Files”. That section also describes option file format and syntax.

这一段的意思是:"你可以在启动 mysql server 时才指定配置参数,不过我们通常希望能够每次启动都遵照同样的参数启动,因此绝大多数情况下参数写在一个文件里。"要了解这个文件怎么回事儿,请查看Section 4.2.2.2, “Using Option Files”。

既然如此,跟着超链接来到新的说明文档。

还是开门见山的有如下内容:

Most MySQL programs can read startup options from option files (sometimes called configuration files). Option files provide a convenient way to specify commonly used options so that they need not be entered on the command line each time you run a program. For the MySQL server, MySQL provides a number of preconfigured option files.

To determine whether a program reads option files, invoke it with the --help option. (For mysqld, use --verbose and --help.) If the program reads option files, the help message indicates which files it looks for and which option groups it recognizes.

第一段老调重弹,说绝大多数 mysql server 启动时都从文件读取启动配置参数, mysql 提供了一些预置的参数。到这里先别急着跟着超链接跳转,下一段字又不多。,
第二段说,为确定 mysql server 是否读取了一个配置文件作为启动参数配置,携带--help参数来调用它,如果 mysql server 读取了配置文件,那么帮助信息(也就是因--help而输出的信息)将会指明 mysql server 查找哪些位置的文件以及它涉及了哪个配置群。

现在我们直接看自己机子上的 mysql 用了哪些文件,这里文件里有什么配置,一定比跳转第一段提到的预置配置参数效率高。所以运行 mysql --help > /tmp/mysqloptions.txt。因为输出内容非常多,所以将它输出到了一个文件里。

查看这个文件,一开始是一段介绍,不看。然后是讲mysql这个命令可以带着什么参数运行,跳过。跳过之后就发现我们要看到的东西:

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

那么再一个一个查看,在我的电脑上,/etc/my.cnf~/.my.cnf均是不存在的,而/etc/mysql/my.cnf有如下内容:

# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d

前面带#的显然是注释了,但注意这里的!includedir,说实话我也不知道这里的!想要表示什么,我一开始还以为表示取非,千万不要引入这些目录呢。但由于之前提到的三个文件全部不存在,而我的问题实实在在没解决,因此看一看吧。

/etc/mysql/conf.d/下,有mysql.cnfmysqldump.cnf两个文件,均打开,并没有与问题有关的参数出现。

/etc/mysql/mysql.conf.d/目录下,有一个mysql.cnf文件,打开后,哇,看到一个大宝贝,如下:

[mysqld]
pid-file    = /var/run/mysqld/mysqld.pid
socket        = /var/run/mysqld/mysqld.sock
datadir        = /var/lib/mysql
log-error    = /var/log/mysql/error.log
# By default we only accept connections from localhost
bind-address    = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

看倒数第三行,bind-address = 127.0.0.1,造成问题的罪魁祸首就在这里了,直接把它注释掉。
然后server mysql restart

此时我们再在 windows 主机上用 navicat 连接云服务器上的 mysql,会发现我们的错误信息已经变了:

Host is not allowed to connect to this MySQL server

这是因为我们尝试在远程用 root 用户登录,而 mysql 对 root 用户限制了不允许远程登录导致的,解决这个错误已经不是这篇文章的重点了,不够关于最后这个小瑕疵的解决方案我也顺便贴在这里了。

首先在云服务器上登录 mysql server,在 mysql 中选择 mysql 数据库(USE mysql),更新 root 用户:update user set host = '%' where user = 'root';。现在已经可以顺利使用 navicat 管理云端 mysql 了。

相关文章

暂住......别动,不想说点什么吗?
  • 全部评论(0
    还没有评论,快来抢沙发吧!