如何将 MySQL 设置为 Rails 3 中的默认数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3617570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set MySQL as the default database in Rails 3?
提问by arscariosus
I started using Rails 2 last April but stopped this June because I thought learning it when Rails 3 was released would be more practical since a lot of it was completely refactored and restructured. I used to work with Ubuntu 10.04 (with SQLite3 as the default db) but now I'm using Windows 7 and MySQL 5. I already installed the gem adapter for MySQL, but to use it I still need to tweak database.yml. Thanks.
我去年四月开始使用 Rails 2,但今年六月停止使用,因为我认为在 Rails 3 发布时学习它会更实用,因为其中很多都被完全重构和重组。我曾经使用 Ubuntu 10.04(使用 SQLite3 作为默认数据库),但现在我使用的是 Windows 7 和 MySQL 5。我已经为 MySQL 安装了 gem 适配器,但要使用它,我仍然需要调整 database.yml。谢谢。
回答by tadman
In terms of database configuration, nothing much has really changed between Rails 2 and 3 with the exception of how you load your MySQL driver. This used to be done in config/environment.rb
but is now done in Gemfile
:
在数据库配置方面,除了您加载 MySQL 驱动程序的方式之外,Rails 2 和 3 之间没有什么真正改变。这曾经在 中完成,config/environment.rb
但现在在Gemfile
:
gem 'mysql'
The default config/database.yml
file is set up with SQLite, but you can easily change this over to be MySQL. A generic version looks like:
默认config/database.yml
文件是使用 SQLite 设置的,但您可以轻松地将其更改为 MySQL。通用版本如下所示:
defaults: &defaults
adapter: mysql
username: localdev
password: mylocaldevpasswordwhateveritis
host: localhost
development:
<<: *defaults
database: project_dev
test:
<<: *defaults
database: project_test
It's the adapter
declaration line that sets what driver to use.
这adapter
是设置要使用的驱动程序的声明行。
回答by Ishu
In tadman's answer, use gem 'mysql2' for the rails 3 since rails 3 now uses the new mysql adapter !!
在 tadman 的回答中,对 rails 3 使用 gem 'mysql2',因为 rails 3 现在使用新的 mysql 适配器!!
回答by Preacher
You can change rails to default to MySql when you generate a new application, but you have to edit a line in your rails installation. You'll have to make the change to every version, and every time you update the rails gem.
您可以在生成新应用程序时将 rails 更改为默认为 MySql,但您必须在 rails 安装中编辑一行。您必须对每个版本以及每次更新 rails gem 进行更改。
I use Ruby-Enterprise. So here's what I do:
我使用红宝石企业。所以这就是我要做的:
In file (where 1.8 is the ruby version and 3.0.4 is the rails version):
在文件中(其中 1.8 是 ruby 版本,3.0.4 是 Rails 版本):
/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/railties-3.0.4/lib/rails/generators/rails/app/app_generator.rb
Edit: In rails-3.1.0-rc1 the file is:
编辑:在 rails-3.1.0-rc1 文件是:
gems/railties-3.1.0.rc1/lib/rails/generators/app_base.rb
Search for this line:
搜索此行:
class_option :database, :type => :string, :aliases => "-d", :default => "sqlite3",
Change "sqlite3" to "mysql".
将“sqlite3”更改为“mysql”。
class_option :database, :type => :string, :aliases => "-d", :default => "mysql",
So instead of doing:
所以,而不是做:
rails new application_name -d mysql
I can just do (and the database.yml and Gemfiles are configured for the mysql2 gem):
我可以这样做(并且为 mysql2 gem 配置了 database.yml 和 Gemfiles):
rails new application_name
This assumes you have the correct mysql2 gem installed already. Also, I've only been doing this since Rails 3 came out. It's probably similar for previous versions. Again, every time you update Rails, you'll have to find and edit that file.
这假设您已经安装了正确的 mysql2 gem。另外,自从 Rails 3 出现以来,我才一直这样做。它可能与以前的版本相似。同样,每次更新 Rails 时,您都必须找到并编辑该文件。
回答by Josh
Since Rails 3.2 you can define a .railsrc file with custom command line options that will always apply to rails new
从 Rails 3.2 开始,您可以使用自定义命令行选项定义一个 .railsrc 文件,这些选项将始终适用于 rails new
So, if you create a file called .railsrc
and put it in your home directory w/ the contents like this -d mysql
it wll make mysql be your default database. You can put any of the command line options in there (including application templateswhich are supper awesome!)
因此,如果您创建一个名为的文件.railsrc
并将其放在您的主目录中,其中包含这样的内容,-d mysql
它将使 mysql 成为您的默认数据库。您可以在其中放置任何命令行选项(包括非常棒的应用程序模板!)
Run rails new --help
from the command line to see all your options.
运行rails new --help
命令行来查看所有选项。