establishing mysqli connection using AWS - php

I am trying to establish a connection using mysqli as to connect with the database. Using the same credentials (host, username, and password), I have been able to successfully login in mysql workbench so I know the credentials are correct. However, I constantly receive the following error:
Failed to connect to MySQL:No such file or directory.
<?php
$con = mysqli_connect("...west-2.rds.amazonaws.com","usernameshown in aws rds","password","database name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
?>

Related

Connection without answer, using PHP to Mysql remotely

Hi so im trying to do a conection betwen my Apache server ( Xampp instalation ) with a remotely machine with a Mysql Server.
My php code :
<?php
$servername = '10.4.41.164:3306';
$username = 'admin';
$password = 'admin';
$db = 'AssistMe';
echo "Pepito";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
echo "Connected succesfully";
}
?>
The error that i get :
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
So before puting my question here i chequed that:
If i can do a ping to the server :
Ping to the server
I added a user in mysql that allows conections from anywhere : User in Mysql
I also allow the firewall of the server to connect from the 3306 port : Firewall of the server
And the last test was to Using Mysql WorkBench and testing if i can connect to my Mysql : Mysql WorkBench
So i dont know why i cant connect to my Mysql BDD remotly from my PHP code.

Remote MySQL connection using php not working

I can connect to the remote MySQL database from the MySQL Workbench using these credentials but when I try to connect using the php script I keep getting a connection timed out error. The MySQL server has its bind address already changed, it is running on a Linode server so I am not sure if that changes anything.
I'm using the php connect test script for MySQLi from W3 Schools.
<?php
$servername = "remote-db-IP";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
The exact error I get is
Warning: mysqli::mysqli(): (HY000/2002): Connection timed out in
/home/glavxfrw/public_html/db-connect-test2.php on line 7 Connection
failed: Connection timed out

Connecting to mysql on another server.

The solutions I got are not working for me. Here is my code -
//Connect to database
$tempCon = new mysqli($domain, $username, $password, $database);
if ($tempCon->connect_errno) {
echo "Failed to connect to MySQL Database: (" . $tempCon->connect_errno . ") " . $tempCon->connect_error;
} else {
echo "connected";
}
The error I am getting is -
Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2003): Can't connect to MySQL server on 'domain.info' (110) in /home/server/public_html/products/websites/EditWebsiteContent.php on line 8
Failed to connect to MySQL Database: (2003) Can't connect to MySQL server on 'doamin.info' (110)
Its because of the firewalls on the server which doesnot allow to connect.
If you are using shared hosting. You need to allow remote connecting to your database from your IP address.

connect mysql database in openshift from another apllication/hosting

I have two application in one domain
1) In java and having phpmyadmin/mysql database.
2) In php5.3
I am try to connect first application database using second application.
but its not possible.
return error.
Failed to connect to MySQL: Can't connect to MySQL server on '127.13.153.130' (113)
here is my code to connect phpmyadmin database.
<?php
// Create connection
$con=mysqli_connect("127.13.153.130","adminz6RCQ***","zs4-EbW-****","testjaphp");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo "connected";
}
?>
Help me if I can do this.
thanks.
Your Java Application needs to be a scalable app so your mysql database is put on its own gear. Once its scalable it will have an externally accessible address that you can use to connect to. Simply clone your app to scalable app see here and then check your environment variables to see what your new address is.

Why php PDO uses a different from hostname to the one used by mysql_connect() when connecting to a remotely hosted mysql database?

I've mysql database and php/apache on two different servers: let's say hostphp.domain.com and hostmysql.domain.com.
On the mysql server I've set a user "my_user" with permissions to connect to "my_database" db from the specific host "hostphp.domain.com".
When I connect to it using mysql_connect it does right. But when I do it via php PDO I get this error:
SQLSTATE[42000] [1044] Access denied for user 'my_user'#'%' to database 'my_database'
I've done some tests and I found the problem is ...#'%', mysql is refusing that connection because "my_user" does not have permission to connect from any host.
Also I've tried to connect using mysql_connect with a wrong password to see the error and I get this:
Could not connect: Access denied for user 'my_user'#'hostphp.domain.com' (using password: YES).
The difference is in ..#'%' and ...#'hostphp.domain.com'.
So that's my question, why php pdo do not declare hostname when connecting to a remote host? (or is doing that wrong).
Thanks and sorry for my english.
Edit.
Some code example, this does not work:
try {
$pdo = new PDO(
'mysql:host=hostmysql.domain.com;port=3306;dbname=my_database',
'my_user',
'my_pass'
);
} catch (PDOException $e) {
die($e->getMessage());
}
but this works ok:
$conn = mysql_connect('hostmysql.domain.com', 'my_user', 'my_pass');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
You have successfully connected to the host using mysql_connect, but you got no errors because you did not attempt to select the database.
Your user probably doesn't have access to your database.
Try running mysql_select_db("my_database"); after connected to the host and you should get the same error.

Categories