Home >Database >Mysql Tutorial >How to Connect to a Remote MySQL Database via Command Line?

How to Connect to a Remote MySQL Database via Command Line?

Susan Sarandon
Susan SarandonOriginal
2024-11-05 14:10:02898browse

How to Connect to a Remote MySQL Database via Command Line?

Connecting to Remote MySQL Databases via Command Line

Accessing remote database servers from the command line can be challenging. In this example, an attempt to connect to a Rackspace-hosted database using the command:

mysql -u username -h my.application.com -ppassword

resulted in the error:

ERROR 2003 (HY000):
Can't connect to MySQL server on 'my.application.com' (10061)

Causes of the Error

This error occurs due to an incorrect command syntax or network connectivity issues.

Resolving the Issue

To establish a direct connection to a remote MySQL console, use the following command:

mysql -u {username} -p'{password}' \
    -h {remote server ip or name} -P {port} \
    -D {DB name}

Replace the placeholders with the relevant values:

  • {username}: MySQL username
  • '{password}': MySQL password (no space after -p)
  • {remote server ip or name}: Remote host's IP address or hostname
  • {port}: Port number (usually 3306 for MySQL)
  • {DB name}: Name of the target database

Example

To connect to a local database named "local" with the username "root" and password "'root," use the command:

mysql -u root -p'root' \
        -h 127.0.0.1 -P 3306 \
        -D local

Upon successful connection, you will be switched to the specified database's console.

The above is the detailed content of How to Connect to a Remote MySQL Database via Command Line?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn