views:

228

answers:

8

Hi guys. I'm working through examples from a book on php/mysql development. I'm working on a linux/apache environment.

I've set up a database and a user. I attempt to connect with this line of code:

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

I get this error:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: YES) in /var/www/hosts/dj/connect.php on line 3 unable to connect to database: Access denied for user 'www-data'@'localhost' (using password: YES)

I can only guess what is happening here: I think www-data is a username for apache. Upon the database connection, the credentials being passed in to mysql are not those of my database user, but rather apache's own credentials. Is that what is happening here?

How do I pass in the credentials I've defined for my user ?

edit: By the way - I do have credentials in the variables $db_hostname, $db_username, $db_password.

they are passed in by another file using require_once. If that file can't be found, then I get an error. So, I know that my username and password are being used by my script.

Both my scripts can be seen here: http://pastebin.com/MUneLEib

#

Solved:

Thanks guys.

A couple of you pointed out that I had coded carelessy.

Also, I was particularly pleased by Neo's answer: he told me why the username of the owner of the apache process was being used.

:)

+1  A: 

The username goes into $db_username, and the password goes into $db_password.

Ignacio Vazquez-Abrams
oh — the hostname goes into `$db_hostname`, silly of Ignacio not mentioning...
Adam Kiss
A: 

Make sure you actually assign your database login username and password to those variables before you try to connect. For example, before the line with mysql_connect():

$db_username = 'myuser';
$db_password = 'mypass123';
Riley Watkins
+1  A: 
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
?>
muruga
A: 

I'd recommend you go through a tutorial regarding MySQL and PHP before trying to go any further - just so you understand how it all works.

Try this one: http://www.tizag.com/mysqlTutorial/mysqlconnection.php

Also, documentation is your friend: http://php.net/manual/en/function.mysql-connect.php

Gary
A: 

The error given could mean that the privilege has not been granted, or perhaps the password is incorrect. Check that user is created and granted access, or just issue this to be sure it set to allow access on the mysql server.

$ mysql -u root -p
password:

(blah blah blah from server)

GRANT ALL PRIVILEGES ON db_base.* TO 'db username'@'localhost' IDENTIFIED BY 'some password';

If all privileges is too much, consider giving only the basic permissions needed:

GRANT SELECT,UPDATE,DELETE ON db_base.* TO 'db username'@'localhost' IDENTIFIED BY 'some password';

By the way, the server response to this statement on success is Query OK, 0 rows affected.

wallyk
+1  A: 

All these other answers are so presumptuous, as if you don't know that $db_username means database username and same for password.

The error says that you've specified an username and password. You just specified the wrong ones. You need to use the username and password of MySQL, NOT the system username/password combination, so no, this will not be www-data. This may be root and some password, but again, these credentials are specified within MySQL, and are not (necessarily) the same as the system users and passwords.

Your MySQL installation should have a root user with a default password (which you should promptly change). There are several options: you can add an user via the MySQL command line or use an interface like cPanel or Webmin if your provider has something like this; I've used both of these and they both have easy interfaces to add new MySQL users and assign them privileges.

Also just a tip: I typically create one user per database and give the user full privileges on the database, and then use that user with the application linked to the database.

And then of course, once you create a MySQL user account and give it privileges on your web app's database, fill in that username and password into your script.

Ricket
Hi.I'm using mysql from the commandline. (learning the cli seems about as easy as learning phpmyadmin).I already have a user for the target db, with all privileges to that db.I've taken a debugging step, and replaced all the variables in the $db_connect function with the literal values they represent. So now:$db_server = mysql_connect('localhost', 'mydb', 'mydb_user') I'm now getting an access denied error for this user. So, now I have two problems: I can't connect with what I'm sure are the correct credentials, and I can't abstract those creds out to another php file.:(
roberto
Oh - but at least I'm not getting an "access denied" error directed at the correct user: mydb_user, rather then www-data.
roberto
@roberto You got it a little wrong. mysql_connect takes 3 parameters: hostname, username, password. You later need to use mysql_select_db to select your database, but you don't put the database name in mysql_connect. I haven't looked at your code but your first comment had this mistake, so if your code has a similar mistake then that's your problem.
Ricket
+6  A: 

I just looked at your code! The variable with the username is $database_username but you are using $db_username.. Change your code to:

$db_server = mysql_connect($db_hostname, $database_username, $db_password);

or you could change the line with username with: $db_username='[your mysql user]';//or the username you created

When you don't pass anything it will be the user mysql assumes but it will not get the password so if you hadn't defined $db_password it would say: (using password:NO)

you set $database_username with you user but you are passing $db_username which is not set so the user is the linux username as default when nothing is passed with the password for the mysql user! Since there is no mysql user with that password or privileges or even with that name you are not given access!

That user is www-data which is as you guessed an apache user assigned to client-side requests!

Neo
Thanks.That actually doesn't solve my problem (I'm now using literals instead of variables for mysql_connect())But, it does actually tell me something that I was very curious about: why the connection was being associated with the apache process.
roberto
Yes, if you look in http.ini in the apache folder you'll see that you can set the default mysql user,pass,host and even database. When it's not set which is the default that is what it associates!
Neo
+3  A: 

Hey.

In your login.php you use the variable $database_username, but in your connection function you use $db_username. Try matching them up.

Atli
Thanks for spotting that.Corrected now.Still not getting a connection though.
roberto