mysql - 错误 2003 (HY000) : Can't connect to MySQL server on [IP] (111 "Connection refused")

标签 mysql mariadb

我正在尝试从 服务器 B (Ubuntu 16.04) 远程连接到 服务器 A (Ubuntu 16.04)。我已经可以在 Server A 上通过 remote@[Server A IP] 使用 MySQL,但是我无法从 Server B 访问该用户。我已经完成了检查 ufw 防火墙、配置文件的过程和 MySQL 用户,但我似乎无法找到问题所在。我可以通过哪些可能的步骤来提供更多信息,或者我提供的某些信息存在问题?

带有数据库的服务器A

配置文件

/etc/mysql/my.cnf

# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.

#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/
[mysqld]
innodb-file-format=barracuda
innodb-file-per-table=1
innodb-large-prefix=1
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

[mysql]
default-character-set = utf8mb4

/etc/mysql/mariadb.cnf

# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.

#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/
[mysqld]
innodb-file-format=barracuda
innodb-file-per-table=1
innodb-large-prefix=1
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

[mysql]
default-character-set = utf8mb4

/etc/mysql/mariadb.conf.d/50-server.cnf

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]

#
# * Basic Settings
#
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
# Bind to all addresses
# bind-address      = 127.0.0.1

#
# * Fine Tuning
#
key_buffer_size     = 16M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10

#
# * Query Cache Configuration
#
query_cache_limit   = 1M
query_cache_size        = 16M

#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Enable the slow query log to see queries with especially long duration
#slow_query_log_file    = /var/log/mysql/mariadb-slow.log
#long_query_time = 10
#log_slow_rate_limit    = 1000
#log_slow_verbosity = query_plan
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id      = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size   = 100M
#binlog_do_db       = include_database_name
#binlog_ignore_db   = include_database_name

#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!

#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

#
# * Character sets
#
# MySQL/MariaDB default is Latin1, but in Debian we rather default to the full
# utf8 4-byte character set. See also client.cnf
#
character-set-server  = utf8mb4
collation-server      = utf8mb4_general_ci

#
# * Unix socket authentication plugin is built-in since 10.0.22-6
#
# Needed so the root database user can authenticate without a password but
# only when running as the unix root user.
#
# Also available for other users if required.
# See https://mariadb.com/kb/en/unix_socket-authentication-plugin/

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

# This group is only read by MariaDB-10.0 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.0]

命令的结果

$ netstat -nat |grep :3306
tcp6       0      0 :::3306                 :::*                    LISTEN
tcp6       0      0 127.0.0.1:54160         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:53974         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:42564         ESTABLISHED
tcp6       0      0 127.0.0.1:42566         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:49010         ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:39372         ESTABLISHED
tcp6       0      0 127.0.0.1:54104         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:42576         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:49178         127.0.0.1:3306          TIME_WAIT
tcp6       0      0 127.0.0.1:3306          127.0.0.1:39326         ESTABLISHED
tcp6       0      0 127.0.0.1:39266         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:53976         ESTABLISHED
tcp6       0      0 127.0.0.1:49142         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:49494         ESTABLISHED
tcp6       0      0 127.0.0.1:42590         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:54104         ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:39264         ESTABLISHED
tcp6       0      0 127.0.0.1:49434         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:49216         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:49010         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:39372         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:49142         ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:42590         ESTABLISHED
tcp6       0      0 127.0.0.1:54162         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:54106         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:49494         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:42576         ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:49434         ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:54160         ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:53974         ESTABLISHED
tcp6       0      0 127.0.0.1:39264         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:42566         ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:49216         ESTABLISHED
tcp6       0      0 127.0.0.1:39326         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:54162         ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:39266         ESTABLISHED
tcp6       0      0 127.0.0.1:42564         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:53976         127.0.0.1:3306          ESTABLISHED
tcp6       0      0 127.0.0.1:3306          127.0.0.1:54106         ESTABLISHED

还有

MariaDB [(none)]> SELECT User, Host FROM mysql.user WHERE Host <> 'localhost';
+--------+---------------+
| User   | Host          |
+--------+---------------+
| remote | [ Server A IP ] |
+--------+---------------+

ufw status | grep 3306
3306                       ALLOW       [ Server B IP ]

服务器 B 尝试连接到服务器 A

$ mysql -u remote -h [ Server A IP ] -p
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '[ Server A IP ]' (111 "Connection refused")

还有

ufw status | grep 3306
3306                       ALLOW       [ Server A IP ]

telnet 158.69.174.80 3306
Trying 158.69.174.80...
telnet: Unable to connect to remote host: Connection refused

最佳答案

你可以在这里看到你的问题

MariaDB [(none)]> SELECT User, Host FROM mysql.user WHERE Host <> 'localhost';
+--------+---------------+
| User   | Host          |
+--------+---------------+
| remote | [ Server A IP ] |
+--------+---------------+

真的需要[Server B IP] here

 GRANT ALL ON <DB>.* TO 'remote'@'server_B_IP';

应该这样做。

关于mysql - 错误 2003 (HY000) : Can't connect to MySQL server on [IP] (111 "Connection refused"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46392938/

相关文章:

php - 随机化特定组 mysql

mysql - 如何确保外键在表中只出现有限次数

用于 python 3.7 的 MySQL 包

c++ - MariaDB C++ 连接器编译

mysql - EXISTS 中的递归 CTE 查询

PHP MYSQL 数据库连接和显示数据不工作

mysql - Maria DB 查询以选择,然后更新表以删除不需要的 HTML 和脚本标签

mysql - 在这种情况下,为什么允许 utf8mb4 编码的 MariaDB(版本 10.1.35)索引前缀超过 767 个字节?

mysql - SQL 保留默认顺序但对左连接应用子顺序

php - 将旧的 mysql 与 PDO 混合