Vasseurth
Vasseurth

Reputation: 6486

How do I change my database from SQLite to MYSQL in Rails

I know that you have to change the database.yml but I don't know what to change it to and how to download MYSQL and all of that jazz.

Upvotes: 11

Views: 13290

Answers (3)

James Hibbard
James Hibbard

Reputation: 17755

As of Rails 6 a command has been added to do this automatically.

$ rails db:system:change --to=mysql
    conflict  config/database.yml
Overwrite /home/jim/Rails projects/myapp/config/database.yml? (enter "h" for help) [Ynaqdhm] y
       force  config/database.yml
        gsub  Gemfile
        gsub  Gemfile

Ref.: https://www.bigbinary.com/blog/rails-6-has-added-a-way-to-change-the-database-of-the-app

Upvotes: 0

tybro0103
tybro0103

Reputation: 49703

Gemfile:

gem 'mysql2'

config/database.yml

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: db_name_here
  pool: 5
  username: root
  password:
  host: localhost

Command line:

bundle install
rake db:create
rake db:migrate

Of course MySQL needs to be installed.

If you're creating a new project:

rails new app_name_here -d mysql

Upvotes: 27

Avram
Avram

Reputation: 103

I ran into the same problem when trying to use the mysql2 gem with Rails 3.0.9. When I ran rake db:create after installing the mysql2 gem, it gave me these warnings:

WARNING: This version of mysql2 (0.3.6) doesn't ship with the ActiveRecord adapter bundled anymore as it's now part of Rails 3.1

WARNING: Please use the 0.2.x releases if you plan on using it in Rails <= 3.0.x

To specify that you only want to use the 0.2.x versions of mysql2, edit your Gemfile so that

gem 'mysql2'

becomes

gem 'mysql2', '~> 0.2.1'

Upvotes: 2

Related Questions