# Passing password to mysql

**URL:** <https://www.ruby-forum.com/t/passing-password-to-mysql/166495>\
**Category:** Ruby\
**Created:** [April 26, 2009, 6:45am UTC](https://www.ruby-forum.com/t/passing-password-to-mysql/166495 "2009-04-26T06:45:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [April 26, 2009, 6:45am UTC](https://www.ruby-forum.com/t/passing-password-to-mysql/166495/1 "2009-04-26T06:45:54Z")

</div>

I would like to use Ruby to open a mysql prompt for the user. The  
password is read from a file, and should be passed to mysel. However, I  
don’t want to have the password appear in the list of processes (don’t  
use the mysql -p option)

A comment at the end of

[http://dev.mysql.com/doc/refman/5.0/en/password-security-user.html](http://dev.mysql.com/doc/refman/5.0/en/password-security-user.html)

shows how to accomplish that within bash scripts, which is quite nice.  
It works like the string in this code:

exec \<\<-END  
{ printf  
‘[client]\ndatabase=%s\nhost=%s\nsocket=%s\nuser=%s\npassword=%s’  
‘db-dev’ ‘localhost’ ‘/var/run/mysqld/mysqld.sock’ ‘db-user’ ‘db-passwd’  
|  
3\<&0 \<&4 4\<&- /usr/local/mysql/bin/mysql --defaults-file=/dev/fd/3  
} 4\<&0  
END

As you can see some clever use of file-descriptors. The exec-call works  
in Ruby, and the mysql prompt is opened, but running “ps” will reveal  
the password (not that surprising).

Is there a way to do this in Ruby?

Stephan

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [April 26, 2009, 7:25am UTC](https://www.ruby-forum.com/t/passing-password-to-mysql/166495/2 "2009-04-26T07:25:48Z")

</div>

> Is there a way to do this in Ruby?

Grab ActiveRecord (part of Rails, ob-vossly), and write its database.yml  
file.  
Oh, and actually use it to rip all your tables. It’s just too easy to  
use!

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [April 26, 2009, 8:00am UTC](https://www.ruby-forum.com/t/passing-password-to-mysql/166495/3 "2009-04-26T08:00:51Z")

</div>

Stephan W. wrote:

> Phlip wrote:
> 
> > > Is there a way to do this in Ruby?  
> > > Grab ActiveRecord (part of Rails, ob-vossly), and write its database.yml  
> > > file.  
> > > Oh, and actually use it to rip all your tables. It’s just too easy to  
> > > use!
> 
> Ok thanks; however I find the mysql prompt sometimes easier and faster.  
> Not always but often enough.

If you mean raw SQL statements, just .execute() them. Otherwise, how  
does the  
MySQL connection driver work? It has to solve your actual problem on the  
inside,  
right?

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [April 26, 2009, 3:52pm UTC](https://www.ruby-forum.com/t/passing-password-to-mysql/166495/4 "2009-04-26T15:52:44Z")

</div>

Phlip wrote:

> Stephan W. wrote:
> 
> > Phlip wrote:
> > 
> > > > Is there a way to do this in Ruby?  
> > > > Grab ActiveRecord (part of Rails, ob-vossly), and write its database.yml  
> > > > file.  
> > > > Oh, and actually use it to rip all your tables. It’s just too easy to  
> > > > use!
> > 
> > Ok thanks; however I find the mysql prompt sometimes easier and faster.  
> > Not always but often enough.
> 
> If you mean raw SQL statements, just .execute() them. Otherwise, how  
> does the  
> MySQL connection driver work? It has to solve your actual problem on the  
> inside,  
> right?

Yes, I am aware of using connection.execute(…), select\_all(…), etc.  
However, I would like to be able to use the mysql prompt.

So is this a case where a bash script can do more than a Ruby script?

Stephan

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [April 26, 2009, 7:33am UTC](https://www.ruby-forum.com/t/passing-password-to-mysql/166495/5 "2009-04-26T07:33:35Z")

</div>

Phlip wrote:

> > Is there a way to do this in Ruby?
> 
> Grab ActiveRecord (part of Rails, ob-vossly), and write its database.yml  
> file.  
> Oh, and actually use it to rip all your tables. It’s just too easy to  
> use!

Ok thanks; however I find the mysql prompt sometimes easier and faster.  
Not always but often enough.

Stephan
