简体   繁体   English

如何在AWS实例中设置与Mysql的jdbc连接?

[英]How to setup jdbc connection to Mysql in AWS instance?

I have a mysql running in an ec2 instance, when I try to use that mysql, I can do follows: 我有一个在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 .....;

That is, I need to first use a pem file to access the server and then I can use mysql. 也就是说,我需要先使用一个pem文件访问服务器,然后才能使用mysql。

Here comes the question, in JDBC, how can I confinger the pem file to make myself get connected? 问题来了,在JDBC中,我该如何配置pem文件以建立连接?

My naive implementation: 我天真的实现:

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

In currently implementation, according to the log, I cannot set up the connection, and server does not response. 在当前的实现中,根据日志,我无法建立连接,服务器也没有响应。 I guess that is because I did not setup the pem file, can anyone help me to finger out the setting? 我想那是因为我没有设置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.

Thanks 谢谢

This is because your AWS Instance is configured to connect to port 3306 from localhost only, more precisely it is allowed to connect from EC2-Instance IP only. 这是因为您的AWS实例配置为仅从本地主机连接到端口3306,更确切地说,仅允许从EC2-Instance IP连接。

So, 所以,
Login to your AWS Console and edit allocated Security Group Policy and make it to Allow to connect from Any IP. 登录到您的AWS Console并编辑分配的Security Group Policy ,并将其设置为允许从任何IP连接。
You can refer to this link for editing Security Group Policy, It is for SSH but process is same for MySQL, except that you will need to add new rule for MySQL. 您可以参考此链接来编辑“安全组策略”,它适用于SSH,但MySQL的过程相同,只是您需要为MySQL添加新规则。

.AWS REF. .AWS参考。

Secondly, Create a new mysql user as follows 其次,如下创建一个新的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'@'%';
So that you would be able to connect to mysql from any IP or If you have a static one then I would suggest you to use that instead of % due to security concerns :) 这样一来,您就可以从任何IP连接到mysql ,或者如果您有一个静态IP,出于安全方面的考虑,我建议您使用它而不是% :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM