我无法连接到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/