Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the socket declaration for, in Ruby on Rails database.yml?

What's the use of socket declaration in config/database.yml ?

Example code:

staging:
  adapter: mysql
  encoding: utf8
  database: (database)
  pool: 5
  username: (user)
  password: (pass)
  socket: /tmp/mysql.sock     # <--------- this line

My app works, wether this line is commented or not. So what is it for ? What reasons can I have to leave it, comment it or change it's value ?

like image 566
Daniel R Avatar asked Jan 05 '11 21:01

Daniel R


2 Answers

When two programs want to talk to each other over the network, one program might open up a TCP connection (a "socket") with the other one. The first program needs to know the IP address of the second computer and the port on which the program is listening.

On Linux, when two programs on the same computer want to talk to each other, they can still open up a TCP connection. But they can also open up a connection via a "socket file". Linux makes the socket file API rather similar to the TCP API, so it's not a big deal to update a program that already communicates over the network via TCP to support communicating via socket files too. Socket files are faster than TCP, but only work when both programs are on the same computer.

like image 176
yfeldblum Avatar answered Oct 13 '22 19:10

yfeldblum


MySQL has two methods of communicating with it on unix-ish systems, tcp/ip and domain sockets. By specifying the socket Rails, or rather the database driver, will use the socket instead of a network connection. It can be quicker to use the socket but YMMV.

http://dev.mysql.com/doc/refman/5.5/en/connecting.html

like image 21
noodl Avatar answered Oct 13 '22 19:10

noodl