Rails Diary 2 - MariaDB 연결과 간단한 Model

1. MariaDB 라이브러리 찾기

MariaDB와 Mysql이 태생이 같고 쿼리가 거의 대부분 동일하다고는 하나 각자가 버전업 하면서 기능상에 차이가 나는 점들이 점점 생기고 있다. Spring에서도 Mysql라이브러리 사용할때와 MariaDB 라이브러리를 사용할 때 약간의 차이를 보이기도 한다. 그래서 Rails에도 Mariadb 전용 라이브러리가 있는지 찾아보았지만 없는듯 하다.

2. mysql2 라이브러리 설치

MariaDB를 사용하지만.. MaraiDB 라이브러리가 없기에 Mysql 라이브러리를 사용해야한다. Rails에서는 mysql2를 사용하는 듯하여 Gemfile 을 추가하고 bundle install 아래와같은 에러가 발생한다.


Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/Blidkaga/.rvm/gems/ruby-2.6.0@base-rails5/gems/mysql2-0.5.2/ext/mysql2
/Users/Blidkaga/.rvm/rubies/ruby-2.6.0/bin/ruby -I /Users/Blidkaga/.rvm/rubies/ruby-2.6.0/lib/ruby/2.6.0
-r ./siteconf20190214-75062-rkmahk.rb extconf.rb
--with-mysql-config\=/usr/local/Cellar/mysq/8.0.15/bin/mysql_config
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Cannot find mysql_config at /usr/local/Cellar/mysq/8.0.15/bin/mysql_config
-----
*** 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
        --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=/Users/Blidkaga/.rvm/rubies/ruby-2.6.0/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

To see why this extension failed to compile, please check the mkmf.log which can be found here:

/Users/Blidkaga/.rvm/gems/ruby-2.6.0@base-rails5/extensions/x86_64-darwin-18/2.6.0/mysql2-0.5.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/Blidkaga/.rvm/gems/ruby-2.6.0@base-rails5/gems/mysql2-0.5.2 for
inspection.
Results logged to
/Users/Blidkaga/.rvm/gems/ruby-2.6.0@base-rails5/extensions/x86_64-darwin-18/2.6.0/mysql2-0.5.2/gem_make.out

An error occurred while installing mysql2 (0.5.2), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'` succeeds before
bundling.

In Gemfile:
  mysql2

에러 문구를 보면 mysql 설치 경로를 찾고 있다. 나는 MariaDB를 설치했으므로, 아래의 경로는 잘못된 경로이다.

...
Cannot find mysql_config at /usr/local/Cellar/mysq/8.0.15/bin/mysql_config
...

MaraiDB의 설치 경로로 해당 파일을 다시 변경 지정해주어야 한다. 그리고 다시 install

$ bundle config build.mysql2 --with-mysql-config=/usr/local/Cellar/mariadb/10.3.12/bin/mysql_config
$ bundle install

3. database.yml 수정

development 설정을 아래와 같이 수정하였다. 그 전에 RAILS_DATABASE_URL 에 환경 변수 값을 입력하였다.

development:
  adapter: mysql2
  url: <%= ENV.fetch("RAILS_DATABASE_URL") %>

4. Model 생성과 Migrate

간단한 Model을 생성해본다.

$ rails generate model book isbn:string title:string price:integer publish:string published:date cd:boolean

db에 Migrate 해본다. 왜 이건 rails 명령어가 아니라 rake 인가

$ rake db:migrate

어라 test/fixtures/books.yml 경로에 테스트 데이터를 넣을 수 있게 예제 데이터 까지 생성해준다. 오.. 예제 데이터를 넣어본다. 좀 멋지다.

$ rake db:fixtures:load FIXTURES=books

5. 느낀점

느낀점..

  • Django와 계속 비교가 되는데 예제데이터까지 미리 만들어준다는건 멋진 것 같다.
  • railsrake 왜 명령어를 2개로 분리 시켜놓았을까 이건 좀 불편하다.
  • Rails는 라이브러리 버전에 영향을 많이 받는 느낌..