首先是配置的细节:
mysql服务器侦听0.0.0.0:3306
netstat -lnp | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3499/mysqld
unix 2 [ ACC ] STREAM LISTENING 10916 3499/mysqld /var/run/mysql/mysql.sock
现在问题。 我无法使用此类与jdbc(mysql-connector-java-5.1.17-bin.jar)从localhost连接到mysql:
public class Connect {
public static void main(String[] args) {
Connection conn = null;
try {
String userName = "user";
String password = "password";
// This also doesn't work with "localhost" in the connect string.
String url = "jdbc:mysql://127.0.0.1:3306/database";
System.out.println("URL: " + url);
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Database connection established");
} catch (Exception e) {
e.printStackTrace();
System.err.println("Cannot connect to database server");
} finally {
if (conn != null) {
try {
conn.close();
System.out.println("Database connection terminated");
} catch (Exception e) { /* ignore close errors */
}
}
}
}
}
更多详情:
telnet给出时间
telnet localhost 3306
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection timed out
nmap显示端口3306在localhost上过滤
nmap -p 3306 localhost
Starting Nmap 5.00 ( http://nmap.org ) at 2011-08-30 19:21 CEST
Interesting ports on localhost (127.0.0.1):
PORT STATE SERVICE
3306/tcp filtered mysql
Nmap done: 1 IP address (1 host up) scanned in 2.31 seconds
如果我使用Connect类从外部连接到服务器3306端口,一切正常。
我认为这与防火墙有关。这是plesk配置中心自动生成的防火墙。
iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)
target prot opt in out source destination
ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:!0x17/0x02 reject-with tcp-reset
DROP all -- * * 0.0.0.0/0 0.0.0.0/0 state INVALID
ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:10000
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:4306
DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:12443
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:11443
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:11444
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8447
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8443
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8880
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:21
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:587
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:25
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:465
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:110
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:995
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:143
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:993
DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:106
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
ACCEPT tcp -- * * 127.0.0.1 0.0.0.0/0 tcp dpt:5432
DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:5432
ACCEPT tcp -- * * 127.0.0.1 0.0.0.0/0 tcp dpt:9008
ACCEPT tcp -- * * 127.0.0.1 0.0.0.0/0 tcp dpt:9080
DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:9008
DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:9080
DROP udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:137
DROP udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:138
DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:139
DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:445
DROP udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:1194
ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:53
ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53
DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmp type 8 code 0
DROP all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy DROP 0 packets, 0 bytes)
target prot opt in out source destination
ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:!0x17/0x02 reject-with tcp-reset
DROP all -- * * 0.0.0.0/0 0.0.0.0/0 state INVALID
ACCEPT all -- lo lo 0.0.0.0/0 0.0.0.0/0
DROP all -- * * 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
target prot opt in out source destination
ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:!0x17/0x02 reject-with tcp-reset
DROP all -- * * 0.0.0.0/0 0.0.0.0/0 state INVALID
ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0
此外,我可以将perl连接到本地mysql。
$dsn = "dbi:mysql:$database:localhost:3306";
$connect = DBI->connect($dsn, $user, $pw);
对于解决这个问题的任何暗示我都会感到遗憾。 马丁
答案 0 :(得分:0)
你可以修改你的IPTables吗?如果可以,请尝试添加此规则:
iptables -I INPUT -s 127.0.0.1 -p tcp --dport 3306 -j ACCEPT
答案 1 :(得分:0)
您的用户是否使用特权来创建从localhost访问数据库? 你是如何创建用户的?
grant all on database.* to 'user'@'localhost' identified by 'password';
grant all on database.* to 'user'@'%' identified by 'password';
任何一个人都可以做到这一点。我没有指定主机名之前遇到过问题。