Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Error 2005: Unknown MySQL server host, including 'localhost'

ErrorERROR 2005 (HY000): Unknown MySQL server host 'localhost' (11001)

Short answer

Error 2005 comes from the client: the name given as host could not be resolved. The value often comes from an option file or application setting, not from what you typed. Run the client with --no-defaults and an explicit address such as 127.0.0.1, then correct the stored host.

  • Applies to MySQL 8.0, MySQL 8.4 LTS, mysql client 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Connect to the server by IP address and ignore option files, so no stored host value can interfere. If this works, the name was the problem.

    mysql --no-defaults --host=127.0.0.1 --protocol=TCP -u root -p
  2. 2Match what you see, then jump to its fix

    1. 1The host named in the message is a name, and connecting by IP works.The host name cannot be resolved by the clientFix
    2. 2You typed no host, or a correct one, yet the message names a different host.A stored host value in an option file or app config is wrongFix

What does the error look like?

  • ERROR 2005 (HY000): Unknown MySQL server host 'localhost' (11001)
  • ERROR 2005 (HY000): Unknown MySQL server host 'no-such-host' (0)

Cause 1 of 2

The host name cannot be resolved by the client

1Check

The manual's example for this error is a connection to a host name that does not exist.

mysql --no-defaults --host=127.0.0.1 -u root -p -e "SELECT 1"

2Fix

Use the server's IP address; for the local machine the manual recommends 127.0.0.1 instead of localhost to get a TCP/IP connection. Correct the name in your DNS or hosts entry separately.

mysql --host=127.0.0.1 --port=3306 -u root -p

3Verify

The prompt opens and the server answers.

SELECT @@hostname, @@port;

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 2

A stored host value in an option file or app config is wrong

1Check

Programs read the [client] group of option files. A host= line there is used by every client, and command-line options are processed after the files, so --host overrides it. --no-defaults skips the files.

grep -R -n -E "^\s*host\s*=" ~/.my.cnf /etc/my.cnf /etc/mysql/ 2>/dev/null

2Fix

Correct or remove the host line in the option file or the application's database settings, or pass --host on the command line.

[client]
host=127.0.0.1

3Verify

Start the client without any host option; it connects.

mysql -u root -p -e "SELECT 1"

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Use 127.0.0.1 for local TCP connections in application settings.
  • Keep one source of truth for the database host, and do not leave old values in option files.
  • Test a new hostname with the mysql client before you put it in the app.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.