4

我正在启动一个新的 rails 3 应用程序,当我运行rake db:create它时会询问我的 root mysql 密码,然后错误提示:

Mysql2::Error: 用户'arcsite_mysql'@'localhost'拒绝访问数据库'arcsite_mysql_test': CREATE DATABASE arcsite_mysql_testDEFAULT CHARACTER SET utf8COLLATEutf8_unicode_ci

如果我在命令行上登录 mysql 并运行,show databases;我会看到:

arcsite_mysql_development

如果我跑步,SHOW GRANTS FOR arcsite_mysql@localhost我会看到:

GRANT ALL PRIVILEGES ON `arcsite_mysql_development`.* TO 'arcsite_mysql'@'localhost' WITH GRANT OPTION 

我的数据库.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: arcsite_mysql_development
  pool: 5
  username: arcsite_mysql
  password: password
  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: arcsite_mysql_test
  pool: 5
  username: arcsite_mysql
  password: password
  host: localhost

因此,出于某种原因,只创建了开发数据库。

版本:

导轨 3.2.13

MySQL:服务器版本:5.6.10 源码分发

mysql2:mysql2-0.3.11

编辑

如果我手动将所有权限授予arcsite_mysql用户,我可以创建表。但是是否期望 Rails 会为开发而不是为测试环境创建用户和表?当我第一次运行命令时,arcsite_mysql用户是由 rails 创建的。rake db:create

编辑 2

第 4 点:https ://rails.lighthouseapp.com/projects/8994/tickets/1459-patch-better-dbcreate-for-mysql-do-not-assume-root-user

提到了一些涉及 mysql 用户创建的“众所周知的”过程。

4

2 回答 2

5

您需要在 MySQL 中授予此用户的访问权限:

GRANT ALL ON *.* TO 'arcsite_mysql'@'localhost' IDENTIFIED BY 'password';

Rails 不会在 MySQL 中创建用户,它只使用存在的用户,这就是为什么在生成时database.yml你会得到类似的东西:

development:
  adapter: mysql2
  encoding: utf8
  database: blog_development
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

Rails 假设 root 用户存在并且在 MySQL 中具有适当的权限(因为 root 拥有它所做的一切),如果不是,则必须创建它(如果使用 MySQL,如果它不存在,则会提示您输入 root 密码来创建用户)或将其更改为可以的。供参考: http: //guides.rubyonrails.org/getting_started.html#configuring-a-database第 3.3.2 节

于 2013-04-23T18:27:27.980 回答
0

如果您希望创建测试数据库,请尝试

rake db:test:prepare
于 2013-04-23T17:43:46.673 回答