ruby-mysql
ruby-mysql copied to clipboard
Works remotely but not locally
I can connect to MySQL from a different machine, but when I try to connect on the same one I get:
irb(main):006:0> my = Mysql.connect('localhost', 'mythbackdoor', 'notactuallymypassword')
Errno::ENOENT: No such file or directory - connect(2) for /tmp/mysql.sock
from /usr/local/lib/ruby/gems/2.4.0/gems/ruby-mysql-2.9.14/lib/mysql/protocol.rb:150:in `initialize'
from /usr/local/lib/ruby/gems/2.4.0/gems/ruby-mysql-2.9.14/lib/mysql/protocol.rb:150:in `new'
from /usr/local/lib/ruby/gems/2.4.0/gems/ruby-mysql-2.9.14/lib/mysql/protocol.rb:150:in `block in initialize'
from /usr/local/lib/ruby/2.4.0/timeout.rb:76:in `timeout'
from /usr/local/lib/ruby/gems/2.4.0/gems/ruby-mysql-2.9.14/lib/mysql/protocol.rb:147:in `initialize'
from /usr/local/lib/ruby/gems/2.4.0/gems/ruby-mysql-2.9.14/lib/mysql.rb:115:in `new'
from /usr/local/lib/ruby/gems/2.4.0/gems/ruby-mysql-2.9.14/lib/mysql.rb:115:in `connect'
from /usr/local/lib/ruby/gems/2.4.0/gems/ruby-mysql-2.9.14/lib/mysql.rb:50:in `new'
from (irb):6
from /usr/local/bin/irb:11:in `<main>'
The issue is possibly due to hardcoded location of the socket file (a server issue, not a problem with the connector.) A workaround could be using '127.0.0.1' instead of 'localhost'.
The path of the socket file is specified by the 6th argument of Mysql.connect ().
example:
Mysql.connect('localhost', 'mythbackdoor', 'notactuallymypassword', nil, nil, '/path/to/socket')
This value can also be given as an environment variable MYSQL_UNIX_PORT:
% export MYSQL_UNIX_PORT=/path/to/socket