1

我试图在我的 Ubuntu 服务器上的一个新的 rails 项目上执行以下命令。

rails g scaffold User email:string username:string

但我收到以下错误:

"/usr/local/rvm/gems/ruby-1.9.3-p392/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect': Access denied for user 'root'@'localhost' (using password: YES) (Mysql2::Error)
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `initialize'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `new'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `mysql2_connection'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/railtie.rb:88:in `block in <class:Railtie>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `instance_exec'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `run'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `each'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `run_initializers'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/application.rb:136:in `initialize!'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /home/rails/membershipServices/config/environment.rb:6:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/application.rb:103:in `require'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/application.rb:103:in `require_environment!'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:25:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

我尝试使用 database.yml 中提供的凭据通过我的服务器访问 mysql,我可以正常登录。但由于某种原因,我不能通过 Ruby on Rails 命令来做到这一点。我的 database.yml 是:

  # MySQL.  Versions 4.1 and 5.0 are recommended.
# 
# Install the MYSQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: membershipServices_development
  pool: 5
  username: root
  password: ******
  socket: /var/run/mysqld/mysqld.sock

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: membershipServices_test
  pool: 5
  username: root
  password: ******
  socket: /var/run/mysqld/mysqld.sock

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: membershipServices_production
  pool: 5
  username: root
  password: ******
  host: localhost
  socket: /var/run/mysqld/mysqld.sock

我再次通过服务器上的终端登录 mysql 验证了提供的用户名和密码是否正确。提前感谢您的帮助!

4

1 回答 1

0

我在这个网站上做了更多的研究,并且能够尝试不同的东西。唯一对我有帮助的是将用户名重命名为 root1 root2 和 root3 以验证它使用的是哪个环境。这样做之后,我验证了正在使用开发。我添加Host: localhost到我的开发连接信息中,就成功了。

于 2013-05-14T16:06:09.153 回答