これまで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-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によれば、デフォルト?のストレージエンジンであるMyISAMとInnoDBを比較すると、表のようになるようです。
項目 | 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)
#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
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)