SyntaxHighlighter

2012年1月31日火曜日

さくらVPS(CentOS5.7)でRubyだDBIだdbd-pgだ

男は黙ってDBI。
# gem install dbi
Fetching: deprecated-2.0.1.gem (100%)
Fetching: dbi-0.4.5.gem (100%)
Successfully installed deprecated-2.0.1
Successfully installed dbi-0.4.5
2 gems installed
Installing ri documentation for deprecated-2.0.1...
Installing ri documentation for dbi-0.4.5...
Installing RDoc documentation for deprecated-2.0.1...
Installing RDoc documentation for dbi-0.4.5...
楽勝。次、dbd-pgだ。
# gem install dbd-pg
Fetching: pg-0.12.2.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing dbd-pg:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** 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:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby
        --with-pg
        --without-pg
        --with-pg-dir
        --without-pg-dir
        --with-pg-include
        --without-pg-include=${pg-dir}/include
        --with-pg-lib
        --without-pg-lib=${pg-dir}/lib
        --with-pg-config
        --without-pg-config
        --with-pg_config
        --without-pg_config


Gem files will remain installed in /usr/lib/ruby/gems/1.9.1/gems/pg-0.12.2 for inspection.
Results logged to /usr/lib/ruby/gems/1.9.1/gems/pg-0.12.2/ext/gem_make.out
ぐぬぬ。libpq-fe.hがないって?あーん?develいれてなかったから入れてみる。
#yum install postgresql91-devel.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.nara.wide.ad.jp
 * epel: ftp.kddilabs.jp
 * extras: ftp.nara.wide.ad.jp
 * updates: ftp.nara.wide.ad.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package postgresql91-devel.x86_64 0:9.1.2-1PGDG.rhel5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================
 Package                    Arch           Version                   Repository      Size
==========================================================================================
Installing:
 postgresql91-devel         x86_64         9.1.2-1PGDG.rhel5         pgdg91         1.6 M

Transaction Summary
==========================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 1.6 M
Is this ok [y/N]: y
Downloading Packages:
postgresql91-devel-9.1.2-1PGDG.rhel5.x86_64.rpm                    | 1.6 MB     00:01     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : postgresql91-devel                                                 1/1 

Installed:
  postgresql91-devel.x86_64 0:9.1.2-1PGDG.rhel5                                           

Complete!
うっし、もう一回!
# gem install dbd-pg
Building native extensions.  This could take a while...
ERROR:  Error installing dbd-pg:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** 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:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby
        --with-pg
        --without-pg
        --with-pg-dir
        --without-pg-dir
        --with-pg-include
        --without-pg-include=${pg-dir}/include
        --with-pg-lib
        --without-pg-lib=${pg-dir}/lib
        --with-pg-config
        --without-pg-config
        --with-pg_config
        --without-pg_config


Gem files will remain installed in /usr/lib/ruby/gems/1.9.1/gems/pg-0.12.2 for inspection.
Results logged to /usr/lib/ruby/gems/1.9.1/gems/pg-0.12.2/ext/gem_make.out
ぐぎぎぎぎ。pg_configがないって?見落としてた。言われるままに再チャレンジ!
#gem install dbd-pg -- --with-pg_config=/usr/pgsql-9.1/bin/pg_config
Building native extensions.  This could take a while...
Fetching: dbd-pg-0.3.9.gem (100%)
Successfully installed pg-0.12.2
Successfully installed dbd-pg-0.3.9
2 gems installed
Installing ri documentation for pg-0.12.2...
Installing ri documentation for dbd-pg-0.3.9...
Installing RDoc documentation for pg-0.12.2...
Installing RDoc documentation for dbd-pg-0.3.9...
お、うまくいったっぽい?
試してみる。
# irb 
irb(main):001:0> require 'dbi'
=> true
irb(main):002:0> dbh = DBI.connect('dbi:Pg:dozaemon:localhost','dozaemon','')
=> #<DBI::DatabaseHandle:0x00・・・以下略>
irb(main):003:0> dbh.select_one('select version()')
=> ["PostgreSQL 9.1.2 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-51), 64-bit"]
irb(main):004:0> exit
でけた!
#昨日のセットアップのままだとパスワードはいらないっす。

