2010年12月6日月曜日

pgpool2による負荷分散

0 コメント
PostgreSQL 9.0.1でストリーミングレプリケーションしたサーバー群を、pgpool2を使って負荷分散させてみました。
イメージはこんな感じで。


pgpool2は3.0.1を使用します。
またこの時、pgpool2のモードは、マスタースレーブモードに設定します。

前回のAをマスターとして、BとCにレプリケーションした状態に、Dというpgpool2サーバーを入れます。
また、Dのpgpool2のポートは5432とし、クライアントはDのサーバーをPostgreSQLデータベースサーバーとしてアクセスするようにします。

pgpool2をマスタースレーブモードで動作させる場合、システムDBは不要ですが、pgpool2をソースからビルドするために、PostgreSQLのライブラリなどが必要になるので、PostgreSQL 9.0.1をソースからビルドしておきます。

pgpool2をインストールします。
$ sudo mkdir /usr/local/pgpool2
$ sudo chown postgres:postgres /usr/local/pgpool2
$ cd /usr/local/src
$ sudo wget http://pgfoundry.org/frs/download.php/2841/pgpool-II-3.0.1.tar.gz
$ sudo tar zxvf pgpool-II-3.0.1.tar.gz
$ chown -R postgres:postgres pgpool-II-3.0.1
$ sudo chown -R postgres:postgres pgpool-II-3.0.1
$ su - postgres
$ cd /usr/local/src/pgpool-II-3.0.1/
$ ./configure --prefix=/usr/local/pgpool2 -with-pgsql=/usr/local/pgsql
$ make
$ make install

pgpool2を設定します。
$ cd /usr/local/pgpool2/etc/
まずpcp.confにpcp.conf.sampleをコピーして編集します。
$ cp pcp.conf.sample pcp.conf
pcp.confに、
postgres:postgresのパスワードのMD5
という記述を追加します。
postgreslのパスワードのMD5で暗号化されたものは、
../bin/pg_md5 ユーザーpostgresのパスワード
で表示されます。

pgpool.confにpgpool.conf.sample-streamをコピーして編集します。
$ cp pgpool.conf.sample-stream pgpool.conf
内容の次の箇所を変更します。
listen_addresses = '*'
port = 5432
pid_file_name = '/var/run/pgpool.pid'
# 次の3行は確認用
log_statement = true             # 実行されるSQL文を出力
log_per_node_statement = true    # どのノードでSQL文が実行されたかを出力
log_connections = true           # ノードに接続されたことを出力

# システムDBは不要だけの一応設定
system_db_hostname = 'localhost'
system_db_port = 5433  # pgpool2を5432ポートで動かすので、PostgreSQLを動かすときは5433ポートにする
system_db_dbname = 'pgpool'
system_db_schema = 'pgpool_catalog'
system_db_user = 'pgpool'
system_db_password = ''

backend_hostname0 = 'マスター(A)のホスト'
backend_port0 = 5432
backend_weight0 = 1
backend_data_directory0 = '/usr/local/pgsql/data'
backend_hostname1 = 'スレーブ(B)のホスト'
backend_port1 = 5432
backend_weight1 = 1
backend_data_directory1 = '/usr/local/pgsql/data'
backend_hostname2 = 'スレーブ(C)のホスト'
backend_port2 = 5432
backend_weight2 = 1
backend_data_directory2 = '/usr/local/pgsql/data'

実際に動かして動作確認してみます。
$ sudo /usr/local/pgpool2/bin/pgpool -n

クライアントから、Dのサーバーにポートを5432でアクセスして、更新クエリと参照クエリを適当に投げてみます。
すると、更新クエリは必ずサーバーAに送られ、参照クエリはA,B,Cのいずれかのサーバーに送られます。

2010年11月10日水曜日

PostgreSQL 9.0でレプリケーション先の追加

0 コメント
前回、PostgreSQL 9.0のレプリケーション機能を試したが、今回は、レプリケーション先の追加を試してみました。
イメージはこんな感じで

サーバーAからサーバーBにレプリケーションされている状態に、新たにサーバーCをレプリケーション先として追加します。

