Ruby on RailsのDBがデフォルトでSQLite3になっていて途中まで使っていたのですが、本番環境を構築するにあたり、MariaDBにしようと思いたちましたので、手順等記録しておきます。
手順
gemインストール
先ず、普通にGemfileにMariaDBのgemを追記してみました。
1 |
gem 'mysql2' |
これを追記してbundle installしたところ、以下のエラーが。
1 2 3 4 5 6 7 8 |
Gem files will remain installed in /home/hase_done/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/mysql2-0.4.10 for inspection. Results logged to /home/xxx/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/extensions/x86_64-linux/2.4.0-static/mysql2-0.4.10/gem_make.out An error occurred while installing mysql2 (0.4.10), and Bundler cannot continue. Make sure that `gem install mysql2 -v '0.4.10'` succeeds before bundling. In Gemfile: mysql2 |
促されるまま
1 |
gem install mysql2 -v '0.4.10 |
を叩くと以下のメッセージが。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
----- mysql client is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try agai----- *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. Provided configuration options: --with-opt-dir --without-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir --ruby=/home/hase_done/.rbenv/versions/2.4.2/bin/$(RUBY_BASE_NAME) --with-mysql-dir --without-mysql-dir --with-mysql-include --without-mysql-include=${mysql-dir}/include --with-mysql-lib --without-mysql-lib=${mysql-dir}/lib --with-mysql-config --without-mysql-config --with-mysql-dir --without-mysql-dir --with-mysql-include --without-mysql-include=${mysql-dir}/include --with-mysql-lib --without-mysql-lib=${mysql-dir}/lib --with-mysqlclientlib --without-mysqlclientlib To see why this extension failed to compile, please check the mkmf.log which can be found here: /home/xxx/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/extensions/x86_64-linux/2.4.0-static/mysql2-0.4.10/mkmf.log extconf failed, exit code 1 Gem files will remain installed in /home/xxx/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/mysql2-0.4.10 for inspection. Results logged to /home/xxx/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/extensions/x86_64-linux/2.4.0-static/mysql2-0.4.10/gem_make.out |
上のmkmf.logを見てみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
"gcc -o conftest -I/home/xxx/.rbenv/versions/2.4.2/include/ruby-2.4.0/x86_64-linux -I/home/hase_done/.rbenv/versions/2.4.2/include/ruby-2.4.0/ruby/backward -I/home/xxx/.rbenv/versions/2.4.2/include/ruby-2.4.0 -I. -I/usr/local/include -I/home/xxx/.rbenv/versions/2.4.2/include -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wno-tautological-compare -Wno-parentheses-equality -Wno-constant-logical-operand -Wno-self-assign -Wunused-variable -Wimplicit-int -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wsuggest-attribute=noreturn -Wsuggest-attribute=format conftest.c -L. -L/home/hase_done/.rbenv/versions/2.4.2/lib -Wl,-rpath,/home/xxx/.rbenv/versions/2.4.2/lib -L/usr/local/lib -Wl,-rpath,/usr/local/lib -L/usr/local/lib/mysql -Wl,-rpath,/usr/local/lib/mysql -L. -L/home/xxx/.rbenv/versions/2.4.2/lib -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-rpath,/home/hase_done/.rbenv/versions/2.4.2/lib -L/home/xxx/.rbenv/versions/2.4.2/lib -lruby-static -lmysqlclient -lpthread -ldl -lcrypt -lm -lc" /bin/ld: cannot find -lmysqlclient collect2: error: ld returned 1 exit status checked program was: /* begin */ 1: #include "ruby.h" 2: 3: /*top*/ 4: extern int t(void); 5: int main(int argc, char **argv) 6: { 7: if (argc > 1000000) { 8: printf("%p", &t); 9: } 10: 11: return 0; 12: } 13: extern void mysql_query(); 14: int t(void) { mysql_query(); return 0; } /* end */ -------------------- |
ぱっと見時点で良くわからなかったので、とりあえず
1 2 |
gem uninstall mysql2 gem install mysql2 |
をしてみましたが改善されません。
もう一度ログを見ると
1 2 |
mysql client is missing. yum install mysql-devel |
とあったのでやってみました。
これが原因でした。
この後、
1 2 |
gem install mysql2 bundle install |
で問題なくインストール出来ました。
railsがMariaDBを使用する設定
デフォルトではSQLite3を使用するようになっているので、config/database.ymlを修正します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
default: &default adapter: mysql2 encoding: utf8 reconnect: false pool: 5 username: xxxx password: xxxx host: localhost timeout: 5000 development: <<: *default database: hoge_dev test: <<: *default database: hoge_test production: <<: *default database: hoge_prod |
DB生成
1 2 3 |
$ rake db:create $ rails db MariaDB > show databases; |
DBが生成されていれば完了です。
参考サイト様
http://gdgd-shinoyu.hatenablog.com/entry/2014/10/23/021921
https://qiita.com/MariMurotani/items/a34878916fcabfc3c638
https://qiita.com/namahage1935/items/5b201e6444eb13660709
http://goodbyegangster.hatenablog.com/entry/2017/02/15/215345
http://kkkw.hatenablog.jp/entry/mariadb/bundle-install-mysql2-error
https://qiita.com/yokoto/items/ea3565f92f5eda57151a#_reference-52a1185fe48ebba5e984