LoginSignup
11
12

More than 3 years have passed since last update.

CentOS7 で Rails 5.2.3 + MariaDB 10.4 をインストール

Last updated at Posted at 2017-08-05

RVM で Ruby 2.5.5 をインストール

gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 \
7D2BAF1CF37B13E2069D6956105BD0E739499BDB

でGPG鍵をインストールできるはずですが、GPGのバージョンの問題でできなかったので、以下のコマンドを実行

command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -

RVMをインストール。

\curl -sSL https://get.rvm.io | bash -s stable

続いて Ruby 2.5 をインストール。

rvm install ruby-2.5

バージョンを確認。

$ ruby --version
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]

~/.gemrc で rdoc をインストールしないようにします。

~/.gemrc
install: --no-document
update: --no-document

bundlerをインストール。

gem install bundler

MariaDB 10.4 をインストール

CentOS7 標準の MariaDB 5.5 がインストールされている場合は、まずアンインストールします。

sudo yum -y remove mariadb-*

つぎに MariaDB Package Repository Setup and Usage に記載されているコマンドを実行、MariaDB のレポジトリファイルを作成します。

curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash

yumで MariaDB 10.4 をインストールします。

sudo yum -y install MariaDB-server MariaDB-client MariaDB-devel

以下のrpmがインストールされます。


$ rpm -qa | grep -i mariadb | sort
MariaDB-client-10.4.6-1.el7.centos.x86_64
MariaDB-common-10.4.6-1.el7.centos.x86_64
MariaDB-compat-10.4.6-1.el7.centos.x86_64
MariaDB-devel-10.4.6-1.el7.centos.x86_64
MariaDB-server-10.4.6-1.el7.centos.x86_64
$ rpm -qa | grep -i galera
galera-4-26.4.2-1.rhel7.el7.centos.x86_64

MariaDB service を起動します。

sudo systemctl start mariadb

Ruby on Rails 5.2.3 をインストール

以下のように Gemfile を作成、

Gemfile
source 'https://rubygems.org'

gem 'rails', '5.2.3'
gem 'mysql2'

bundle install を実行します。

make: *** [mysql2.so] Error 1

make failed, exit code 2

Gem files will remain installed in /home/vagrant/.rvm/gems/ruby-2.5.5/gems/mysql2-0.5.2 for inspection.
Results logged to
/home/vagrant/.rvm/gems/ruby-2.5.5/extensions/x86_64-linux/2.5.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

というエラーでmysql2のビルドに失敗します。

sudo yum -y install MariaDB-shared

で MariaDB-shared をインストールすると解決しました。

ToDoアプリを作ってみる

以下のコマンドを実行、新規Railsアプリを作成します。

rails new todo
cd todo

デフォルトだとデータベースに SQLite3 を使うようになっているので、config/database.ymlの development データベース設定を以下のように修正します。


development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: development
  pool: 5
  username: rails5
  password: password
  host: localhost

MariaDB Server は 10.4 以降 unix_socket plugin がデフォルトの認証方法になっていますので,mysql_native_password で認証できるよう,Rails から接続用のユーザを作成します。

MariaDB [(none)]> GRANT ALL ON development.* TO rails5@'localhost' IDENTIFIED BY 'password';
MariaDB [(none)]> GRANT ALL ON production.* TO rails5@'localhost';

(ユーザ名,パスワードは適宜変更してください)

Gemfile の末尾にgem 'mysql2'を追記,

echo "gem 'mysql2'" >> Gemfile

17行目付近の

Gemfile
# gem 'mini_racer', platforms: :ruby

のコメントを外し,

Gemfile
gem 'mini_racer', platforms: :ruby

にします。
scaffold でアプリケーションの土台を生成します。

bundle install
rails generate scaffold tasks title:string content:text due:date

データベースを作成,テーブル作成,Railsサーバを起動します。

$ rake db:create
Created database 'development'

$ rake db:migrate
== 20190705063555 CreateTasks: migrating ======================================
-- create_table(:tasks)
   -> 0.0022s
== 20190705063555 CreateTasks: migrated (0.0023s) =============================

$ rails s -b 0.0.0.0
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.5.5-p157), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

サーバの port 3000 にブラウザでアクセスして以下のようにタスク新規作成ができれば問題ないはずです。

image.png

11
12
1

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
11
12