ERROR 2003(HY000):无法连接到[IP]上的MySQL服务器(111"连接被拒绝")

时间:2017-09-24 17:35:34

标签: mysql mariadb

我正在尝试从 Server B (Ubuntu 16.04)远程连接到 Server A (Ubuntu 16.04)。我可以在服务器A 上使用远程@ [服务器A IP]使用MySQL,但我无法从服务器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

3 个答案:

答案 0 :(得分:4)

您可以在此处查看您的问题

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

真的需要[Server B IP]这里

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

这应该这样做。

答案 1 :(得分:0)

MYSQL绑定地址设置为0.0.0.0,因为默认情况下它通常只会监听127.0.0.1

RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf

答案 2 :(得分:0)

问题是因为MySQL用户{user}@{IP}具有{IP}作为MySQL主机服务器的IP,而不是连接到主机的服务器的IP; {IP}用于连接服务器客户端的IP。