Ruby on rails mysql问题

时间:2011-04-03 07:17:07

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

我正在创建我的第一个ROR应用程序。 详细说明:

创建新应用 - >

rails new simple_cms -d mysql

创建控制器和相应的视图 - >

rails generate controller demo index

然后,当我启动rails服务器后,http://localhost:3000页面工作正常。但是当我尝试转到我刚创建的页面http://localhost:3000/demo/index时,它会产生一个MYSQL错误:

Access denied for user 'root'@'localhost' (using password: NO) 

起初,我认为这只是一个数据库连接问题,所以我通过MYSQL接口创建了一个名为simple_cms_development的新数据库 我也找了一个相应的用户:simple_cms 授予特权。

最后,我已经配置了database.yml文件以适应细节:

development:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: simple_cms_development
  pool: 5
  username: simple_cms
  password: developer
  host: localhost

但是,它仍然会吐出相同的mysql问题: 用户'root'@'localhost'拒绝访问(使用密码:NO)

请。任何知道如何使用mysql作为rails app的数据库的人。真的很感激帮助。非常感谢你!

更新:这就是database.yml文件中的所有内容

# MySQL.  Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
#   gem install mysql2
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: simple_cms_development
  pool: 5
  username: root
  password: developer
  host: localhost

# 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: simple_cms_test
  pool: 5
  username: root
  password:
  host: localhost

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: simple_cms_production
  pool: 5
  username: root
  password:
  host: localhost

2 个答案:

答案 0 :(得分:1)

唯一可能导致您出现问题的原因是您使用不同的环境加载服务器。我认为在您的情况下,您正在生产模式下加载服务器。

答案 1 :(得分:1)

我发现您需要将以下行添加到config/database.yml

host: localhost

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  host: your_host # <----- normally localhost
  database: the_db_I_made
  pool: 5
  username: the_user_I_made
  password: the_password
  socket: /var/lib/mysql/mysql.sock