Solving “Unknown MySQL server host” in AWS RDS

Every once and a while when your application connects to Amazon RDS you get the error “Unknown MySQL server host”. We have around 100 connections per second and the problem occurs typically 1-10 times per day.

What causes the problem?
The problem is caused by the internal name servers in AWS. Instead of always returning an Ip address they sometimes either time out or return an empty answer.

When the host lookup fails – the Unknown MySQL server host error is displayed.

Solution

By using PHP function gethostbyname(), we can get IP address from DNS name.

<?php $ip_addr = gethostbyname('abcdefgh.stuvwxyz.ap-southeast-1.rds.amazonaws.com'); 
   $con = mysqli_connect($ip_addr,"my_user","my_password","my_db");
?>

Leave a comment