scripting MySQL from BASH over SSH - mysql

I have a CentOS server that I want to script from BASH running it on my laptop.
I want to run a script locally that:
logs into the server over ssh and executes some MySQL statements
copies files that I need to where I need them
The file copying is easy. I can do that.
But how can one connect to a MySQL server over the SSH port and execute statements? I think I am just stuck on the connect part. executing the statements I can build up on a variable and batch execute.
I have an SSH pub/priv keypair from my laptop to this server as well.

You can specify commands to run on the remote machine, as the last argument to ssh:
ssh user#remote 'mysql -u user ...'
The problem with this is that it will be a hassle to deal with the various '" escaping in the mysql command(s).
A better way, in my opinion, is to open an SSH tunnel to the remote machine when you connect:
ssh -L 12341:127.0.0.1:3306 user#server &
This would connect your local 12341 port, to the remote machine's 3306 (mysqld) port. After the connection is done, you can connect to it from your local machine like:
mysql -h 127.0.0.1 -p 12341
So you can place your SQL statements into a file, and cat it into mysql:
cat commands | mysql -h 127.0.0.1 -p 12341
Don't forget to kill the SSH connection after you are done.
Note that tunneling requires the remote server to have PermitTunnel "yes" in its sshd_config.

just use ssh to run mysql on the remote server. For example
ssh user#server 'mysql -uimauser -p imadb -e "select * from table"'.
Everything in quotes will be run remotely by ssh.

You can do what #WishCow said or you can put all MySQL statements in a .sql file, copy this file to server and then call mysql to execute these statements, 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'

Related

Keep ssh session alive after connecting to mysql

I want to ssh into a remote box, and call mysql inside and start a connection. I can retrieve the password and login to mysql so I wanted to make a script to do this in one go:
passwd=$(get_password)
ssh $TEST_BOX << EOT
mysql -u $USER -p$passwd -h $FOO --port=$BAR
EOT
This ssh's into the box, runs the command to run and login to mysql, but it doesn't persist the connection and just drops. Is there some flag I'm missing in the ssh man page to keep this alive so I can actually make queries to the db?
Don't redirect into ssh. Try
ssh -t $hostname 'mysql -u ....'

Run MySQL Script on remote Server

I have a MySQL server installed in a remote Ubuntu VM that i access using a VPN and i have some sql scripts that i want to run on that server, to create new Stored procedures and queries.
The the details of the virtual machine are:
IP: 192.168.58.61
Hostname: les12a.fi.fr
I am using putty and i am writting directly the commands, but i would like to run directly a script that is on my Windows machine.I have tried using Source, and path but this works fine when MySQL is on my machine. What is the correct syntax to do that on remote server?
just do:
shell> mysql -u user -p -h HOSTNAME DBNAME < yourscript.sql
HOSTNAME: put the hostname if could be resolved or the ip of the remote server
enjoy
SSH to your server, and then run:
mysql -u USERNAME -p DATABASE_NAME < scripts.sql
Obviously, your scripts should be stored in scripts.sql, which should be in your working (current) folder.

Accessing to mysql from host to container in cli

I created my container like this:
$ docker run -d -p 33060:3306 myimage
Then I try connect from host to mysql server in container:
$ mysql -uroot -proot -P 33060
I got this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
It odd because in Navicat only I changed the port and work fine:
But If I have the IP of the container:
$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' mycontainer
172.17.0.55
Then I can connect to mysql server successfully:
$ mysql -uroot -proot -h 172.17.0.55
But it is a tedious task have to check the ip each time I create a new container to connect to mysql. There any settings I can do to make this task simpler?
This is not a Docker issue. By default the mysql command-line client will connect to a local (Unix) socket instead of a network one, even if you specify -P.
This behavior is described in the documentation:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given
You have to pass the -hlocalhost option, or you can set your connection defaults in /etc/mysql/my.cnf

SSH directly into MySQL server

i got a remote webserver running with a mysql database.
Right now im using SSH to do any serverside management, and i access the MySQL often. I wondered if its possible for me to make a script that would ssh into the server and if run with "-sql" (subject to change) on the command line it would instead go into mysql.
What i made so far:
#!/bin/bash
if [ "$1" == "-l" ]; then
ssh user#192.168.0.101 //local address for privacy;
mysql -u root -p;
else
ssh user#192.168.0.101
fi
This results in an SSH session and when it ends my computer will try and create a mysql connection on the local machine.
#!/bin/bash
if [ "$1" == "-l" ]; then
ssh user#192.168.0.101 'mysql -u root -p';
else
ssh user#192.168.0.101
fi
This results in a password request and then nothing. I'm assuming its because using ssh with a command expects a response and then shuts down the connection.
Is there any way to do this, i realise that it's not of any significant importance, but it is fun to play around with
Thanks in advance
The mysql command only executes interactively if it's input is a terminal. When you run ssh with a command argument, it doesn't normally allocate a pseudo-tty on the server. So mysql just processes its standard input without displaying a prompt.
Use the -t option to force this:
ssh -t user#192.168.0.101 'mysql -u root -p'
One option you might want to consider for solving this kind of access problem is through the use of the tunneling facility in ssh:
ssh -l user -L 33306:192.168.0.101:3306 192.168.0.101
or maybe
ssh -l user -L 33306:127.0.0.1:3306 192.168.0.101
This creates a port on your local machine (33306) which tunnels to the mysql port (3306) on the remote machine.
Then on your local machine you run a local copy of mysql:
mysql --port=33306 -u root -p
which should connect through the tunneled port to your database.
Try like this. Feed mysql password with the command. So you don't have to enter the password.
ssh user#192.168.0.101 'mysql --user="root" --password="password" --database="database" --execute="My Query;"'
Also I suggest you to set keybased ssh authentication. Hence you can also avoid typing ssh password every time.

Is there a way to connect MySQL with binding local port?

I'm working on detect(ip:port) the login behavior of MySQL on different client, But I only get one client machine to use, what I want is to use the same IP and various(explicitly specific, not random select) PORT to connect the MySQL server, because you can specific a port to bind in your client code.
Is there a way(On command line mysql or MySQL C API) to specific the port number on connecting the MySQL server?
Try This
ssh -i /Users/xxxx/key.pem user#data.server.com -L 53306:localhost:3306 -f sleep 60 >> logfile
mysql -u user -p -h 127.0.0.1 -P 53306