How to connect to AWS RDS with php? - php

Can't connect to RDS with PHP. I have a simple script:
<?php
define('DB_SERVER', 'rdsendPointName:3306');
define('DB_USERNAME', 'actualusername');
define('DB_PASSWORD', 'passwordThatISetforRDS');
define('DB_DATABASE', 'RDSinstancenamehere');
echo "hello"
/* Connect to MySQL and select the database. */
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
?>
The php file is placed in the www/html folder. I can open the browser and get to the php page but then I get the connection error.
Failed to connect to MySQL: Unknown MySQL server host
Security group: I have opened the port 3306 and all tcp port but still no luck.
I run the php file from the ec2 instance itself and the result is the same.
It can't get any simpler than this. All I have is a php file, the RDS instance is running (at least that is what I can see from the aws console).

If you look at the documentation for mysqli_connect, the first parameter should be the hostname, and there are optional parameters if you want to specify a non-standard port.
So if you remove the :3306 from the DB_SERVER value, it should work.

Related

Connect database from online PHP site hosted in Heroku

I created a php website and host it online by using Heroku recently. However, I couldn't connect my database because I use localhost phpmyadmin database from my online site. How should I connect my localhost database from my online site? Or how to make my host my database online, and then make the connection? Here is my db.php file to make the connection with mysqli, in case if there's any need to tweak the db connection PHP codes.
//step 1: Establish database connection
DEFINE("DB_HOST", 'localhost');
DEFINE("DB_USER", 'root');
DEFINE("DB_PASS", '');
DEFINE("DB_NAME", 'my_db_name');
// Create connection
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Set charset to UFT8
mysqli_set_charset($con, "utf8");
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
If you are hosting your database on your local machine, whilst having a webserver that needs to connect to your database, you will have to make sure your database can be accessed by another machine. The host is only ‘localhost’ if the Local IP is identical for the machine and database. Your db.php is correct and will function properly, as long as you ensure that the connection details are correct, since localhost is not correct there will be errors.
You might want to look into how to portforward depending on what database software/installation you are using. Or look into whether your host offers database solutions sch as or similar to PhpMyAdmin (in case of most webhosts).

Error when connecting to external db - mysqli_connect(): (HY000/2002): Connection timed out

I moved my database to an external server because the one my webhosting provider offered is just too limited (no remote access). So I set a MariaDB up on a Vultr server (CentOS 7). everything works fine in phpmyadmin and I already imported the other db.
When trying to connect from my website I get this warning tho:
Warning: mysqli_connect(): (HY000/2002): Connection timed out in blabla/assets/php/config.php on line 8 ERROR: Could not connect. Connection timed out
PHP code
define('DB_SERVER', '11.22.33.44');
define('DB_USERNAME', 'user');
define('DB_PASSWORD', 'pw');
define('DB_NAME', 'data');
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
It worked perfectly fine with the old db so I assume Im missing someting but I can't figure it out.
I tried opening the 3306 port, tried different users, allowing all IPs but nothing works.
Thanks for everyone who tried to help but sadly the problem is not on my end.
Turned out that one.com also does not allow outgoing connections to other databases than their own.

How can I access the localhost/phpmyadmin because all day I got This site can’t be reached?

I am using windows and trying to develop an app in php where I connect a database mySQL created on mySQL workbench. I connect with root and a saved pass. But it seems I cannot connect to the page afterwards.
I use Windows 10.
<?php
define('db_user', 'what is the user?');
define('db_pass', 'mypass');
define('db_host', 'localhost');
define('db_name', 'movies');
&db_conn = mysqli_connect(db_host, db_user, db_pass, db_name);
if(!&db_conn){
die('error connecting to database');
}
echo 'you have connected succesfully';
?>
My database is called movies. I don't know what is the user?
phpMyAdmin is not a database; it is just one of the tools you use to manage what is inside. Your database is MySQL, which should be started and listening on a particular port. The default is usually 3306 or for MariaDB 3307. When MySQL server is located on the same machine as your web server, you can connect via localhost or 127.0.0.1.
If you haven't set up any users in you MySQL server, then the default usually is root with no password. Check your users using this command:
SELECT * FROM mysql.user;
The user must also be authorized for an access via either localhost or IP, whichever you use.
Then to open a connection to MySQL in PHP using the mysqli class, you would usually use this code:
<?php
// your connection code
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'root', '', 'movies');
$mysqli->set_charset('utf8mb4');

mysqli_connect(): (HY000/2002) failed to connect, remote server [duplicate]

This question already has an answer here:
SQLSTATE[HY000] [2002] A connection attempt failed.. - When attempting to connect from Local to remote server
(1 answer)
Closed 3 years ago.
I am trying to connect to a remote server database using PHP code (mysqli)
$host = 'myIP:3306';
$user = 'root';
$pass = 'pass';
$conn = mysqli_connect("$host", "$user", "$pass");
//check connection
if (mysqli_connect_errno())
{
echo "Error: " . mysqli_connect_error();
}
I used myIP/phpmyadmin to make a database and i used putty to check if mysql was running correctly. I also checked the default port for mysql on my server, which was the default (3306).
The error given is described as either a connection fail/time-out/no response.
I also tried using PDO instead of mysqli but it gave me the same results. All other examples i could find where about localhosts and the solution given in those posts is using the socketpath to connect (which doesn't seem like a solution for me as it's remote..).
It's a linux server provided by school.
Any ideas? Thanks in advance.
With mysqli_connect(), you can use 6 parameters:
host, username, password, dbname, port, socket
Host and Port are separate.
Try using:
$conn = mysqli_connect("myIP", "$user", "$pass", "dbname", "3306");
Also, the default port is 3306 so you probably do not even have to provide it.

Why is my php code not connecting to my remote MySql database?

I'm trying to connect to a remote MySql database and I get this error message:
Warning: mysqli_connect(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\myLocalDiectory\RemoteConn.php on line 9 "Resource not found" error 404.
Here's my php file that I'm trying to use:
<?php
define("DB_SERVER", "serverName");
define("DB_USER", "userName");
define("DB_PASS", "password");
define("DB_NAME", "dbName");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
Am I missing something? Thanks for any help!
The first thing that I would check (if you haven't done so) is that you can in fact connect to the database from the computer that runs your PHP script. This to rule out a network or firewall problem.
The first thing would be pinging the server. In a DOS prompt run:
ping servername
Where "servername" is the same string that you put in your PHP script above. If this does not reply with a string similar to the one below, specifically, the first word is not "Reply":
Reply from 192.168.239.132: bytes=32 time=101ms TTL=124
This means that there is most likely no connectivity between the computer running the PHP script an the mysql server. I would then check if the server and the computer are properly connected to the network, if the server is up, an if there is not firewall in your computer running the PHP script or on the server.
Now, if your test above shows "Reply" to the ping, you can test if you can connect to the Mysql service from your php server. For this you can use Mysql workbench (http://dev.mysql.com/downloads/workbench/) and from there create a connection with the database parameters that you are giving to your script. If you cannot connect with Mysql workbench, you might need to disable a firewall in your Mysql server, a firewall in your computer running PHP, or enable the Mysql server to accept remote connections for the database and username that you use in your PHP script (some distributions Mysql server are installed to only accept local connections for safety).
If the problem is a permission in the server (the user can only connect locally but not from a remote computer for instance), you can enable the permission in the Mysql server with the GRANT command: http://dev.mysql.com/doc/refman/5.1/en/grant.html
define("DB_USER", "userName");
Try root instead of userName and define DB_HOST.

Categories