0

when I try to connect to a MySql database using the actual hostname of the server I receive the following message:

mysql_connect(): Access denied for user 'user'@'ip-...-.internal' (using password: YES) in /home/[username]/sendtitles.php on line 8

Namely,

$conn = mysql_connect("localhost",$user,$password); //WORKS!

$conn = mysql_connect($actual_hostname ,$user,$password); //Doesn't work :(

I would appreciate your help on understanding why that happens.

1
  • Where do you set $actual_hostname and what's its value? Change this code $conn = mysql_connect($actual_hostname ,$user,$password); to this var_dump($actual_hostname); //$conn = mysql_connect($actual_hostname ,$user,$password); and tell us what's the output? Commented Jul 17, 2011 at 18:08

5 Answers 5

7

Your MySQL user can only connect from localhost. To change this, have a look the GRANT command. On a side note I would suggest using a dedicated user for the database instead of root for this.

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

1 Comment

Thanks for the answer, it's the correct but I accepted the oldest. The funny thing is that both attempts where from the same machine that hosts the db, and the acl permitted: username - localhost When I used the hostname the user was perceived as external while when I used localhost I had no problem.
1

You should (almost) always use "localhost" as your hostname.

Your actual host name might be referring to something else. For example... Your website is being hosted on some (shared) server. But when your "actual hostname" is "www.website.com", this URL is redirected to your webserver through its DNS. But obviously, this is not the exact address where your website is hosted. That might be something like "http://www.hosting.com/web/data/yourwebsite".

So either you use "localhost" or you should use the exact direct URL as given to you by your webhost.

Comments

0

Your root is setup with ACL's to allow authentication requests from a specific IP/host.

Review: http://dev.mysql.com/doc/refman/5.1/en/adding-users.html

Comments

0

I think the problem comes either from $actual_hostname, which can contain a mistake, or from $user and $password, those aren't maybe the same as on your local db.
Log these values, and check them.

Comments

-1

perhaps $actual_hostname is not string ..check ... properly .. output it . One more recommendation is to define such variables as CONSTANT . like

define ("DB_SERVER","localhost");
define ("DB_USER","root");
define ("DB_PASS","password");

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.