Once you know that MySQL has been successfully installed and is running, getting access to a database can be the next hiccup you encounter. The three most important variables for connecting to MySQL are the hostname, username, and password. These values will be matched against the records in the mysql database to approve access.
Next, MySQL will check to see if the user has the permission to run specific queries on specific databases (see Chapter 2, "Running MySQL," for more information). However, an inability to connect to MySQL as a whole is a far more common issue than an inability to run a particular query.
Common solutions to access denied errors are
Reload MySQL after altering the privileges so that the changes take effect. Use either the mysqladmin tool or run FLUSH PRIVILEGES in the mysql client.
Double-check the password used. The error message Access denied for user: 'root@localhost' (Using password: YES) normally indicates that the password is wrong or mistyped. (This is not always the cause but is the first thing to check.)
Use the PASSWORD() function when setting privileges or updating a password.
If you are having trouble using a hostname, try omitting the hostname, using a different hostname (such as localhost if you are working directly on the computer), running mysqladmin flush-hosts (Figure A.6), starting mysqld with --skip-name-resolve, or changing the host setting for the user within the mysql database.
Figure A.6. If you are having particular difficulty with a hostname, flush MySQL's host cache with the mysqladmin tool.
If you are using a .my.cnf, my.cnf, or my.ini file to establish parameters for client programs, don't forget to check these settings (Figure A.7). Also inspect any my.cnf or my.ini files.
Figure A.7. Settings in a .my.cnf file can affect access to a MySQL utility without your awareness.
The error message Can't connect to... (error number 2002) indicates that either MySQL is not running or is not running on the socket or TCP/IP port tried by the client. First check that MySQL is running, and then attempt to connect using a different port or socket.
Another common reason why you cannot connect to MySQL is because the client program cannot find the mysql.sock file. This is a Unix and Mac OS X issue, which I discuss next.
The absolutely most important step you should take in debugging an access issue is to confirm that MySQL is running! All too frequently, a failure to start MySQL is the cause.