我正在使用Ruby v2.0并且我正在使用来自此处的Ruby-mysql v2.9.14连接器:https://rubygems.org/gems/ruby-mysql
运行此代码时出现了一些错误:
require 'mysql'
begin
connection = Mysql.new 'localhost', 'root', 'root'
connection.list_dbs.each do |db|
puts db
end
rescue Mysql::Error => e
puts e.errno
puts e.error
ensure
connection.close if connection
end
ERROR:
No such file or directory - "/tmp/mysql.sock" (Errno::ENOENT)
from /Library/Ruby/Gems/2.0.0/gems/ruby-mysql-2.9.14/lib/mysql/protocol.rb:150:in `new'
from /Library/Ruby/Gems/2.0.0/gems/ruby-mysql-2.9.14/lib/mysql/protocol.rb:150:in `block in initialize'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
from /Library/Ruby/Gems/2.0.0/gems/ruby-mysql-2.9.14/lib/mysql/protocol.rb:147:in `initialize'
from /Library/Ruby/Gems/2.0.0/gems/ruby-mysql-2.9.14/lib/mysql.rb:115:in `new'
from /Library/Ruby/Gems/2.0.0/gems/ruby-mysql-2.9.14/lib/mysql.rb:115:in `connect'
from /Library/Ruby/Gems/2.0.0/gems/ruby-mysql-2.9.14/lib/mysql.rb:50:in `new'
from /Users/Joakim/Google Drive/Skole/IT/Ruby/Modul 6/ruby-mysql.rb:4:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
我的老师告诉我,这是'司机'崩溃,但我不确定 - 我能做些什么吗?
答案 0 :(得分:0)
驱动程序尝试通过套接字文件/tmp/mysql.sock
进行连接,该文件在您的系统中不可用(您可以检查该文件)。驱动程序假定您希望通过套接字文件进行连接,因为您指定了localhost
。
这是驱动程序does:
@host_info = (host.nil? || host == "localhost") ? 'Localhost via UNIX socket' : "#{host} via TCP/IP"
尝试将localhost
更改为127.0.0.1
。
<强> EDITED 强>
似乎您使用mysql的自定义端口,8889。因此,您需要在连接到数据库时指定:
connection = Mysql.new 'localhost', 'root', 'root', nil, 8889
以下是method definition的摘要:
# Connect to mysqld.
# @param [String / nil] host hostname mysqld running
# @param [String / nil] user username to connect to mysqld
# @param [String / nil] passwd password to connect to mysqld
# @param [String / nil] db initial database name
# @param [Integer / nil] port port number (used if host is not 'localhost' or nil)
# @param [String / nil] socket socket file name (used if host is 'localhost' or nil)
# @param [Integer / nil] flag connection flag. Mysql::CLIENT_* ORed
# @return self
def connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, socket=nil, flag=0)