Friday, January 9, 2009

MySQL Access denied for @locahost

So my first blog post is an error really near to me. I struggled with it for too long, since, from the instructions I received, it should have been really easy. So what was my problem you ask?

I installed a brand new MySQL database and I have never set one up before. To keep things interesting, this database was installed in Hardy Heron Ubuntu Linux. My application required a new user to be created with a password and some priveliges. This can be very easily done using PHPMyAdmin, also located in the Debian repositories.

After you've installed phpmyadmin, just go to http://localhost/phpmyadmin in your browser. Log in with your root password, this is the password you chose when you installed the MySQL server. It may also be blank, so try that if your having trouble. No go to the privilages section and add a user.

Now we get to my problem, after I had created a user and set its privilages, I could not connect to the database with that user. The important thing to know is that the host of this new user was set to "%". Which is any database, so I thought I would have no problem. I did...

After looking around I saw that many people have this kind of problem and it stems from many different reasons.
See: http://dev.mysql.com/doc/refman/5.1/en/access-denied.html for a complete list of things to look out for. From what I read the magor issues are:

The MySQL server is not running,
the user does not have the correct permissions to access the database,
the Unix environment was not set up correctly (more common in RPM Linux),
Program that use old passwords,
Password not being generated correctly,
and then my error...

Now for all the other fixes, the're solutions are simplish:
Go to phpmyadmin en make sure you've set the privleges for your new user. Check that the boxes are ticked that should be ticked.

If you cannot connect as root, your environment might not have been set up correctly. Run the "mysql_install_db" script. You can get this script by installing the "debian-helper-scripts" package. If you can connect as root, your environment should be OK.

If you using custom programs or programs where you've configured user passwords for them, make sure you didn't change the user password in mysql.

You should not just insert a new password into the mysql/user table. The password must be hashed first. The easiest thing is just to use phpmyadmin for this.

Now my error:
I created a user with host=%. The problem was that when you try to connect with: "mysql -u user-name database-name", that environment looks at what users may access the database. Usually you already have a user installed with name="" and host="localhost". Because the host is more specific than than the user you created's host, mysql will choose this default user. This user, of course does not have access to your database and so you get an access denied message along the lines of: "Access denied for user ''@'localhost' to database 'database-name'".

To fix this, all you have to do, is set your user to have host=host-name. Where do you do this? In phpmyadmin of course. Another option is to remove the ""@localhost user from the system by doing a delete in phpmyadmin.

I hope this fixed your problem or at least gave you and idea of where to start looking for a fix and that you enjoyed my very first blog :-)

No comments:

Post a Comment