Snow LeopardにRedmineを導入その2

前のエントリの手順だと、Redmineデータベースの初期化の時点でつまづいてしまうことが判明。

というわけで、こちらに従って、きちんと64bitバイナリからMySQLをインストールするところからやり直し。


■追記
Snow LeopardRedmineを導入する方法をまとめました。Snow LeopardにRedmineを導入まとめ - sugarballの日記こちらの方がわかりやすいかもしれません。。

前のエントリの尻拭い

不幸にも前のエントリに従って進めた人以外は不要です。

とりあえず、前のエントリでインストールしておいたmysqlRubyGemsをアンインストールしてから行う。てか、「gem uninstall mysql」したのに、「gem list」で「mysql (2.8.1)」が表示されるんだけど何なの…

あと、MySQL自動起動するのをキャンセルする。

macmini:~ root# launchctl
launchd% unload /Library/LaunchDaemons/org.macports.mysql5.plist

で、MySQLをアンインストール。

macmini:~ sugarball$ sudo port uninstall mysql5-server
--->  Deactivating mysql5-server @5.0.86_0
--->  Uninstalling mysql5-server @5.0.86_0
macmini:mysql sugarball$ sudo port deactivate mysql5
--->  Deactivating mysql5
macmini:mysql sugarball$ sudo port uninstall mysql5
--->  Uninstalling mysql5 @5.0.86_0

passengerのインストール

とりあえず、まずはこちらを行う。

macmini:~ sugarball$ sudo gem install -r passenger
Building native extensions.  This could take a while...
Successfully installed passenger-2.2.5
1 gem installed
Installing ri documentation for passenger-2.2.5...
Installing RDoc documentation for passenger-2.2.5...

次に、Apacheモジュールのビルドを行う。

macmini:~ sugarball$ sudo passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v2.2.5.

This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.

Here's what you can expect from the installation process:

 1. The Apache 2 module will be installed for you.
 2. You'll learn how to configure Apache.
 3. You'll learn how to deploy a Ruby on Rails application.

Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.
1  ←1を入力し、エンターキーを入力。

(省略)

--------------------------------------------
The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
   PassengerRoot /Library/Ruby/Gems/1.8/gems/passenger-2.2.5
   PassengerRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby

After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!

Press ENTER to continue.

次に、httpd.confを編集する。Apacheモジュールのビルドを行ったときに、「Please edit your Apache configuration file, and add these lines」と言われてた内容を追記する。

macmini:~ sugarball$ sudo vi /etc/apache2/httpd.conf
↓の3行を追加
LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
PassengerRoot /Library/Ruby/Gems/1.8/gems/passenger-2.2.5
PassengerRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby

MySQLのダウンロード

(2009年10月11日現在の情報です)
http://dev.mysql.com/downloads/mysql/5.1.html こちらから、「Mac OS X 10.5 (x86_64)」のパッケージ(拡張子は.dmg)をダウンロード、インストールする。使っているのはSnow Leopardだけど、10.5(Leopard)用のものしか現在は無いみたい。

まずは、dmgをマウントし、mysql-5.1.39-osx10.5-x86_64.pkgをダブルクリックし、MySQLのインストールを行う。次に、MySQLStartupItem.pkgをダブルクリック。デフォルトのまま選択していけばインストールはOK。次に、MySQL.prefPaneをダブルクリックする。すると選択肢が出てくるけど、僕は「このコンピュータのすべてのユーザ用にインストールする」を選択した。多分どっちでもいいと思う。

my.cnfの記述

/usr/local/mysql//support-files/my-small.cnf を/etc/my.cnfとしてコピーし、/etc/my.cnfを編集する。

macmini:mysql sugarball$ sudo vi /etc/my.cnf
…
[mysqld]## USER-SETTINGS
default-character-set = utf8
default-storage-engine=innodb
skip-character-set-client-handshake
##

MySQLデータベースのインストール

mysql_install_dbを実行する。成功したっぽい。

macmini:mysql sugarball$ sudo ./scripts/mysql_install_db --user=mysql
Installing MySQL system tables...
091011 23:11:42 [Warning] Forcing shutdown of 2 plugins
OK
Filling help tables...
091011 23:11:42 [Warning] Forcing shutdown of 2 plugins
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h macmini.local password 'new-password'

Alternatively you can run:
./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl

Please report any problems with the ./bin/mysqlbug script!

