编辑:我正在尝试从远程主机访问 Mysql:
mysql -u root -p -h 192.168.56.147
但我有这条信息:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.56.147' (111)
我已经在目标机器上完成了这个:
iptables -I INPUT -j ACCEPT
当我执行 netstat 时,我有这个:
root@opencv:/# netstat -ntlp | grep LISTEN
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1268/mysqld
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1249/sshd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 1482/0
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 1552/1
tcp6 0 0 :::22 :::* LISTEN 1249/sshd
tcp6 0 0 ::1:6010 :::* LISTEN 1482/0
tcp6 0 0 ::1:6011 :::* LISTEN 1552/1
看来我的服务器只在内部监听,而不在外部监听。
这是我的主机文件:
127.0.0.1 localhost
127.0.1.1 opencv
# The following lines are desirable for IPv6 capable hosts
#::1 localhost ip6-localhost ip6-loopback
#ff02::1 ip6-allnodes
#ff02::2 ip6-allrouters
这是我的hosts.allow:
# /etc/hosts.allow: list of hosts that are allowed to access the system.
# See the manual pages hosts_access(5) and hosts_options(5).
#
# Example: ALL: LOCAL @some_netgroup
# ALL: .foobar.edu EXCEPT terminalserver.foobar.edu
#
# If you're going to protect the portmapper use the name "rpcbind" for the
# daemon name. See rpcbind(8) and rpc.mountd(8) for further information.
#
ALL: 192.168.0.0
并且 telnet 也不起作用:
root@DEBIANBASE:~# telnet 192.168.56.147 3306
Trying 192.168.56.147...
telnet: Unable to connect to remote host: Connection refused
根据 Alexander K 的评论,下面是我的 mysql 配置的详细信息:
root@opencv:/# find . -name "my.cnf"
./var/lib/dpkg/alternatives/my.cnf
./etc/mysql/my.cnf
./etc/alternatives/my.cnf
但在每个文件中,都没有带有“bind-address”选项或“skip-networking”选项的行(我在Ubuntu 16.10中)
深入研究后,该文件位于此处:
nano /etc/mysql/mysql.conf.d/mysqld.cnf
我编辑了以下内容:
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1
然后我有了这个:
root@DEBIANBASE:~# mysql -u root -p -h 192.168.56.147
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'192.168.56.153' (using password: YES)
通过在我的本地 MySQL 服务器上执行以下操作添加远程用户后:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.56.153' IDENTIFIED BY 'YOUR PASSWORD' WITH GRANT OPTION;
FLUSH PRIVILEGES;
成功了!谢谢 Alexandre。
答案1
文件 /etc/hosts 和 /etc/hosts.allow 与 MySQL 绑定到哪个 IP 无关。
正如您在此处看到的,它仅绑定到 127.0.0.1(环回)。
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1268/mysqld
您需要编辑 /etc/my.cnf 并进行如下注释:
#skip-networking
#bind-address = 127.0.0.1
这将使其绑定到所有可用的 IP。
显然,如果您有多个 IP(内部和外部),您可以选择仅绑定到内部 IP。
还要确保您有相应的根用户,允许从您正在连接的 IP 进行访问。