我正在启动一个新的rails 3应用程序,当我运行rake db:create
时,它会询问我的root mysql密码,然后出错:
Mysql2 ::错误:用户'arcsite_mysql'@'localhost'拒绝访问数据库'arcsite_mysql_test':CREATE DATABASE
arcsite_mysql_test
DEFAULT CHARACTER SETutf8
COLLATEutf8_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
我的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: 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
因此,出于某种原因,只创建了开发数据库。
版本:
Rails 3.2.13
MySQL:服务器版本:5.6.10源代码分发
mysql2:mysql2-0.3.11
修改
如果我手动向arcsite_mysql
用户授予所有权限,我可以创建表格。但是,预计Rails会为开发而不是测试环境创建用户和表吗?当我第一次运行arcsite_mysql
命令时,rake db:create
用户是由rails创建的。
编辑2
提到了一些涉及mysql用户创建的“众所周知”的过程。
答案 0 :(得分: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密码来创建用户,如果它不是exst)或改变它。供参考:http://guides.rubyonrails.org/getting_started.html#configuring-a-database第3.3.2节
答案 1 :(得分:0)
如果您希望创建测试数据库,请尝试
rake db:test:prepare