The latest information about MySQL is available at http://www.mysql.com/
Support MySQL by buying support/licenses from http://shop.mysql.com/

MySQLの起動、パスワードを設定

MySQLの起動は、システム環境設定から「その他」の箇所にある「MySQL」をクリックすると、MySQLの設定画面になり、「Start MySQL Server」ボタンがあるので、そこから起動できる。

macmini:mysql sugarball$ sudo ./bin/mysqladmin -u root password '(MySQLパスワード)'
macmini:mysql sugarball$ sudo ./bin/mysqladmin -u root -h macmini.local password '(MySQLパスワード)'
./bin/mysqladmin: connect to server at 'macmini.local' failed
error: 'Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server'

しかし、相変わらず「-h」オプションをつけるとダメっぽい。

Redmine用のデータベース・ユーザーの作成

mysqlコマンドにPATHが通っていないので、/usr/local/bin/mysql にでもシンボリックリンクを作成しておく。

macmini:local sugarball$ sudo mkdir /usr/local/bin
macmini:local sugarball$ sudo ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql

データベースredmineを作成して、さらにユーザーredmineを作成、redmineユーザーはデータベースredmineに対して全ての権限を持つようにする。

macmini:local sugarball$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 314
Server version: 5.1.39 MySQL Community Server (GPL)

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

mysql> grant all privileges on redmine.* to redmine identified by '(Redmine用パスワード)';
Query OK, 0 rows affected (0.00 sec)

mysql>  select user,host,password from mysql.user;
+---------+---------------+-------------------------------------------+
| user    | host          | password                                  |
+---------+---------------+-------------------------------------------+
| root    | localhost     | *(暗号化されたパスワード)               |
            …
| redmine | %             | *(暗号化されたパスワード)               |
+---------+---------------+-------------------------------------------+
6 rows in set (0.00 sec)

mysql> exit
Bye

RubyGemsMySQL用データベースドライバをインストール

ここのとおり。

しかし、やはり「No definition for next_result」などのメッセージが出る。不安だ。。

macmini:~ sugarball$ sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
Building native extensions.  This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Installing ri documentation for mysql-2.8.1...

No definition for next_result

No definition for field_name
(省略)

Redmineのインストール

前回のエントリのとおり。

Redmineデータベースの初期化

さて、いよいよ問題の箇所。

macmini:redmine sugarball$ rake db:migrate RAILS_ENV=production
(in /Users/sugarball/Library/redmine)
rake aborted!
uninitialized constant MysqlCompat::MysqlRes

(See full trace by running task with --trace)

ダメだった…orz
※もしかすると、sudoが抜けていただけかも。。

回り道

そう言えば。gemのバージョンが1.3.1だった。コレが原因?と思ってアップデートしておいた。と、一応、MySQLRubyGemsもアンインストールしておく。

macmini:redmine sugarball$ gem --version
1.3.1
macmini:redmine sugarball$ gem uninstall mysql
Successfully uninstalled mysql-2.8.1
macmini:redmine sugarball$ sudo gem update --system
Password:
Updating RubyGems
Updating rubygems-update
Successfully installed rubygems-update-1.3.5
:0:Warning: Gem::SourceIndex#search support for String patterns is deprecated
Updating RubyGems to 1.3.5
Installing RubyGems 1.3.5
RubyGems 1.3.5 installed

=== 1.3.5 / 2009-07-21

Bug fixes:

* Fix use of prerelease gems.
* Gem.bin_path no longer escapes path with spaces. Bug #25935 and #26458.

Deprecation Notices:

* Bulk index update is no longer supported (the code currently remains, but not
  the tests)
* Gem::manage_gems was removed in 1.3.3.
* Time::today was removed in 1.3.3.


------------------------------------------------------------------------------

RubyGems installed the following executables:
        /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/gem

macmini:redmine sugarball$ sudo gem install rubygems-update
Successfully installed rubygems-update-1.3.5
1 gem installed
Installing ri documentation for rubygems-update-1.3.5...
Installing RDoc documentation for rubygems-update-1.3.5...
Could not find main page README
Could not find main page README
Could not find main page README
Could not find main page README
macmini:redmine sugarball$ sudo update_rubygems
Installing RubyGems 1.3.5
RubyGems 1.3.5 installed

=== 1.3.5 / 2009-07-21

Bug fixes:

