数据库
首页 > 数据库> > mysql – rake db:仅创建创建开发数据库

mysql – rake db:仅创建创建开发数据库

作者:互联网

我正在开始一个新的rails 3应用程序,当我运行rake db:创建它请求我的root mysql密码然后出错说:

Mysql2::Error: Access denied for user ‘arcsite_mysql’@’localhost’ to database ‘arcsite_mysql_test’: CREATE DATABASE arcsite_mysql_test DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci

如果我在命令行登录mysql并运行show databases;我看到了这个:

arcsite_mysql_development

如果我为arcsite_mysql @ localhost运行SHOW GRANTS,我会看到:

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会为开发而不是测试环境创建用户和表吗?当我第一次运行rake db:create命令时,arcsite_mysql用户是由rails创建的.

编辑2

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

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

解决方法:

您需要在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节

标签:mysql,ruby-on-rails,rake,mysql2
来源: https://codeday.me/bug/20190620/1244353.html