-1

我能够创建一个“rails new demoproject”并在文件夹中成功运行“rails s”,但是当我尝试使用数据库“rails new demoproject -d mysql”创建一个rails项目并在其中运行“rails s”时我收到以下错误的文件夹:

user$ rails server
=> Booting WEBrick
=> Rails 4.0.0.beta1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2013-04-20 16:06:26] INFO  WEBrick 1.3.1
[2013-04-20 16:06:26] INFO  ruby 1.9.3 (2013-02-22) [x86_64-darwin12.3.0]
[2013-04-20 16:06:26] INFO  WEBrick::HTTPServer#start: pid=1945 port=3000


Started GET "/" for 127.0.0.1 at 2013-04-20 16:06:39 -0400

Mysql2::Error (Access denied for user 'root'@'localhost' (using password: NO)):
  mysql2 (0.3.11) lib/mysql2/client.rb:44:in `connect'
  mysql2 (0.3.11) lib/mysql2/client.rb:44:in `initialize'
  activerecord (4.0.0.beta1) lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `new'
  activerecord (4.0.0.beta1) lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `mysql2_connection'
  activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/connection_pool.rb:446:in `new_connection'
  activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/connection_pool.rb:456:in `checkout_new_connection'
  activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/connection_pool.rb:427:in `acquire_connection'
  activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/connection_pool.rb:364:in `block in checkout'
  /Users/user/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
  activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/connection_pool.rb:363:in `checkout'
  activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/connection_pool.rb:273:in `block in connection'
  /Users/user/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
  activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/connection_pool.rb:272:in `connection'
  activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/connection_pool.rb:552:in `retrieve_connection'
  activerecord (4.0.0.beta1) lib/active_record/connection_handling.rb:79:in `retrieve_connection'
  activerecord (4.0.0.beta1) lib/active_record/connection_handling.rb:53:in `connection'
  activerecord (4.0.0.beta1) lib/active_record/migration.rb:739:in `current_version'
  activerecord (4.0.0.beta1) lib/active_record/migration.rb:747:in `needs_migration?'
  activerecord (4.0.0.beta1) lib/active_record/migration.rb:357:in `check_pending!'
  activerecord (4.0.0.beta1) lib/active_record/migration.rb:346:in `block in call'
  activesupport (4.0.0.beta1) lib/active_support/logger_silence.rb:16:in `silence'
  activerecord (4.0.0.beta1) lib/active_record/migration.rb:345:in `call'
  actionpack (4.0.0.beta1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:373:in `_run__3314375952420028571__call__callbacks'
  activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:78:in `run_callbacks'
  actionpack (4.0.0.beta1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.0.beta1) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.0.beta1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.0.0.beta1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.0.beta1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0.beta1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0.beta1) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0.beta1) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0.beta1) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0.beta1) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0.beta1) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0.beta1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0.beta1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0.beta1) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0.beta1) lib/rails/engine.rb:510:in `call'
  railties (4.0.0.beta1) lib/rails/application.rb:96:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /Users/user/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/user/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/user/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p392/gems/actionpack-4.0.0.beta1/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p392/gems/actionpack-4.0.0.beta1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.5ms)
  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p392/gems/actionpack-4.0.0.beta1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p392/gems/actionpack-4.0.0.beta1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (51.9ms)

当我转到 localhost:3000 时,我在 Web 浏览器的标题中收到“动作控制器:异常捕获”错误,并且可以在此处查看网页上的 mysql2 错误:http: //postimg.org/image/ 9wemvhwkj/

更新:这是 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
  database: cms_development
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.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
  database: cms_test
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

production:
  adapter: mysql2
  encoding: utf8
  database: cms_production
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock
4

3 回答 3

1

您的 database.yml 文件中的用户名和密码无权访问数据库

你需要

  • 转到用户 mysql2 shell
    • 创建新用户
    • 新数据库
    • 授予用户对数据库的权限
  • 转到 database.yml 文件并更新开发配置以匹配
    • 用户名
    • 密码
    • 数据库适配器
    • 数据库名称
于 2013-04-20T21:39:21.447 回答
0

我在你们的帮助下解决了我的问题:

在终端中输入:(mysql -u root -p如果-p您在安装 mysql 时没有设置 root 密码,则没有)

然后输入CREATE DATABASE cms_development;(替换cms为您的 Rails 项目名称)

然后打开位于 rails 项目文件夹中的 config/database.yml

在“开发:”下输入我的密码在“密码:”下

于 2013-04-22T00:51:17.670 回答
0

Mysql2::Error (Access denied for user 'root'@'localhost' (using password: NO)):- 检查数据库用户名\密码

于 2013-04-20T20:22:54.250 回答