如何将mysql2连接到Ruby on Rails?

时间:2017-11-13 12:00:12

标签: mysql ruby-on-rails ruby database windows

我正在努力研究一个RoR项目,我创建了名为tester的项目来检查我的数据库连接,我用过的命令是'rails new tester -d mysql' 我的gemfile包含了mysql2 gem 我已经安装了mysql和workbench,我的database.yml看起来像这样:

default: &default   
adapter: mysql2   
encoding: utf8
pool: <%=ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root   
password: password
host: localhost   
socket: MySQL

development:   <<: *default   
database: tester_development

test:   <<:*default
database: tester_test 

production:   <<: *default   
database: tester_production
username: tester   
password: <%=ENV['TESTER_DATABASE_PASSWORD'] %>

我的cmd错误如下:

C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/mysql2-0.4.9-x64-mingw32/lib/mysql2/mysql2.rb:2:in `require': cannot load such file -- mysql2/2.4/mysql2 (LoadError)
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/mysql2-0.4.9-x64-mingw32/lib/mysql2/mysql2.rb:2:in `<top (required)>'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/mysql2-0.4.9-x64-mingw32/lib/mysql2.rb:31:in `require'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/mysql2-0.4.9-x64-mingw32/lib/mysql2.rb:31:in `<top (required)>'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/bundler-1.16.0/lib/bundler/runtime.rb:81:in `require'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/bundler-1.16.0/lib/bundler/runtime.rb:81:in `block (2 levels) in require'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/bundler-1.16.0/lib/bundler/runtime.rb:76:in `each'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/bundler-1.16.0/lib/bundler/runtime.rb:76:in `block in require'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/bundler-1.16.0/lib/bundler/runtime.rb:65:in `each'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/bundler-1.16.0/lib/bundler/runtime.rb:65:in `require'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/bundler-1.16.0/lib/bundler.rb:114:in `require'
        from C:/Users/donki/Desktop/testing/tester/config/application.rb:7:in `<top (required)>'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:133:in `require'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:133:in `block in perform'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `tap'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `perform'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

对不起,很长的帖子。我真的不明白错误是什么。当我尝试运行rails服务器时发生错误。 帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

好像你正在使用windows和ruby 2.4

但是我觉得mysql2库到目前为止还不支持windows中的ruby 2.4,

所以,最好使用ruby 2.3而不是ruby2.4

希望它会对你有所帮助。

reference