当我在一个简单的ruby文件中运行以下代码时:
require 'mysql2'
client = Mysql2::Client.new(:host => "localhost", :username => "root")
我收到以下回复。
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mysql2/mysql2 (LoadError)
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/lib/ruby/gems/2.3.0/gems/mysql2-0.4.8/lib/mysql2.rb:31:in `<top (required)>'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:133:in `require'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:133:in `rescue in require'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:40:in `require'
我没有在RoR中使用这个宝石,现在只是纯粹的红宝石修修补补。 我通过Homebrew安装了gem。
gem list mysql2 - &gt; mysql2(0.4.8)
mysql Ver 14.14 Distrib 5.7.19,for osx10.11(x86_64)
ruby 2.3.1p112(2016-04-26修订版54768)[x86_64-darwin15]
答案 0 :(得分:0)
在与ruby文件相同的目录中创建Gemfile
# Gemfile
source "https://rubygems.org"
gem "mysql2"
然后执行bundle install
然后执行bundle exec ruby my_simple_ruby_file.rb