mysql部署後沒法遠程鏈接的緣由(錯誤代碼10061),服務監聽127.0.0.1和0.0.0.0的區別

  在Ubuntu上部署mysql服務並添加了一個非root用戶後,發現沒法遠程鏈接,html

Navicat鏈接mysql出現2003——can't connect to mysql server on localhost(10061)。mysql

在網上搜索了一圈後,幾乎都指向一個解決方案:修改配置文件/etc/mysql/my.cnf中的一項配置:sql

bind-address          = 127.0.0.1

改成:ubuntu

bind-address            = 0.0.0.0

雖然你們都這麼說,可是沒有說明緣由,因而我又找了一番,終於找到了,特轉載於此,vim

原文在這裏,感謝原做者。服務器

========== 如下爲轉載 ==========tcp

服務監聽在127.0.0.1和0.0.0.0上,到底有什麼區別呢?給個實際的例子,你們看看ide

今天發如今nb1380的mysql從nb1381連不上?  oop

一、使用grant加權限,失敗spa

mysql> select user,host,password from mysql.user;
11 rows in set (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'nb1381' identified by '  ' with grant option;

二、telnet失敗,考慮多是iptables限制,加規則,無效

root@nb1380:/var/log/mysql# iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

三、猜想多是監聽在127.0.0.1上,而不是0.0.0.0上,查看my.cnf,果真是這個緣由   

root@nb1380:/var/log/mysql# vim /etc/mysql/my.cnf 
bind-address            = 127.0.0.1 改成:
bind-address            = 0.0.0.0

 重啓mysql,果真telnet通了

root@nb1381:~# telnet nb1380 3306
Trying 192.168.64.43...
Connected to nb1380.
Escape character is '^]'.

 mysql也能鏈接了

root@nb1381:~# mysql -uroot -p -hnb1380
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.6.33-0ubuntu0.14.04.1 (Ubuntu)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

總結:

3306端口監聽在127.0.0.1,只有本機客戶端能夠訪問,其餘服務器沒法訪問

3306端口若是監聽在0.0.0.0上,若是沒有端口限制,那麼其餘服務器則能夠鏈接該服務器的該端口

 ========== 轉載結束 ==========

  文章到這裏還沒結束,經過錯誤代碼10061,我查到了官方文檔的說法。鏈接以下:

https://dev.mysql.com/doc/refman/8.0/en/can-not-connect-to-server.html

這裏剪輯部份內容:

The error (2003) Can't connect to MySQL server on 'server' (10061) indicates that the network connection has been refused. You should check that there is a MySQL server running, that it has network connections enabled, and that the network port you specified is the one configured on the server.

Make sure that the server has not been configured to ignore network connections or (if you are attempting to connect remotely) that it has not been configured to listen only locally on its network interfaces. If the server was started with --skip-networking, it will not accept TCP/IP connections at all. If the server was started with --bind-address=127.0.0.1, it will listen for TCP/IP connections only locally on the loopback interface and will not accept remote connections.

其實官方文檔已經寫清楚了,之後有問題,找官方文檔結合別人的實踐經驗效率更高。

相關文章
相關標籤/搜索