2013-04-23 29 views
3

我開始一個新的Rails應用程序3,當我運行rake db:create它要求我的MySQL的root密碼,然後出現了錯誤說:耙分貝:創建只有創造發展DATABSE

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 

如果我跑SHOW GRANTS FOR [email protected]我看到這一點:

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創建。

EDIT 2

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

提到關於 「衆所周知」 的過程,涉及到mysql用戶創建的東西。

+1

用戶'arcsite_mysql'是否存在於mysql中?如果此用戶存在並且具有適當的權限,我認爲它不應該要求您提供root密碼。如果不是這樣的話: 'GRANT ALL *。* TO'arcsite_mysql'@'localhost'IDENTIFIED BY'password';' – 2013-04-23 17:51:32

+0

我以爲Rails負責在mysql中創建用戶嗎?用戶在運行'rake db:create'命令之前並不存在。 – 2013-04-23 17:54:54

+0

稍微更新了這個問題。用戶創建行爲似乎有點時髦 – 2013-04-23 18:04:19

回答

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

我會做的唯一更正是軌道將創建一個用戶(雖然有一些時髦的副作用)。可能最適合我自己創建數據庫和用戶。只是希望人們在遇到問題時能夠意識到這種「魔力」。以下是與數據庫創建相關的對話:https://rails.lighthouseapp.com/projects/8994/tickets/1459-patch-better-dbcreate-for-mysql-do-not-assume-root-user。第4步提到了涉及mysql用戶創建的「衆所周知」的過程 – 2013-04-23 18:31:48

+0

呵呵,我不知道!我自己使用postgresql,據我所知在rails中沒有相同的功能。我會更新我的答案。這裏是一個鏈接到當前在rails的提交感興趣的人:https://github.com/rails/rails/blob/master/activerecord/lib/active_record/tasks/mysql_database_tasks.rb,因爲另一個有點過時 它雖然很奇怪......我很好奇爲什麼現在失敗了。 – 2013-04-23 19:07:48

0

如果你正在尋找有創建測試數據庫,嘗試

rake db:test:prepare 
+0

當我每次運行'rake db:test:prepare' – 2013-04-23 17:47:48

+0

時,我得到相同的錯誤。上面的truluck的註釋,看起來像arcsite_mysql只有一個數據庫的權限。您需要授予該用戶在服務器實例中的所有權限。 – 2013-04-23 17:56:49

+0

更新了一些問題。用戶創建行爲似乎有點時髦 – 2013-04-23 18:03:37

相關問題