使用Java通过SSH连接到MySQL [英] Connect to MySQL over SSH using Java

查看:912
本文介绍了使用Java通过SSH连接到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java连接到Amazon EC2服务器上的MySQL数据库.我可以使用Jsch库创建ssh隧道并将端口转发到该服务器,但是连接到DB总是以异常结束

I want to connect to MySQL database on Amazon EC2 server using Java. I am able to create ssh tunnel using Jsch library and forward ports to that server, however connecting to DB always ends with exception

java.io.EOFException:无法读取来自服务器的响应.预期读取4个字节,读取0个字节,然后连接意外丢失.

java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

我认为我陷入了与Hei相同的情况( SSH隧道传输到远程访问MySQL数据库)

I think i am stuck in the same situation as Hei (SSH tunneling to remote access MySQL database)

这是我的代码:



    public static void main(final String[] args) throws SQLException {
            final int lport = 3306;
            final String host = "xxx.xxx.xxx.xxx";
             final String rhost = "xxx.xxx.xxx.xxx";
             final int rport = 3306;
            final String user = "user";
            final String dbuserName = "user";
             final String dbpassword = "password";
            final String dburl = "jdbc:mysql://localhost:" + rport  + "/information_schema?useUnicode=true&characterEncoding=UTF-8";
             final String driverName = "com.mysql.jdbc.Driver";
             Session session = null;

            try {
                //Set StrictHostKeyChecking property to no to avoid UnknownHostKey issue
                final java.util.Properties config = new java.util.Properties();
                config.put("StrictHostKeyChecking", "no");
                final JSch jsch = new JSch();
                session = jsch.getSession(user, host, 22);
                jsch.addIdentity("path\\to\\pemfile.pem");
                session.setConfig(config);
                session.connect();
                System.out.println("Connected");
                final int assinged_port = session.setPortForwardingL(lport, rhost, rport);
                System.out.println("localhost:" + assinged_port + " -> " + rhost + ":" + rport);
                System.out.println("Port Forwarded");

                //mysql database connectivity
                Class.forName(driverName).newInstance();
                dbConn = DriverManager.getConnection(dburl, dbuserName, dbpassword);

                System.out.println("Database connection established");
                System.out.println("DONE");

            } catch (final Exception e) {
                e.printStackTrace();
            } finally {
                if (dbConn != null && !dbConn.isClosed()) {
                    System.out.println("Closing Database Connection");
                    dbConn.close();
                }
                if (session != null && session.isConnected()) {
                    System.out.println("Closing SSH Connection");
                    session.disconnect();
                }
            }
        }

任何人都可以帮助解决此问题吗?谢谢

Can anyone help to solve this problem? Thanks

推荐答案

使用AppFog(在AWS之前)遇到了完全相同的问题.解决此问题的唯一方法是从连接字符串中删除以下内容:

I've got exactly the same problem using AppFog (in front of AWS). The only way I could workaround this issue was dropping the following from my connection string:

"?useUnicode=true&characterEncoding=UTF-8"

当然,您应该确保这不会影响您的应用程序要求.

Off course, you should be sure that this will not affect your application requirements.

这篇关于使用Java通过SSH连接到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