Not able to connect AWS RDS (MYSQL) From EC2 - php

This question already has answers here:
PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers
(12 answers)
Closed yesterday.
Hi I am trying to host PHP code in AWS EC2(Windows) server on IIS.
I am able to host the php webpages but the issue is the the webpages are not able to communicate to AWS RDS.
PHP Code for Database:
<?php
$con=mysqli_connect("mysqldatabase.xxxxxxxxxx.ap-south-1.rds.amazonaws.com", "admin", "xxxxxxx", "detsdb", "3306");
if(mysqli_connect_errno()){
echo "Connection Fail".mysqli_connect_error();
}
?>
I am using default VPC rule and in both RDS and EC2 Security Group(Both Inbound and Outbound) I have opened all traffic as well as Opened SG id with port 3306 with each other.
**Error on Webpage: Connection FailServer sent charset unknown to the client. Please, report to the developers **
When I am trying the same code from my personal computer, I am able to communicate with RDS.
I am using Wamp server to communicate and host it locally.
written a simple code to test the functionality that's also working:
<?php
$con = mysqli_connect("mysqldatabase.xxxxxx.ap-south-1.rds.amazonaws.com", "admin", "xxxxx", "detsdb", "3306");
if($con){
echo "Connection Success";
}
else
{
echo "Error";
}
?>
Please help me on this.
Edit: Screenshot attcahed for both Ec2 and RDS SG

On the windows server make a .txt file, then rename the extension to .UDL open the file and use it as a quick way to test the connection.
Next follow this fix:
PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers
If this fails create a second RDS DB in a private subnet and test John's advice about DNS resolving to private IP using the UDL file.

Go to the server and try to connect RDS directly and check whether you can able to access or not.

Basically Its a parameter group issue: so what i did was: Created a new Parameter group and then edited them. Searched all character-set parameters. These are blank by default. edit them individually and select utf8 from drop down list. character_set_results, character_set_server and SAVE.
Now you can able to connect

Related

Can't access my AWS hosted database using PHP

I am having a huge issue with accessing my AWS database from within the php code for a site i am building. I have tried many different connection methods, and all seem to give an error that says
Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on '(the endpoint of my hosted DB)' (111)
I've looked up error 111, but I can not seem to understand why it applies to me in this scenario. I can access my database from any computer, using the same information I'm using here to try to connect. I'm starting to think it's the host I'm using for my site that's the problem. I'm using the free biz.nf host just for testing, but I'm not sure my site host really should have any effect on the php inside my site. Here's my connect statement
$mysql_host="DB endpoint";
$mysql_user="myusername";
$mysql_pass="mypassword";
$my_db="myDBname";
$con = mysqli_connect($mysql_host,$mysql_user,$mysql_pass);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Thanks for the help!
You likely need to update your security group and route tables for your subnets to allow the host on biz.nf to access the RDS instance.
Check your RDS dashboard for the given instance, and examine the settings on your security group and subnets.

PHP MySql unknown server host [duplicate]

This question already has answers here:
Warning: mysqli_connect(): Unknown MySQL server host
(4 answers)
Closed 1 year ago.
I am running Ubuntu 12.04.4 LTS with MySQL 5.5.38 and PHP 5.3.10, using Webmin 1.680 (although I do use the terminal for administration as well). I am on a dynamic IP so I have been using dyndns to host a website, which has been working flawlessly. I want to expand my website to access a mysql database. I am attempting to use PHP to connect to mysql, specifically a specific database I set up using Webmin. However, I keep getting the error:
"Unknown MySQL server host '127.0.0.1:3306'"
I have checked the mysql configuration and it is set to that IP and port. I have also checked my server hosts and that IP is the local host. My router is set to forward port 3306 to my server. This happens whether I connect locally or remote. I am connecting with the following PHP string:
$link = mysqli_connect("127.0.0.1:3306", "username", "password", "dbname");
The solutions I have found in my quest, which do not work... changing the host in the connection string to "localhost:3306" or to my dyndns host name "XYZ.dyndns.org:3306", or to my server's local IP - and changing the mysql binding address to match. I have tried commenting out the binding address in the config file. I have found similar questions asked on this forum and others but none have a solution that works for me. I am new to databases but have done a lot of research on using PHP to manipulate them, I just can't get past this connection error. I have been self-teaching how to run a server and have been able to figure out every problem myself up until this one. I can log into mysql from the terminal on my server but executing the "show databases" command it doesn't list the database I created through webmin, even though I the user to have full access/control of that database.
I had a similar issue when I started out using mysqli. If you need to include the port, it has its own place in mysqli_connect.
$link = mysqli_connect("127.0.0.1", "username", "password", "dbname", 3306);
should work better for you. But I believe 3306 is the default port, so you may be able to just leave it off.

What is my host name for MySQL?

