使用 MySQL 而不是 SQLite 创建一个新的 Ruby on Rails 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3585/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 11:53:56  来源:igfitidea点击:

Create a new Ruby on Rails application using MySQL instead of SQLite

mysqlruby-on-railsruby

提问by Daniel Broekman

I want to create my Rails application with MySQL, because I like it so much. How can I do that in the latest version of Rails instead of the default SQLite?

我想用 MySQL 创建我的 Rails 应用程序,因为我非常喜欢它。如何在最新版本的 Rails 而不是默认的 SQLite 中做到这一点?

回答by Daniel Broekman

Normally, you would create a new Rails app using

通常,您会使用以下命令创建一个新的 Rails 应用程序

rails ProjectName

To use MySQL, use

要使用 MySQL,请使用

rails new ProjectName -d mysql

回答by Michiel de Mare

If you already have a rails project, change the adapter in the config/database.ymlfile to mysqland make sure you specify a valid username and password, and optionally, a socket:

如果您已经有一个 rails 项目,请将config/database.yml文件中的适配器更改为mysql并确保指定有效的用户名和密码,以及可选的套接字:

development:
  adapter: mysql2
  database: db_name_dev
  username: koploper
  password:
  host: localhost
  socket: /tmp/mysql.sock

Next, make sure you edit your Gemfile to include the mysql2 or activerecord-jdbcmysql-adapter (if using jruby).

接下来,确保编辑 Gemfile 以包含 mysql2 或 activerecord-jdbcmysql-adapter(如果使用 jruby)。

回答by Robbie Done

For Rails 3 you can use this command to create a new project using mysql:

对于 Rails 3,您可以使用此命令使用 mysql 创建一个新项目:

$ rails new projectname -d mysql

回答by Abhinav

Go to the terminal and write:

转到终端并写入:

rails new <project_name> -d mysql

回答by Drake Mandin

If you have not created your app yet, just go to cmd(for windows) or terminal(for linux/unix) and type the following command to create a rails application with mysql database:

如果您还没有创建您的应用程序,只需转到 cmd(对于 Windows)或终端(对于 linux/unix)并键入以下命令以使用 mysql 数据库创建一个 rails 应用程序:

$rails new <your_app_name> -d mysql

$rails new <your_app_name> -d mysql

It works for anything above rails version 3. If you have already created your app, then you can do one of the 2 following things:

它适用于 rails 版本 3 以上的任何内容。 如果您已经创建了您的应用程序,那么您可以执行以下两项操作之一:

  1. Create a another_nameapp with mysql database, go to cd another_name/config/ and copy the database.yml file from this new app. Paste it into the database.yml of your_app_nameapp. But ensure to change the database names and set username/password of your database accordingly in the database.yml file after doing so.
  1. 使用 mysql 数据库创建another_name应用程序,转到 cd another_name/config/ 并从这个新应用程序复制 database.yml 文件。将其粘贴到your_app_name应用程序的 database.yml 中。但请确保更改数据库名称并在执行此操作后在 database.yml 文件中相应地设置数据库的用户名/密码。

OR

或者

  1. Go to cd your_app_name/config/ and open database.yml. Rename as following:
  1. 转到 cd your_app_name/config/ 并打开 database.yml。重命名如下:

development:
adapter: mysql2
database: db_name_name
username: root
password:
host: localhost
socket: /tmp/mysql.sock

开发:
适配器:mysql2
数据库:db_name_name
用户名:root
密码:
主机:localhost
套接字:/tmp/mysql.sock

Moreover, remove gem 'sqlite3' from your Gemfile and add the gem 'mysql2'

此外,从您的 Gemfile 中删除 gem 'sqlite3' 并添加 gem 'mysql2'

回答by Coder

If you are using rails 3 or greater version

如果您使用的是 rails 3 或更高版本

rails new your_project_name -d mysql

if you have earlier version

如果你有更早的版本

rails new -d mysql your_project_name

So before you create your project you need to find the rails version. that you can find by

因此,在创建项目之前,您需要找到 rails 版本。你可以找到

rails -v

回答by huacnlee

rails -d mysql ProjectName

回答by Dipali Nagrale

Create application with -d option

使用 -d 选项创建应用程序

rails new AppName -d mysql

回答by vijay chouhan

rails new <project_name> -d mysql

OR

或者

rails new projectname

Changes in config/database.yml

config/database.yml 中的更改

development:
  adapter: mysql2
  database: db_name_name
  username: root
  password:
  host: localhost
  socket: /tmp/mysql.sock

回答by George Bellos

$ rails --help 

is always your best friend

永远是你最好的朋友

usage:

用法:

$ rails new APP_PATH[options]

also note that options should be given after the application name

另请注意,应在应用程序名称之后给出选项

rails and mysql

导轨和 mysql

$ rails new project_name -d mysql

rails and postgresql

导轨和 postgresql

$ rails new project_name -d postgresql