まず、サーバーCにubuntuを入れてPostgreSQL9.0をソースからビルドしておきます。

次に、サーバーBとサーバーCのPostgreSQLを停止します。
サーバーBで
$ sudo /etc/init.d/postgresql stop
サーバーCで
$ sudo /etc/init.d/postgresql stop

サーバーBの/usr/local/pgsql/dataをサーバーCにコピーします。
サーバーBで
$ su - postgres
$ cd /usr/local/pgsql/
$ tar zcvf data.tar.gz data
$ scp data.tar.gz 192.168.168.238:/usr/local/pgsql/
$ exit
サーバーCで
$ su - postgres
$ cd /usr/local/pgsql/
$ tar zxvf data.tar.gz
$ exit

最後に、サーバーBとサーバーCのPostgreSQLを起動します。
サーバーBで
$ sudo /etc/init.d/postgresql start
サーバーCで
$ sudo /etc/init.d/postgresql start

これで、サーバーCがレプリケーション先として追加できました。
動作を確認してみます。
レプリケーション元のデータを更新します。
サーバーAで
$ su - postgres
$ psql testdb
testdb=# select * from table1;
 id |        create_date
----+----------------------------
  1 | 2010-11-08 11:19:10.162554
(1 row)

testdb=# insert into table1(id) values (2);
INSERT 0 1
testdb=# select * from table1;
 id |        create_date
----+----------------------------
  1 | 2010-11-08 11:19:10.162554
  2 | 2010-11-10 14:54:15.611948
(2 rows)
サーバーBとサーバーCで
$ su - postgres
$ psql testdb
testdb=# select * from table1;
 id |        create_date
----+----------------------------
  1 | 2010-11-08 11:19:10.162554
  2 | 2010-11-10 14:54:15.611948
(2 rows)
となり、更新内容が反映されていることが確認できました。


あと、レプリケーション先のサーバーBとサーバーCのPostgreSQLが停止している間に、サーバーAのデータが更新された場合も確認してみました。

まず、レプリケーション先のPostgreSQLを停止します。
サーバーBで
$ sudo /etc/init.d/postgresql stop
サーバーCで
$ sudo /etc/init.d/postgresql stop

次に、レプリケーション元のサーバーAのデータを更新します。
$ su - postgres
$ psql testdb
testdb=# select * from table1;
 id |        create_date
----+----------------------------
  1 | 2010-11-08 11:19:10.162554
  2 | 2010-11-10 14:54:15.611948
(2 rows)

testdb=# insert into table1(id) values (3);
INSERT 0 1
testdb=# select * from table1;
 id |        create_date
----+----------------------------
  1 | 2010-11-08 11:19:10.162554
  2 | 2010-11-10 14:54:15.611948
  3 | 2010-11-10 14:59:57.012077
(3 rows)

レプリケーション先のPostgreSQLを起動します。
サーバーBで
$ sudo /etc/init.d/postgresql start
サーバーCで
$ sudo /etc/init.d/postgresql start

レプリケーション元のサーバーAの更新内容が反映されているか確認します。
サーバーBとサーバーCで
$ su - postgres
$ psql testdb
testdb=# select * from table1;
 id |        create_date
----+----------------------------
  1 | 2010-11-08 11:19:10.162554
  2 | 2010-11-10 14:54:15.611948
  3 | 2010-11-10 14:59:57.012077
(3 rows)
となり、更新内容が反映されていることが確認できました。

2010年11月5日金曜日

PostgreSQL 9.0でレプリケーション

0 コメント
PostgreSQL 9.0でレプリケーション機能が追加されたので、試してみました。

環境は、OSにubuntu 10.04 LTS Serverを使ってみました。
ESXi4.1に仮想PCを2台用意して、それぞれにubuntuを入れて、PostgreSQLをソースからビルドしておきます。

