DEV Community

FRedwuSong
FRedwuSong

Posted on

rails database 改成 mysql 所遇到的問題

環境:

  • ruby 2.5.9

  • Rails 6.1.7.6

  • spring 3.1.1

  • sqlite3 1.4.4

rails 專案預設是使用 sqlite ; 因為要練習的關係所以想換成 MySQL

rails6 有新增一個指令可以直接切換 rails 所使用的資料庫種類
https://github.com/rails/rails/issues/34710

rails db:system:change --to=postgresql|mysql|sqlite
Enter fullscreen mode Exit fullscreen mode

但是我卻遇到問題

rails db:system:change --to=mysql
Running via Spring preloader in process 61295
invalid option: --to=mysql
Enter fullscreen mode Exit fullscreen mode

發現是 spring 版本的問題,需要改 spring 的版本
https://github.com/rails/spring/issues/678
改用 rubygems 預設的 版本 1.7.2
https://rubygems.org/gems/spring/versions/1.7.2?locale=zh-TW
編輯 gemfile

gem 'spring', '~> 1.7', '>= 1.7.2'
gem 'mysql2', '~> 0.5'
# 這邊還要再把 sqlite3 1.4.4 換成 mysql2 0.5.5
Enter fullscreen mode Exit fullscreen mode

之後再 bundle install。
rails db:system:change --to=mysql

    conflict  config/database.yml
Overwrite /private/tmp/rails_617/mysqlpractice/config/database.yml? (enter "h" for help) [Ynaqdhm] Y
       force  config/database.yml
        gsub  Gemfile
        gsub  Gemfile
Enter fullscreen mode Exit fullscreen mode

恭喜!!!!~~~
已經成功把 sqlite 換成 MySQL 啦~~~

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more