Sunday, April 15, 2012

Debian に MySQL をインストールしてみます

仕事上のなりゆきから, MySQL の扱い方を勉強しなければならなくなりましたので, 手元に検証用の環境を作ることにしました. OS は Debian GNU/Linux 6.0 (Squeeze) です.

MySQL のパッケージを apt-get コマンドでダウンロードし, インストールしてもいいのですが, やや古いバージョン (5.1.61) を使用することになってしまうのが気に入りません. そこで, 今回は MySQL 公式サイトの MySQL :: Download MySQL Community Server から最新バージョン (5.5.23) のバイナリもしくはソースを入手したうえで, インストールをおこなってみたいと思いました.

ダウンロードページでは, プラットフォームに Debian Linux という選択肢があります. これを選択するとパッケージファイル (deb ファイル) をダウンロードすることができます. ただ, わたしには deb ファイルを使ってソフトウェアをインストールする仕組みについてほとんど理解がありません. とりあえず dpkg コマンドを使えばいいのでしょうか.

# dpkg --install mysql-5.5.23-debian6.0-i686.deb
Selecting previously deselected package mysql.
(Reading database ... 14474 files and directories currently installed.)
Unpacking mysql (from mysql-5.5.23-debian6.0-i686.deb) ...
Setting up mysql (5.5.23) ...

30 ~ 40 秒後にコマンドプロンプトが返って来ます. これだけですと, 本当にインストールされたのかどうか, されたのならどこにされたのか, よくわかりません. 少し調べてみましたところ, /var/lib/dpkg/infomysql.list というテキストファイルが作成されていました. これを見ると, MySQL は /opt/mysql/server-5.5 にインストールされたらしいことがわかりました.

しかし, 変なのです. 作成されたディレクトリやファイルのオーナーが, ユーザーは 7161, グループは uucp となっています.

# ls -al /opt/mysql/server-5.5
total 80
drwxr-xr-x 12 7161 uucp  4096 Apr 15 13:48 .
drwxr-xr-x  3 7161 uucp  4096 Apr 15 13:47 ..
drwxr-xr-x  2 7161 uucp  4096 Apr 15 13:48 bin
-rw-r--r--  1 7161 uucp 17987 Mar 30 04:07 COPYING
drwxr-xr-x  2 7161 uucp  4096 Apr 15 13:48 docs
drwxr-xr-x  3 7161 uucp  4096 Apr 15 13:48 include
-rw-r--r--  1 7161 uucp  7604 Mar 30 04:07 INSTALL-BINARY
drwxr-xr-x  3 7161 uucp  4096 Apr 15 13:48 lib
drwxr-xr-x  4 7161 uucp  4096 Apr 15 13:48 man
drwxr-xr-x 10 7161 uucp  4096 Apr 15 13:48 mysql-test
-rw-r--r--  1 7161 uucp  2552 Mar 30 04:07 README
drwxr-xr-x  2 7161 uucp  4096 Apr 15 13:48 scripts
drwxr-xr-x 27 7161 uucp  4096 Apr 15 13:48 share
drwxr-xr-x  4 7161 uucp  4096 Apr 15 13:48 sql-bench
drwxr-xr-x  2 7161 uucp  4096 Apr 15 13:48 support-files

よくわからなくて気持ちが悪いです. とりあえず, mysql というユーザーとグループを作成し, オーナーを変更してみます. というか, 基本的には公式サイトの MySQL :: MySQL 5.5 Reference Manual :: 2.2 Installing MySQL from Generic Binaries on Unix/Linux を参考にしながら, セットアップをおこなってみます.

# groupadd mysql
# useradd -r -g mysql mysql
# chown -R mysql:mysql /opt/mysql/server-5.5
# /opt/mysql/server-5.5/scripts/mysql_install_db --user=mysql
Installing MySQL system tables...
/opt/mysql/server-5.5/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

Installation of system tables failed!  Examine the logs in
/opt/mysql/server-5.5/data for more information.

You can try to start the mysqld daemon with:

    shell> /opt/mysql/server-5.5/bin/mysqld --skip-grant &

and use the command line tool /opt/mysql/server-5.5/bin/mysql
to connect to the mysql database and look at the grant tables:

    shell> /opt/mysql/server-5.5/bin/mysql -u root mysql
    mysql> show tables

Try 'mysqld --help' if you have problems with paths.  Using --log
gives you a log in /opt/mysql/server-5.5/data that may be helpful.

Please consult the MySQL manual section
'Problems running mysql_install_db', and the manual section that
describes problems on your OS.  Another information source are the
MySQL email archives available at http://lists.mysql.com/.

Please check all of the above before mailing us!  And remember, if
you do mail us, you MUST use the /opt/mysql/server-5.5/bin/mysqlbug script!

早速失敗してしまいました. libaio.so.1 が見つからないとのことです. ググってみますと, MySQL Bugs: #60544: mysqld requires libaio.so and does not start という記事が見つかりました. どうも, これは不具合ではなく, 暗黙のうちにおこなわれた意図的な仕様変更っぽいです. そこで, apt-get コマンドで libaio1 パッケージをインストールしてから, mysql_install_db スクリプトをもう一度実行してみることにします.

# apt-get install libaio1
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  libaio1
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 7,772 B of archives.
After this operation, 73.7 kB of additional disk space will be used.
Get:1 http://ftp.jp.debian.org/debian/ squeeze/main libaio1 i386 0.3.107-7 [7,772 B]
Fetched 7,772 B in 0s (16.2 kB/s)
Selecting previously deselected package libaio1.
(Reading database ... 21000 files and directories currently installed.)
Unpacking libaio1 (from .../libaio1_0.3.107-7_i386.deb) ...
Setting up libaio1 (0.3.107-7) ...

# /opt/mysql/server-5.5/scripts/mysql_install_db --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
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:

/opt/mysql/server-5.5/bin/mysqladmin -u root password 'new-password'
/opt/mysql/server-5.5/bin/mysqladmin -u root -h debian password 'new-password'

Alternatively you can run:
/opt/mysql/server-5.5/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 /opt/mysql/server-5.5 ; /opt/mysql/server-5.5/bin/mysqld_safe &

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

Please report any problems with the /opt/mysql/server-5.5/bin/mysqlbug script!

今度は成功したような気がします. MySQL サーバーを起動してみます.

# /opt/mysql/server-5.5/bin/mysqld_safe &

ps コマンドで見てみますと, mysql ユーザーが mysqld デーモンを起動したということがわかりました. mysqld_safe スクリプトを --user オプションをつけずに実行すると, mysql ユーザーが mysqld デーモンを起動するということになっているのでしょうか. スクリプトをしっかり読んだわけではありませんが, なんとなくそんな気がします.

なお, このままでは, MySQL の管理者アカウントである root ユーザーにはパスワードが設定されていない状態ですので, そのあたりの初期設定をおこなう必要がありますが, そのあたりは割愛します.

2 comments:

  1. Hi, Neat post. There's a problem along with your website in web explorer, could check this? IE still is the market leader and a good section of folks will omit your wonderful writing due to this problem.

    ReplyDelete
  2. It is actually a nice and helpful piece of information. I'm satisfied that you simply shared this helpful info with us. Please keep us informed like this. Thank you for sharing.

    ReplyDelete