0

I’m trying to connect to a remote server, so I run this test script:

$servername = "remotedomain.com";
$username = "dbusername";
$password = "dbpassword";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

However, I get this response:

Connection failed: Access denied for user 'dbusername'@'www.mydomain.com' (using password: YES)

It’s not trying to connect to the remote server, is trying to connect to my local server, does someone know why?

3
  • Are you using a shared hosting account for these domains? It looks like a remote permissions issue (as in your remote account isn't accepting remote connections to the DB). Commented Nov 22, 2015 at 23:55
  • Possible duplicate of How to connect to a MySQL database on another domain? Commented Nov 22, 2015 at 23:56
  • Please remember to come back and ask questions if something is not clear, or accept a response if it was useful to you. It benefits you, those trying to answer your questions, and the community at large. Commented Nov 24, 2015 at 2:51

2 Answers 2

2

Meaning of Error Message

The error tells you the account you're trying to use to establish a connection does not have the access. (This can be due to incorrect values being used, or actually having no access.) After fixing the values you're using, you should check the account directly in the database and verify that it actually allows remote connections. (Sometimes they're set to allow local connections by default. See below.)

Also, the database account is not the same as a system/OS account; they're independent of the operating system user accounts, so check for those, too.

For example, a connection string should look more like this: [email protected], without the www part.

Database Account Access Confirmation

This is what you should check on the database side to verify if your account does have the remote connection privilege:

mysql> use mysql;
Database changed
mysql> select User, Host from user;
+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| ray              | %         |   <<--- allows remote conns; wildcard accepts any host
| root             | 127.0.0.1 |
| root             | ::1       |   <<--- does NOT allow remote conns
| root             | localhost |
+------------------+-----------+
6 rows in set (0.00 sec)

Notice that those that do not have the % wildcard and instead have a reference to the local host, either by name or IP address, will get an access denied error message. For example:

➜  ~  mysql -h data-hive.local -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'main-hive' (using password: YES)

To play around with account access settings, you should look at the MySQL Documentation.

Sign up to request clarification or add additional context in comments.

Comments

0

Use the IP/Subnet of the host rather than the domain name. also double check to make sure root has access from hosts/IPs other than localhost.

mysql> use mysql;
mysql> SELECT host FROM user WHERE user = 'dbusername';

you need to grant permissions to the IP you are connecting from.

mysql> GRANT SELECT, INSERT ON dbname.* TO 'dbusername'@'10.0.0.1';

Replace

dbname

with the database you want access to (or put in a wildcard), then replace the IP with the address or subnet your client is connecting from.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.