2012年1月30日月曜日

さくらVPS(CentOS5.7)へYUMでPostgreSQL9.1.2をインストール

既存のyumではver8しかないため、yumのリポジトリをダウンロード+インストール
# wget http://yum.postgresql.org/9.1/redhat/rhel-5-i386/pgdg-centos91-9.1-4.noarch.rpm
# rpm -ivh pgdg-centos91-9.1-4.noarch.rpm 
念のためyumのリストにあるか確認
# yum list | grep postgresql
んで、インストール
# yum install postgresql91-server.x86_64
今のうちにシステム起動時に自動起動するように設定。
確認、設定、確認。OK。
# chkconfig --list | grep postgres
# chkconfig postgresql-9.1 on
# chkconfig --list | grep postgres
postgresql-9.1  0:off   1:off   2:on    3:on    4:on    5:on    6:off
データベースを初期化。ここでデフォルトのエンコーディングを指定した方が無難。
あとlocaleもこの段階では指定しない方がいいと思われ。
service postgresql-9.1 initdb --no-locale -E UTF8
サービスを起動。ここまでで一応インストール完了。
# service postgresql-9.1.2 start
DB作成ユーザを追加します。必須ではないのですが作った方が管理はしやすいです。
#  useradd dozaemon
postgresの管理者権限にsuして、postgres側にユーザを追加します。
# su - postres
$ createuser dozaemon
今作成したdozaemonで初期データベースを作成します。
$ exit
# su - dozaemon
$ createdb dozaemondb
作成したデータベースを確認するよ。
$ psql -l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 dozaemon  | dozaemon | UTF8     | C       | C     | 
 postgres  | postgres | UTF8     | C       | C     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(4 rows)

でけた!
もしinitdbからやり直したい場合は、データベースデータを丸ごと消してしまえばいいっす。
自分の環境では /var/lib/pgsql/9.1/data/のdataフォルダ毎消しちゃいました。
#自己責任でお願いします。

2012年1月29日日曜日

さくらVPS(CentOS5.7)でRuby1.9.2を入れてしばらくしたらセグメンテーション違反

何もしてないのにRuby1.9.2インストールの数時間後に
$ irb
<internal:lib/rubygems/custom_require>:29:in `require': closed stream (IOError)
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from /usr/lib/ruby/1.9.1/irb.rb:13:in `<top (required)>'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from /usr/bin/irb:9:in `<main>'
とか
$ heroku logs
<internal:lib/rubygems/custom_require>:29: [BUG] Segmentation fault
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]

-- control frame ----------
c:0005 p:---- s:0016 b:0016 l:000015 d:000015 CFUNC  :require
c:0004 p:0013 s:0012 b:0012 l:000011 d:000011 METHOD <internal:lib/rubygems/custom_require>:29
c:0003 p:0011 s:0007 b:0007 l:001498 d:000440 EVAL   /usr/bin/heroku:9
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:001498 d:001498 TOP   
---------------------------
-- Ruby level backtrace information ----------------------------------------
/usr/bin/heroku:9:in `<main>'
<internal:lib/rubygems/custom_require>:29:in `require'
<internal:lib/rubygems/custom_require>:29:in `require'

セグメンテーション違反です


とか。

コンソール開きっぱなしにしておいただけでこうなるってどういうこと?
って調べてみると以下が。

http://www.ruby-forum.com/topic/133765

どうやらprelinkがいかんらしい。
なので下記を参考に修正してみた
http://selfkleptomaniac.org/archives/1341
どうだろ?
# vi /etc/prelink.conf.d/ruby.conf
# cat /etc/prelink.conf.d/ruby.conf  
-b /usr/bin/ruby

#2012/2/28追加
まったく問題起こってません♪
Bloggerでソースコードを表示

最低限の使い方ではここが詳しい
http://recoboo.blog28.fc2.com/blog-entry-91.html

テーマの種類はここで
http://memogawari497.blog112.fc2.com/blog-entry-11.html