基于 debian 9 的云主机上,按照官方安装说明安装了 mysql 5.7 后,在本地(window 10 环境)使用 navicat 链接此 mysql 报错,报错信息以下:html
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
个人搜索结果如图redis
能够看到,第一个搜索结果讲的是 mysql 8.0 版本的事情,咱们使用的是 5.7 版本,先不看。第二个结果是 mysql connector ,这个和咱们在说的关联不大,也不看。第三个说的是 Troubleshooting(处理重大问题;解决难题) Problems Connecting to MySQL ,感受上和咱们的问题关系很大,因此先看这个。sql
传送门数据库
官方文档的意思是:确认 mysql server 正在运行。windows
按照官方文档给出的验证方法,确认 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套接字文件进行链接,该文件与服务器正在侦听的文件不一样。"网络
若是看到这里还不明白的话,看到接下来的一句话应该能大概理解这一点想要说什么了。socket
"To correct this when you invoke a client program, specify a --port option to indicate the proper port number" --- 谷歌翻译 ===> "要在调用客户端程序时更正此问题,请指定--port选项以指示正确的端口号"tcp
这也就是说确认咱们的端口是不是正确的。
那咱们检查 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.cnf
和mysqldump.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 了。