LoginSignup
3
3

More than 3 years have passed since last update.

ゼロから始める開発環境構築(Rails MySQL) その2

Last updated at Posted at 2019-11-02

Rubyの準備

前回の続き
CentOSにRailsを入れる準備

こちらの記事を大変参考にしました。

※引き続きコマンド実行後の結果は基本省略しています。

[vagrant@localhost ~]$ sudo yum install -y git gcc openssl-devel readline-devel zlib-devel sqlite-devel gcc-c++ libicu-devel cmake vim

rbenvをインストール(Rubyのバージョン管理ツール。)

[vagrant@localhost ~]$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv

bashにパスを通します。(仮想環境上の)

[vagrant@localhost ~]$ vi ~/.bash_profile

下記コマンドを.bash_profileの下に追記してください

~/.bash_profile
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

下記コマンドで変更の反映(一回ログアウトして入りなおしても可)

[vagrant@localhost ~]$ source ~/.bash_profile

パスが通ればこれが出るはずです

[vagrant@localhost ~]$ rbenv --version
rbenv 1.1.2-4-g577f046

ruby-buildプラグインをインストール


[vagrant@localhost ~]$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

これでRubyが入れられます。バージョンはとりあえず2.5.0にしています。
※Rubyがインストールできるまでにそれなりに時間かかるのでしばらくほっといてください

[vagrant@localhost ~]$ rbenv install 2.5.0

Ruby 2.5.0 に指定

[vagrant@localhost ~]$ rbenv global 2.5.0
[vagrant@localhost ~]$ rbenv rehash

これでRubyが使えます。

$ ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

Bundler(Rubyのライブラリ管理ツール)のインストール

[vagrant@localhost ~]$ gem install bundler

Railsのインストール、とりあえずバージョンを5.1.5にしてます。

[vagrant@localhost ~]$ gem install rails -v 5.1.5

確認してRailsが使えるか確認

[vagrant@localhost ~]$ rails -v
Rails 5.1.5

この時点でDBはsqliteで構わない方はこれで開発がすすめられます。

雑談ですがAWS Cloud9はこの辺の環境がボタン一つで出来て、プログラミング初心者からしてみるとほんと神がかったサービスだなと思います。笑

MySQLの準備

RPMパッケージのインストール

[vagrant@localhost ~]$ sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

MySQLをインストール

[vagrant@localhost ~]$ sudo yum install mysql-community-server

インストールできたかの確認

[vagrant@localhost ~]$ mysqld --version
/usr/sbin/mysqld  Ver 8.0.17 for Linux on x86_64 (MySQL Community Server - GPL)

MySQLの設定

MySQLの起動

[vagrant@localhost ~]$ sudo systemctl start mysqld.service

MySQLの自動起動設定

[vagrant@localhost ~]$ sudo systemctl enable  mysqld.service

初期パスワードの確認をします。

[vagrant@localhost ~]$ sudo cat /var/log/mysqld.log | grep password
2019-10-13T12:57:26.788363Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ynILd;skY0Q#

「ynILd;skY0Q#」が今回の初期パスワード(この後使うのでどっかにコピペしとく)
パスワードとかもろもろの設定

[vagrant@localhost ~]$ mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

初期パスワードを入れる

The existing password for the user account root has expired. Please set a new password.

New password:

新しいパスワードを聞かれる
パスワードはパスワードが最低 1 つの数値文字を含み、1 つの小文字および大文字を含み、1 つの特殊文字 (英数字以外) を含む必要があります。(自分が良く怒られるので)
もう一度設定したいパスワードを入れる

Re-enter new password:

引き続き設定
rootパスワードの設定

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

匿名ユーザー削除

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

リモートホストからのrootログインを禁止する

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

testデータベース削除

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

ユーザー権限が保存されているテーブルをリロード

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

新しいパスワードの確認もかねてDBに入る

[vagrant@localhost ~]$ mysql -u root -p
Enter password:(設定したパスワード)

入れればOK

[vagrant@localhost ~]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.17 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye

MySQLの文字コードの設定

[vagrant@localhost ~]$ sudo vi /etc/my.cnf

以下のコードを追記

/etc/my.cnf
#文字コードの設定を追加
character_set_server=utf8
skip-character-set-client-handshake

MySQLの再起動

[vagrant@localhost ~]$ sudo systemctl restart mysqld.service

RailsでMySQLを使用するためのパッケージをインストール
たまにインストールできなかったりする・・原因はよくわかりませんが><
何回かやったり一度仮想環境を立ち上げなおしたりすると入ったりもする

[vagrant@localhost ~]$ sudo yum install mysql-devel

Railsアプリケーション立ち上げ

これからRails×MySQLのアプリケーション立ち上げのための準備をします。

ここから先の手順は共有フォルダ設定も込みで進めている方前提で作っているのでそうじゃない方は一部必要ないオプションがあったりもします。

とりあえず共有フォルダ設定しているディレクトリにてプロジェクトを立ち上げてみます。
前回からの手順通りに進めている場合
ローカル環境 ~/Desktop/study
仮想環境   /vagrant

が共有フォルダとして設定されています。
ので/vagrantに移動するか前回の記事でエイリアスを設定されている方はログアウトしてvsshで入りなおしても大丈夫です。

今回はprojectディレクトリを作ってそこにアプリケーションを作ります。(作成はできた前提で話を進めます。)

[vagrant@localhost project]$ bundle init
Writing new Gemfile to /vagrant/project/Gemfile

Gemfileを下記のように編集します。

[vagrant@localhost project]$ vi Gemfile
Gemfile
source "https://rubygems.org"

gem "rails", "5.1.5"

Railsをインストール
ホストOSとの共有フォルダ内にインストールすると実行速度が遅くなるそうなので、共有フォルダ外にインストールします(ここはあまり詳しくないです)

[vagrant@localhost project]$ bundle install --path ~/bundler/project/vender/bundle

rails newします

[vagrant@localhost project]$ rails new . --database=mysql
       exist
      create  README.md
      create  Rakefile
      create  config.ru
      create  .gitignore
    conflict  Gemfile
Overwrite /vagrant/project/Gemfile? (enter "h" for help) [Ynaqdhm]

Gemfileの変更はyを押す
bundle updateしてね的なこと言われた場合はbundle update

DBを作成してみる

下記のファイルに設定したMySQLのパスワードを入れる

database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: (ここにパスワードを入れる)
  socket: /var/lib/mysql/mysql.sock

Gemfileにもう一つGemを入れる(一番下に追記)

Gemfile
gem 'therubyracer'

改めてbundleしなおして下記コマンドを実行

[vagrant@localhost project]$ rails db:create:all

設定してあるパスワードを入れて作成される

最終確認

[vagrant@localhost project]$ rails db
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 8.0.17 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+---------------------+
| Database            |
+---------------------+
| information_schema  |
| mysql               |
| performance_schema  |
| project_development |
| project_production  |
| project_test        |
| sys                 |
+---------------------+
7 rows in set (0.00 sec)

mysql>

意外とよく使うコマンド

rails sとかでサーバー起動してもアクセスが受け付けられないことで困ることがあります。

そんな時は下記コマンドでいつも対処しています。(このIPからのアクセスは許可するよオプション)

$ rails s -b 192.168.33.10

次回はworkbenchを設定してさらに開発を快適にしていきます・・・
その3へ・・

3
3
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
3
3