Can't access my AWS hosted database using PHP - 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.

Related

Not able to connect AWS RDS (MYSQL) From EC2

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

Connect remotely to cpanel Database with php

I am trying to access to the database of my live website from localhost. My website is working on cpanel hosting.
I created database and added user to that database but can not connect to this database from my localhost .
this is the code of database information
<?php
$conf['dbuser']='admin_root';
$conf['dbpass']='rootroot!##$';
$conf['dbname']='bsaiiian_os';
$conf['dbhost']='example.com';
$conf["title"]='DEMO';
?>
I think the problem is from $conf['dbhost'] variable where I'm putting the url of my site and not sure if that is right.
When I'm uploading my script to the hosting and put
$conf['dbhost']='localhost';
the connection is established .
this is error message:
Connection failed: No connection could be made because the target machine actively refused it.
Due to security reasons cPanel's MySQL requires that you allow your IP in order to have access.
This is done through:
cPanel -> Remote MySQL Connection
You need to navigate there and allow your local computer's IP in order to make the connection.
Side Note: This stands true for all IPs, even the server's one that your account is hosted on. You will see your cPanel account's IP already present in there - this is to allow access to MySQL for any files from your cPanel account

phpMyAdmin refuses MYSQL connection - How to fix it?

I'm creating a web application in PHP and I need to connect to a database and retrieve information from it. The database in question is being hosted on phpMyAdmin. I'm currently using the following PHP code to connect to it.
//Attempt to connect to the database
$conn = mysql_connect('localhost', 'my_username', 'my_password') or die (mysql_error());
//Tell the user if they were successful
echo "Connection successful!";
//Close the connection
mysql_close();
When I run the website, it produces the following SQL error:
"No connection could be made because the target machine actively refused it."
I'm sure that my username and password are spelled correctly and I believe that 'localhost' is the server name that I need to use. Is there a different mysql_connect command that I need to use for phpMyAdmin? If not, how can I solve this problem?
Edit:
Upon publishing the website to Microsoft Azure (where I need to host it), I've found that it produces a different error:
"An attempt was made to access a socket in a way forbidden by its access permissions."
How will I be able to fix this error? Or will fixing the original error also solve this one?
Do not use mysql_* functions. They are deprecated. For more information, see this question: Why shouldn't I use mysql_* functions in PHP?
phpMyAdmin hosts nothing; it is simply a PHP application that connects to your database just like your own app is attempting to do.
Take a look at the phpMyAdmin config file and ensure you are using the same host. Also try the same username/password. If this succeeds, it's advisable to set up a dedicated username/password for your application. This can be done within phpMyAdmin on the privileges page.
Try use IP instead of localhost.
User has permissions? Check them

Can't connect to remote SQL database in PHP, but remote access is set up and confirmed working

I'm trying to learn to use SQL stuff in PHP but I'm having an issue simply connecting to my database.
The database is almost certainly working with remote access to some extent. I disabled iptables temporarily (I know, bad bad bad!), so there's no firewall blocking access. The MySQL user I am using is configured to be able to connect from any IP. The SQL server is listening properly to all connections and is run on a Linux dedicated server.
[root#1742CC-XEON ~]# netstat -lpnut | grep mysql
tcp 0 0 0.0.0.0:3315 0.0.0.0:* LISTEN 3915/mysqld
I've also used the exact same credentials to successfully remotely connect to my database in Java, like so:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://__ip__:_port_/_dbname_", "username", "password");
System.out.println("Connection established.");
This functions just fine, and I can successfully run queries, etc. Since I have iptables disabled at the moment this connection functions just fine anywhere that has Java and the Java SQL connector.
Now, I'm trying to connect to the same database in PHP:
$db = mysqli_connect($sql_host, $sql_user, $sql_pass, $sql_db, $sql_port);
And this connection fails with the following error:
Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on 'xx.xx.xx.ip' (111)
Note that $sql_user, $sql_pass, $sql_host, etc. are all the same as the credentials used in the Java connection, so there shouldn't be any issue with the user not being able to access the server from a new IP, since that user is granted access on % and I've made sure that it is indeed able to access the SQL server from multiple IPs not explicitly allowed in the users table. I've also tried different users, including root, and all give the same connection error.
I've spent a few hours looking around but I honestly can't find anything that seems to be the right answer, would really appreciate any help!
Is it possible that this is caused by my webhost? I've tried this script on HostGator webhost as well as the whois.com hosting and both give the same error. Could they possibly be preventing the PHP script from connecting to the remote database?
Have you tried telneting to the server on 3315 from your webhosts?

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