This sounds like a stupid question, but I'm beginning in PHP and MySQL and failing at the first step, trying to connect to the database:
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect("localhost", "root", "root", "ASOIAF.odb");
if (mysqli_connect_errno()) {
echo "Failed to connect to database";
} else {
echo "Connected to database";
}
mysqli_close($con);
?>
</body>
</html>
I don't understand the first host parameter. A yahoo help page said that the host should be "mysql", but when I used this it failed to connect as well. The database, ASOIAF, is in OpenDocument database format (odb), on LibreOffice Base. I'm using Uniserver to run PHP and MySQL; both are currently running. The web application is stored in Uniserver's www folder, and run on Google Chrome through localhost.
What should I be inserting in the host portion of the connect statement? Or have I made a more basic syntax error in the PHP code that is preventing a connection from being made?
What is my host name for MySQL?
We don't know what you have called the computer you are running MySQL on.
A yahoo help page said that the host should be "mysql"
That is specific to Yahoo Web Hosting. It doesn't sound like you are using their service, so that is inapplicable.
I'm using Uniserver to run PHP and MySQL
That suggests the computer running the webserver running PHP and the computer running the MySQL server are the same machine. That makes localhost appropriate.
localhost is the standard name given to a computer on the private network that only it connects to. For MySQL is has a special meaning and causes the connection to be made via a socket instead of TCP/IP (which is more efficient).
What should I be inserting in the host portion of the connect statement?
localhost if the above is correct.
Or have I made a more basic syntax error in the PHP code that is preventing a connection from being made?
I can't see any syntax errors. I suspect the problem may be down to your database configuration. Have you told the MySQL server about ASOIAF.odb? Is that what the database is actually called inside MySQL (as opposed to just being the file that stores the configuration for LibreOffice to connection to that database).
Your biggest mistake is not using mysqli_error to find out what problems PHP is reporting.
if (mysqli_connect_errno()) {
echo "Failed to connect to database";
echo mysqli_error();
use
mysqli_connect("localhost", "root");
Hope it will work

Remote mySQL connection error

I posted this question earlier but only provided a link to the output instead of posting the output here. With the additional details, I trust there will be enough info.
This script works locally on my server but when loaded on a remote server the error shown after the code below is given... Can someone tell me why it works locally and not remotely. A % wildcard has been set for allowed hosts.
<?php
$version_link = mysql_connect('gjinternetsolutions.com', 'gj_Guest', 'Password1');
mysql_select_db("gj_Software", $version_link);
if (mysql_errno())
{
$error = "<p>MySQL error ".mysql_errno().": ".mysql_error()."\n</p>";
die($error);
}
$version_query = "SELECT * FROM `VersionCheck` where `Software`='RedemptionFee'";
$version_result = mysql_query($version_query);
$version_row = mysql_fetch_array($version_result);
if (mysql_errno())
{
$error = "<p>MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$version_query\n</p>";
die($error);
}
$new_version=$version_row['Version'];
$new_URL=$version_row['URL'];
echo "The latest version of the Redemption Fee Module is: ".$new_version;
?>
This is the error that is given from the above script...
Warning: mysql_connect(): Can't connect to MySQL server on 'gjinternetsolutions.com' (4) in /home/nicedayh/public_html/CheckRemoteMySQL.php on line 3
Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/nicedayh/public_html/CheckRemoteMySQL.php on line 4
MySQL error 2003: Can't connect to MySQL server on 'gjinternetsolutions.com' (4)
At first I thought something was wrong with my script but after seeing it work perfectly on the local machine, I am not certain. Several people have tried loading it remotely and had no success so it is not just one remote machine that has an issue.
I think you have to put :3306 after your database location or whatever port your MySQL is on
Are you able to ping gjinternetsolutions.com?
My server in New Zealand recognises this host name, however my server in the US does not. Your A records may have not propagated to your computer yet, or your name servers NS1.GJINTERNETSOLUTIONS.COM and NS2.GJINTERNETSOLUTIONS.COM might be having issues.
A. Is your mysql database listening on the same server/IP that gjinternetsolutions.com is hosted on since that is what you are using to connect to?
B. If it is, is mysql service running on the db server?
C. If it is, is port 3306 open on the firewall to let traffic through?
D. If it is, is your my.cnf file on the db server set to localhost only using bind-address = 127.0.0.1? If so, comment that out.
E. If it's not, do you have mysql database setup to accept connections from a remote server i.e. not localhost?
http://www.rackspace.com/knowledge_center/article/mysql-connect-to-your-database-remotely
or if you use a cpanel
http://my.kualo.com/knowledgebase/1059-how-to-set-your-whm-server-to-use-a-remote-mysql-server.html
One of these is probably the issue. This at least should get somewhere to look.

Unknown MySQL server host

When trying to connect to my database server, i encounter the problem of unknown host:
Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2005): Unknown MySQL server host 'xxxxxxxxxxxxx:port' in index.php on line 18
the line 18 is that line where i try to request to connection the the MySQL server:
$this->db = new mysqli($db_host, $db_user, $db_psw, $db_name);
I host my database on the 1&1 website hosting company.
This is usually the case when name resolving doesn't work on the host. If your connect destination is always the same, you might want to use its IP address for connecting instead.
If you use this code:
$Mysqli= new mysqli('mysql2.servidoreswindows.net:3306',
'usu', 'pass', 'dbname');
you can try to write host without port
That is:
$Mysqli= new mysqli('mysql2.servidoreswindows.net', 'usu', 'pass', 'dbname');
Please pay attention with AWS security groups:
In my case I can connect to RDS from my computer through Telnet and
I can connect through Mysql Workbench also but I cant connect from
my EC2 instance that is within the same VPC as the RDS.
The solution was:
I have created a security group (exampl1) for RDS and assigned to it.
I have created a security group (exampl2) for EC2 and assigned to it.
and I have assigned the RDS security group (exampl1) to the EC2 too. << this saves me.
Info: If your EC2 has assigned 2 or more security groups, then in the RDS security group inbound source has to create rules as many security groups has your EC2 assigned.
Amazon docs says:
The EC2 instance in the VPC shares the same VPC security group with the DB instance.
http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html
I know this is an old question. But I ran into this same issue today, and none of the answers I found were the right one.
The issue in my case ended up being that port 3306 is not open on our firewall, and I was attempting to connect to an internal mysql database from an external server.
I'm not sure why the error it gives you is "Unknown Host" in this case. I would have expected something more like "Unable to connect." or "Connection refused.", but using the exact same code from an internal server worked fine, leading me to this conclusion.
Make sure you're not including the "http://" part. I was using http://example.com in my .env file. When I left it as example.com it worked.

Categories