Finding Hostname of MySQL installed on AWS - mysql

I am not able to figure out hostname of my MySQL database which is installed on AWS T2 micro server.
I can connect via phpmyadmin. And connecting via PHP code as localhost works ok too.
However can't find hostname or ip which is needed to connect it from service hosted on other server (where localhost won't be an option)
Tried running following
SELECT variable_value FROM global_variables WHERE variable_name = 'hostname'
However the output is ip-172-31-23-11. This doesn't work as hostname when I try to connect.

The output from SELECT variable_value FROM global_variables WHERE variable_name = 'hostname' is showing you the non fully qualified private DNS name.
This should be fine to connect to this instance from the same VPC but if you need to connect from outside of the VPC then instead you should use the public DNS. This, as with the private DNS, is shown in the EC2 dashboard under the instance details (and elsewhere).
As an example:
You then also need to consider network controls such as VPC ACLs and Security Groups. Make sure the security group of your instance allows access from the originating IP over 3306 (default MySQL port).
You'll also want to check the bind-address in /etc/my.cnf to either allow connections specifically from certain addresses as well as localhost/127.0.0.1 or simply remove or comment out the line to allow MySQL to listen to all incoming traffic.

If the application you're trying to connect to the DB with lives on another server, you'll need to get either the public DNS, public IP address, or create an entry in DNS (Route53) that points "database.example.com" into your public IP/DNS name. You can then use one of those as the connection string within the app.
depending on settings, you may also have to follow some of the instructions here.
you might need to bind mysql to listen on the hosts network interface in order to get outside connectivity.
I suggest using telnet to figure that out. if you can't connect (via the command: "telnet 54.4.54.4 5432" (where 54.4.54.4 is your public ip/DNS/route53 entered hostname) then you need to check your security group as well and make sure port 5432 is open to connections from where you're trying to telnet from.

Related

I am not able to connect to mysql server running on Google Compute engine from another instance using internal IP

