Rails没有连接到mysql2

时间:2015-09-08 10:22:22

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

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

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 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:inretrieve_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 connection'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activerecord-3.2.13/lib/active_record/model_schema.rb:223:intable_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 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:inblock 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 each'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/attr_encrypted-1.3.4/lib/attr_encrypted.rb:125:inattr_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 attr_encrypted'
    from /home/kop/rails/howtobuyafranchise/lib/franchise_core/lib/models/billing_info.rb:6:in'
    from /home/kop/rails/howtobuyafranchise/lib/franchise_core/lib/models/billing_info.rb:2:in <top (required)>'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:inrequire'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in block in require'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:inload_dependency'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in require'
    from /home/kop/rails/howtobuyafranchise/lib/franchise_core/lib/franchise.rb:35:in'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:in require'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:inblock (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 each'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:inblock in require'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:in each'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:inrequire'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/bundler-1.10.6/lib/bundler.rb:134:in require'
    from /home/kop/rails/howtobuyafranchise/config/application.rb:7:in'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/railties-3.2.13/lib/rails/commands.rb:53:in require'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/railties-3.2.13/lib/rails/commands.rb:53:inblock in '
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/railties-3.2.13/lib/rails/commands.rb:50:in tap'
    from /home/kop/.rvm/gems/ruby-2.1.3@rails3213/gems/railties-3.2.13/lib/rails/commands.rb:50:in'
    from script/rails:6:in require'
    from script/rails:6:in'
kop@ubuntu:~/rails/howtobuyafranchise$ 

虽然另一个项目可以连接到此数据库。

2 个答案:

答案 0 :(得分:0)

看起来您缺少port参数,请尝试添加

port:3306

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

答案 1 :(得分:0)

尝试添加

socket: /var/run/mysqld/mysqld.sock

或者你的mysqld存在的地方