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

前端 未结 19 1025
难免孤独
难免孤独 2020-12-04 05:46

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?

相关标签:
19条回答
  • 2020-12-04 06:05

    Normally, you would create a new Rails app using

    rails ProjectName
    

    To use MySQL, use

    rails new ProjectName -d mysql
    
    0 讨论(0)
  • 2020-12-04 06:06

    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:

    $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:

    1. Create a another_name app 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_name app. But ensure to change the database names and set username/password of your database accordingly in the database.yml file after doing so.

    OR

    1. Go to cd your_app_name/config/ and open database.yml. Rename as following:

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

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

    0 讨论(0)
  • 2020-12-04 06:09

    In Rails 3, you could do

    $rails new projectname --database=mysql
    
    0 讨论(0)
  • 2020-12-04 06:11

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

    $ rails new projectname -d mysql
    
    0 讨论(0)
  • 2020-12-04 06:12

    If you are using rails 3 or greater version

    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 -v
    
    0 讨论(0)
  • 2020-12-04 06:13
    $ 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

    $ rails new project_name -d mysql
    

    rails and postgresql

    $ rails new project_name -d postgresql
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题