1

我正在尝试在不使用 rails 的情况下使用 activerecord 和 mysql,但我似乎无法将我的 ruby​​ 代码连接到 mysql。我的猜测是我的 require 语句或我的 gem 的安装方式有问题。下边是:

  • A. 我的脚本
  • B. 我的本地 gem 列表显示 gems 已安装
  • C. 我的路径变量
  • D. 我的 gem 环境和
  • E. 我收到的错误信息

请帮忙,这已经消耗了我一整天!(在我的ruby-1.9.3-P327@rails3tutorial2ndEd选项下,我在珠宝盒中显示了 activerecord-mysql-adapter,但在我运行时没有显示在终端中gem list --local。这与它有什么关系吗?)
(我也尝试了这个问题的解决方案sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib 它没有用为了我。

A. 脚本:

(尝试使用和不使用shebang - 均未成功)

  1 require 'rubygems'                                                                 
  2 require 'active_record'                                                            
  3                                                                                    
  4 ActiveRecord::Base.establish_connection(                                           
  5  :adapter => 'mysql',                                                              
  6  :host    => 'xxxx',                                                          
  7  :username=> 'xxxx',                                                               
  8  :password=> 'xxxx',                                                           
  9  :database=> 'players')                                                            
 10                                                                                    
 11 class Player < ActiveRecord::Base                                                  
 12 end                                                                                
 13                                                                                    
 14  Player.find(:all) 

B. 运行 gem list 时终端中的 Gems:

$ gem list mysql

*** LOCAL GEMS ***

mysql (2.9.1)
mysql2 (0.3.11)

$ gem list activerecord

*** LOCAL GEMS ***

activerecord (3.2.12, 3.2.8)

C、路径

$ $PATH
-bash: /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/bin:/Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327@global/bin:/Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/bin:/Users/emmanueleleyae2/.rvm/bin:/usr/local/mysql/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin: No such file or directory

$ $GEM_PATH
-bash: /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327:/Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327@global: No such file or directory

$ $GEM_HOME
-bash: /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327: is a directory
emmanuel-eleyaes-computer:chapter01 emmanueleleyae2$

D.宝石环境

$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.8.25
  - RUBY VERSION: 1.9.3 (2012-11-10 patchlevel 327) [x86_64-darwin11.4.0]
  - INSTALLATION DIRECTORY: /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327
  - RUBY EXECUTABLE: /Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-11
  - GEM PATHS:
     - /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327
     - /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

E. 错误信息:

/Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Please install the mysql adapter: `gem install activerecord-mysql-adapter` (Could not find mysql (~> 2.8.1) amongst [actionmailer-3.2.8, actionpack-3.2.8, activemodel-3.2.12, activemodel-3.2.8, activerecord-3.2.12, activerecord-3.2.8, activeresource-3.2.8, activesupport-3.2.12, activesupport-3.2.8, addressable-2.3.2, arel-3.0.2, blankslate-2.1.2.4, bluecloth-2.2.0, builder-3.0.4, bundler-1.2.1, capybara-1.1.2, cgi_multipart_eof_fix-2.5.0, childprocess-0.3.6, coffee-rails-3.2.2, coffee-script-2.2.0, coffee-script-source-1.4.0, daemons-1.1.9, daemons-1.0.10, diff-lcs-1.1.3, erubis-2.7.0, excon-0.16.10, execjs-1.4.0, fastthread-1.0.7, ffi-1.2.0, gem_plugin-0.2.3, gli-2.5.4, heroku-2.33.2, heroku-api-0.3.7, hike-1.2.1, i18n-0.6.1, journey-1.0.4, jquery-rails-2.1.4, jquery-rails-2.1.3, json-1.7.5, launchy-2.1.2, libwebsocket-0.1.7.1, mail-2.4.4, mime-types-1.19, mongrel-1.2.0.pre2, multi_json-1.4.0, multi_json-1.3.7, mysql2-0.3.11, netrc-0.7.7, nokogiri-1.5.5, parslet-1.5.0, pg-0.12.2, polyglot-0.3.3, rack-1.4.1, rack-cache-1.2, rack-protection-1.4.0, rack-ssl-1.3.2, rack-test-0.6.2, rails-3.2.8, railties-3.2.8, rake-10.0.2, rake-0.9.2.2, rdoc-3.12, rest-client-1.6.7, rspec-2.9.0, rspec-core-2.9.0, rspec-expectations-2.9.1, rspec-mocks-2.9.0, rspec-rails-2.9.0, rubygems-bundler-1.1.0, rubyzip-0.9.9, rvm-1.11.3.5, sass-3.2.3, sass-rails-3.2.5, selenium-webdriver-2.27.1, showoff-0.7.0, sinatra-1.3.5, sprockets-2.1.3, sqlite3-1.3.6, sqlite3-1.3.5, thor-0.16.0, tilt-1.3.3, treetop-1.4.12, tzinfo-0.3.35, uglifier-1.3.0, websocket-1.0.4, xpath-0.1.4]) (LoadError)
    from /Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
    from /Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems.rb:1231:in `gem'
    from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/mysql_adapter.rb:5:in `<top (required)>'
    from /Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
    from /Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
    from /Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
    from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `block in require'
    from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require'
    from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:50:in `resolve_hash_connection'
    from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:29:in `spec'
    from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:130:in `establish_connection'
    from test.rb:4:in `<main>'
4

2 回答 2

2

你有没有试过改变

:adapter => 'mysql'

:adapter => 'mysql2'
于 2013-03-17T22:32:04.347 回答
0
change adapter mysql2

development:
  adapter: mysql2
  database: library_development
  username: root
  password: 
  host: localhost
test:
  adapter: mysql2
  database: library_test
  username: root
  password: 
  host: localhost
production:
  adapter: mysql2
  database: library_production
  username: root
  password: 
  host: localhost
于 2014-01-17T07:06:26.543 回答