Rails6アプリのデータベースをSQliteからMySQLに変更したい
現在作成中のRailsアプリをAWSへデプロイするためにデータベースを
SQliteからMySQLに変更したいのですが
エラーが発生し先に進めず困っております。
ご教授いただける方がいらっしゃれば教えていただけると幸いです。
###実行環境
version
1ProductName: macOS 2ProductVersion: 11.2.1 3ruby 2.7.2 4Rails 6.0.3
試したこと
①MySQLへ変更
terminal
1$ rails db:system:change --to=mysql 2 conflict config/database.yml 3Overwrite /Users/name/appName/config/database.yml? (enter "h" for help) [Ynaqdhm] y 4 force config/database.yml 5 gsub Gemfile 6 gsub Gemfile
上記コマンドにより変化したGemfile
Gemfile
1#コマンド実行前 2group :development, :test do 3 gem 'sqlite3', '1.4.1' 4end 5 6group :production do 7 gem 'pg', '1.1.4' 8end 9 10#コマンド実行後 11group :development, :test do 12 gem 'mysql2', '1.4.1' 13end 14 15group :production do 16 gem 'mysql2', '1.1.4' 17end
database.ymlの変化
config/database.yml
1#コマンド前 2default: &default 3 adapter: sqlite3 4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 5 timeout: 5000 6 7development: 8 <<: *default 9 database: db/development.sqlite3 10 11test: 12 <<: *default 13 database: db/test.sqlite3 14 15production: 16 <<: *default 17 database: db/production.sqlite3 18 19#コマンド実行後 20default: &default 21 adapter: mysql2 22 encoding: utf8mb4 23 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 24 username: root 25 password: 26 socket: /tmp/mysql.sock 27 28development: 29 <<: *default 30 database: [appName]_development 31 32test: 33 <<: *default 34 database: [appName]_test 35 36production: 37 <<: *default 38 database: [appName]_production 39 username: [appName] 40 password: <%= ENV['[APPNAME]_DATABASE_PASSWORD'] %>
エラーメッセージ
terminal
1$ bundle install 2 3[!] There was an error parsing `Gemfile`: You cannot specify the same gem twice with different version requirements. 4You specified: mysql2 (= 1.4.1) and mysql2 (= 1.1.4). Bundler cannot continue. 5 6 # from /Gemfile:49 7 # ------------------------------------------- 8 # group :production do 9 > gem 'mysql2', '1.1.4' 10 # end 11 # -------------------------------------------
###Gemfileの変更(バージョンの指定をなくす)
Gemfile
1group :development, :test do 2 gem 'mysql2' 3end 4 5group :production do 6 gem 'mysql2' 7end
###パスを通す
terminal
1$ bundle config --local build.mysql2 "--with-cppflags=-I/usr/local/opt/openssl/include" 2#または 3$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib" 4#または 5$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
エラーメッセージ
上記のどのパターンのパスを指定しても
下記のエラーが発生してしまいます。
terminal
1$ bundle install 2 3・・・ 4Fetching mysql2 0.5.3 5Installing mysql2 0.5.3 with native extensions 6Errno::EACCES: Permission denied @ rb_sysopen - /Users/username/appname/vendor/bundle/ruby/2.7.0/gems/mysql2-0.5.3/CHANGELOG.md 7An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue. 8Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling. 9 10In Gemfile: 11 mysql2
###Gemのインストール
エラーメッセージ通りにgemをインストールしてみる
terminal
1$ sudo gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/' 2 3Building native extensions. This could take a while... 4ERROR: Error installing mysql2: 5 ERROR: Failed to build gem native extension. 6 7 current directory: /Users/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3/ext/mysql2 8/Users/username/.rbenv/versions/2.7.2/bin/ruby -I /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0 -r ./siteconf20210307-39936-1kuenmr.rb extconf.rb 9checking for rb_absint_size()... *** extconf.rb failed *** 10Could not create Makefile due to some reason, probably lack of necessary 11libraries and/or headers. Check the mkmf.log file for more details. You may 12need configuration options. 13 14Provided configuration options: 15 --with-opt-dir 16 --without-opt-dir 17 --with-opt-include 18 --without-opt-include=${opt-dir}/include 19 --with-opt-lib 20 --without-opt-lib=${opt-dir}/lib 21 --with-make-prog 22 --without-make-prog 23 --srcdir=. 24 --curdir 25 --ruby=/Users/username/.rbenv/versions/2.7.2/bin/$(RUBY_BASE_NAME) 26/Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:471:in `try_do': The compiler failed to generate an executable file. (RuntimeError) 27You have to install development tools first. 28 from /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:564:in `try_link0' 29 from /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:582:in `try_link' 30 from /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:794:in `try_func' 31 from /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:1083:in `block in have_func' 32 from /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:971:in `block in checking_for' 33 from /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:361:in `block (2 levels) in postpone' 34 from /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:331:in `open' 35 from /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:361:in `block in postpone' 36 from /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:331:in `open' 37 from /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:357:in `postpone' 38 from /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:970:in `checking_for' 39 from /Users/username/.rbenv/versions/2.7.2/lib/ruby/2.7.0/mkmf.rb:1082:in `have_func' 40 from extconf.rb:25:in `<main>' 41 42To see why this extension failed to compile, please check the mkmf.log which can be found here: 43 44 /Users/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/extensions/x86_64-darwin-20/2.7.0/mysql2-0.5.3/mkmf.log 45 46extconf failed, exit code 1 47 48Gem files will remain installed in /Users/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3 for inspection. 49Results logged to /Users/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/extensions/x86_64-darwin-20/2.7.0/mysql2-0.5.3/gem_make.out
ヒントだけでもいいので、わかる方教えて頂きたいです。
よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー