Railsを導入してmysqlを利用したプロジェクトを作成
rubyを導入した前回の続き。
今回はいよいよRailsを導入。
【Railsの導入】
とりあえずgem updateしたら早速エラーが発生した。
$ gem update
Updating installed gems
Updating CFPropertyList
Fetching: CFPropertyList-2.2.7.gem (100%)
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
書き込み権限が無いらしい。
カモン管理者権限。
$ sudo gem update
ok.
巷ではシステムのgemに直接railsを入れるのではなく、プロジェクトごとにbundlerなるものでgemを管理するのが流行っているらしいが、手順が増えるので利用せず。
railsをバージョン指定でインストール。
今回は4.0.2を使用。
$ gem install rails --version="4.0.2"
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
書き込み権限が無いらしい。
カモン管理ry
$ sudo gem install rails --version="4.0.2"
途中でなんか出た。
rake's executable "rake" conflicts with /usr/bin/rake
Overwrite the executable? [yN]
既に"rake"があるとな。上書きしちゃえ。 y enter
またなんか出た。
railties's executable "rails" conflicts with /usr/bin/rails
Overwrite the executable? [yN]
なんと"rails"も入っているとな? ええい、ままよ。 y enter
しばらく待つと…
30 gems installed
ok.
railsに紐づいて29個のgemが入ったっぽい。
念のためちゃんと入っている事を確認。
$ rails -v
Rails 4.0.2
Rails導入完。
適当なディレクトリを作成してカレントをそこに移動。
オプションでDBを指定してプロジェクト作成。
ちなみにこの時点ではまだMySQLをインストールしていない。
↖︎注記:読み進めれば分かりますが、予め入れておく事をオススメします。
Invalid application name test. Please give a name which does not match one of the reserved rails words.
予約語「test」と一致してるから別の名前を使えとな。
なんか色々createしたあと、こんなのが出た。
Your user account isn't allowed to install to the system Rubygems.
You can cancel this installation and run:
bundle install --path vendor/bundle
to install the gems into ./vendor/bundle/, or you can enter your password
and install the bundled gems to Rubygems using sudo.
Password:
管理者パスワード入れたら動いた。
ディレクトリを確認すると、ちゃんと初期ファイルが作られている。
プロジェクト作成成功。
【ブラウザからアクセス 前編】
先ずはサーバ起動。
$ rails server
Usage:
rails new APP_PATH [options]
作成したディレクトリに移動しないと駄目ですよねー。わかってましたよー。
では改めて、
$ rails server
Could not find gem 'sdoc (>= 0) ruby' in the gems available on this machine.
Run `bundle install` to install missing gems.
bundleインストールしろと怒られた。
素直に従いましょう。
パスワード求められるので入力。プロジェクト作成時と同じ。
再発するパーミッションエラー。
Errno::EACCES: Permission denied - /Library/Ruby/Gems/2.0.0/build_info/coffee-script-source-1.7.0.info
An error occurred while installing coffee-script-source (1.7.0), and Bundler
cannot continue.
Make sure that `gem install coffee-script-source -v '1.7.0'` succeeds before
bundling.
困ったときの管理者権限。
$ sudo bundle install
中略
An error occurred while installing mysql2 (0.3.15), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.15'` succeeds before bundling.
やっぱりMySQL必要ですよねー。わかってましたよー。
【いったんrubyから離れてhomebrewでMySQLを導入するお話です】
$ brew update
問題なくインストール完了。
コンソールにmysqlの起動方法について説明が表示された。
OS起動時に自動起動するように設定をする or 毎回手動で起動する のどちらからしい。
WindowsでMySQL使う時もサービス切って手動起動しているので、今回も手動起動しましょう。
1. mysqlフォルダに移動 (設定に移動は不要かも?とりあえず場所確認の意味で)
$ cd /usr/local/Cellar/mysql/5.6.16
2. 初期設定
2014/3/3追記-----
最初にunsetを実施
unset TMPDIR
追記ここまで-----
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/user/local/var/mysql --tmpdir=/tmp
FATAL ERROR: The parent directory for the data directory '/user/local/var/mysql' does not exist.
If that path was really intended, please create that directory path and then
restart this script.
If some other path was intended, please use the correct path when restarting this script.
--datadirオプションの設定は色んなサイトでuserってなってたけど、自分の環境ではusrだった。
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
ok.
3. パスワード設定
3.1 mysql起動
$ mysql.server start
3.2 パスワード設定
$ mysqladmin -u root password NEW_PASSWORD
3.3 確認
$ mysql -u root -p
Enter password:
パスワード入力でmysqlに切り替わればok.
mysql> show databases;
で作成されたDBを確認してみる。問題なし。
mysql> exit
でmysqlモード終了。
3.4 mysql停止
$ mysql.server stop
mysqlの導入完了。
railsの設定再開。
【ブラウザからアクセス 後編】
$ sudo bundle install
ok.
railsサーバ起動。
$ rails server
次のように表示されればok.
ちなみにサーバ起動中はターミナルが下記状態で固まる模様。
=> Booting WEBrick
=> Rails 4.0.2 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2014-03-02 14:19:55] INFO WEBrick 1.3.1
[2014-03-02 14:19:55] INFO ruby 2.0.0 (2013-06-27) [universal.x86_64-darwin13]
[2014-03-02 14:19:55] INFO WEBrick::HTTPServer#start: pid=44155 port=3000
【 いよいよブラウザアクセス】
http://localhost:3000/
エラー発生
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
mysqlが起動していないのが原因だった。
︎別のターミナルを立ち上げて、mysqlを起動して再チャレンジ。解決。
エラー発生
Access denied for user 'root'@'localhost' (using password: NO)
エラー文でぐぐるとパスワード設定のやり直しすれば良いと出てきたので、やってみたものの解決せず。
少し考えて、今回のケースでの意味を悟った。
「接続にパスワードが使われていない」だけ。
作成したrailsプロジェクトディレクトリ/config/database.ymlファイルを開く。
development、test、productionのpasswordが空になっているので設定する。
ユーザはとりあえずrootのままで。
このエラーはこれで解決。
エラー発生
Unknown database 'test_rails_development'
DBが無いとな。
mysql入ってとりあえず中身は空のDBを作成して解決。
mysql> create database test_rails_development default character set utf8;
ようやく眼前に広がる「Welcome aboard!」の文字。
よし、動いた。
最後にraildサーバを control+c で停止して終了。
参考
Ruby on Rails環境構築と、Rails is not currently installed on this system. (略) の謎
http://ryoichi0102.hatenablog.com/entry/2014/01/31/090722
gemでバージョンを指定してインストールする方法