how to coneect to amaonz RDS localhost from my pc - php

i try to connect to my amazon RDS server in my home pc
but i cant to do that i get a error:
this is my code:
$mysqli = new mysqli('server.eu-west-1.rds.amazonaws.com:3306',
'serverName', 'serverPassword', 'serverTable');
$mysqli->set_charset('utf8'); var_dump($mysqli);
but i get an error:
in my SecurityGroup at amazon:

There are multiple reasons which could lead to this issue. Few of them are
Placing RDS instance in private subnet
Security group restrictions
NACL restrictions
In your case, its most likely you have attached a security group to amazon RDS instance which is not accessible from outside. Also make sure you have enabled public access to your database instance in RDS.

i fix that
looks at DB Instances in RDS:
dont forget to do yes for Publicly Accessible :)
enter image description here

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.

public webserver trying to query sqlserver on internal network with private IP

I am using php to send a query to an existing database to use existing login credentials for a webbased program to allow access to a members section page. My web server is public and has a public ip as well as a private ip. However, my sql server is sitting on the local lan and does not have a public ip. I am very new to php, but I feel I have a done a good job writing it. the code works amazingly when I hosted the site for internal use with a dummy database on my pc. I set up Apache on my pc to run the php code and set up a sql database for testing purposes. I could access the site and login and out from any other computer in the office, by typing in my 192.168.x.x into the web browser.
Now that the site is moved to the web server and I am linking to an active database on another server it doesnt want to work. I am pretty sure i dont have any coding erors causing this its a configuration issue. I am wondering what ports should be open where? and will existing DB users be able to query the DB from a remote private ip? I realize this may be a beginners question, but I have looked everywhere for days now and my brain is fried. I need a basic checklist of the main things to look for or set when establishing this type of connection.
Website is running on 192.168.1.1 with public ip of 173.72.173.x
SQL DB is running on 192.168.1.2
I log into the sql engine on the sql server with 'user' and 'password' so my config file i use:
$server = "192.168.1.2:3306"; // server to connect to.
$database = "myusers"; // the name of the database.
$db_user = "user"; // mysql username to access the database with.
$db_pass = "password"; // mysql password to access the database with.
$table = "dbo.users"; // the table that this script will set up and use.
And I call it into every file that need to query anything.
So are theses setting right? Or do I need to create another user on my sql engine to access the db from a remote host? and what about ports? sql server has 3306 opened, but that's it?
If you are using MySQL (you mentioned port 3306), you will most likely want to check the my.cnf configuration file on the MySQL server, and have it allow remote connections.
Just do a fast search for bind-address = 127.0.0.1 and comment it by putting a hash# in front of it and disable interface binding.
Since you did not specify the OS you are using, I can't know for sure whether it may be bound to a specific NIC or not. Try it yourself.

Connect to Amazon RDS with PHP

