无法使用Mysql

时间:2015-07-01 17:24:32

标签: mysql ruby-on-rails ruby

我无法获得Ruby on Rails应用程序来连接到Mysql数据库

Ruby版本:2.2.1,Mysql版本:5.5

database.yml中:

  default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: manutdfc1999
  host: 127.0.0.1
  port: 3306 
  socket: /tmp/mysql.sock

  development:
  <<: *default
  database: ruby

<<: *default
database: ruby
username: manutdfc1999
password: <%= ENV['SAOIRSE_DATABASE_PASSWORD'] %>

的Gemfile:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'
# Use mysql as the database for Active Record
gem 'mysql2'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

我的mysql数据库名为ruby,在我的localhost上 当我执行rake db:migrate时,我收到以下警告

davemac_1977@mysql:~/workspace/saoirse $ rake db:migrate
rake aborted!
ActiveRecord::AdapterNotSpecified: 'mysql2' database is not configured. Available: ["default", "adapter", "encoding", "pool", "username", "password", "host", "port", "socket"]
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_connection'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:231:in `resolve_string_connection'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:213:in `resolve_connection'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:139:in `resolve'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:151:in `block in resolve_all'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:150:in `each'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:150:in `resolve_all'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:69:in `resolve'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/core.rb:46:in `configurations='
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/railtie.rb:117:in `block (2 levels) in <class:Railtie>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:44:in `each'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/base.rb:316:in `<module:ActiveRecord>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/base.rb:26:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/tasks/mysql_database_tasks.rb:8:in `<class:MySQLDatabaseTasks>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/tasks/mysql_database_tasks.rb:3:in `<module:Tasks>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/tasks/mysql_database_tasks.rb:2:in `<module:ActiveRecord>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/tasks/mysql_database_tasks.rb:1:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:50:in `<module:DatabaseTasks>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:37:in `<module:Tasks>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:4:in `<module:ActiveRecord>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:3:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/railtie.rb:41:in `block (3 levels) in <class:Railtie>'
Tasks: TOP => db:migrate => db:load_config
(See full trace by running task with --trace) 

如何解决此问题?

3 个答案:

答案 0 :(得分:0)

看起来它是database.yml文件中缩进的问题。 YML文件应该有2个空格缩进。

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: manutdfc1999
  host: 127.0.0.1
  port: 3306 
  socket: /tmp/mysql.sock

development:
  <<: *default
  database: ruby

production:
  <<: *default
  database: ruby
  username: manutdfc1999
  password: <%= ENV['SAOIRSE_DATABASE_PASSWORD'] %>

答案 1 :(得分:0)

使用/var/run/mysqld/mysqld.sock代替/tmp/mysql.sock

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: manutdfc1999
  host: 127.0.0.1
  port: 3306 
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *default
  database: ruby

production:
  <<: *default
  database: ruby
  username: manutdfc1999
  password: <%= ENV['SAOIRSE_DATABASE_PASSWORD'] %>

设置执行后:

rake db:create
rake db:migrate

答案 2 :(得分:0)

派对有点晚了,但这可能是你的mysql2版本没有与当前的rails排队。

gem'mysql2','0.3.18'