* Fix use of prerelease gems.
* Gem.bin_path no longer escapes path with spaces. Bug #25935 and #26458.

Deprecation Notices:

* Bulk index update is no longer supported (the code currently remains, but not
  the tests)
* Gem::manage_gems was removed in 1.3.3.
* Time::today was removed in 1.3.3.


------------------------------------------------------------------------------

RubyGems installed the following executables:
        /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/gem

これでもダメだったので、とりあえずrailsもアップデートしてみた。

macmini:redmine sugarball$ sudo gem update rails
Updating installed gems
Updating rails
Successfully installed activesupport-2.3.4
Successfully installed activerecord-2.3.4
Successfully installed rack-1.0.0
Successfully installed actionpack-2.3.4
Successfully installed actionmailer-2.3.4
Successfully installed activeresource-2.3.4
Successfully installed rails-2.3.4
Gems updated: activesupport, activerecord, rack, actionpack, actionmailer, activeresource, rails
Installing ri documentation for activesupport-2.3.4...
Installing ri documentation for activerecord-2.3.4...
Installing ri documentation for rack-1.0.0...
Installing ri documentation for actionpack-2.3.4...
Installing ri documentation for actionmailer-2.3.4...
Installing ri documentation for activeresource-2.3.4...
Installing ri documentation for rails-2.3.4...
Installing RDoc documentation for activesupport-2.3.4...
Installing RDoc documentation for activerecord-2.3.4...
Installing RDoc documentation for rack-1.0.0...
Installing RDoc documentation for actionpack-2.3.4...
Installing RDoc documentation for actionmailer-2.3.4...
Installing RDoc documentation for activeresource-2.3.4...
Installing RDoc documentation for rails-2.3.4...

で、またgem install mysqlしておいた。

macmini:redmine sugarball$ sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
Building native extensions.  This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Installing ri documentation for mysql-2.8.1...

No definition for next_result

No definition for field_name

Redmineデータベースの初期化(再チャレンジ)

これで行けるだろう、と再チャレンジ。というか、前回はsudoを忘れていたので、今回はsudoをつけて実行する。

macmini:redmine sugarball$ sudo rake db:migrate RAILS_ENV=production
(in /Users/sugarball/Library/redmine)
== 1 Setup: migrating =========================================================
-- create_table("attachments", {:force=>true})
   -> 0.0563s
…(省略)

できた!!

次に、デフォルトのトラッカー、ロール、ワークフローなどの初期データを投入。

macmini:redmine sugarball$ sudo rake redmine:load_default_data RAILS_ENV=production
(in /Users/sugarball/Library/redmine)

Select language: bg, ca, cs, da, de, en, es, fi, fr, he, hu, it, ja, ko, lt, nl, no, pl, pt, pt-br, ro, ru, sk, sr, sv, th, tr, uk, vn, zh, zh-tw [en] ja
====================================
Default configuration data loaded.

Redmine、起動せよ!

Ctrl-Cで停止できるみたい。で、ブラウザから3000番ポートでアクセスすると、、、

macmini:redmine sugarball$ ./script/server -e production
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails 2.1.2 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with production environment...
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready.  TERM => stop.  USR2 => restart.  INT => stop (no restart).
** Rails signals registered.  HUP => reload (without restart).  It might not work well.
** Mongrel 1.1.5 available at 0.0.0.0:3000
** Use CTRL-C to stop.

ずいぶんあっさりしてるけど、これでいいのかな…。ともあれ、できた。


Apache+PassengerでRedmineを動作させる

これまでに書いてきた方法だと、3000番ポートでしかアクセスできない。どうせなら、80番ポート(http)でRedmineを使えるようにしたいよね、と。


僕は「http://<ホスト名>/redmine/」という具合に、サブディレクトリで動かしたい。これは、httpd.confへの追記とシンボリックリンクの作成をすればよい。

macmini:~ sugarball$ sudo vi /etc/apache2/httpd.conf

(下の2行を追記。追記箇所は、LoadModule passenger_module … を記述したあたりでいいと思う)
RailsAutoDetect off
RailsBaseURI /redmine

macmini:~ sugarball$ sudo ln -s /Users/sugarball/Library/redmine/public /Library/WebServer/Documents/redmine

これでOK。あとは、apacheを再起動するとRedmineも起動するようになり、「http://<ホスト名>/redmine/」というURLでRedmineにアクセスできる。