■レプリケーション元のPostgreSQLの設定を変更
まず、PostgreSQLを停止します。
$ sudo /etc/init.d/postgresql stop
PostgreSQLの管理ユーザーになって、設定ファイルを編集します。
$ su - postgres
$ cd /usr/local/pgsql/data
postgresql.confに次の設定を追加します。
listen_addresses = '*'
port = 5432
wal_level = hot_standby
archive_mode = on
archive_command = 'cp %p /usr/local/pgsql/data/pg_archive/%f'
max_wal_senders = 3
pg_hba.confに次の設定を追加します。
host    replication     all             192.168.168.0/24        trust
アーカイブディレクトリを作成します。
$ mkdir pg_archive
PostgreSQLの管理ユーザーを抜けます。
$ exit

■レプリケーション先のPostgreSQLの設定を変更
まず、PostgreSQLを停止します。
$ sudo /etc/init.d/postgresql stop
PostgreSQLの管理ユーザーになって、設定ファイルを編集します。
$ su - postgres
$ cd /usr/local/pgsql/data
postgresql.confに次の設定を追加します。
listen_addresses = '*'
port = 5432
wal_level = hot_standby
archive_mode = on
archive_command = 'cp %p /usr/local/pgsql/data/pg_archive/%f'
max_wal_senders = 3
hot_standby = on
pg_hba.confに次の設定を追加します。
host    replication     all             192.168.168.0/24        trust
recovery.confというファイルを作成して、次の設定を記述します。
standby_mode = 'on'
primary_conninfo = 'host=レプリケーション元のホスト port=5432 user=postgres'
restore_command = 'cp /usr/local/pgsql/data/pg_archive/%f %p'
trigger_file = '/usr/localpgsql/data/trigger'
アーカイブディレクトリを作成します。
$ mkdir pg_archive
PostgreSQLの管理ユーザーを抜けます。
$ exit

■レプリケーション元のPostgreSQLを起動して、データベースを作成
$ sudo /etc/init.d/postgresql start
$ su - postgres
$ createdb testdb
$ psql -l
                              List of databases
   Name    |  Owner   | Encoding | Collation | Ctype |   Access privileges
-----------+----------+----------+-----------+-------+-----------------------
 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
 testdb    | postgres | UTF8     | C         | C     |
(4 rows)

