How to fix ERROR 2005 (HY000): Unknown MySQL server host in MySQL
MySQL Error 2005 occurs when the MySQL client cannot resolve or connect to the specified hostname. Common causes include typos in the hostname, incorrect hostname format (including protocol), DNS resolution failures, network connectivity issues, or the port being included in the hostname. Fixing requires verifying the hostname spelling, using correct connection syntax, and testing network connectivity.
Quick fix
Fixes 2005
# Correct format examples:
mysql -h localhost -u root -p
mysql -h 127.0.0.1 -u root -p
mysql -h db.example.com -u root -p
mysql -h mysql-server -u root -p # Docker container name
# INCORRECT formats that cause Error 2005:
mysql -h http://localhost -u root -p # ❌ Do not include http://
mysql -h localhost:3306 -u root -p # ❌ Port in hostname
mysql -h "localhost 3306" -u root -p # ❌ Space instead of colon
mysql -h local host -u root -p # ❌ Space in hostname
mysql -h localhost. -u root -p # ❌ Trailing dot- Verify the hostname spelling and format
- Use separate host and port options in command line
- Test DNS resolution for the hostname
- Test network connectivity to the resolved IP address
# Correct format examples:mysql -h localhost -u root -pmysql -h 127.0.0.1 -u root -pmysql -h db.example.com -u root -pmysql -h mysql-server -u root -p # Docker container name# INCORRECT formats that cause Error 2005:mysql -h http://localhost -u root -p # ❌ Do not include http://mysql -h localhost:3306 -u root -p # ❌ Port in hostnamemysql -h "localhost 3306" -u root -p # ❌ Space instead of colonmysql -h local host -u root -p # ❌ Space in hostnamemysql -h localhost. -u root -p # ❌ Trailing dotERROR 2005 (HY000): Unknown MySQL server host
What this error means
MySQL Error 2005 (HY000) is a client connection error that indicates the MySQL client failed to resolve or reach the specified MySQL server host. This error occurs during the initial connection phase before any authentication is attempted. The error means that either: (1) the hostname is not a valid hostname format, (2) the hostname cannot be resolved to an IP address by the DNS system, (3) the resolved IP address is not reachable from the client, or (4) the hostname syntax itself is malformed. Unlike authentication errors (Error 1045: Access denied) or connection refused errors (Error 2003: Cannot connect), Error 2005 means the client could not even identify where the server is. This is typically a configuration or environment issue rather than a server problem—the server may not even be receiving the connection attempt.
Typical symptoms
- Connection fails immediately with "Unknown MySQL server host" message when attempting mysql command or connecting from an application
- Application displays connection error at startup before any database query is executed
- Error occurs consistently for the same hostname but not for others (e.g., "localhost" works but "db.example.com" fails)
- Connection works in one environment (e.g., local machine) but fails in another (e.g., Docker container, different server)
- MySQL Workbench or GUI tools connect successfully with the same hostname that fails from command line
- Error message includes the hostname that failed, helping identify typos or incorrect format
Common causes
- Hostname contains protocol prefix like "http://example.com" or "https://example.com" instead of just "example.com"
- Hostname is misspelled or contains invalid characters, spaces, or trailing characters
- Port number is included in the hostname parameter instead of using separate -P option (e.g., "localhost:3306" instead of -h localhost -P 3306)
- DNS server is misconfigured, unreachable, or cannot resolve the hostname to an IP address
- Network firewall or routing issue prevents the client from reaching the resolved IP address
- Missing host value in command line (e.g., "mysql --host --user jon" where --host lacks a value)
- Password contains special characters like "@" that confuse the connection string parser when embedded in connection URI
- Host is specified as an IP address with invalid syntax or CIDR notation (e.g., "192.168.1.0/24")
- In cloud environments (AWS RDS, Azure MySQL, Google Cloud), the endpoint/hostname has not yet been provisioned or DNS has not propagated
How to fix "ERROR 2005 (HY000): Unknown MySQL server host"
The most common cause is a typo or incorrect format. Ensure the hostname is exact and does not include protocols or ports.
# Correct format examples:
mysql -h localhost -u root -p
mysql -h 127.0.0.1 -u root -p
mysql -h db.example.com -u root -p
mysql -h mysql-server -u root -p # Docker container name
# INCORRECT formats that cause Error 2005:
mysql -h http://localhost -u root -p # ❌ Do not include http://
mysql -h localhost:3306 -u root -p # ❌ Port in hostname
mysql -h "localhost 3306" -u root -p # ❌ Space instead of colon
mysql -h local host -u root -p # ❌ Space in hostname
mysql -h localhost. -u root -p # ❌ Trailing dotDouble-check your connection command or connection string configuration.
When connecting via command line, always separate the hostname and port using distinct options, not a combined string.
# Correct: separate -h and -P options
mysql -h db.example.com -P 3306 -u root -p
# Correct: default port (3306) does not need -P
mysql -h db.example.com -u root -p
# If using non-default port:
mysql -h 192.168.1.10 -P 3307 -u root -p
# For TCP socket connections (Windows):
mysql -h . -u root -p # Dot means localhost via TCPIf you must use a connection string format (e.g., in code), the syntax is "host:port", but ensure your client library is parsing it correctly.
If using a domain name (not localhost or IP), verify DNS can resolve it.
# Linux/Mac: Use nslookup or dig
nslookup db.example.com
dig db.example.com
host db.example.com
# Windows: Use nslookup
nslookup db.example.com
# All platforms: Use ping to test both resolution and reachability
ping db.example.com
# If nslookup returns "server can't find db.example.com":
# - Hostname does not exist in DNS
# - DNS server is wrong
# - Network has no DNS connectivityIf DNS resolution fails, contact your infrastructure team or DNS provider. For local testing, add the hostname to your hosts file instead:
# Linux/Mac: Edit /etc/hosts
127.0.0.1 db.local
# Windows: Edit C:\Windows\System32\drivers\etc\hosts
127.0.0.1 db.local
# Then connect:
mysql -h db.local -u root -pConfirm the client can reach the IP address where MySQL is running.
# Ping the resolved IP:
ping 203.0.113.45
# Check if MySQL port (3306) is reachable:
telnet 203.0.113.45 3306
# or using netcat:
nc -zv 203.0.113.45 3306
# On Mac/Linux, use bash timeout to avoid hanging:
timeout 5 bash -c "cat < /dev/null > /dev/tcp/203.0.113.45/3306" && echo "Port open" || echo "Port closed"If these network tests fail:
- The MySQL server is not running at that IP
- A firewall is blocking port 3306
- The IP address is not routable from your network
- For cloud databases (AWS RDS, Azure), the security group/NSG rules do not allow your IP
Work with your network or cloud provider to restore connectivity.
In containerized environments, use the service name (not localhost) to connect between containers.
# Docker Compose: use service name from docker-compose.yml
mysql -h mysql -u root -p # "mysql" is the service name
# Docker (bridged network): use container name or IP
mysql -h mysql-container -u root -p
# Kubernetes: use the service DNS name
mysql -h mysql-service.default.svc.cluster.local -u root -p
# Find your service IP:
kubectl get svcNever use 127.0.0.1 or localhost in Docker if MySQL runs in a separate container. Use the container or service name instead.
Cloud-managed databases (AWS RDS, Azure MySQL, Google Cloud SQL) require proper endpoint configuration.
# AWS RDS endpoint example:
mysql -h my-database.c9akciq32.us-east-1.rds.amazonaws.com -u admin -p
# Azure endpoint example:
mysql -h myserver.mysql.database.azure.com -u admin@myserver -p
# Verify the endpoint in your cloud console:
# AWS: RDS Dashboard > Databases > Endpoint
# Azure: MySQL Server > Overview > Server name
# Google Cloud: Cloud SQL Instances > Connection name
# Ensure your client IP is in the security group (AWS) or firewall rules (Azure):
# AWS: RDS > Security group > Inbound rules > MYSQL/Aurora 3306
# Azure: Connection security > Add current client IPWait 1-2 minutes after creating a database before attempting to connect, as DNS propagation takes time. Also verify the database status is "available" or "running" in the cloud console.
If this error occurs in an application (Node.js, Python, PHP, etc.), verify the connection config is correct.
// Node.js / mysql2 - CORRECT
const connection = mysql.createConnection({
host: 'db.example.com',
user: 'root',
password: 'secret',
database: 'mydb'
});
// INCORRECT - includes port in host
const connection = mysql.createConnection({
host: 'db.example.com:3306', // ❌ Error 2005
user: 'root'
});
// INCORRECT - includes protocol
const connection = mysql.createConnection({
host: 'https://db.example.com', // ❌ Error 2005
user: 'root'
});
// CORRECT - port separate
const connection = mysql.createConnection({
host: 'db.example.com',
port: 3306,
user: 'root',
password: 'secret',
database: 'mydb'
});Check your environment variables, config files, or connection strings for typos and correct format.
Advanced notes
Error 2005 is frequently the result of incorrect hostname syntax rather than server problems. MySQL expects a bare hostname or IP address without protocol prefixes, port numbers, or spaces. The distinction between Error 2005 (unknown host) and Error 2003 (connection refused) is important: Error 2005 means the client never reached the server, while Error 2003 means the client reached an IP but the server was not listening. This helps narrow down whether the issue is DNS/network or server-specific.
In multi-region cloud deployments, ensure you are using the correct regional endpoint. For example, AWS RDS endpoints are region-specific (us-east-1.rds.amazonaws.com vs eu-west-1.rds.amazonaws.com). Using the wrong region will cause DNS to return the wrong IP (or not resolve) and trigger Error 2005.
For password-containing connection strings, special characters like "@" in the password can be misinterpreted as the "user@host" separator. URL-encode the password (e.g., "p%40ssw0rd" for "p@ssw0rd") if embedding it in a connection URI.
When debugging Docker networking, remember that containers on custom networks can reach each other by service name, but containers on the bridge network need to use the container's IP or be linked. Use docker network inspect to verify connectivity and docker inspect <container> to check the IP.
For DNS-heavy scenarios, consider adding retry logic and fallback to IP addresses in your connection code, or use a connection pooling library that handles transient DNS failures gracefully.
Frequently asked questions
- What does "2005" mean?
- MySQL Error 2005 (HY000) is a client connection error that indicates the MySQL client failed to resolve or reach the specified MySQL server host. This error occurs during the initial connection phase before any authentication is attempted. The error means that either: (1) the hostname is not a valid hostname format, (2) the hostname cannot be resolved to an IP address by the DNS system, (3) the resolved IP address is not reachable from the client, or (4) the hostname syntax itself is malformed.
- How do I fix "2005"?
- MySQL Error 2005 occurs when the MySQL client cannot resolve or connect to the specified hostname. Common causes include typos in the hostname, incorrect hostname format (including protocol), DNS resolution failures, network connectivity issues, or the port being included in the hostname. Fixing requires verifying the hostname spelling, using correct connection syntax, and testing network connectivity.
- What causes "2005"?
- Hostname contains protocol prefix like "http://example.com" or "https://example.com" instead of just "example.com" Hostname is misspelled or contains invalid characters, spaces, or trailing characters Port number is included in the hostname parameter instead of using separate -P option (e.g., "localhost:3306" instead of -h localhost -P 3306)
- Is the "2005" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- 📚MySQL Official Documentation
- 📖MySQL 8.0 Error Reference - Error 2005(Official Docs)
- 📖MySQL 8.0 Reference Manual - Connecting to MySQL(Official Docs)
- 📖MariaDB Knowledge Base - Error 2005(Official Docs)