...
Posted on Nov 11, 2023 | Category MySQL | 162
Share:

How to access mysql remote database from command line?


Let’s say you have a remote server. You want to access the database from your local machine’s command line. You can use the below command to directly login to a remote mysql console.

mysql -u {username} -p'{password}' -h {hostip} -P {port} -D {database}

-u = User name for login

-p = Password for login

-h = Host Server IP

-P = Port of the remote server

-D = Database you want to connect

* Please don’t give space between -p’{password}’

Example:

mysql -u db_admin -p'pass123456' -h 127.0.0.1 -P 3306 -D testdb

On the other hand, If you’re already logged in to your server then only username and password will be sufficient if the database is inside of it.

mysql -u {username} -p

In this case no database selected. You can check the database list by writting this command.

show databases;

show databases;

Now use the command below to access a specific database

use {database};

or you can directly access the database after login,

mysql -u {username} -p -D {database}

Tags: Web Development