【问题标题】:How to setup jdbc connection to Mysql in AWS instance?如何在 AWS 实例中设置与 Mysql 的 jdbc 连接?
【发布时间】:2015-02-25 04:57:06
【问题描述】:

我有一个在 ec2 实例中运行的 mysql,当我尝试使用该 mysql 时,我可以执行以下操作:

dilin-mbp:~ dilin$ ssh -i /Users/dilin/Documents/SelfProject/MM_AWS_MainKey.pem ec2-user@54.69.23.214  
Last login: Tue Feb 24 05:07:47 2015 from c-73-189-116-135.hsd1.ca.comcast.net

   __|  __|_  )
   _|  (     /   Amazon Linux AMI
  ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2014.09-release-notes/
[ec2-user@ip-172-31-45-13 ~]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1969
Server version: 5.5.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from .....;

也就是说,我需要先用一个pem文件访问服务器,然后才能使用mysql。

问题来了,在 JDBC 中,如何配置 pem 文件以使自己连接起来?

我的幼稚实现:

    String url = "jdbc:mysql://54.69.23.214:3306/";
    String dbName = "snapsono";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "pwd";
    SnapList snapList = new SnapList();
    try {
        Driver mysqlDriver = (Driver) Class.forName(driver).newInstance();
        DriverManager.registerDriver(mysqlDriver);
        logger.info("about to get access");
        Connection conn = DriverManager.getConnection(url + dbName,
                userName, password);
        logger.info("get Initialized!!!");
        Statement st = conn.createStatement();
        ResultSet res = st.executeQuery("SELECT * FROM event");
        while (res.next()) {
            int id = res.getInt("userid");
            String idUrl = res.getString("url");
            SnapProfile profile = new SnapProfile(id,idUrl);
            snapList.getList().add(profile);
        }
        conn.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

在目前的实现中,根据日志,我无法建立连接,服务器没有响应。我猜那是因为我没有设置 pem 文件,谁能帮我找出设置?

[INFO] Starting scanner at interval of 40 seconds.
Feb 24, 2015 8:51:16 PM snap.sono.demo.impl.GeneralImpl getDbHello
INFO: >>> enter getDbHello
Feb 24, 2015 8:51:16 PM snap.sono.demo.impl.GeneralImpl getDbHello
INFO: about to get access
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link     failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

谢谢

【问题讨论】:

    标签: java mysql database jdbc


    【解决方案1】:

    这是因为您的 AWS 实例配置为仅从 localhost 连接到端口 3306,更准确地说,它只允许从 EC2-Instance IP 连接。

    所以,
    登录到您的AWS Console 并编辑分配的Security Group Policy 并将其设置为允许从任何IP 连接。
    您可以参考此链接编辑安全组策略,它适用于 SSH,但 MySQL 的过程相同,只是您需要为 MySQL 添加新规则。

    .AWS REF.

    其次,新建mysql用户如下
    create user 'appuser'@'localhost' identified by 'pass123';
    create user 'appuser'@'%' identified by 'pass123';
    grant all privileges on *.* to 'appuser'@'localhost'
    grant all privileges on *.* to 'appuser'@'%';
    这样您就可以从任何 IP 连接到 mysql,或者如果您有一个静态 IP,那么出于安全考虑,我建议您使用它而不是 % :)

    【讨论】:

    • 我知道这可能是端口连接问题,但在安全组中,我授予了我当前 ip 的访问权限,正如您从我的帖子中看到的那样,我确实能够从 ip 获得访问权限,也许我应该使用 ssh 做本地端口转发
    • 我猜你已经从你的 IP 授予了 SSH 访问权限,所以从你的系统在端口 3306 上远程登录你的 EC2,看看它是否能够连接。您系统的防火墙也可能是罪魁祸首。
    猜你喜欢
    • 2012-02-19
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 2019-10-01
    相关资源
    最近更新 更多