# Netssh and mysl

**URL:** <https://www.ruby-forum.com/t/netssh-and-mysl/160379>\
**Category:** Ruby\
**Created:** [February 15, 2009, 4:01pm UTC](https://www.ruby-forum.com/t/netssh-and-mysl/160379 "2009-02-15T16:01:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9e8a1a/32.png) [@adad](https://www.ruby-forum.com/u/adad)\
**Post date:** [February 15, 2009, 4:01pm UTC](https://www.ruby-forum.com/t/netssh-and-mysl/160379/1 "2009-02-15T16:01:20Z")

</div>

hi is there a way to run netssh and mysql.  
some thing on the lines:

Net::SSH.start(“ipaddress”, “username”, :password=\>“psswd”) do |ssh|  
ssh.exec(“mysql -uusername -ppassword -P3306 dbname”)  
ssh.exec(“use dbname;”)  
ssh.exec(“select \* from tablename;”)

---

<div class="post-metadata">

**Author:** ![adad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9e8a1a/32.png) [@adad](https://www.ruby-forum.com/u/adad)\
**Post date:** [February 15, 2009, 11:01pm UTC](https://www.ruby-forum.com/t/netssh-and-mysl/160379/2 "2009-02-15T23:01:39Z")

</div>

Ad Ad wrote:

> hi is there a way to run netssh and mysql.  
> some thing on the lines:
> 
> Net::SSH.start(“ipaddress”, “username”, :password=\>“psswd”) do |ssh|  
> ssh.exec(“mysql -uusername -ppassword -P3306 dbname”)  
> ssh.exec(“use dbname;”)  
> ssh.exec(“select \* from tablename;”)

Hi Ad,  
I’m not an expert on MySQL, but I believe there is a way you could pipe  
a SQL script to the database via command line and get results back. That  
might work.

Alternatively, instead of using net-ssh to get information from that  
database, you could use one of the MySQL libraries. Connecting to a  
database remotely should be a very supported feature.

I believe the recommended gem is just called “mysql”. You could then use  
that to write a simple MySQL client.

I’ll defer to others on this list who are MySQL experts. : )

Hope this helps!

---

<div class="post-metadata">

**Author:** ![adad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9e8a1a/32.png) [@adad](https://www.ruby-forum.com/u/adad)\
**Post date:** [February 16, 2009, 3:16pm UTC](https://www.ruby-forum.com/t/netssh-and-mysl/160379/3 "2009-02-16T15:16:01Z")

</div>

Hi,

the box on which mysql is staged accepts connections only from  
localhost. I couldn’t get mysql to connect directly using the mysql gem.

But I did use netssh to send commands to mysql through ruby and get some  
results back.

ssh.exec(“mysql -u username -ppasswd --batch -e “select \* from table”  
schemaname \> /tmp/mydata.txt”)

which is good enough for now.

thanks for your input.

---

<div class="post-metadata">

**Author:** ![adad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9e8a1a/32.png) [@adad](https://www.ruby-forum.com/u/adad)\
**Post date:** [February 16, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/netssh-and-mysl/160379/4 "2009-02-16T17:01:42Z")

</div>

Ad Ad wrote:

> the box on which mysql is staged accepts connections only from  
> localhost. I couldn’t get mysql to connect directly using the mysql gem.

Sounds like ssh tunneling is what you want.

To demonstrate using the command-line ssh, type the following on the  
‘client’ host:

ssh -L12345:127.0.0.1:3306 [remote.host.com](http://remote.host.com)

While this ssh session is open, point any Mysql client (such as the Ruby  
mysql client) at localhost:12345, and the connection will be redirected  
down the ssh tunnel and connect to the mysql server at the other end.  
The connection at the other end will originate from the ssh daemon, so  
is “local” as far as the mysql server is concerned.

The advantage of this approach is you have full native SQL access,  
rather than having to mess around with sending command lines, and so  
higher layers will run on top of this too (like ActiveRecord)

Once you have this working to your satisfaction, you may wish to improve  
this by using Net::SSH to set up the tunnel from within Ruby as well.  
I’ve not done this, but I believe it’s possible. Read the docs, and ask  
if you can’t work it out.

---

<div class="post-metadata">

**Author:** ![adad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9e8a1a/32.png) [@adad](https://www.ruby-forum.com/u/adad)\
**Post date:** [February 16, 2009, 5:24pm UTC](https://www.ruby-forum.com/t/netssh-and-mysl/160379/5 "2009-02-16T17:24:58Z")

</div>

Brian C. schrieb:

> Once you have this working to your satisfaction, you may wish to improve  
> this by using Net::SSH to set up the tunnel from within Ruby as well.  
> I’ve not done this, but I believe it’s possible. Read the docs, and ask  
> if you can’t work it out.  
> another method could be to install the mysql-gem on the remote server  
> and then execute a ruby script via the ssh.exec method in your local  
> script =D
