Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run MySQL query on remote machine through ssh in command line

I am trying to run MySQL query on remote machine with this command:

ssh [email protected] "mysql -uroot -proot -e \"use test";"" 

I am not able to use that database.

Please suggest a working command.

like image 476
lk121 Avatar asked Jul 09 '13 09:07

lk121


People also ask

How do I connect to a remote MySQL server using SSH?

Open a new terminal window. Direct your local MySQL client to 127.0. 0.1:3306 with the MySQL server username and password. Your connection to the remote MySQL server will be encrypted through SSH, allowing you to access your databases without running MySQL on a public IP.


2 Answers

Try this:

mysql -h host -u root -proot -e "show databases;"; 
like image 90
kun tang Avatar answered Sep 18 '22 09:09

kun tang


Try this:

ssh root@host "mysql database -e 'query to run on table_name; more queries to run;'" 

Same can be done with user@host if that user has permission to execute SQL queries let alone launch mysql in general. Using -e is the same as --execute, which will run whatever you put within the trailing quotes (single or double) and quit. The standard output format would be the same as you would see using --batch.

like image 34
King-Wzrd Avatar answered Sep 19 '22 09:09

King-Wzrd