LoginSignup
9
7

More than 5 years have passed since last update.

Ruby on Rails 5 with JDBC(MySQL)環境を作る

Last updated at Posted at 2016-09-17

Ruby on Rails 5 with JDBC(MySQL)環境を作ります。
前提条件として、MySQL Serverをローカルで稼働しており、必要なデータベース(jruby_schema、jruby_schema_development)は作成しているものとします。
また今回は、JDBCはJARファイルを使います。

1. Gitのインストール

[Linux]

$ yum -y install git

*rootユーザーもしくはsudoで

[Mac]

2. rbenvのインストール(anyenvでも可)

[Linux]

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

[Mac]

$ brew install rbenv
$ brew install ruby-build

参考リンク

rbenv を使って ruby をインストールする(CentOS編)
MacにrbenvでRubyを簡単にインストールしてみる

2. jrubyのインストール、設定

$ rbenv install --list
  (中略)
  jruby-9.1.3.0
  jruby-9.1.4.0
  jruby-9.1.5.0
  maglev-1.0.0
  maglev-1.1.0-dev
  maglev-2.0.0-dev
  (中略)
$ rbenv install jruby-9.1.5.0
$ rbenv rehash
$ rbenv versions
* system
  jruby-9.1.5.0
$ rbenv global jruby-9.1.5.0

3. Gemのインストール

$ gem install bundler rails jdbc-mysql

4. Railsアプリの初期化

$ rbenv exec rails new test-app -d jdbcmysql
$ cd test-app

5. Gemfileの変更(jdbcのRails5対応)

test-app/Gemfile
# 下記をコメントアウト
# gem 'activerecord-jdbc-adapter'
# 下記を追記
gem 'activerecord-jdbc-adapter', github: "jruby/activerecord-jdbc-adapter",
                                 branch: "rails-5"
# ただし、jdbc-mysqlは今回使わない
# gem 'jdbc-mysql', github: 'jruby/activerecord-jdbc-adapter',
#                   branch: 'rails-5'

6. Gemfileにjbunlderの追加

test-app/Gemfile
# 以下を追記
# Use jbundler https://github.com/mkristian/jbundler
gem 'jbundler', '>= 0.9.3'

7. Gemfileにjar-dependenciesの追加

test-app/Gemfile
# 下記を追記
# Use jar-dependencies https://github.com/mkristian/jar-dependencies
gem 'jar-dependencies', '>= 0.3.5'

8. Gemfileにdevelopment環境向けgemを追記

test-app/Gemfile
# 下記を追記
group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

9. Jarfileを追加

test-app/Jarfile
# 以下を記述したJarfileを保存
jar 'mysql:mysql-connector-java', '5.1.39'
# 6.0.Xだとエラーになる

10. bundlerでGemを再インストール

$ bundle install

11. jbundlerでJAR(MySQL JDBC)をインストール

$ jbundle install

12. config/boot.rbの修正

test-app/config/boot.rb(変更前)
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require 'bundler/setup' # Set up gems listed in the Gemfile.
test-app/config/boot.rb(変更後)
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
#追記開始
require 'jbundler' # Use jbundler for some JARs.
#追記終わり
require 'bundler/setup' # Set up gems listed in the Gemfile.

12. config/database.yamlの修正

test-app/config/database.yaml
# MySQL. Versions 5.0 and up are supported.
#
# Install the MySQL driver:
#   gem install activerecord-jdbcmysql-adapter
#
# Configure Using Gemfile
# gem 'activerecord-jdbcmysql-adapter'
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.7/en/old-client.html
#
default: &default
  adapter: jdbc
  jndi: jdbc/jruby
  username: root
  password: password
  host: localhost
  encoding: utf8
  reconnect: true
  pool: 8
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://127.0.0.1:3306/jruby_schema
  database: jruby_schema
  properties:
    autoReconnect: true
    autoReconnectForPools: true
    defaultFetchSize: 1000
    requireSSL: false
    useCursorFetch: true
    useSSL: false
    useUnicode: true

development:
  <<: *default
  database: jruby_schema_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: jruby_schema_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="mysql://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default

13. Railsアプリをスタート

$ jruby -S bin/rails s
9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7