I located the /etc/mysql/my.cnf file
I changed the bind-address
first to 0.0.0.0 I received a '111 Connection refused'
then I changed it to the instance I am trying to connect to It still says '111 Connection refused'
the firewall rule allows connection on port 3306 using internal ip so I dont know the problem. Thanks
It seems that you have already taken the necessary steps to connect to MySQL server from GCE your instance. Based on the Stackoverflow case here the recommendation to resolve this problem is to comment this line below (add # at the beginning of the line) in your my.cnf file:
skip-networking
Once done you need to restart MySQL service
sudo service mysql restart
On the GCP side, if you have allowed the IP ranges you want to use with the specified port(3306), it should work fine.
You can also use “nmap” command to verify if port 3306 is open on your GCE instance with MySQL installed.
GCP provides CloudSQL which is a managed MySQL instance. You can access CloudSQL from your GCE VM using private address. This feature reached beta recently. This link provides detailed information about using private IP to connect to your Cloud SQL instances.
Before configuring a Cloud SQL instance to use private IP, you need some steps to be taken. This document provides step by step instructions for configuring an instance to use private IP.
You can use 'netstat -tunlp | grep 3306' command to verify that the MYSQL process is running on port 3306.
This error can also occur when mysql user account does not accept connections from any IP addresses. It may still use localhost parameter to connect to the server. I would recommend adding a user with remote access or granting existing user access to remote sources. You can see this: link for the similar issue and follow the steps recommended there to resolve.

Amazon EC2 and RDS accross AWS Accounts [duplicate]

From EC2 instance i-78a8df00, I'm trying to connect to RDS instance mysql.************.us-east-1.rds.amazonaws.com. They are both in the U.S. East region. I added the security group of EC2 instance (sg-********) to the RDS security group, but that didn't help. It appears to be a firewall/DNS issue as it is timing out when running this command:
ubuntu#ip-10-195-189-237:~$ mysql -h mysql.************.us-east-1.rds.amazonaws.com
ERROR 2003 (HY000): Can't connect to MySQL server on 'mysql.************.us-east-1.rds.amazonaws.com' (110)
I can connect to RDS instance fine from my local machine using the same line as above. I tried various forum solutions but those don't help.
I had similar problem, when I spun a new EC2 instance, but didn't change setting in RDS security group of inbound IP address allowed to connect to port 3306 of my RDS instance.
The confusing bit was an option in RDS dashboard, called Security Groups. You don't need it to solve the problem.
What you really need is:
Go to list of RDS instances
Click on the instance you are trying to connect
Click Security group rules section
This should open a new browser tab or window with details of security group.
Locate several tabs in bottom part, select Inbound rules tab and click Edit button.
Change value to the IP address of your EC2 instance or IPv4 CIDR blocks, e.g.
174.33.0.0/16
To get this value, you can either ssh into your instance and run ip addr or run EC2 Manager in browser and locate value of Private IPs in your instance details.
Additional information for people who might run into similar issues trying to connect to RDS or RedShift:
1) Check security groups
Verify the security group for the RDS instance allows access from the security group your source server belongs to (or its IP added directly if external to AWS). The security group you should be looking at is the one specified in the RDS instance attributes from the RDS console UI (named "security group").
NOTE: Database security groups might be different from AWS EC2 security groups. If your RDS instance is in classic/public EC2, you should check in the "database security group" section of the RDS UI. For VPC users, the security group will be an normal VPC security group (the name sg-xxx will be listed in the RDS instance's attributes).
2) Confirm DNS isn't an issue.
Amazon uses split DNS, so a DNS lookup external to AWS will return the public IP while a lookup internal to AWS will return a private IP. If you suspect it is a DNS issue, have you confirmed different IPs are returned from different availability zones? If different AZs get different IPs, you will need to contact AWS support.
3) Confirm network connectivity by establishing a socket connection.
Tools like tracepath and traceroute likely won't help since RDS currently drops ICMP traffic.
Test port connectivity by trying to establish a socket connection to the RDS instance on port 3306 (mysql, or 5432 for postgres). Start by finding the IP of the RDS instance and using either telnet or nc:
telnet x.x.x.x 3306
nc -vz x.x.x.x 3306
a) If your connection attempt isn't successful and immediately fails, the port is likely blocked or the remote host isn't running a service on that port. you may need to engage AWS support to troubleshoot further. If connecting from outside of AWS, try to connect from another instance inside AWS first (as your firewall might be blocking those connections).
b) If your connection isn't successful and you get a timeout, packets are probably being dropped/ignored by a firewall or packets are returning on a different network path. You can confirm this by running netstat -an | grep SYN (from a different CLI window/session while running and waiting for the telnet/nc command to timeout). Connections in the SYN state mean that you've sent a connection request, but haven't received anything back (SYN_ACK or reject/block). Usually this means a firewall or security group is ignoring or dropping packets.
Check to make sure you're not using iptables or a NAT gateway between your host and the RDS instance. If you're in a VPC, also make sure you allow egress/outbound traffic from the source host.
c) If your socket connection test was successful, but you can't connect with a mysql client (CLI, workbench, app, etc.), take a look at the output of netstat to see what state the connection is in (replace x.x.x.x with the actual IP address of the RDS instance):
netstat -an | grep x.x.x.x
If you were getting a connection established when using telnet or NC, but you see the 'SYN' state when using a mysql client, you might be running into an MTU issue.
RDS, at the time this is written, may not support ICMP packets used for PMTUD (https://en.wikipedia.org/wiki/Path_MTU_Discovery#Problems_with_PMTUD). This can be a problem if you're trying to access RDS or RedShift that's in a VPC from a classic ec2 instance via ClassicLink. Try lowering the MTU with the following, then testing again:
sudo ip link show
# take note of the current MTU (likely 1500 or 9001)
sudo ip link set dev eth0 mtu 1400
If the lower MTU worked, be sure to follow up with AWS customer support for help and mention that you are seeing an MTU issue while trying to connect to your RDS instance. This can happen if TCP packets are wrapped with encapsulation for tunneling, resulting in a lower usable MTU for packet data / payload. Lowering the MTU on the source server allows the wrapped packets to still fit under the limit.
If it didn't work, set your MTU back to it's default and engage AWS support for further troubleshooting.
While Mark's problem seemed to have something to do with multi-AZ routing & EC2 classic, I ran into this exact same problem today.
To fix it, I modified the Security Group that was created automatically with my RDS instance by adding the two private IP addresses from my EC2 instance.
This was a fairly obvious problem, but I'm new to AWS in general, so hopefully this is useful for others like myself.
Apparently, multi-AZ screws everything up. Since the default multi-AZ config placed my database in region us-east-1d, and my EC2 instance was in region us-east-1a, the DNS was not routing correctly. I re-created the RDS instance as non-multi-AZ, and made it live in us-east-1a, and all is happy.
If there are any super geniuses out there in regards to DNS routing on AWS with RDS, ELB, and multi-AZ capabilities, it would be pretty awesome to know how to do this, since this isn't documented anywhere in Amazon Web Service's documentation.
After struggling for 3 days I finally found why mine was not connecting ...
Add outbound rule on your EC2 instance for port 3306 and inbound rules on your RDS server on port 3306. The inbound value should be the security of the EC2 instance
Example:
Your EC2 security group is - sg.ec2
And RDS security group is - sg.rds
So go to edit outbound rules of sg.ec2 and add Custom TCP at port 3306 and destination to 0.0.0.0/0
Then, go to edit inbound rule of sg.rds and add an inbound rule at port 3306 and source as sg.ec2
I had the similar problem today when my EC2 instance suddenly lost access to RDS instance and Wordpress stopped working. The security groups were correct and I could even connect to MySQL from console on EC2 instance but not from PHP. For some reason restarting EC2 server helped me.
Looks like sometime between the last posting and this posting, Amazon fixed the DNS routing issue, because everything works fine now for multi-AZ rds...
Solution: I had to adjust the inbound rule of the RDS instance's security group to allow connections from the IP address of my EC2 instance.
Longer explanation: When creating an AWS RDS instance I selected the option to be able to connect to it via public IP address. In doing so I ASSUMED that I would then be able to connect to it from ANY IP Address, but that was not the case. Rather, when the instance got created/configured, it took the public IP address of my laptop and set the inbound rule of the RDS security group to only accept connections from that IP address. So I had to manually add a rule to allow incoming MYSQL/Aurora connections from IP address of the EC2 instance.
I ran into the same issue, I was not able to connect from EC2 instance to the RDS both resources in the same VPC and multi AZ setup.
The multi-AZ setup was not a problem for me.
I was missing only the OUTBOUND rule 3306 from my EC2 instance Security group, to my RDS Security group.
According to the Amazon doc, we should use:
PROMPT> mysql -h <endpoint> -P 3306 -u <mymasteruser> -p
where endpoint and mymasteruser(username) are from your RDS instance.
I solved that problem using IP public adress directly(of the endpoint) instead of the endpoint(****.us-east-1.rds.amazonaws.com).You can get the ip public address using "ping" command(ping ****.us-east-1.rds.amazonaws.com)

How to make MySQL RDS available to only my IP?

In AWS, I set up an RDS which was originally not publicly available. I set it up using this tuturial. The Apache server that resulted is publicly accessible; the MySQL instance is not.
Now I'm trying to modify access to the RDS to make it where I can get to it with HeidiSQL.
I've added the RDS to the public security group, and tried a few other mods to no effect.
I'd like make this where it can be accessed only from my IP. But in an attempt to just get a connection, I've set it right now where the security group has it wide open. However, when I try to connect from HeidiSQL or Telnet using port 3306, no TCP connection is established.
Here is the configuration. Any idea what I'm missing?
VPC:
Subnet:
Gateway:
RouteTable:
Security Group:
DB Security Group:
RDS:
RDS instance was inside private subnet and that was the reason Apache (inside VPC) was able to connect to it but you were not able to connect from outside. If you change the private subnet route table to make it public by pointing to 0.0.0.0/0 > IGW - it worked.
To make it only accessible from your machine, you need to go to the attached security group and change the TCP 3306 source from open to all to My-IP.

Connect Remotely to mysql running on a Amazon EC2 Instance

I'm trying to connect MySQL Workbench to an Amazon EC2 (Linux) instance that hosts a MySQL Database. (Not RDS but localhost). However for some reason, I can't get it to connect remotely.
Things Ive done:
- Set the security group to allow any IP to access port 3306
- Created a mysql user and granted all privledges on it.
- Modified the my.cnf to include bind-address=0.0.0.0 However i still cant connect.
On this instance I do have SSL cert installed and I am forwarded all http request to https?.. But im not sure if this has anything to do with it.
If anyone could guide my in the righ direction I would appreciate it.
I would personally give it an Elastic IP so it will have a public IP, then bind the mysql to that IP. Make sure the iptables are set to accept the mysql connections. I also wouldnt suggest leaving mysql open to every ip unless this is an absolute necessity.

Connect to MySQL on AWS from local machine

I am trying to set up a dev environment on my local machine that accesses a MySQL DB on AWS, but I keep getting a "Can't connect" message.
mysql_connect('xxx.xxx.xxx.xxx:3306', 'USERNAME', 'PASSWORD');
I also commented out the bind-address in the my.cnf file, and granted permissions to the IP address that is connecting.
Anyone ever successfully get this working?
My experience in Aug-2013 was as follows for an RDS instance created through Elastic Beanstalk.
0) Assuming the RDS instance has already been created
1) Log in to the management console: https://console.aws.amazon.com/console/home
2) Select Services->VPC
3) Select Security Groups (on the left hand side)
4) Select the group whose description says "Security Group for RDS DB..."
5) In the Security Group Selected panel at the bottom of the page, choose "Inbound"
6) Select MySQL as the rule.
7) Type the ip address of my local machine e.g. 145.23.32.15/32
8) Click Add Rule and Apply Rule Changes
After doing this I could connect to the database using mysql from my local machine.
a) From management console select Services->RDS
b) Click on DB Instances (I have only one) and select "Go to Details Page" for the required instance
c) Obtain Host and Port from the endpoint
d) From a terminal session do soemthing like: mysql --host blah.blah.blah.us-west-2.rds.amazonaws.com --port 3306 -u my-user-name -p
If you are using MySql on AWS via an RDS instance you must add the IP address you want to connect from to the "DB Security Groups". To do this go to your AWS Managment Console and select RDS.
1. Select "DB Security Groups" on the left panel
2. Select "default"
3. Select "CIDR/IP" from the select box and enter your workstations public IP address. Example:
23.234.192.123/32 (dont forget the /32 for a single ip)
4. Click "Add"
5. Wait a few minutes for it to go into effect and then connect your MySql client.
This only applies for RDS instances, if you are using MySql installed on an EC2 instance then the instructions are the same as accessing MySql from any remote machine.
I suppose this is firewalled by Amazon, try using a SSH tunnel:
http://blogs.oracle.com/divyen/entry/connecting_mysql_server_on_amazon
Note: Do not open MySQL to the public internet, not even when using IP filtering. SSH tunnels are way more secure. Best part of it: The tunnel could be accessible with localhost:3306 on your machine, no need to change the config : )
I am on a Windows 7 machine, and had to make the following 3 changes to be able to connect to AWS RDB.
VPC Security Group update in AWS Console (similar to what mikemay has above)
From https://console.aws.amazon.com, click on Services (top left) and choose VPC.
Next select Security Groups
Click on the Security Group which has the description "Security Group for RDS DB..."
On the "Inbound" tab, choose "MYSQL" in the Create a New Rule dropdown.
Add your IP address in CIDR format and click on Add Rule.
Click on Apply Rule Changes.
my.cnf update in local MySQL configuration
Change "bind-address = 127.0.0.1" to "bind-address = 0.0.0.0"
Comment out "skip-networking"
Turn OFF Windows Firewall
Go to Control Panel/System and Security/Windows Firewall and turn OFF Windows Firewall.
After these changes, I am able to connect through both
MySQL WorkBench using Database->Connect to Database
Command Prompt with
mysql.exe -h <AWS DB Endpoint> -U <UserName> -P <Port Number, likely 3306> -p
I have been using MySQL Workbench http://www.mysql.com/products/workbench/ with RDS and it works great. Very easy to create and save a new database service instance. Click "New Server Instance" under "Server Administration" and follow the prompts. You will need to enter the information provided in the AWS RDS webpage for that instance (for example, it's endpoint).
NOTE: In order for you to actually connect, you MUST add your IP address in the "DB Security Groups." The link is in the left-hand column, which is titled "Navigation." I use the "CIDR/IP" option (the other is EC2 Security Group). Make sure to include a "/##" after the IP, such as the "/32" they use in the example (you will see it on the page). In a few seconds, the IP address should be authorized.
After that, go back to MySQL Workbench and complete the New Server Instance creation process.
To use the connection, your code might look something like this (that excerpts of my Java code):
String url = "jdbc:mysql://yourdatabasename.foo.us-east-1.rds.amazonaws.com:3306/";
String userName = "your_user_name";
String password = "your_password";
String dbName = "your_db_name";
String driver = "com.mysql.jdbc.Driver";
Connection connection = DriverManager.getConnection(url + dbName, userName, password);
When we create RDS need to configured the firewall to accept mySQL connections from local or other instance, as such the packet is being dropped at the firewall level, to resolve this you need to:
Login into your AWS console Go to RDS
Note down the security group of your mySQL server (in my case awseb-e)
click security groups
click your group in the center menu awseb
click inbound tab
Select mySQL from the list, add the details of your client server and save the rule
NOTE : once you choose my IP ,AWS select your IP , if you need other ip use
https://www.whatismyip.com/my-ip-information/
and add your IP4 IP