# MySQL configured to use tunneled localhost port

**URL:** <https://www.ruby-forum.com/t/mysql-configured-to-use-tunneled-localhost-port/118480>\
**Category:** Rails\
**Created:** [October 27, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/mysql-configured-to-use-tunneled-localhost-port/118480 "2007-10-27T17:20:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jbsmith](https://www.ruby-forum.com/u/jbsmith)\
**Post date:** [October 27, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/mysql-configured-to-use-tunneled-localhost-port/118480/1 "2007-10-27T17:20:15Z")

</div>

Today I discovered an interesting behavior of the mysql database adapter  
when connecting to a tunneled localhost port for MySQL from OSX.

i started a tunnel using the usual ssh command line options:

ssh -p 22 -L 3307:localhost:3306 removed\_email\_address@domain.invalid

then in database.yml  
THIS WORKS

development:  
adapter: mysql  
encoding: utf8  
database: dev\_data  
username: dev\_admin  
password: password  
host: 127.0.0.1  
port: 3307

THIS FAILS  
development:  
adapter: mysql  
encoding: utf8  
database: dev\_data  
username: dev\_admin  
password: password  
host: localhost  
port: 3307

It seems that the adapter will automatically force a mysql.sock to be  
used when host: is set to localhost.  
Switching host: to the actual IP seems to allow the port option to go  
into effect.

Just wanted to post this so others may find the solution I did, more  
quickly.

---

<div class="post-metadata">

**Author:** ![jbsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jbsmith](https://www.ruby-forum.com/u/jbsmith)\
**Post date:** [January 8, 2008, 12:56am UTC](https://www.ruby-forum.com/t/mysql-configured-to-use-tunneled-localhost-port/118480/2 "2008-01-08T00:56:24Z")

</div>

Thank you! That is exactly how to get ruby mysql to run on a port  
instead of on the unix socket. Odd that it would be necessary.  
Note also that for mysql command line to work with ‘just a socket’ and  
not automatically use the port, you need to run  
mysql -uwhatever -p --protocol=tcp --port=whatever # or it won’t use the  
port, only uses the local socket (at least on os x).  
Thank you.  
-Roger

> then in database.yml  
> THIS WORKS
> 
> development:  
> adapter: mysql  
> encoding: utf8  
> database: dev\_data  
> username: dev\_admin  
> password: password  
> host: 127.0.0.1  
> port: 3307

---

<div class="post-metadata">

**Author:** ![jbsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jbsmith](https://www.ruby-forum.com/u/jbsmith)\
**Post date:** [October 28, 2007, 1:34am UTC](https://www.ruby-forum.com/t/mysql-configured-to-use-tunneled-localhost-port/118480/3 "2007-10-28T01:34:28Z")

</div>

On 2007-10-27 08:20:15 -0700, Jb Smith  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> THIS WORKS  
> host: 127.0.0.1
> 
> THIS FAILS  
> host: localhost
> 
> It seems that the adapter will automatically force a mysql.sock to be  
> used when host: is set to localhost.

Yes, I’ve noticed this myself; it’s not particular to Rails. I have to  
do this when using CocoaMySQL to connect to my remote database via ssh  
tunnel. I sure would like to know why it works this way too… Is  
“localhost” just a magic keyword for using the socket?

–Andrew V.
