mysql - Rails 无法连接到 mysql2 中的数据库

标签 mysql ruby-on-rails ruby ruby-on-rails-3 rubygems

我无法连接到mysql中的数据库。 gem 文件:

gem 'rails', '3.2.13'
gem 'mysql2'

在 config/database.yml 中:

development:
adapter: mysql2
encoding: utf8
reconnect: false
database: local
pool: 5
username: root
password: 123
host: localhost

test: &test
adapter: mysql2
encoding: utf8
reconnect: false
database:  local_test
pool: 5
username: root
password: 123
host: localhost

production:
adapter: mysql2
encoding: utf8
reconnect: false
database:  local_development
pool: 5
username: root
password: 123
host: localhost

cucumber:
<<: *test

当我运行服务器时,出现以下错误:

ActiveRecord::ConnectionNotEstablished

(ActiveRecord::ConnectionNotEstablished)


/home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in <code>retrieve_connection': ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in</code>retrieve_connection'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in <code>connection'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activerecord-3.2.13/lib/active_record/model_schema.rb:223:in</code>table_exists?'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/attr_encrypted-1.3.4/lib/attr_encrypted/adapters/active_record.rb:59:in <code>attribute_instance_methods_as_symbols'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/attr_encrypted-1.3.4/lib/attr_encrypted.rb:130:in</code>block in attr_encrypted'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/attr_encrypted-1.3.4/lib/attr_encrypted.rb:125:in <code>each'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/attr_encrypted-1.3.4/lib/attr_encrypted.rb:125:in</code>attr_encrypted'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/attr_encrypted-1.3.4/lib/attr_encrypted/adapters/active_record.rb:51:in <code>attr_encrypted'
    from /home/kop/rails/howtobuyafranchise/lib/franchise_core/lib/models/billing_info.rb:6:in</code>'
    from /home/kop/rails/howtobuyafranchise/lib/franchise_core/lib/models/billing_info.rb:2:in <code><top (required)>'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in</code>require'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in <code>block in require'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in</code>load_dependency'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in <code>require'
    from /home/kop/rails/howtobuyafranchise/lib/franchise_core/lib/franchise.rb:35:in</code>'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:in <code>require'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:in</code>block (2 levels) in require'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:in <code>each'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:in</code>block in require'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:in <code>each'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:in</code>require'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler.rb:134:in <code>require'
    from /home/kop/rails/howtobuyafranchise/config/application.rb:7:in</code>'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/railties-3.2.13/lib/rails/commands.rb:53:in <code>require'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/railties-3.2.13/lib/rails/commands.rb:53:in</code>block in '
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/railties-3.2.13/lib/rails/commands.rb:50:in <code>tap'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/railties-3.2.13/lib/rails/commands.rb:50:in</code>'
    from script/rails:6:in <code>require'
    from script/rails:6:in</code>'
kop@ubuntu:~/rails/howtobuyafranchise$ 

虽然不同的项目可以连接到这个数据库。

最佳答案

看起来你缺少端口参数,尝试添加

port:3306

(或您的数据库正在监听的任何端口)到您的配置

关于mysql - Rails 无法连接到 mysql2 中的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32455215/

相关文章:

php - 如何在没有 "bind_param"的情况下进行更新

ruby-on-rails - Rails:使用 ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR 测试失败

ruby - 事件记录查找和更新

php - MYSQL 无法在不同的表上加载 OR 运算符

php - 如果 Row1 = 值 1,则更新其他行

mysql - 如何为非常大的表创建 MYSQL FULLTEXT 索引?

css - Less::错误/无法识别的输入 - twitter-bootstrap-rails

ruby-on-rails - 乘客 EBADF 文件描述符异常 502

ruby-on-rails - 在 Rails before_save 方法中将多个属性大写

Ruby:在数组中搜索特定条件的简单方法是什么?