0

I'm trying to run mysql command into a docker container from a remote machine, through SSH. In particular the command is this one:

ssh user@servername /usr/bin/sudo /usr/bin/docker exec CONTAINER '/usr/bin/mysql --user=USER --password=PASSWORD -e "show databases;"'

I receive this response: ERROR 1049 (42000): Unknown database 'databases;"'

The same command (/usr/bin/sudo /usr/bin/docker exec CONTAINER /usr/bin/mysql --user=USER --password=PASSWORD -e "show databases;") launched on the machine where the container is running, works!

Someone could help me? thank you

  • Can you check if the database name is correct ? Ref: https://stackoverflow.com/questions/19678769/error-1049-42000-unknown-database-mydatabasename – Harshit Jul 01 '20 at 08:01
  • Thank you for the answer. I run "show databases", I don't need to pass a database name to the command. – Franz Pedrotti Jul 01 '20 at 08:04
  • If I was setting this up, I might port-forward the MySQL connection to my local machine (using the `docker run -p` and `ssh -L` options) and then use a local client. That gets around potential shell-quoting issues and doesn't need `sudo`. – David Maze Jul 01 '20 at 11:14

2 Answers2

1

use this command

ssh user@servername 'sudo docker exec CONTAINER mysql --user=USER --password=PASSWORD -e "show databases;"'

We have to pass the complete command (to be executed on remote shell) in quotes('').

Munish
  • 1,377
  • 9
  • 16
  • Thank you for the answer. I also tried this, but got the same error. – Franz Pedrotti Jul 01 '20 at 08:36
  • ssh user@servername '/usr/bin/sudo /usr/bin/docker exec CONTAINER /usr/bin/mysql --user=USER --password=PASSWORD -e "show databases;"' – Franz Pedrotti Jul 01 '20 at 08:40
  • //this is working on my setup, without qoutes i m getting same error as you mentioned ssh test@182.118.10.15 '/usr/bin/docker exec 05102b9ff195 /usr/bin/mysql --user=root --password=root -e "show databases;"' – Munish Jul 01 '20 at 08:46
  • Thank you. I really don't know, i get always that error also with quotes. – Franz Pedrotti Jul 01 '20 at 08:52
0

I resolved using /usr/bin/mysqlshow instad of /usr/bin/mysql. Now it returns the database list correctly. I post the entire command i use to save the mysql docker container database list remotely:

LIST_DB=($(ssh -q USER@SERVERNAME /usr/bin/sudo /usr/bin/docker exec CONTAINER "/usr/bin/mysqlshow -u USER -pPASSWORD" 2>&1 | sed 's/|//g' | sed 's/\-//g' | sed 's/\+//g' |sed -e 's/^[[:blank:]]*//g' | grep -v "Using a password" | grep -Ev "_schema|tmp|innodb|sys|Databases"))