线程“主” com.mysql.jdbc.exceptions.jdbc4.CommunicationsException中的AWS RDS MYSQL异常:spark-scala导致通信链接失败

时间:2018-12-24 13:07:13

标签: mysql scala amazon-web-services apache-spark amazon-rds

def main(args: Array[String]) {
    val session = SparkSession.builder().appName("mydataloader").master("local[*]").getOrCreate()
    val jdbcHostname = "myhost.us-east-2.rds.amazonaws.com"
    val jdbcPort = 3306
    val jdbcDatabase = "dbmane"
    val jdbcUsername = "user1"
    val jdbcPassword = "password"
    val jdbcUrl = "jdbc:mysql://${jdbcHostname}:${jdbcPort}/${jdbcDatabase}"

    import java.util.Properties
    val connectionProperties = new Properties()
    Class.forName("com.mysql.jdbc.Driver")
    connectionProperties.put("user", s"${jdbcUsername}")
    connectionProperties.put("password", s"${jdbcPassword}")
    import java.sql.DriverManager
    val connection = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword)
    println("connected " + connection)
}

这是我的Scala Spark代码,用于连接到mysql RDS实例。但是我明白了 Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

成功发送到服务器的最后一个数据包是0毫秒前。驱动程序尚未收到来自服务器的任何数据包。

我可以通过下面给出的Java代码连接到同一对象。

try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("Where is your MySQL JDBC Driver?");
        e.printStackTrace();
        return;
    }
    System.out.println("MySQL JDBC Driver Registered!");
    Connection connection = null;

    String jdbc_url = "jdbc:mysql://" + "myhost.us-east-2.rds.amazonaws.com" + "/" + "dbmane"
            + "?user=" + "user1" + "&password=" + "password";

    try {
        connection = DriverManager.getConnection(jdbc_url);
        System.out.println("Connected  " + connection);

        } catch (SQLException e) {
        e.printStackTrace();
        System.out.println("Connection Failed!:\n" + e.getMessage());
    }

对此有任何帮助吗?为什么从Spark-Scala连接中获取异常。

0 个答案:

没有答案