end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

srcからMySQL 5.1.30をinstall

これまでdbにはpostgresを使用していましたが、私の周りではmysqlへの移行が進んでいるようです。今回、mysql 5.1.30をsrcからinstallしたので、その手順を書き留めておきます。

参考にしたもの

今回のinstallでは、dev.mysql.com にあるリファレンスマニュアルを参考にしながら、作業を進めました。

http://dev.mysql.com/doc/refman/5.1/ja/index.html
http://dev.mysql.com/doc/refman/5.1/ja/quick-install.html

ユーザ,グループの追加

mysqlはデフォルトでは、mysqlというユーザで実行されるらしいので
user,groupを追加します。

# groupadd mysql
# useradd -g mysql mysql

buildとinstall

$ wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.30.tar.gz
$ tar -zxvf mysql-5.1.30.tar.gz
$ cd mysql-5.1.30
$ ./configure --prefix=/usr/local/mysql \
              --with-plugins=innobase,partition \
              --with-charset=utf8 \
              --with-extra-charsets=all \
              --with-mysqld-user=mysql
$ make
$ su
# make install

ここでは私が指定したconfigureのオプションのみを解説します。

--prefix=/usr/local/mysql

install先をデフォルトの/usr/localから変更しました。

--with-plugins=innobase,partition

ストレージエンジンにInnoDBを追加しました。ただし、デフォルトのストレージエンジンを変更するには、後述のmy.cnfで指定する必要があります。
また、パーティショニングも可能にしています。

--with-charset=utf8 --with-extra-charsets=all

デフォルト文字コードをutf8にしていますが、他の文字コードも使用できるようにしました。

--with-mysqld-user=mysql

mysqldの実行ユーザ

オプションファイルの作成

# cp support-files/my-medium.cnf /etc/my.cnf

オプションファイルとは、メモリやport等を定義するファイルでsupport-files以下にあるファイルを元にします。my.cnfというファイル名は慣習的なもので、ファイルの設置先は次のような優先度となるようです。

path note
/etc/my.cnf
$MYSQL_HOME/my.cnf サーバ固有のオプション
defaults-extra-file --defaults-extra-file=pathで指定されたファイル
~/.my.cnf


私の場合、my-medium.cnf を少々、変更しましたが、その変更内容を解説します。

[mysqld]
default-storage-engine = INNODB	#行追加
basedir = /usr/local/mysql	#行追加
datadir = /data/mysql	        #行追加
port            = 3306
socket          = /tmp/mysql.sock
<略>
#skip-federated  #←コメントアウト
<略>
#log-bin=mysql-bin #←コメントアウト
default-storage-engine=INNODB

まず、web+db press vol.45によれば、デフォルト?のストレージエンジンであるMyISAMInnoDBを比較すると、表のようになるようです。

項目 InnoDB MyISAM
パフォーマンス ○〜◎
トランザクション ×
障害時のリカバリ ×
レプリケーション
所有企業 Innobase/oracle sun

今回は、この表と、次のurlにあるストレージエンジンのパフォーマンスに関する記事等から、InnoDBを使用することにしました。
http://www.inter-office.co.jp/contents/164/

また、利用可能なエンジンは、SHOW ENGINES で確認できます。

mysql> SHOW ENGINES;
+------------+---------+-------------------------------+--------------+
| Engine     | Support | Comment                       | Transactions |
+------------+---------+-------------------------------+--------------+
| CSV        | YES     | CSV storage engine     <略> | NO           |
| MRG_MYISAM | YES     | Collection of identical<略> | NO           |
| MEMORY     | YES     | Hash based, stored in m<略> | NO           |
| InnoDB     | DEFAULT | Supports transactions, <略> | YES          |
| MyISAM     | YES     | Default engine as of My<略> | NO           |
+------------+---------+-------------------------------+--------------+
5 rows in set (0.00 sec)
basedir = /usr/local/mysql datadir = /data/mysql

mysqlのインストール先と、データの保管先です。

#skip-federated

mysql5.0からfederatedというエンジンが追加されたようですが、
許諾テーブルの作成時( mysql_install_db )に次のようなエラーが表示されたのでコメントアウトしました。

[mysql@colinux mysql]$ ./bin/mysql_install_db --user=mysql
Installing MySQL system tables...
090129 12:37:16 [ERROR] /usr/local/mysql/libexec/mysqld: unknown option '--skip-federated'
090129 12:37:16 [ERROR] Aborting
090129 12:37:16 [Note] /usr/local/mysql/libexec/mysqld: Shutdown complete
#log-bin=mysql-bin

デフォルトでは、「mysql-bin.連番」のバイナリログが出力されますが、レプリケーションしない場合、必要ないのでコメントアウトしてます。

my.cnfのサンプルは support-files/ にあります

以下は、mysql-5.1.47 の例ですが、my.cnfのサンプルは、support-files/ 以下にあります。

