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.
Related
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 ....'
i have a problem with our new database. The only way to access the database is throw a SSH tunnel. But it doesn't work...
I use following commands:
sshpass -p <PASSWORD> ssh <USER>#<DOMAIN> -p <PORT> -L 3306:localhost:3306 -f -N
I think the ssh tunnel works and is established.
Now I want to connect via Shell-File the database.
deposit=`mysql -h localhost --port=3306 -u <DATABASEUSER> --password=<DATABASEPASSWORD> --skip-column-names -e "<MYSQLSYNTAX>"`
But there is always folowing error:
ERROR 1045 (28000): Access denied for user '<DATABASEUSER>'#'localhost' (using password: YES)
Do you have any ideas or am I doing something wrong?
Thank you very much!
Assuming all your permissions are okay, it may be worth swapping localhost for 127.0.0.1.
As per the MySQL docs: http://dev.mysql.com/doc/refman/5.5/en/connecting.html
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 to specify a port
number. To ensure that the client makes a TCP/IP connection to the
local server, use --host or -h to specify a host name value of
127.0.0.1, or the IP address or name of the local server.
As I understand you are trying to create a tunnel between your computer and a remote computer that's running SQL server. In your ssh command substitute 3306:localhost:3306 with the remote computer IP address. Note this should be its internal local IP address if you are not in the same local network as the remote computer. Check your SQL Database permissions, username and password as well.
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
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 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.