Keep ssh session alive after connecting to mysql - 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 ....'

Related

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.

How to unblock with mysqladmin flush hosts

I have gone through similar cases listed here but it doesn't seem to work.
I was using MySQL Workbench to establish a connection with my database which is hosted on another server. Tried a few times and unable to connect followed by this error. I am confused as to where I should even do this flush. On PhpMyadmin under the SQL query? Cos when I tried to input the following command, it returns as syntax error. I am using a windows OS thus no shell shell scripting for me to input this information either. I am accessing the database temporarily via Cpanel/ phpmyadmin now.
Please help to tell where I should input this data and if my syntax is wrong. Thanks for help.
mysqladmin flush-hosts;
or
mysqladmin -umyname -pmypassword flush-hosts;
My error message as follows:
Failed to connect to MYSql at 192...* with user myName
Host 'host-92...*.as13285.net' is blocked because of many connection
errors; unblock with 'mysqladmin flush-hosts'
mysqladmin is not a SQL statement. It's a little helper utility program you'll find on your MySQL server... and "flush-hosts" is one of the things it can do. ("status" and "shutdown" are a couple of other things that come to mind).
You type that command from a shell prompt.
Alternately, from your query browser (such as phpMyAdmin), the SQL statement you're looking for is simply this:
FLUSH HOSTS;
http://dev.mysql.com/doc/refman/5.6/en/flush.html
http://dev.mysql.com/doc/refman/5.6/en/mysqladmin.html
You should put it into command line in windows.
mysqladmin -u [username] -p flush-hosts
**** [MySQL password]
or
mysqladmin flush-hosts -u [username] -p
**** [MySQL password]
For network login use the following command:
mysqladmin -h <RDS ENDPOINT URL> -P <PORT> -u <USER> -p flush-hosts
mysqladmin -h [YOUR RDS END POINT URL] -P 3306 -u [DB USER] -p flush-hosts
you can permanently solution your problem by editing my.ini file[Mysql configuration file]
change variables max_connections = 10000;
or
login into MySQL using command line -
mysql -u [username] -p
**** [MySQL password]
put the below command into MySQL window
SET GLOBAL max_connect_errors=10000;
set global max_connections = 200;
check veritable using command-
show variables like "max_connections";
show variables like "max_connect_errors";
You can easily restart your MySql service. This kicks the error off.

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

scripting MySQL from BASH over SSH

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'

I can connect to DB through Sequel Pro, but not through command line

I can connect to my db with Sequel Pro just fine, but I can't through the command line. I've followed their instructions here: http://www.sequelpro.com/docs/Connecting_to_a_MySQL_Server_on_a_Remote_Host#Do_I_need_to_use_the_Terminal_for_SSH_connections.3F
In their command I substitute the following from the connection window and SSH tab in Sequel Pro:
ssh -L 1234:mysqlhost:3306 sshuser#sshhost
mysqlhost => MySQL Host
sshuser => SSH User
sshhost => SSH Host
And when prompted for a password, I use the one from "SSH Password"
I'm not sure what Sequel Pro is doing differently behind the scenes.
That instruction from the Sequel Pro docs isn't quite the whole story; it's just telling you how to set up a tunnel. You need a second step to actually use it to connect to a MySQL server.
The actual process is two steps:
Create the tunnel.
ssh -N -L 1234:mysqlhost:3306 sshuser#sshhost
The -N that I added just tells ssh that you're only setting up a tunnel and you don't want to start a shell on sshhost. Running this command will look like it does nothing: that's what it should look like.
As long as the ssh command is running, connections to port 1234 on your local machine will be tunneled through sshhost to port 3306 (the MySQL port) on mysqlhost.
Connect to MySQL using the tunnel.
You now need to run the mysql command line client. The ssh command you just ran is still running, so the easiest thing for you to do is open a new Terminal window or tab, and run:
mysql -P 1234 -u mysqluser -p
to connect to your database. The -P 1234 part is the only out-of-the-ordinary part of this command, and it just makes the mysql client connect using the port you set up in the first command to do the tunneling.
When you're done with the tunnel, either close the original Terminal window or use Ctrl-C to stop the ssh process.