无法连接rails和mysql2

时间:2018-04-12 23:46:23

标签: ruby-on-rails ruby connect mysql2

所以我第一次尝试从命令提示符执行简单的数据库转储,并且rails命令保持中止错误。这是使用rails 5.0.7和mysql 0.5.0运行Ruby 2.3.0。

C:\Users\MR_ra\Documents\Sites\matt_radin_cms>rails db:schema:dump --trace 
** Invoke db:schema:dump (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:schema:dump

rails aborted!

NameError: uninitialized constant Mysql2::Client::REMEMBER_OPTIONS
C:/Ruby23/lib/ruby/gems/2.3.0/gems/mysql2-0.5.0-x86-mingw32/lib/mysql2/client.rb:14:in `default_query_options'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/mysql2-0.5.0-x86-mingw32/lib/mysql2/client.rb:25:in `initialize'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/connection_adapters/mysql2_adapter.rb:25:in `new'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/connection_adapters/mysql2_adapter.rb:25:in `mysql2_connection'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:729:in `new_connection'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:773:in `checkout_new_connection'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:752:in `try_to_checkout_new_connection'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:713:in `acquire_connection'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:490:in `checkout'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:364:in `connection'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:883:in `retrieve_connection'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/connection_handling.rb:128:in `retrieve_connection'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/connection_handling.rb:91:in `connection'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/railties/databases.rake:241:in `block (4 levels) in <top (required)>'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/railties/databases.rake:240:in `open'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/activerecord-5.0.7/lib/active_record/railties/databases.rake:240:in `block (3 levels) in <top (required)>'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/task.rb:271:in `block in execute'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/task.rb:271:in `each'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/task.rb:271:in `execute'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/task.rb:213:in `block in invoke_with_call_chain'
C:/Ruby23/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/task.rb:193:in `invoke_with_call_chain'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/task.rb:182:in `invoke'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:160:in `invoke_task'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:116:in `block (2 levels) in top_level'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:116:in `each'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:116:in `block in top_level'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:125:in `run_with_threads'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:110:in `top_level'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/railties-5.0.7/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:186:in `standard_exception_handling'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/railties-5.0.7/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/railties-5.0.7/lib/rails/commands.rb:18:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:schema:dump

2 个答案:

答案 0 :(得分:0)

看起来你的机器或适配器设置中没有安装Mysql是错误的。

首先在你的机器上安装mysql服务器。 之后通过下面的命令安装mysql2 gem或者在应用程序gemfile中添加

gem install mysql2

gem&#39; mysql2&#39;

然后执行以下适配器设置

默认值:&amp;默认

adapter:mysql2

用户名:xxxxx

密码:xxxxx

host:localhost

pool:&lt;%= ENV.fetch(&#34; RAILS_MAX_THREADS&#34;){5}%&gt;

超时:5000

希望这会帮助你。

答案 1 :(得分:0)

我认为mysql2 gem版本的问题,你可以定义像这样的版本

gem 'mysql2', '~> 0.3.17'

然后bundle install,它解决了我手上的问题。

database.yml

default: &default
  adapter: mysql2
  host: localhost
  encoding: unicode
  username: root
  password: #=> if you use password to your local databse then use otherwise empty this
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: database_development