1

For example, when trying to connect to server xy.xy.xyz.xyz like below, does the script have to exist on that server to work?

<?php
$servername = "xy.xy.xyz.xyz";
$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";
?>
8protons
  • 3,591
  • 5
  • 32
  • 67
  • 3
    Short answer: no. Long answer: without knowing any details about the network which you are on nor the server settings of the DB server, it is near impossible to tell you why your connection is failing. – MonkeyZeus May 25 '16 at 20:52
  • Short answer , no. You have to make certain that port to the database is open on the DB server, AND that the appropriate privileges are set in MySQL to account for establishing sessions from a remote host. – YvesLeBorg May 25 '16 at 20:52
  • No, that's why you have `$servername = "xy.xy.xyz.xyz"` – Mark Baker May 25 '16 at 20:54

2 Answers2

3

No it doesn't. The DB server has to be configured to allow remote connections though.

You can check this for further details for MySQL.

Community
  • 1
  • 1
hynner
  • 1,352
  • 1
  • 11
  • 20
1

No, what's most likely happening is that your MySQL database isn't allowing users to sign in if they are trying to access it from an outside IP address.

Run the following command through MySQL

SELECT * 
FROM information_schema.user_privileges
WHERE GRANTEE LIKE '%username%';

Change the username value to whatever the actual user name value is. If you're seeing a bunch of rows where the GRANTEE column is 'username'@'localhost' and nothing that looks like 'username'@'%', then your user is being limited to only localhost (same machine) access.

If you want to grant your user non-local access, you can use the following:

GRANT ALL PRIVILEGES
ON mydatabasename.*
TO 'username'@'%'
IDENTIFIED BY 'mypassword'

In the above, % means my user can connect from anywhere. This example grants all administrator rights to your user, but you can change that if you wish.

Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248