耙子问题 db:create

Issue with rake db:create

我认为这是一个常见问题,但我已经尝试了我在其他几篇文章中阅读的内容,但我遇到了同样的命运。

我正在使用 rbenv,因为这是我首先遇到的。

rake db:create
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_handling.rb:50:in `establish_connection'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/mysql_database_tasks.rb:8:in `establish_connection'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/mysql_database_tasks.rb:15:in `create'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:93:in `create'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:107:in `block in create_current'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:275:in `block in each_current_configuration'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:274:in `each'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:274:in `each_current_configuration'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:106:in `create_current'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:17:in `block (2 levels) in <top (required)>'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:240:in `call'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:235:in `each'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:106:in `each'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
    /Users/cmunger/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:75:in `run'
    /Users/cmunger/.rbenv/versions/2.2.3/bin/rake:33:in `<main>'
    Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"blogApp", "password"=>"*****", "socket"=>"/tmp/mysql.sock", "database"=>"blog_test"}
    rake aborted!
    Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).

Gem::LoadError: can't activate mysql2 (~> 0.3.13), already activated mysql2-0.4.2. Make sure all dependencies are added to Gemfile

我的版本和捆绑安装。 mysql2 有没有...

cmunger@Claytons-Mac:~/Desktop/Devlopment/blog$ rbenv versions
  system
* 2.2.3 (set by RBENV_VERSION environment variable)
cmunger@Claytons-Mac:~/Desktop/Devlopment/blog$ bundle install
Using actionmailer 4.2.4
Using actionpack 4.2.4
Using actionview 4.2.4
Using activejob 4.2.4
Using activemodel 4.2.4
Using activerecord 4.2.4
Using activesupport 4.2.4
Using arel 6.0.3
Using binding_of_caller 0.7.2
Using builder 3.2.2
Using bundler 1.10.6
Using byebug 8.2.1
Using coffee-rails 4.1.0
Using coffee-script 2.4.1
Using coffee-script-source 1.10.0
Using concurrent-ruby 1.0.0
Using debug_inspector 0.0.2
Using erubis 2.7.0
Using execjs 2.6.0
Using globalid 0.3.6
Using i18n 0.7.0
Using jbuilder 2.3.2
Using jquery-rails 4.0.5
Using json 1.8.3
Using loofah 2.0.3
Using mail 2.6.3
Using mime-types 2.99
Using mini_portile2 2.0.0
Using minitest 5.8.3
Using multi_json 1.11.2
Using mysql2 0.4.2
Using nokogiri 1.6.7
Using rack 1.6.4
Using rack-test 0.6.3
Using rails 4.2.4
Using rails-deprecated_sanitizer 1.0.3
Using rails-dom-testing 1.0.7
Using rails-html-sanitizer 1.0.2
Using railties 4.2.4
Using rake 10.4.2
Using rdoc 4.2.0
Using sass 3.4.20
Using sass-rails 5.0.4
Using sdoc 0.4.1
Using spring 1.5.0
Using sprockets 3.5.2
Using sprockets-rails 2.3.3
Using thor 0.19.1
Using thread_safe 0.3.5
Using tilt 2.0.1
Using turbolinks 2.5.3
Using tzinfo 1.2.2
Using uglifier 2.7.2
Using web-console 2.2.1
Bundle complete! 12 Gemfile dependencies, 54 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

我认为下面的错误可能是我问题的根源:

Gem::LoadError: can't activate mysql2 (~> 0.3.13), already activated mysql2-0.4.2. Make sure all dependencies are added to Gemfile

我尝试将其改回,但我得到:

gem 'mysql2', '~> 0.4.2'
ERROR:  While executing gem ... (Gem::CommandLineError)
    Unknown command mysql2,

我可以提供任何进一步的信息,请询问。我在使用 rbenv 安装时关注了 wiki,这就是我所在的位置。

您需要在您的命令中添加 bundle exec,否则您将在全局 gemset 上执行它:

bin/rake db:create

作为替代方案,您可以使用 binstubs:

bundle exec rake db:create

仅在您的 gemfile 中使用(目前)

gem mysql2

在您的命令行中,运行

$ gem install -v '0.3.13'
$ bundle exec bundle update
$ bundle exec bundle install

除非您是 运行 Rails 4.2.5 或更高版本,否则您需要在 Gemfile 中添加以下内容:

gem 'mysql2', '~> 0.3.10'

来源:mysql gem compatibility listing

对于 Rails 4.2.5 或更高版本使用此

gem 'mysql2', '~> 0.3.18'

然后

bundle install

给已经尝试了上述所有方法但仍然面临同样问题的人一些额外的建议。

我最初尝试过

  1. 正在安装 rbenv
  2. rbenv globalrbenv local 设置为所需的版本
  3. 在我的项目中创建一个 .ruby-version 文件设置为所需的版本 4,将建议的rbenv条目放入我的.bash_profile
  4. rbenv version 测试了我的设置并得到了我预期的结果
  5. 并尝试了该线程中建议的其他一些方法

没有任何效果。

最后,经过数小时的撞击和尝试不同的事情,最终解决问题的方法是重启。所以只是一个提示:如果所有这些建议都失败了,请不要像我一样浪费时间:重新启动即可。

我第一次遇到这个问题是在 运行 bundle install 的时候。上面列出的 rbenv 步骤结合重新启动解决了这个问题。然后问题又出现在 rails db:create 命令上,尽管 bundle install 仍在同一个 bash 会话中工作!我再次检查了所有内容,最终修复的是重启。