gpt4 book ai didi

java - 我无法通过 ssh 连接到 mysql,我不知道为什么使用 Java

转载 作者:行者123 更新时间:2023-11-29 23:26:47 25 4
gpt4 key购买 nike

好的 - 所以我已经在这方面工作了一段时间(有时没有工作),我已经快到了,但还没有。我正在尝试隧道进入 mysql 数据库,并且能够使用 Jsch 成功连接 ssh。但是当我尝试连接到数据库时,它给我一个“用户'usernamne'@'localhost'访问被拒绝(使用密码:YES)

这是以下代码:(当然我屏蔽了敏感信息)

public void connect(){
String user = "user";
String password = "password";
String host = "host.com";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = null;
Properties info = new Properties();

JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
jsch.addIdentity("/Users/user/.ssh/id_rsa", password);

session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
if(session.isConnected()){
session.disconnect();
}
session.connect();
conn = DriverManager.getConnection("jdbc:mysql://localhost:4417/dbname","dbuser", "dbpassword");
System.out.print("Connection successful!" + "\n\n");
System.out.print("Connection:" + conn + "\n\n");

Statement stmt = conn.createStatement();
ResultSet resultSet = stmt.executeQuery("SELECT * FROM TABLENAME limit 1");
System.out.print(resultSet);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (JSchException j){
j.printStackTrace();
}


}

我已阅读此处的其他帖子,但尚未找到问题的确切解决方案。我能够正确地 ssh。更有趣的是,如果我在命令行中执行完全相同的过程,它就可以正常工作。所以我不知道为什么它在这里不起作用。

最佳答案

试试这个。它可能对你有帮助。已经在我的设置中进行了测试。享受。 :)

在远程 mysql 数据库中创建数据库用户。

创建由“pass”标识的用户“myuser”@“clientip”;

创建由“pass”标识的用户“myuser”@“%”;

将 mydb.* 上的 ALL 授予 'myuser'@'clientip';

将 ALL ON mydb.* 授予 'myuser'@'%';

同花顺特权;

<小时/>

ssh 隧道代码。

    int assigned_port;   
final int local_port=8080;

// Remote host and port
final int remote_port=3306;
final String remote_host="192.168.150.139";

try {
JSch jsch = new JSch();

// Create SSH session. Port 22 is your SSH port which
// is open in your firewall setup.
session = jsch.getSession("sshuser", remote_host, 22);
session.setPassword("sshpassword");

// Additional SSH options. See your ssh_config manual for
// more options. Set options according to your requirements.
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("Compression", "yes");
config.put("ConnectionAttempts","2");

session.setConfig(config);

// Connect
session.connect();

// Create the tunnel through port forwarding.
// This is basically instructing jsch session to send
// data received from local_port in the local machine to
// remote_port of the remote_host
// assigned_port is the port assigned by jsch for use,
// it may not always be the same as
// local_port.

assigned_port = session.setPortForwardingL(local_port,
remote_host, remote_port);

} catch (JSchException e) {
LOGGER.log(Level.SEVERE, e.getMessage()); return;
}

if (assigned_port == 0) {
LOGGER.log(Level.SEVERE, "Port forwarding failed !");
return;
}

// Database access credentials. Make sure this user has
// "connect" access to this database;

// these may be initialized somewhere else in your code.
final String database_user="myuser";
final String database_password="pass";
final String database = "mydb";

// Build the database connection URL.
StringBuilder url =
new StringBuilder("jdbc:mysql://localhost:");

// use assigned_port to establish database connection
url.append(assigned_port).append ("/").append(database).append ("?user=").
append(database_user).append ("&password=").
append (database_password);

try {
Class.forName(
"com.mysql.jdbc.Driver");
System.out.println(url.toString());
java.sql.Connection connection =
java.sql.DriverManager.getConnection(url.toString());

String q="select * from customer";
PreparedStatement ps=connection.prepareStatement(q);
ResultSet rs= ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1)+rs.getString(2));
}


} catch (ClassNotFoundException |
java.sql.SQLException e) {
LOGGER.log(Level.SEVERE, e.getMessage());
e.printStackTrace();
}
finally{
session.disconnect();
}

关于java - 我无法通过 ssh 连接到 mysql,我不知道为什么使用 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26892873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com