如何在opensuse上连接到mysql localhost?

发布于 2024-12-02 07:12:00 字数 7306 浏览 6 评论 0原文

首先是配置的详细信息:

  1. openSUSE 11.3,安装并运行 Parallels Plesk Panel 10(64 位)服务器
  2. MySQL(无“跳过网络”)
  3. java 1.6.0_24 (Java HotSpot(TM) 64 位服务器 VM(内部版本 19.1) -b02,混合模式))

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) 从本地主机连接到 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 在本地主机上被过滤

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);

对于解决此问题的任何提示,我都会感到遗憾。 马丁

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();
            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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜雨飘雪 2024-12-09 07:12:00

你能修改你的IPTables吗?如果可以的话,尝试添加此规则:

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

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
故事还在继续 2024-12-09 07:12:00

您创建的用户是否具有从本地主机访问数据库的权限?
你是如何创建用户的?

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

任何一个都可以解决问题。我之前在没有指定主机名的情况下遇到过问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文