Resolving MySQL Error 2005 – (CR_UNKNOWN_HOST) Unknown MySQL server host

Encountering Error 2005 in MySQL can be a frustrating experience. This error code signifies that the MySQL client is unable to resolve the hostname of the MySQL server. It can occur due to various reasons, from DNS issues to incorrect configurations. In this guide, we’ll walk through how to diagnose and fix this error, providing examples and sample code to cover all the possibilities.

Understanding the Error

MySQL Error 2005 – (CR_UNKNOWN_HOST) occurs when the MySQL client cannot determine the IP address of the MySQL server host from the given hostname. This could be due to a typo in the hostname, a DNS resolution issue, or a misconfiguration in the MySQL client or server.

Diagnosing the Error

To diagnose this error, follow these steps:

  1. Verify the Hostname: Ensure that the hostname provided in the connection string is correct. Typos are a common cause of this error.
  2. Check DNS Resolution: Use tools like ping or nslookup to verify that the hostname resolves to an IP address.
  3. Review MySQL Configuration: Check the my.cnf or my.ini file on the server to ensure the bind-address is correctly set, allowing connections from the intended hosts.
  4. Check Hosts File: On some systems, the hosts file (/etc/hosts on Unix/Linux or C:\Windows\System32\drivers\etc\hosts on Windows) may override DNS. Ensure that the file correctly maps the hostname to the server’s IP address.
  5. Firewall Settings: Ensure that any firewalls between the client and server are configured to allow MySQL traffic on the default port (3306).

Fixing the Error

Here are some examples and sample code to help you fix MySQL Error 2005:

Example 1: Correcting the Hostname

Incorrect hostname in the connection string:

mysql -h incorrect_hostname -u user -p

Corrected hostname:

mysql -h correct_hostname -u user -p

Example 2: Testing DNS Resolution

Using ping to check DNS resolution:

ping correct_hostname

If the ping command does not resolve to an IP address, there is a DNS issue that needs to be addressed.

Example 3: Configuring MySQL Server

Checking the bind-address in the MySQL configuration file (my.cnf or my.ini):

[mysqld]
bind-address = 0.0.0.0

Setting bind-address to 0.0.0.0 allows connections from any host, which can be a security risk. It’s better to set it to the specific IP address of the server if possible.

Example 4: Editing the Hosts File

Adding an entry to the hosts file on the client machine:

192.168.1.10 correct_hostname

This maps the correct IP address to the hostname on the client machine, bypassing DNS.

Example 5: Configuring Firewalls

Ensure that the server’s firewall allows incoming connections on port 3306 (default MySQL port) from the client’s IP address.

By carefully diagnosing the cause of MySQL Error 2005 and methodically applying the appropriate fix, you can restore connectivity between your MySQL client and server. Make sure to apply changes cautiously and always back up configuration files before modifying them. If you’re still facing issues after trying these solutions, consider reaching out to your network administrator or seeking further assistance from MySQL support forums.

Leave a Comment