Run mysql query in remote machine through ssh in c

2019-02-01 08:45发布

Hi i am trying to run mysql query in remote machine

i tried with this

ssh user@192.168.2.26 "mysql -uroot -proot -e \"use test";""

i am not able to use that database

Please suggest working command

Thanks

4条回答
甜甜的少女心
2楼-- · 2019-02-01 09:06

Try this:

mysql -h host -u root -proot -e "show databases;";
查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-01 09:16

MySql seems to have a special command line syntax which includes the database.

mysql -u user -p -e 'SQL Query' database

This documentation is rather old but I got it to work

http://www.cyberciti.biz/faq/run-sql-query-directly-on-the-command-line/

Final working command with ssh:

ssh user@host "mysql -u user -e 'show tables;' databasename"

查看更多
该账号已被封号
4楼-- · 2019-02-01 09:17

This ended up working for me in a bash script:

query='USE [database]; SELECT ...'   
mysql='mysql -u [username] -p[password] -e '"'""$query""'"
ssh [username]@[server] -t "$mysql"

If you want to make it more safe then add a prompt for the password instead of storing it somewhere potentially unsafe.

查看更多
狗以群分
5楼-- · 2019-02-01 09:32

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.

查看更多
登录 后发表回答