phpMyAdmin error: #2002 Cannot log in to the MySQL server

#2002 Cannot log in to the MySQL server

Setting up PHP, MySQL and phpMyAdmin seems to be a trivial thing on mac. However, occasionally, it throws errors that are hard to debug. As the #2002 error.

My installation consisted of phpMyAdmin 3.4.4, MySQL 5.5 and PHP 5.3.4 under Mac OS X 10.6.8 (Snow Leopard).

Here a guide how to make things work.

You can follow this tutorial or download an automated PHP script which will test your system for PHP, Webserver and MySQL extension availability.
Note: You still need to edit some settings in the script.

First, make sure your MySQL Server is up and running.

Do a sample PHP script to test if you get an MySQL connection at all. Make additional changes to the script depending if you changed default settings. If you installed MySQL server with default options, changes will be minimal. The source of the script can be as follows:

<?php

$db = mysql_connect("localhost", "root", "password");
// Make sure to include your chosen username and password during MySQL installation

if (!$db) die('Could not connect' . mysql_error());
echo 'Connected successfully';

Make sure you include your MySQL Server chosen username and password. Review what kind of error MySQL gives you if any.

If the script says it can not connect to the database server, change localhost to 127.0.0.1 and try again.

That seems to be a problem under some environments. If that is the case, make sure to use the numeric IP (127.0.0.1) in the configuration of phpMyAdmin instead of localhost.

If you get a connection without errors, that means something is wrong with your phpMyAdmin settings.

In the setup script of phpMyAdmin (or config.inc.php depending if you prefer to edit the configuration file in a text editor), make sure you are using socket as Connection type.

Follow the phpMyAdmin quick install instructions if you did not yet setup phpMyAdmin.

Even if after this change you cannot login in your phpMyAdmin administrative interface:

Edit /etc/php.ini, and replace the occurrences of /var/mysql/mysql.sock with /tmp/mysql.sock

This will work if you have installed MySQL with default settings.

As of time of writing, phpMyAdmin 3.4.4 is the newest version. Always make sure you do have the latest version, not to miss bug fixes and new features. Download the latest phpMyAdmin.

If you do not understand some parts of this guide or want to share your opinion, feel free to leave a comment.

About these ads

17 thoughts on “phpMyAdmin error: #2002 Cannot log in to the MySQL server

  1. rob

    hello i need help’

    i tried all the solutions but still access denied every time i make a connection with mysql,
    i think i’ve read and applied all the solution like changing the localhost to 127.0.0.1and changing the localhost to 127.0.0.1 on config.inc.php,my connection type is socket.i also check the c:\windows\system32\drivers\ and the localhost there is also 127.0.0.1..
    i did everything but still went wrong..my o.s is windows..

    this is the scenario..i used manual installation for mysql server but when i found out about xampp..i uninstalled it and used the mysql included from the xampp package..do you think
    that is the reason of connection problem..

    and this one makes me confused..
    $db = mysql_connect(“localhost:3306″, “username”, “password”);
    this line from the php script, am i supposed to put the username and password of my mysql server?or i set new username and password,its like i can put any new user name and password on this line
    $db = mysql_connect(“localhost:3306″, “usename”, “password”);.

    Reply
  2. upcode Post author

    In the function mysql_connect() you should put the actual already existing username and password of your MySQL database. If it connects with any username and password, then your MySQL server is configured to accept any connection, without authentication.

    Reply
  3. rob

    i got it..

    i think my mysql server is not configured to accept any connection, without authentication.

    but what if i could no longer remember my exact username and password of my mysql database?

    what would i do?

    or as i a solution, may i set my configuration to accept any connection,without authentication? if that’s the right solution..please tell me how.

    Reply
  4. Pingback: MySQL server has gone away – where has it gone? | Mr Daz

  5. gunjan

    I have installed Xampp, now I want to install MySQL 5.4 for using it with glassfish server (net beans) for java uses but I could not install it. There is an error at security execution it says that root password does not match

    Reply
    1. upcode Post author

      You need to have root account privileges to install an MySQL server, are you sure you are entering the right root password?

      Reply
  6. Loo

    In my case worked with localhost and tcp connection type, but the MySQL service had to be started (live it Automatic!).

    Reply
  7. Jon

    Splendid, thanks for this. My upgrade from 5.2.x to 5.3.20 broke all connections to “localhost”, but switching to 127.0.0.1 fixed things fine.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s