debian下的mysql 5.7沒法從windows主機上遠程訪問:2003/10061錯誤 can't connection

問題描述

基於 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.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 了。

相關文章
相關標籤/搜索