■レプリケーション元のデータをレプリケーション先にコピー
レプリケーション元のデータをレプリケーション先にコピーします。
$ cd /usr/local/pgsql
$ tar zcvf data.tar.gz data
$ scp data.tar.gz レプリケーション先のホスト:/usr/local/pgsql/
レプリケーション先で
$ su - postgres
$ cd /usr/local/pgsql
設定ファイルのバックアップを取っておきます。
$ mkdir ~/conf_backup
$ cp -a data/*.conf ~/conf_backup/
レプリケーション元のデータを展開します。
$ tar zxvf data.tar.gz
バックアップした設定ファイルを戻します。
$ cp -a ~/conf_backup/*.conf data/
プロセスIDは削除します。
$ rm -f data/postmaster.pid
PostgreSQLを起動します。
$ exit
$ sudo /etc/init.d/postgresql start

■確認
レプリケーション元でtestdbにテーブルを作成してみます。
$ su - postgres
$ psql testdb
testdb=# create table table1 (id integer, create_date timestamp default current_timestamp);
testdb=# \d
         List of relations
 Schema |  Name  | Type  |  Owner
--------+--------+-------+----------
 public | table1 | table | postgres
(1 row)
testdb=# \d table1
                   Table "public.table1"
   Column    |            Type             |   Modifiers
-------------+-----------------------------+---------------
 id          | integer                     |
 create_date | timestamp without time zone | default now()
レプリケーション先で確認します。
$ su - postgres
$ psql testdb
testdb=# \dt
         List of relations
 Schema |  Name  | Type  |  Owner
--------+--------+-------+----------
 public | table1 | table | postgres
(1 row)
testdb=# \d table1
                   Table "public.table1"
   Column    |            Type             |   Modifiers
-------------+-----------------------------+---------------
 id          | integer                     |
 create_date | timestamp without time zone | default now()
レプリケーション元でデータベースを作成します。
$ createdb testdb2
$ psql -l
                              List of databases
   Name    |  Owner   | Encoding | Collation | Ctype |   Access privileges
-----------+----------+----------+-----------+-------+-----------------------
 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
 testdb    | postgres | UTF8     | C         | C     |
 testdb2   | postgres | UTF8     | C         | C     |
(5 rows)
レプリケーション先で確認します。
$ psql -l
                              List of databases
   Name    |  Owner   | Encoding | Collation | Ctype |   Access privileges
-----------+----------+----------+-----------+-------+-----------------------
 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
 testdb    | postgres | UTF8     | C         | C     |
 testdb2   | postgres | UTF8     | C         | C     |
(5 rows)

割と簡単に設定できました。
そのうち、レプリケーション先の追加や、レプリケーション先の昇格などやってみたいと思います。

2010年7月16日金曜日

DataTableをBindingSourceのDataSourceに設定したときの注意点

0 コメント
ADO.NETのDataTableをBindingSourceのDataSourceに設定して、BindingSourceのFilterに条件を設定したとき、DataTable.DefaultViewのRowFilterに、BindingSourceのFilterの内容が設定されるみたい。
このため、1つのDataTableをBindingSourceのDataSourceやComboBoxのDataSourceなど、複数のコントロールのDataSourceに使用すると、どれか1つのコントロールでFilterを設定すると、すべてのコントロールでFilterがかかった状態になる。

これを回避するために、1つのDataTableを複数のコントロールのDataSourceとして使いたいときは、各コントロールのDataSourceにそれぞれDataViewを(DataTableから)生成して設定すればよい。

VirtualBoxでubuntuの仮想サーバーをコピー

0 コメント
コマンドプロンプトで、
vboxmanage clonevdi コピー元.vdi コピー先.vdi
で、仮想マシンのハードディスクを複製して、複製したハードディスクを使って仮想マシンを登録。

MACアドレスが変わるためかubuntuではeth1が追加されてしまい、ネットワークがうまく繋がらなくなってしまうので、
/etc/udev/rules.d/70-persistent-net.rules
を編集。

2010年6月4日金曜日

VMWareメモ

0 コメント
■VMWareにSSHで接続
unsupportedモードでコンソールに入って、inetdのsshdを有効にする。
・コンソール画面で、ALT+F1を押す。
・続けて、rootのパスワードを入力する(画面には何も表示されない)。
・/etc/inetd.confをviなどで開いて、sshの行のコメントを外す。
・VMWareを再起動。
これでSSHで接続できるようになる。

■VMWare-Toolsのインストール(ubuntu10.04の場合)
・仮想マシンが起動している状態で、vSphere Clientから、仮想マシンの右クリック→[ゲスト]→[VMWare Toolsのインストール/アップグレード]を選択。
・CD-ROMをマウント
$ sudo mount /dev/sr0 /cdrom
・カーネルのヘッダファイルをインストール
$ uname -a
$ sudo apt-get install linux-headers-xxxxxxxx
・開発環境をインストール
$ sudo apt-get install build-essential
・VMWare Toolsのインストール
$ tar zxvf /cdrom/VMwareTools-4.0.0-171294.tar.gz
$ cd vmware-tools-distrib/
$ sudo ./vmware-install.pl
質問にはすべてデフォルトでOK
インストールが終わったら、プロセスを確認。
$ ps ax | grep vmware-tools
14351 ? Ss 0:00 /usr/lib/vmware-tools/sbin32/vmware-guestd --background /var/run/vmware-guestd.pid
という感じで、vmware-toolsのプロセスが走っていれば完了。
・vSphere Clientの仮想マシンを右クリックして→[電源]→[ゲストのシャットダウン]をして、仮想マシンがシャットダウンされればOK。

2010年2月26日金曜日

RedmineをPostgreSQLで構築

0 コメント
ubuntu8.04 LTS Serverに、データベースをPostgreSQLを使ってRedmineサーバーを構築した時の手順です。
Redmine用のユーザーとデータベースを次の構成で作成します。
ubuntuのユーザー名redmine
redmineのホームディレクトリ/home/redmine
データベース名redmine
データベースのユーザー名redmine

Apache2をインストールします。
$ sudo apt-get install apache2

PostgreSQLをインストールします。
$ sudo apt-get install postgresql
$ su - postgres
$ createuser -P redmine
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
$ createdb redmine -O redmine -E UTF8
$ exit

rubyとruby on Railsをインストールします。
$ cd
$ sudo apt-get install ruby
$ sudo apt-get install libpgsql-ruby
$ sudo apt-get install rdoc irb ri ruby1.8-dev libyaml-ruby libzlib-ruby libopenssl-ruby
$ sudo apt-get install imagemagick librmagick-ruby1.8 build-essential
$ wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
$ tar zxvf rubygems-1.3.5.tgz
$ cd rubygems-1.3.5
$ sudo ruby setup.rb
$ sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
$ sudo gem update --system
$ sudo gem install rails --version 2.1.2
$ sudo gem install postgres-pr
$ sudo gem install rake

Redmineをインストールします。
$ cd
$ wget http://rubyforge.org/frs/download.php/67144/redmine-0.8.7.tar.gz
$ tar zxvf redmine-0.8.7.tar.gz
$ mv redmine-0.8.7 redmine
$ cd redmine
$ cp config/database.yml.example config/database.yml
$ vi config/database.yml
内容を次のように変更します。
production:
  adapter: postgresql
  database: redmine
  host: localhost
  username: redmine
  password: パスワードを指定します。
  encoding: utf8
続けて
$ rake config/initializers/session_store.rb
$ rake db:migrate RAILS_ENV=production
$ rake load_default_data RAILS_ENV=production
(in /home/redmine/rubygems-1.3.5/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
「languageを選択して」といわれるので、jaと入力します。
====================================
Default configuration data loaded.

Note: The rake task load_default_data has been deprecated, please use the replacement version redmine:load_default_data
$ cp config/email.yml.example config/email.yml
$ vi config/email.yml
次のように変更します。
production:
  delivery_method: :smtp
  smtp_settings:
    address: SMTPサーバーを指定します。
    port: SMTPのポートを指定します。
    domain: ドメインを指定します。
    #authentication: :login
    #user_name: redmine@example.net
    #password: redmine

ここで、一旦サーバーを起動してテストしてみます。
$ script/server -e production
ウェブブラウザから、http://サーバーアドレス:3000/にアクセスしてみます。
このとき、ログインユーザーはadminでパスワードもadminです。
無事ログインできたら、Administration→Settingをクリックして、以下の設定を変更します。
  • Default languageをJapaneseにします。
  • Users display formatをAdmin Redmineにします(名前の表示を氏 名の順に表示)。
  • Attachment max. sizeを、添付ファイルの上限サイズに変更します(Kbyte単位)。
  • Host name and pathをホストアドレスにします。
これで、ウェブブラウザを閉じて、ターミナルもCtrl+Cでサーバースクリプトを停止します。

Passenger(Ruby on RailsアプリケーションをApacheで動かすためのソフトウエア)をインストールします。
$ sudo apt-get install apache2-prefork-dev libapr1-dev libaprutil1-dev
$ sudo gem install passenger
$ sudo passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v2.2.8.

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 /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8/ext/apache2/mod_passenger.so
   PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8
   PassengerRuby /usr/bin/ruby1.8

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.


--------------------------------------------
Deploying a Ruby on Rails application: an example

Suppose you have a Rails application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:

   <virtualhost *:80>
      ServerName www.yourhost.com
      DocumentRoot /somewhere/public    # <-- be sure to point to 'public'!
      <directory /somewhere/public>
         AllowOverride all              # <-- relax Apache security settings
         Options -MultiViews            # <-- MultiViews must be turned off
      </Directory>
   </VirtualHost>

And that's it! You may also want to check the Users Guide for security and
optimization tips, troubleshooting and other useful information:

  /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8/doc/Users guide Apache.html

Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)
http://www.modrails.com/

Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.
と表示されたらOK。
Apache2の設定方法も親切に教えてくれるので、それを参考にApache2の設定を行います。
$ cd /etc/apache2/mods-available/
$ sudo vi passenger.load
次のように編集します。
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8/ext/apache2/mod_passenger.so
続けて
$ sudo vi passenger.conf
次のように編集します。
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8
PassengerRuby /usr/bin/ruby1.8
続けて
$ sudo a2enmod passenger
$ cd /etc/apache2/sites-available/
$ sudo cp default default.org
$ sudo vi default
DocumentRootを
        DocumentRoot /var/www/
から
        DocumentRoot /home/redmine/redmine/public
に変更します。またDocumentRootの下に以下を追加します。
        <directory /home/redmine/redmine/public>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
続けて
$ sudo a2enmod rewrite
$ sudo /etc/init.d/apache2 restart
でapacheを再起動します。
これで、ウェブブラウザでサーバーに接続できればインストール完了です。


[おまけ]
・PDFのCSVの文字化けを回避する方法。
/home/redmine/redmine/lang/ja.ymlを
general_csv_encoding: SJIS
general_pdf_encoding: UTF-8
から
general_csv_encoding: CP932
general_pdf_encoding: CP932
に変更します。

・リポジトリのソースコードのエンコーディングを設定。
管理→設定→リポジトリ→リポジトリのエンコーディングの欄に
SO-2022-JP, EUC-JP, UTF-8, SHIF_JIS, WINDOWS-31J
を設定します。

2010年2月25日木曜日

PostgreSQLをソースからビルド

0 コメント
ubuntu8.04 LTS Serverで、PostgreSQLをソースからビルドする手順です。

コンパイラなどのビルドに必要なパッケージがインストールされていないときは、build-essentialをインストールします。
$ sudo apt-get install build-essential

ビルドに必要なパッケージをインストールします。
sudo apt-get install libreadline5-dev zlib1g-dev

PostgreSQLの管理者のpostgresユーザーを作成します。
$ sudo adduser postgres
Adding user `postgres' ...
Adding new group `postgres' (1001) ...
Adding new user `postgres' (1001) with group `postgres' ...
Creating home directory `/home/postgres' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for postgres
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [y/N] y

PostgreSQLのディレクトリを作成します。
$ sudo mkdir /usr/local/pgsql
sudo chown postgres:postgres /usr/local/pgsql

ソースをダウンロードしてきます。今回は現時点で最新の8.4.2を使います。
$ cd /usr/local/src
sudo wget ftp://ftp2.jp.postgresql.org/pub/postgresql/source/v8.4.2/postgresql-8.4.2.tar.gz
sudo tar zxvf postgresql-8.4.2.tar.gz
sudo chown -R postgres:postgres postgresql-8.4.2

ユーザーをpostgresに切り替えてビルドします。
$ su - postgres
Password:
$ cd /usr/local/src/postgresql-8.4.2/
$ ./configure
$ make
$ make install

postgresユーザーの環境変数の設定を行います。
~/.profileの末尾に
# User specific environment and startup programs
PGHOME=/usr/local/pgsql
PGDATA=$PGHOME/data
PGLIB=$PGHOME/lib
PATH=$PATH:$HOME/bin:$PGHOME/bin
export PGHOME PGDATA PGLIB PATH
を追加して、
$ source ~/.profile
で環境変数の反映します。

PostgreSQLの初期化を行います。
$ initdb --no-locale

postgresユーザーから元のユーザーに戻ります。
$ exit

/etc/ld.so.confにライブラリを追加します。
sudo vi /etc/ld.so.conf
で、/usr/local/pgsql/libを追加して、
sudo ldconfig -v
を実行します。

OS起動時にPostgreSQLが自動起動するように設定します。
$ sudo cp /usr/local/src/postgresql-8.4.2/contrib/start-scripts/linux /etc/init.d/postgresql
$ sudo chmod 755 /etc/init.d/postgresql
$ sudo update-rc.d postgresql start 90 2 3 4 5 . stop 10 0 1 6 .

PostgreSQLを起動します。
$ sudo /etc/init.d/postgresql start

2010年2月22日月曜日

bashのコマンドラインのヒストリ機能をvi風に

0 コメント
bashのコマンドラインのヒストリは、ctrl+rで検索したりしますが、いまひとつ使いにくいので、vi風のヒストリに変更します。
$ set -o vi
これで、ヒストリがviのようになります。
例えば、過去のコマンドを検索するときは、escキーを押して、/検索文字で検索され、nキーで次候補、shift+nキーで前候補が検索されます。また、escキーを押して、kキーで1つ前のコマンド、jキーで1つ先のコマンドに移動し、hキーとlキーで左右のカーソル移動などなどです。

ただ、毎回 set -o vi と入力するのも面倒なので、~/.inputrcというファイルを用意します。
set editing-mode vi
set keymap vi
これで、ログイン時にヒストリがviモードになります。

デフォルトのエディタを変更

0 コメント
ubuntu8.04 LTS Serverで、デフォルトのエディタを変更する方法です。

$ sudo update-alternatives --config editor
`editor' を提供する 3 個の alternatives があります。

  選択肢       alternative
-----------------------------------------------
          1    /usr/bin/vim.tiny
          2    /bin/ed
*+        3    /bin/nano

デフォルト[*] のままにするには Enter、さもなければ選択肢の番号のキーを押してください:1
Using '/usr/bin/vim.tiny' to provide 'editor'.

PostgreSQLをインストール後に、initdbする。

0 コメント
ubuntuでパッケージからPostgreSQLをインストールした後で、EUC_JPなデータベースを作ろうとするとエラーになるときの対応法です。

ubuntu8.04 LTS ServerにPostgreSQLを次の手順でインストールします。
$ sudo apt-get install postgresql-8.3
postgresユーザーになって、EUC_JPでcreatedbしてみます。
$ su - postgres
$ createdb データベース名 -O オーナー -E EUC_JP
そうすると
createdb: database creation failed: ERROR:  encoding EUC_JP does not match server's locale ja_JP.UTF-8
DETAIL:  The server's LC_CTYPE setting requires encoding UTF8.
と表示され、データベースを作成することが出来ません。

対応方法として、これで正しいのか確証はないですけど、次の手順で対応しました。
$ sudo localedef -i ja_JP -f euc-JP ja_JP.EUC-JP
$ cd /var/lib/postgresql/8.3
$ sudo mv main main_org
$ sudo mkdir main
$ sudo chown -R postgres:postgres main
$ su - postgres
$ /usr/lib/postgresql/8.3/bin/initdb -D /var/lib/postgresql/8.3/main --encoding=UTF8 --no-locale
$ exit
$ sudo mv main main_new
$ sudo mv main_org main
$ sudo /etc/init.d/postgresql-8.3 stop
$ sudo mv main main_org
$ sudo mv main_new main
$ sudo su - root
# cd /var/lib/postgresql/8.3/main
# ln -s /etc/postgresql-common/root.crt root.crt
# ln -s /etc/ssl/certs/ssl-cert-snakeoil.pem server.crt
# ln -s /etc/ssl/private/ssl-cert-snakeoil.key server.key
# exit
$ sudo /etc/init.d/postgresql-8.3 start

改めで、EUC_JPのデータベースを作成してみます。
$ su - postgres
$ createdb xxxx -E EUC_JP
$ psql -l
        List of databases
   Name    |  Owner   | Encoding
-----------+----------+----------
 postgres  | postgres | UTF8
 template0 | postgres | UTF8
 template1 | postgres | UTF8
 xxxx      | postgres | EUC_JP
(4 rows)
として、EUC_JPのデータベースを作成することが出来ました。

2010年2月3日水曜日

oracleインスタントクライアントのインストール

0 コメント
ubuntuで、PHPからoracleに接続するために、oracleインスタントクライアントをインストールします。

まず、OTNから以下のファイルをダウンロードします。
  • Instant Clientパッケージ - Basic
  • Instant Clientパッケージ - SDK
  • Instant Clientパッケージ - SQL*Plus

次の手順でインストールします。
$ sudo mkdir -p /opt/oracle
$ cd /opt/oracle
$ sudo unzip ダウンロードしたInstant Clientパッケージ - Basic
$ sudo unzip ダウンロードしたInstant Clientパッケージ - SDK
$ sudo unzip ダウンロードしたInstant Clientパッケージ - SQL*Plus
$ sudo mv instantclient_10_2 instantclient
$ cd instantclient
$ sudo ln -s libclntsh.so.10.1 libclntsh.so
$ sudo ln -s libocci.so.10.1 libocci.so
$ sudo su -
# echo /opt/oracle/instantclient > /etc/ld.so.conf.d/oracle-instantclient
# exit
$ sudo pecl install oci8
画面に
 1. Please provide the path to the ORACLE_HOME directory. Use 'instantclient,/path/to/instant/client/lib' if you're compiling with Oracle Instant Client : autodetect

1-1, 'all', 'abort', or Enter to continue:
と表示されるので、all と入力します。

続いて、
Please provide the path to the ORACLE_HOME directory. Use 'instantclient,/path/to/instant/client/lib' if you're compiling with Oracle Instant Client [autodetect] :
と表示されるので、 instantclient,/opt/oracle/instantclient と入力します。

続いて、
 1. Please provide the path to the ORACLE_HOME directory. Use 'instantclient,/path/to/instant/client/lib' if you're compiling with Oracle Instant Client : instantclient,/opt/oracle/instantclient

1-1, 'all', 'abort', or Enter to continue:
と表示されるので、エンターキーを押します。

しばらくすると、
Build process completed successfully
Installing '/usr/lib/php5/20060613/oci8.so'
install ok: channel://pecl.php.net/oci8-1.3.5
configuration option "php_ini" is not set to php.ini location
You should add "extension=oci8.so" to php.ini
と表示されればOK。

PHPから利用できるように
$ sudo su -
# echo extension=oci8.so >> /etc/php5/apache2/php.ini
# exit
$ sudo /etc/init.d/apache2 restart
を行い、apacheを再起動します。

tnsnames.oraにサーバー情報を追加します。
$ cd /opt/oracle
$ sudo vi tnsnames.ora

環境変数の設定を追加します。
$ cd ~/
$ vi .profile
末尾に
LD_LIBRARY_PATH=/opt/oracle/instantclient
PATH=/opt/oracle/instantclient:$PATH
NLS_LANG=JAPANESE_JAPAN.AL32UTF8
TNS_ADMIN=/opt/oracle
export LD_LIBRARY_PATH PATH TNS_ADMIN NLS_LANG
を追加します。
$ source ~/.profile
を行って環境変数を設定してから
$ sqlplus ユーザー/パスワード@接続文字
で接続できれば完了!

[補足]
phpinfo()の結果を見ると、NLS_LANGとLD_LIBRARY_PATH等の環境変数の設定が反映されていないので、
$ sudo vi /etc/init.d/apache2
で、
ENV="env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin"


ENV="env -i LANG=C PATH=/opt/oracle/instantclient:/usr/local/bin:/usr/bin:/bin NLS_LANG=JAPANESE_JAPAN.AL32UTF8 LD_LIBRARY_PATH=/opt/oracle/instantclient TNS_ADMIN=/opt/oracle"

と修正して、apacheを再起動した。

あと、もしかしたら
$ sudo dpkg-reconfigure apache
したかも。

2010年2月1日月曜日

ユーザーにsudoを許可する

0 コメント
ubuntuでは、su - rootは使わず、sudoでやるのが一般的らしい。
ただ最初はインストール時に作ったユーザーのみがsudoできるようで、あとで追加したユーザーがsudoできるようにする方法のまとめ。

sudoに関する設定は /etc/sudoers に記述してあるようで、インストール直後は
# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.
#

Defaults        env_reset

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL

# Uncomment to allow members of group sudo to not need a password
# (Note that later entries override this, so you might need to move
# it further down)
# %sudo ALL=NOPASSWD: ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
というようになっていて最後の、%admin ALL=(ALL) ALL という部分が「ユーザーがadminというグループに属していればsudoが使える」という設定らしい。
なので、
$ sudo usermod -G admin ユーザー
コマンドで、ユーザーをadminグループに属させることでOK。