falcon
falcon

Reputation: 357

Connect to server database from localhost

What I'm trying to do is connecting to server database from localhost.

$host = 'http://www.my-domain.com/phpmyadmin/';
$user = 'u5er';
$pass = 'pa55w0rd';
$db = 'db_name';

$con = mysqli_connect($host,$user,$pass,$db) or die("Error " . mysqli_error($con));
$sql = "SELECT col FROM test WHERE id = '1'";

$result = mysqli_query($con,$sql);

Errors

Warning: mysqli_connect(): in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 362

Warning: mysqli_connect(): in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 362

Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 362
Error

Is it possible to resolve it? How to find out if my server allows external connections? And how to define from which IP addresses it should allow access to database?

Thank you for any suggestion.

EDIT: Ok, I have changed the host to my-domain.com and now it reports the following errors. My IP has no acces to MySQL server...

Warning: mysqli_connect(): (HY000/1130): Host '88.146.210.54' is not allowed to connect to this MySQL server in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 362
Failed to connect to MySQL: Host '88.146.210.54' is not allowed to connect to this MySQL server
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 368

Upvotes: 0

Views: 43533

Answers (6)

Reids Meke Meke
Reids Meke Meke

Reputation: 357

if you upload your localhost file to web server, some host provider, use "localhost" too as hostname

$host = 'localhost';
$user = 'u5er';
$pass = 'pa55w0rd';
$db = 'db_name';

"Host 'XXX.XXX.XXX' is not allowed to connect to this MySQL server" appear when you didn't configure host to grant accessing database

Upvotes: 2

bitfhacker
bitfhacker

Reputation: 292

Yes, it's possible to resolve that. Change the variable $host to the right hostname (localhost or other).

To find out if your server allows external connections you can use an online port scan to see if you have the mysql port (default is 3306) open to the outside world.

To define which IP addresses can access your database you should use an firewall rule.

Upvotes: 1

user3674632
user3674632

Reputation: 19

Your IP address must be registered to your host in order to connect to the database

Upvotes: 1

Kinnectus
Kinnectus

Reputation: 899

Most external databases are connected to via an IP or domain/subdomain with correct port number. You have put "http" which is port 80. Most MySQL are 3306. Your host/extremal provider should have given you an IP of the database server and logon details...

Upvotes: 1

DMCoding
DMCoding

Reputation: 1237

The host string is wrong. A valid mysql URL will be of the form 'mysql://hostname.tld/database'. However its very unusual that administrators make SQL databases available over the internet. Most likely you should try to run your php application from the same server or network as the database server.

Upvotes: 0

Martin Konecny
Martin Konecny

Reputation: 59671

Your logging has an error is on the following line:

$con = mysqli_connect($host,$user,$pass,$db) or die("Error " . mysqli_error($con));

$con is never assigned a value (base mysqli_connect failed) and you are passing it to mysqli_error().

Try the following instead - it will give you the information you need about why you cannot connect:

$con = mysqli_connect($host,$user,$pass,$db);
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

As user Dagon mentioned, your host string should be something like my-domain.com or even more likely, localhost and not http://www.my-domain.com/phpmyadmin/

Upvotes: 1

Related Questions