MySQL scripts from BASH via SSH

I have a CentOS server that I want to use a script from BASH by running it on my laptop.

run a script locally, that: - logs onto the server via ssh and executes some mysql statements - copies the files I need, where I need them

Easy file copying. I can do it.

But how can I connect to the MySQL server through the SSH port and follow the instructions? I think I was just stuck on the connecting part. executing statements that I can create for variable and batch execution.

I have an SSH pub / priv keypair from my laptop to this server.

any help?

+5
source share
3 answers

, ssh:

ssh user@remote 'mysql -u user ...'

, "" (-) mysql.

, , SSH :

ssh -L 12341:127.0.0.1:3306 user@server &

12341 3306 (mysqld). , , , :

mysql -h 127.0.0.1 -p 12341

, SQL- mysql:

cat commands | mysql -h 127.0.0.1 -p 12341

SSH- .

, PermitTunnel "yes" sshd_config.

+9

ssh mysql .

ssh user@server 'mysql -uimauser -p imadb -e "select * from table"'.

ssh.

+5

You can do what @WishCow said, or you can put all the MySQL statements in a file .sql, copy this file to the server, and then call mysql to execute these instructions, something like this:

echo "show databases;" > test.sql
echo "use some_database;" >> test.sql
echo "show tables;" >> test.sql
scp test.sql remote-user@remote-server:
ssh remote-user@remote-server 'mysql -u mysql-user -pmysql-pass < test.sql'
+5
source

All Articles