Windowsでruby開発 - MySQL連携編
前回に引き続き、Railsの開発環境構築をやっていこうと思います。
SQLiteではなく、MySQLで実装したいので、
インストールまでは、かなり前の記事でやっていました。
ですが、まだRailsとの連携は行われておりません。
そこで、MySQLへの連携を行いたいと思います。
ここまでの流れは以下の記事を確認してください。
Windowsでruby開発 - 開発環境構築編 - Program LOG
Windowsでruby開発 - apache連携編 - Program LOG
phpmyadminのインストール
ブラウザからMySQLを操作できるようにphpMyAdminをインストールしたいと思います。
phpMyAdminとphp-mysql、php-mcryptをEPELポジトリを有効にしてインストールします。
[vagrant@localhost ~]$ sudo yum --enablerepo=epel install -y php php-mbstring phpMyAdmin php-mysql php-mcrypt
※今回は、開発環境のみの使用になるので、特にセキュリティの設定は行いません。
hostsファイルに以下を追加
192.168.33.30 php.simplebbs.net
そして、以前作成した、「/etc/httpd/conf.d/vhost.conf」を編集
[vagrant@localhost conf.d]$ sudo vi /etc/httpd/conf.d/vhost.conf ・・・・以下を追記ServerName php.simplebbs.net DocumentRoot /usr/share/phpMyAdmin/ ErrorLog /var/log/php_error.log TransferLog /var/log/php_access.log AllowOverride all Options -MultiViews Order allow,deny allow from all
そして、httpdのリスタートを行います。
[vagrant@localhost /]$ sudo service httpd restart
以上で、設定が完了となります。
「php.simplebbs.net」へブラウザでアクセスすると、接続できます。
また、開いた際に、認証が開きます。
これは、以前作成した「development」「password」で無事にログインが完了しました。
データベースを作成しておきましょう。
今回は「simplebbs_production」のようにしました。
データベース接続の切り替え
まずは、Gemfileを以下のように書き換えます。
gem "sqlite3" ↓ gem "mysql2"
続いて、bundle installを実行します。
[vagrant@localhost simplebbs]$ bundle install Fetching gem metadata from https://rubygems.org/........... Fetching additional metadata from https://rubygems.org/.. ・・・中略 Gem files will remain installed in /home/vagrant/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/mysql2-0.3.15 for inspection. Results logged to /home/vagrant/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/extensions/x86_64-linux/2.0.0-static/mysql2-0.3.15/gem_make.out 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.
うん。。。エラーが出ました。
mysql2が入っていないという事ですね。。。
[vagrant@localhost simplebbs]$ gem install mysql2 -v '0.3.15' Building native extensions. This could take a while... ERROR: Error installing mysql2: ERROR: Failed to build gem native extension. /home/vagrant/.rbenv/versions/2.0.0-p451/bin/ruby extconf.rb checking for ruby/thread.h... yes checking for rb_thread_call_without_gvl() in ruby/thread.h... yes checking for rb_thread_blocking_region()... yes checking for rb_wait_for_single_fd()... yes checking for rb_hash_dup()... yes checking for rb_intern3()... yes Using mysql_config at /usr/bin/mysql_config checking for mysql.h... no checking for mysql/mysql.h... no mysql.h is missing. please check your installation of mysql and try again. *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. Provided configuration options: ・・・中略 extconf failed, exit code 1 Gem files will remain installed in /home/vagrant/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/mysql2-0.3.15 for inspection. Results logged to /home/vagrant/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/extensions/x86_64-linux/2.0.0-static/mysql2-0.3.15/gem_make.out
ぬおおおおおおおお!今度はまた別のエラー・・・。
「mysql.h・・・no」って「mysql.h」が分からないのでコンパイルが出来ないそうです。
確かに「/usr/include」の中にいねー。。。ってかdevelインストールしてねぇーw
って事で以下を実行。
[vagrant@localhost simplebbs]$ sudo yum install -y --enablerepo=remi mysql-devel
インストールが完了したので、再度トライです!
[vagrant@localhost simplebbs]$ gem install mysql2 -v '0.3.15' Building native extensions. This could take a while... Successfully installed mysql2-0.3.15 Parsing documentation for mysql2-0.3.15 Installing ri documentation for mysql2-0.3.15 Done installing documentation for mysql2 after 1 seconds 1 gem installed
お・・・イケたっぽい?
じゃあ、もう一度、bundleやります。
[vagrant@localhost simplebbs]$ bundle install Resolving dependencies... Using rake 10.3.1 Using i18n 0.6.9 Using json 1.8.1 Using minitest 5.3.3 Using thread_safe 0.3.3 ・・・中略 Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
イケた!いけました~!w
続いて、エディタで「config/database.yml」を開いて以下のように編集
development: *default database: db/development.sqlite3 ・・・以下のように編集 development: adapter: mysql2 database: simplebbs_dev username: development password: 19820120 host: 127.0.0.1 encoding: utf8
続いて設定をロードします。
[vagrant@localhost simplebbs]$ RAILS_ENV=development rake db:schema:load /vagrant/docroot/simplebbs/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /vagrant/docroot/simplebbs/config/application.rb to limit the frameworks that will be loaded. [vagrant@localhost simplebbs]$ RAILS_ENV=development rake db:seed rake aborted! ActiveRecord::StatementInvalid: Mysql2::Error: Specified key was too long; max key length is 767 bytes: CREATE UNIQUE INDEX `unique_schema_migrations` ON `schema_migrations` (`version`) Tasks: TOP => db:abort_if_pending_migrations (See full trace by running task with --trace)
ん・・・?エラー?w
しかもMySQLのエラー?
ググってみたところ、「MySQLのUNIQUEなINDEXには長さ767byteまでしか使えない」っていうエラー?らしい?
対応策も記載があったので、感謝!MySQL(InnoDB) で "Index column size too large. The maximum column size is 767 bytes." いわれるときの対策 - かみぽわーる
[vagrant@localhost simplebbs]$ sudo vi /etc/my.cnf ・・・・以下を追記 [mysqld] innodb_file_format = Barracuda innodb_file_per_table = 1 innodb_large_prefix [vagrant@localhost simplebbs]$ sudo service mysqld restart Stopping mysqld: [ OK ] Starting mysqld: [ OK ] [vagrant@localhost simplebbs]$ RAILS_ENV=development rake db:seed
今度は何にもエラーが出ませんでした~!
scaffoldingでテストしてみよう!
Ruby on Railsのscaffoldで接続出来ているかテストしてみようと思います。
[vagrant@localhost simplebbs]$ rails generate scaffold bbs_test title:string name:string body:string invoke active_record create db/migrate/20140428082835_create_bbs_tests.rb create app/models/bbs_test.rb invoke test_unit create test/models/bbs_test_test.rb create test/fixtures/bbs_tests.yml invoke resource_route route resources :bbs_tests invoke scaffold_controller create app/controllers/bbs_tests_controller.rb invoke erb create app/views/bbs_tests create app/views/bbs_tests/index.html.erb create app/views/bbs_tests/edit.html.erb create app/views/bbs_tests/show.html.erb create app/views/bbs_tests/new.html.erb create app/views/bbs_tests/_form.html.erb invoke test_unit create test/controllers/bbs_tests_controller_test.rb invoke helper create app/helpers/bbs_tests_helper.rb invoke test_unit create test/helpers/bbs_tests_helper_test.rb invoke jbuilder create app/views/bbs_tests/index.json.jbuilder create app/views/bbs_tests/show.json.jbuilder invoke assets invoke coffee create app/assets/javascripts/bbs_tests.js.coffee invoke scss create app/assets/stylesheets/bbs_tests.css.scss invoke scss create app/assets/stylesheets/scaffolds.css.scss
続いて、rakeコマンドを実行します。
[vagrant@localhost simplebbs]$ rake db:migrate 20140428082835 CreateBbsTests: migrating =================================== create_table(:bbs_tests) > 0.0284s 20140428082835 CreateBbsTests: migrated (0.0284s) ==========================
phpMyadminを確認したところ、正常にテーブルが作成されていました。
「http://simplebbs.net/bbs_tests」でアクセスしたところ、正常に動作の確認が出来ました。
ちょっといつもより長くなってしまいましたが、今回はMySQLとrubyの連携を行いました。
ここいらでちゃんとBox化しておこうと思います。。前にやらかしたので。。。はいw
今後の課題としては、やはりchef-soloの導入ですね。
vagrantを色々触ってみて、chef-soloとの連携がかなり重要という事がわかってきました。
まぁ、環境構築はいくらやってもきりが無いので、ひとまず掲示板を構築してみたいと思います。
使用環境
- ホストOS: Windows7 x64
- VirtualBox: 4.3.10
- Vagrant: 1.5.2
- ゲストOS: CentOS6.4 x64
- Ruby : 2.0.0-p451
- Ruby on Rails : 4.1.0
- passenger : 4.0.41
- apache : 2.2.15
- NetBeans IDE 8.0