Home >Database >Mysql Tutorial >How to Fix 'ERROR 2003 (HY000): Can't connect to MySQL server' When Connecting from the Command Line?
Connecting to a Remote MySQL Database from the Command Line
Accessing a remote MySQL database from the local machine command line can be challenging, especially when encountering connection errors. One such error arises when using the following command:
mysql -u username -h my.application.com -ppassword
This error, "ERROR 2003 (HY000): Can't connect to MySQL server on 'my.application.com' (10061)," indicates a failed connection to the remote database.
Causes and the Solution
The error often occurs due to incorrect port configuration, firewall issues, or improper syntax. To resolve it, follow the below steps:
1. Ensure Correct Port Configuration:
Verify that the remote MySQL server is listening on the default port (3306) or a custom port. Use the following command to determine the port:
grep ^port /etc/mysql/my.cnf
2. Check Firewall Settings:
If the remote server is protected by a firewall, ensure that it allows incoming connections on the appropriate port. Allow external access to the port using the following command:
sudo ufw allow <port>
3. Use Proper Syntax:
The correct syntax for connecting to a remote MySQL database is:
mysql -u {username} -p'{password}' \ -h {remote server ip or name} -P {port} \ -D {DB name}
For example, to connect to a database named "local" on a remote server at IP address 127.0.0.1 using the default port, use:
mysql -u root -p'root' \ -h 127.0.0.1 -P 3306 \ -D local
Note: Remember to remove the space after "-p" as specified in the MySQL Command Line Options documentation.
By following these steps, you should be able to connect to the remote MySQL database from your local machine command line.
The above is the detailed content of How to Fix 'ERROR 2003 (HY000): Can't connect to MySQL server' When Connecting from the Command Line?. For more information, please follow other related articles on the PHP Chinese website!