mysql connection with server(cpanel) error - php

Warning: mysqli::__construct(): (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.
try{
$mysqli = new mysqli('cpanelhost','cpanel_db_user','cpanel_db_password','cpanel_db','port');
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli ->connect_error;
exit();
}
$sql = "INSERT INTO table (column1, column2) VALUES ('value1', 'value2')";
$mysqli->query($sql) ;
$mysqli->close();
}
catch(Exception $e){
$mysqli->close();
return $e;
}

I would highly recommend you to:
Ask your hosting provider if they ALLOW remote MySQL connections.
Ask your host if port 3306 is open.
If the above is covered, then you will need to:
Add the IP address of the remote machine ( where you are trying to establish this connection ) to "Remote MySQL" in cPanel.
Use your server's hostname or IP as the MySQL Host in your connection configuration.
And if you still cannot establish a successful connection, I would recommend switching to a managed hosting provider which fully supports remote MySQL connections.

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.

RDS access from my own computer using apache server

Hi guys i m trying to access my RDS instance from my computer using apache in wamp after fiddling around i found out about the security group i checked the security group and made it all traffic all ports all ips 0.0.0.0/0 i even tried to make it my ip but when i tried to run a script in my computer the script is
<?php
$conn = new mysqli("xxxxxxxxxxxxxxxxxxxx.rds.amazonaws.com","xxxxxxxxx","xxxxxx","xxxxx");
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
else
{
echo "yess";
}
?>
this gave me error "Connection failed: 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. "
what to do? any idea would be appreciated.

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.

Can't open connection to remote database

I have php script running on XAMPP on my local PC and I want to access some databse other than one on my localhost. Even if I call script with my own IP address I can't connect to the database.
PHP script looks like:
<?php
$host = $_GET['host'];
$username = $_GET['username'];
$pass = $_GET['pass'];
$database = $_GET['database'];
$con = mysql_connect($host,$username,$pass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("performance_schema", $con);
$zavrsni = "zavrsni";
$result = mysql_query("SELECT `OBJECT_NAME`,`COUNT_INSERT`,`AVG_TIMER_INSERT`,`COUNT_UPDATE`,`AVG_TIMER_UPDATE`,`COUNT_DELETE`,`AVG_TIMER_DELETE` FROM `table_io_waits_summary_by_table` where `OBJECT_SCHEMA` =\"".$database."\"");
while($row = mysql_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
mysql_close($con);
?>
I call this script as:
http://zavrsni.noip.me/dohvat.php?username=root&pass=ficko1&database=zavrsni&host=zavrsni.noip.me
And then I get error:
Warning: mysql_connect(): 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:\xampp\htdocs\dohvat.php on line 8
Could not connect: 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.
I know that this is not the most secure way of connecting the database, but I'm new to PHP so don't judge the terrible code.
Your database is not open for connections from outside.
Log in to the server on which the database resides, log in to your database as root and then enter:
USE zavrsni;
GRANT ALL PRIVILEGES ON * to root#% identified by 'ficko1';
or better
GRANT SELECT ON * to root#% identified by 'ficko1';
Please note that it global grants are highly unsure. Learn how to use the privilege system and later grant only those privileges which are actually needed.
If this still does not work, your mysql server is not permitting connections from outside. Have a look at http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

MySQL Connection String Using PHP

I am trying to connect to the database that is hosted on my server through my local machine.
my server has cPanel 11, and it is a typical shared server, powered by CentOS, PHP and MySQL installed.
to be precise i am holding the reseller account in the same server. i want to access the database between different accounts or domains.
in MySQL connection String i tried defining the domain name, and it isn't working. here is what i tried.
<?php
$link = mysql_connect('mydomain.com', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
this is giving the following error
Warning: mysql_connect() [function.mysql-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://bhatkalnews.com:3306) in C:\wamp\www\test\conn.php on line 4
Warning: mysql_connect() [function.mysql-connect]: 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:\wamp\www\test\conn.php on line 4
Fatal error: Maximum execution time of 60 seconds exceeded in C:\wamp\www\test\conn.php on line 4
what do i have to define in connection string to make it work?
Firewall ?
try
telnet mydomain.com 3306
on the command line.
If that doesn't work, the connection is blocked by a firewall, most likely
Did you try adding the port explicitly:
mysql_connect('mydomain.com:3306', 'mysql_user', 'mysql_password');
or the port youre running on if not the default 3306. This of course assumes your server allows remote connections.
Also make sure the user in quest has access from the IP address youre connecting from. This isnt youre issue right now, but it may be the next question you have after you get the server to respond :-)
Please refer,
http://forums.mysql.com/read.php?52,294772
You need to add your IP address/range on Access Host list in Remote MySQL in cPanel.

Categories