my-huge.cnf.sh
This is for a large system with memory of 1G-2G where the system runs mainly MySQL.
my-innodb-heavy-4G.cnf.sh
This is a MySQL example config file for systems with 4GB of memory running mostly MySQL using InnoDB only tables and performing complex queries with few connections.
my-large.cnf.sh
This is for a large system with memory = 512M where the system runs mainly MySQL.
my-medium.cnf.sh
This is for a system with little memory (32M - 64M) where MySQL plays an important part, or systems up to 128M where MySQL is used together with other programs (such as a web server)
my-small.cnf.sh
This is for a system with little memory (<= 64M) where MySQL is only used from time to time and it's important that the mysqld daemon doesn't use much resources.

datadirの作成と所有者の変更

# mkdir /data
# mkdir /data/mysql
# chown -R mysql:mysql /data/mysql
# chown -R mysql:mysql /usr/local/mysql

許諾テーブルの作成

mysqlを起動するには、権限データベース(mysql)が必要になるようです。mysql_install_db コマンドは、権限データベース(mysql)、
テストデータベース(test)、初期ユーザ(ローカルより接続できる、管理権限を持つmysqlユーザ)を作成します。

[root@colinux etc]# /usr/local/mysql/bin/mysql_install_db --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK
<略>

とりあえず、起動

install自体はここまでなので、mysqld_safe でmysqlを起動できれば、作業完了です。

[mysql@colinux mysql]$ ./bin/mysqld_safe --user=mysql &
[1] 15367
[mysql@colinux mysql]$ 090129 13:20:22 mysqld_safe Logging to '/data/mysql/colinux.err'.
090129 13:20:22 mysqld_safe Starting mysqld daemon with databases from /data/mysql
[mysql@colinux mysql]$ 

mysql自動起動

mysqlでは起動scriptも提供されているので、これを配備するだけで自動起動を実現できます。

http://dev.mysql.com/doc/refman/5.1/ja/automatic-start.html

# cd /etc/rc.d/init.d
# cp ~endo/tmp/mysql-5.1.30/support-files/mysql.server mysql
# chmod 755 mysql
# /sbin/chkconfig --add mysql 
# /sbin/chkconfig --list mysql 

おまけ databaseとuserの作成

mysql> create database endo_test;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| endo_test          | 
| mysql              | 
| test               | 
+--------------------+
4 rows in set (0.00 sec)

mysql> grant all privileges on endo_test.* to endo@localhost identified by 'ないしょ'
Query OK, 0 rows affected (0.00 sec)

DBD::mysqlでperlからmysqlに接続(機種依存文字や0x5Cも問題なし)

mysqlのinstallが完了したので、今回はDBD::mysqlperlからmysqlへの接続方法を紹介します。

DBD::mysqlのinstall

$ /usr/local/bin/perl Makefile.PL \
    --mysql_config=/usr/local/mysql/bin/mysql_config \
    --testdb=endo_test \
    --testuser=endo \
    --testpassword=ないしょ
$ make
$ make test
# make install

私の場合、mysqlを/usr/local/mysql へinstallした為、「perl Makefile.PL」でいくつかのoptionを指定する必要があります。

--mysql_config

mysql_configの場所を指定しています。

--testdb --testuser --testpassword

「make test」では、テスト用のdatabaseを使用しますが、そのdb名や接続ユーザを指定しています。

DBD::mysqlの接続テスト(機種依存文字や0x5Cも問題なさそう)

以下では、DBD::mysqlでの接続テストを行うscriptを記載しておきます。
今回行った範囲では、機種依存文字(㈱,①)や0x5C(ソ)も問題なさそうです。

#!/usr/local/bin/perl

use strict;
use warnings;
use encoding "cp932"; #DBのサーバ側のencodingはutf8
use Encode;
use DBI;
use Data::Dumper;

my $DB = "DBI:mysql:dbname=endo_test;host=localhost;";
my $DB_USER = "endo";
my $DB_PASS = "ないしょ";
my $DBI_OPT = { AutoCommit => 0 };

sub main {

    my $dbh = connect_db();

    test_insert($dbh);
    test_select($dbh);

#    $dbh->commit();
    $dbh->disconnect();
}

sub test_insert {
    my ($dbh) = @_;
    my $sql =<<EOF;
insert into test values (?);
EOF
    my $sth = $dbh->prepare($sql);
    #機種依存文字と0x5C問題を確認します。
    $sth->execute( encode("utf8","遠藤㈱エンドウソ①" ) );

}

sub test_select {
    my ($dbh) = @_;

    my $sql =<<EOF;
select * from test
EOF
    my $sth = $dbh->prepare($sql);
    $sth->execute();
    while (my $row = $sth->fetchrow_hashref() ) {
	#server側のencodeはutf8なので、一旦decodeし、cp932にencodeしてます
        print STDERR encode("cp932", decode("utf8","$row->{col1}")."\n" );
    }
}

sub connect_db {
    my $dbh = DBI->connect($DB,$DB_USER,$DB_PASS,$DBI_OPT);
#    $dbh->do("SET NAMES $CLIENTENCODING") or die "cannot set encoding";
    return $dbh;
}

main();
[endo@colinux tmp]$ ./mysql_test.pl 
遠藤㈱エンドウソ①
[endo@colinux tmp]$