开发者

How to connect to mysql localhost on opensuse?

first the details of the configuration:

  1. openSUSE 11.3 with Parallels Plesk Panel 10 (64-bit) Server
  2. MySQL installed and running (no "skip-networking")
  3. java 1.6.0_24 (Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode))

The mysql server listens on 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

Now the problem. I can not connect to the mysql from localhost with jdbc (mysql-connector-java-5.1.17-bin.jar) using this class:

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();
开发者_StackOverflow中文版            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 */
                }
            }
        }
    }
}

More details:

telnet gives time out

telnet localhost 3306
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection timed out

nmap shows the port 3306 is filtered on 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

If I connect from outside to the servers 3306 port with the Connect class, everything works fine.

As I think this is something with the firewall. This is the automatically generated firewall from the plesk configuration center.

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

Furthermore I can connect with perl to the local mysql.

$dsn = "dbi:mysql:$database:localhost:3306";
$connect = DBI->connect($dsn, $user, $pw);

I would be regretful for any hint to the solution of this. Martin


Can you modify your IPTables? If you can, try adding this rule:

iptables -I INPUT -s 127.0.0.1 -p tcp --dport 3306 -j ACCEPT


Is your user created with priviledges to access the database from localhost? How did you create the user?

grant all on database.* to 'user'@'localhost' identified by 'password';
grant all on database.* to 'user'@'%' identified by 'password';

Either one might do the trick. I had problems before without the hostname specified.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