I am trying to connect my RDS Instance with my PHP connection file.
This is what I have in my file:
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'User Name');
define('DB_PASSWORD', 'Password');
define('DB_DATABASE', 'DATABASE');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
I replaced localhost with my endpoint (rds thing) url, username and password with my RDS Instance user and pass and database name the one I've set when I created the instance. But it doesn't seem to work.
Is there anything else I have to do that I am not aware of or should it work?
RDS instances do not have a static IP address attached to them, you always use the endpoint for the host. For example:
database1.jlsdfjhgsdf.us-east-1.rds.amazonaws.com
If you are connecting to the instance with a username other than the root database account, you will need to grant the user privileges.
Check security group settings. If you are connecting from another EC2 instance on Amazon you will need to attach that security group. If you are trying to connect from outside AWS, you will need to whitelist the IP address you are accessing from.
Some ideas:
Try using the actual IP of the instance, then it should work.
Did you authorized access to your DB instance?
You may want to have a look at Get Started with Amazon RDS to properly setup your RDS instance
I was facing a similar issue whilst trying to connect an EC2 Apache server using PHP to the RDS MySQL instance.
Weirdly I could establish a connection via CLI - once in mysql running status will tell you which user youre logged in with, plus the port, server name etc.
Turned out some AMI images have SELinux enforcement - meaning the apache server cant send network requests as pointed out by this gentlemen (http://www.filonov.com/2009/08/07/sqlstatehy000-2003-cant-connect-to-mysql-server-on-xxx-xxx-xxx-xxx-13/)
Other points:
Make sure inbound ports are set for your RDS DB
In MySQL make sure the host is set to '%' as opposed to localhost
Always use the endpoint string to connect as the RDS IP changes
I was recently having a lot of trouble with this also but was able to fix it. I made sure my security groups (for the RDS and for EC2) were allowing each other. I was able to run my script from the terminal and connect to my database also from the terminal, but I couldn't get the script to run/connect to MySQL from a browser. It turns out I did not have mysql-server installed-- once I installed that and restarted httpd and mysqld it worked like a charm.
This article is what led me to installing mysql-server and the service starts/restarts. Hope it helps! -- http://www.rndmr.com/amazon-aws-ec2-easy-web-serverset-up-guide-with-a-mac-and-coda-2-256/
Just accepts all incoming connections.
I also had the connection problem between the ec2 (apache + php server) and the RDS (Mysql server) when following the tutorial at http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateDBInstance.html.
I solved it by using the double quote when specifying the connection value while the guideline is using single quote.
define('DB_SERVER', "localhost");
define('DB_USERNAME', "User Name");
define('DB_PASSWORD', "Password");
define('DB_DATABASE', "DATABASE");
I was trying to connect to my DB instance using node-mysql. I found that I the endpoint that RDS provided me with did a DNS lookup. Followed that up and changed the URL to that one. I was only able to connect with mysql via command line until then. When I changed it to the resulting endpoint after the lookup, node-mysql was finally able to connect.

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.

php access to remote database

Help!
I have a PHP (PHP 5.2.5) script on HOST1 trying to connect to an MySql database HOST2. Both hosts are in Shared Host environments controlled through CPanel.
HOST2 is set to allow remote database connections from HOST1.
The PHP connect I'm using is:-
$h2 = IPADDRESS;
$dbu = DBUSER;
$dbp = DBPASS;
$DBlink = mysql_connect($h2, $dbu, $dbp);
This always fails with:-
Access denied for user '<dbusername>'#'***SOMESTRING***' (using password: YES)
nb: SOMESTRING looks like it could be something to do with the shared host environment.
Any ideas???
BTW: I can make remote connections to HOST2 from my laptop using OpenOffice via ODBC, and SQLyog. The SQLyog and ODBC settings are exactly the same as the PHP script is trying to use.
somestring is probably the reverse-lookup for your web-server.
Can you modify privileges from your cPanel? Have you done anything to allow access from your workstation (ODBC)?
The error-message seems to indicate that you have network-access to the mysql-server, but not privileges for your username from that specific host.
If you're allowed to grant privileges for your database, invoking:
GRANT SELECT ON database.* TO username#ip.address.of.host1 IDENTIFIED BY 'password'
might work for you. I just wrote this out of my head, you might want to doublecheck the syntax in mysql-docs.
Have you read the MySQL documentation on Causes of Access denied Errors?
Have you contacted support for your hosting provider? They should have access to troubleshoot the database connection. People on the internet do not have access.
Do you need to specify the database name? Your account might have access to connect only to a specific database. The mysql_connect() function does not allow you do specify the database, but new mysqli() does. I'm not sure if this is relevant -- it might allow you to connect but give you errors when you try to query tables that aren't in your database.
Are you sure you're using the right password? MySQL allows each account to have a different password per client host. Admittedly, this is not a common configuration, but it's possible. Your hosting provider should be able to tell you.
Just some ideas:
HOST1 does not have remote access to HOST2 (shared host is disallowing)
MySQL account does not have access from HOST1 (IP address specified on account creation, or wildcard)
Edit:
In response to your comment, I meant that HOST1 cannot get to the MySQL port on HOST2. Web services will work, of course, because port 80 is open to the public. As another user pointed out though, you are getting a response, so you are reaching it. I would try specifying the DB, and double checking the account creation command you ran.
For the second piece, I meant this: http://dev.mysql.com/doc/refman/5.0/en/adding-users.html
You can specify what host the username can connect from. If it isn't set to HOST2's IP or the wildcard, HOST2 can't log in with those credentials.
The error message means that you can contact the mySql server, but the user you are trying to log in as, does not have access.
Either the user does not have access at all, or it has access locally, but not from the host you are connecting from.
You should try to use the hostname and port like $h2 = IPADDRESS:3307;

Categories