5
2

More than 3 years have passed since last update.

vagrant(CentOS7)環境でrails6 + MySQLの環境構築(半分は自分メモ)

Posted at

はじめに

今回の投稿が初投稿になります。至らない点が多々あるかと思いますが参考にしていただけると幸いです。
最後の方がグダグダしてたりするのでご了承ください。
また、VirtualBox + Vagrantは始めから入っている状態から入っている前提でお話しさせていただきます。

こちらの記事を参考にさせていただきました。
Windows 10 + VirtualBox + Vagrant + CentOS + Rails で、”Yay! You’re on Rails!”まで

環境

ホストos
・windous10 pro
・Vagrant 2.2.6

ゲストos
・centos7
・ruby 2.7.0
・rails 6.0.2.2

ホストOSでの準備

はじめに、vagrantがはいっているか確認します。

C:\Users\hogetarou\vagrant>vagrant --version
Vagrant 2.2.6

続いて作業用のディレクトリを作っていきます。

C:\Users\hogetarou\vagrant>mkdir rails
C:\Users\hogetarou\vagrant>cd rails
C:\Users\hogetarou\vagrant\rails>vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
    box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop

Enter your choice: 3
==> box: Adding box 'centos/7' (v1905.1) for provider: virtualbox
The box you're attempting to add already exists. Remove it before
adding it again or add it with the `--force` flag.

Name: centos/7
Provider: virtualbox
Version: 1905.1

C:\Users\hogetarou\vagrant\rails>vagrant box list
centos/7 (virtualbox, 1905.1)

box listにcentos/7が追加されていたら問題ないです。

C:\Users\hogetarou\vagrant\rails>vagrant init centos/7

このコマンドでディレクトリの中にVagrantfileが作成されていますので設定します。
33行目あたりに
config.vm.network "private_network", ip: "192.168.33.10"
があるのでコメントアウトを外してください。

終わったら仮想環境を立ち上げてsshで仮想環境に入ります。

C:\Users\hogetarou\vagrant\rails>vagrant up
...
...結構長いです
...
C:\Users\hogetarou\vagrant\rails>vagrant ssh
[vagrant@localhost ~]$

ゲストOSでの作業

rbenvのインストール

gitをインストールしてからいろいろ持ってきます。

$ sudo yum install -y git

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

ruby-buildのインストール

$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ cd ~/.rbenv/plugins/ruby-build
$ sudo ./install.sh

rbenvの確認

$ rbenv -v
rbenv 1.1.2-28-gc2cfbd1

必要なパッケージをインストール

$ sudo yum install -y openssl-devel readline-devel zlib-devel mysql-devel gcc gcc-c++
$ sudo yum install -y epel-release
$ sudo yum install -y nodejs --enablerepo=epel

rbenvを使ってrubyをインストール

初めどのバージョンがインストールできるか確認します。

$ rbenv install -l
...
2.7.0-dev
2.7.0-preview1
2.7.0-preview2
2.7.0-preview3
2.7.0-rc1
2.7.0-rc2
2.7.0
2.8.0-dev
jruby-1.5.6
jruby-1.6.3
...

今回はruby2.7.0をインストールします。
注釈 このインストールは長いです!!何も反応がありませんが、ちゃんと動いてます!待ってあげて!

$ rbenv install 2.7.0

rubyを設定します。

$ rbenv rehash
$ rbenv global 2.7.0

バージョンの確認

$ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]

railsをインストール

バージョンを指定するときは
$ gem install -v 6.0.0 rails
みたいな感じで指定可能です。

$ gem install rails

バージョンの確認

$ rails -v
Rails 6.0.2.2

DB(MySQL)をインストール

npmとnode.jsがしっかりとインストールされているか確認

$ npm -v
3.10.10
$ node -v
v6.17.1

mariaDBが入っている可能性があるので念のため削除

$ sudo yum remove mariadb-libs
$ sudo rm -rf /var/lib/mysql/

MySQLの公式のyumリポジトリを追加。
そしてインストール。確認もしましょう。

$ sudo yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
...
...もろもろ

$ sudo yum -y install mysql-community-server
...
...もろもろ

$ sudo mysqld --version
mysqld  Ver 5.7.29 for Linux on x86_64 (MySQL Community Server (GPL))

一行目のコマンドでCentOS起動の際自動で起動してくれるようになります。
MySQLserver起動

$ sudo systemctl enable mysqld.service
$ sudo systemctl start mysqld.service

止めるときはこれ
今回は使わない予定。

$ sudo systemctl stop mysqld.service

続いてuserとデータベースを作っていきます。

$ mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

ぬは。パスワードが無いって怒ってますね。
ってことでパスワードを探してログインします。

$ sudo  cat /var/log/mysqld.log | grep root
2020-03-26T14:23:10.121821Z 1 [Note] A temporary password is generated for root@localhost: あなたのパスワード

ps.私のパスワードの最後の文字が > でそれがパスワードの一部だと気づかないで結構な時間取られました。

そしてログイン

$ mysql -u root -p
Enter password: あなたのパスワード

初めにrootのパスワードを変えていきます。
パスワードですが8文字以上、英数大文字小文字と記号を使わないと、パスワードポリシーエラーになります。
例)Hogetarou<<0327

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'パスワード';

そして、userを作っていきます。
ここで作ったユーザー名とパスワードは後で使います!!!!!

mysql> create user 'ユーザー名'@'localhost' identified by 'パスワード(rootと別)';

最後に確認します。waninokoが私です。

mysql> select User,Host from mysql.user;
+---------------+-----------+
| User          | Host      |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
| waninoko      | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

railsアプリ作成

$ rails new sample -d mysql

赤字でなんか文句言われました。
要するに、必要なgemが足りないって言ってきてます。

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

In Gemfile:
  mysql2
         run  bundle binstubs bundler
Could not find gem 'mysql2 (>= 0.4.4)' in any of the gem sources listed in your Gemfile.
         run  bundle exec spring binstub --all
bundler: command not found: spring
Install missing gem executables with `bundle install`
       rails  webpacker:install
Could not find gem 'mysql2 (>= 0.4.4)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

さっき作成アプリケーションのディレクトリに移動してインストールします。

$ sudo yum install mysql-devel

終わったらgemfileのインストール

$ bundle

yarnをインストール

こちらを参考にさせていただきました。
ruby on rails 環境をセットアップするときの自分メモ

$ sudo yum remove nodejs npm #古いのが入っていれば削除
$ curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -
$ sudo yum install -y nodejs
$ sudo npm install yarn -g

webpackerをインストール

bundle exec rails webpacker:install

nodeのバージョンエラーになる場合はこちらを実行してまたwebpakerをインストール

$ sudo npm install -g n
$ sudo n 9.11.2

これでとりあえずrails serverは問題なく起動するようになりました。
ここからデータベースの設定をしていきます。あと少しですw

database.ymlの設定とDBの作成

先ほど控えたDBのユーザー名とパスワードを書きます。
僕はatomのremote-ftpを使いましたが、viが書ける人はそちらを使ってもらって大丈夫です。

/config/database.yml
...
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: ユーザー名
  password: パスワード
  host: localhost
  socket: /var/lib/mysql/mysql.sock
...

続いてデータベースを作っていきます。

$ rails db:create
...
...
...
Created database 'sample_development'
Created database 'sample_test'

これでデータベースは完成!!!!
rails serverを起動

$ rails s -b 192.168.33.10
...
...
* Listening on tcp://192.168.33.10:3000
Use Ctrl-C to stop

最後にホストOS側のブラウザからアクセスして終わりです。お疲れさまでした。
http://192.168.33.10:3000/

5
2
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
5
2