LoginSignup
2
0

More than 5 years have passed since last update.

【高速構築】GitHubで新規レポジトリ作成からRailsプロジェクトのサービスを起動する(mysql編)

Posted at

※ruby, git, bundle, mysql などはあらかじめインストールされている前提で進めます。

環境情報
$ rails -v
Rails 5.1.5
$ ruby -v
ruby 2.2.5p319 (2016-04-26 revision 54774) [x86_64-linux]
$ bundle -v
Bundler version 1.13.7
$ mysql --version
mysql  Ver 14.14 Distrib 5.6.34, for Linux (x86_64) using  EditLine wrapper

Railsプロジェクト新規設定

  • GitHubページ上で新しいレポジトリを作成する

  • 作成されたレポジトリのURLをコピーしてクローンする

$ git clone https://github.com/hoge_project
  • Gemfileの作成
$ cd hoge_project
$ bundle init
Writing new Gemfile to /home/hoge_project/Gemfile
  • Gemfileを編集
Gemfile
# gem "rails" <- コメント外す
  • bundle install
$ bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/..........
Fetching version metadata from https://rubygems.org/..
Fetching dependency metadata from https://rubygems.org/.
Resolving dependencies...

vendor/bundle以下にgemが格納されるようにする。
--path vendor/bundleを入れないとシステム側のgemに格納されるためクリーンな状態にならない。

  • Railsプロジェクト作成
$ bundle exec rails new . -B -d mysql --skip-test
create  README.md
create  Rakefile
create  config.ru
create  .gitignore
conflict  Gemfile
Overwrite /home/hoge_project/Gemfile? (enter "h" for help) [Ynaqdh] y #上書き許可

デフォルト指定のDBがPostgreSQLになっているので、プロジェクト作成時にDBをMysqlに変更し、かつtestフォルダを作らないようにする。(テストコードはrspecで書くことが多いのであらかじめ作らないほうがいいかも)
上記コマンドを実行した際、Gemfileを上書きしていいかと聞かれるのでyと答えておきましょう。
(Gemfileを開くとRailsで必要なgemが一通り記載されている)

  • 再度bundle install
$ bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/..........
Fetching version metadata from https://rubygems.org/..
Fetching dependency metadata from https://rubygems.org/.
Resolving dependencies...

Mysql接続設定

mysqlはrootユーザで接続しちゃっていいよってなら、この設定部分は飛ばしちゃってRailsサービス起動して終了。高速で構築したいけど、rootユーザは避けたい場合の設定です。

  • rootでログイン
$ mysql -u root -p
  • ユーザ作成&権限追加
mysql> create user 'tarou'@'localhost' identified by 'hogehoge';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on hoge_project_development.* to tarou@localhost identified by 'hogehoge';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on hoge_project_test.* to tarou@localhost identified by 'hogehoge';
Query OK, 0 rows affected (0.00 sec)
  • config/database.ymlの設定変更
database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  username: tarou # 変更 
  password: hogehoge # 変更
  socket: /var/lib/mysql/mysql.sock
  • DB作成
$ bundle exec rake db:create
Created database 'hoge_project_development'
Created database 'hoge_project_test'

サービス起動

$ bundle exec rails s -b 0.0.0.0
2
0
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
2
0