Mac OS X 10.6 Snow LeopardにMySQL Sandboxをインストールする

Mac OS X 10.6.6
MySQL Sandbox 3.0.17
MySQL MySQL Community Server 5.5.10
Mac OS X ver. 10.6 (x86, 64-bit), Compressed TAR Archive

ここを参考に、rootでCPANからMySQL Sandbox本体をインストールします。ソースコードからインストールする場合は、ここからダウンロードできます。

$ sudo cpan -i Test::More
$ sudo cpan -i YAML
$ sudo cpan -i MySQL::Sandbox

MySQL Sandbox本体は以下のディレクトリにインストールされます。

Installing /opt/local/lib/perl5/site_perl/5.8.9/MySQL/Sandbox.pm
Installing /opt/local/lib/perl5/site_perl/5.8.9/MySQL/Sandbox/Recipes.pm
Installing /opt/local/lib/perl5/site_perl/5.8.9/MySQL/Sandbox/Scripts.pm
Installing /opt/local/share/man/man3/MySQL::Sandbox.3pm
Installing /opt/local/share/man/man3/MySQL::Sandbox::Recipes.3pm
Installing /opt/local/share/man/man3/MySQL::Sandbox::Scripts.3pm
Installing /opt/local/bin/low_level_make_sandbox
Installing /opt/local/bin/make_multiple_custom_sandbox
Installing /opt/local/bin/make_multiple_sandbox
Installing /opt/local/bin/make_replication_sandbox
Installing /opt/local/bin/make_sandbox
Installing /opt/local/bin/make_sandbox_from_installed
Installing /opt/local/bin/make_sandbox_from_source
Installing /opt/local/bin/msandbox
Installing /opt/local/bin/sb
Installing /opt/local/bin/sbtool
Installing /opt/local/bin/test_sandbox

同時に~/sandboxesも作成されますが、このディレクトリは所有者がrootになっているため、一旦削除します。

$ ls -la ~
drwxr-xr-x     2 root      staff     68  3 21 13:16 sandboxes
$ rmdir ~/sandboxes

MySQL公式サイトから、インストールしたいバイナリパッケージをダウンロードします。今回はMac OS X ver. 10.6 (x86, 64-bit)用のMySQL Community Server 5.5.10をダウンロードしました。バイナリパッケージを置くディレクトリとして、あらかじめ~/opt/mysqlを作成しています。

$ mkdir -p ~/opt/mysql
$ cd ~/opt/mysql
$ wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.10-osx10.6-x86_64.tar.gz/from/http://ftp.iij.ad.jp/pub/db/mysql/

ダウンロードしたバイナリパッケージを指定してmake_sandboxを実行すると、~/sandboxesにサンドボックスが作成されます。このとき、ベースディレクトリや使用するポートは、MySQLのバージョンから自動的に決定されます。

$ make_sandbox ~/opt/mysql/mysql-5.5.10-osx10.6-x86_64.tar.gz
unpacking /Users/tetsuyai/opt/mysql/mysql-5.5.10-osx10.6-x86_64.tar.gz
Executing low_level_make_sandbox --basedir=/Users/tetsuyai/opt/mysql/5.5.10 \
	--sandbox_directory=msb_5_5_10 \
	--install_version=5.5 \
	--sandbox_port=5510 \
	--no_ver_after_name \
	--my_clause=log-error=msandbox.err
    The MySQL Sandbox,  version 3.0.17
    (C) 2006-2010 Giuseppe Maxia
installing with the following parameters:
upper_directory                = /Users/tetsuyai/sandboxes
sandbox_directory              = msb_5_5_10
sandbox_port                   = 5510
check_port                     = 
no_check_port                  = 
datadir_from                   = script
install_version                = 5.5
basedir                        = /Users/tetsuyai/opt/mysql/5.5.10
tmpdir                         = 
my_file                        = 
operating_system_user          = tetsuyai
db_user                        = msandbox
remote_access                  = 127.%
ro_user                        = msandbox_ro
rw_user                        = msandbox_rw
repl_user                      = rsandbox
db_password                    = msandbox
repl_password                  = rsandbox
my_clause                      = log-error=msandbox.err
prompt_prefix                  = mysql
prompt_body                    =  [\h] {\u} (\d) > 
force                          = 
no_ver_after_name              = 1
verbose                        = 
load_grants                    = 1
no_load_grants                 = 
no_run                         = 
no_show                        = 
do you agree? ([Y],n) Y
loading grants
.. sandbox server started
Your sandbox server was installed in $HOME/sandboxes/msb_5_5_10

~/sandboxesが存在しない場合、make_sandboxコマンドによって作成されます。作成された~/sandboxesの所有者はtetsuyaiになっていました。

$ ls -la ~
drwxr-xr-x    12 tetsuyai  staff    408  3 21 15:01 sandboxes

サンドボックスは~/sandboxesにmsb_5_5_10という名前で作成されています。

$ ls -la ~/sandboxes
total 72
drwxr-xr-x  12 tetsuyai  staff   408  3 21 15:01 .
drwxr-xr-x+ 80 tetsuyai  staff  2720  3 21 15:00 ..
-rwxr-xr-x   1 tetsuyai  staff    64  3 21 15:01 clear_all
drwxr-xr-x  20 tetsuyai  staff   680  3 21 15:01 msb_5_5_10
-rw-r--r--   1 tetsuyai  staff  3621  3 21 15:01 plugin.conf
-rwxr-xr-x   1 tetsuyai  staff    66  3 21 15:01 restart_all
-rwxr-xr-x   1 tetsuyai  staff  2145  3 21 15:01 sandbox_action
-rwxr-xr-x   1 tetsuyai  staff    68  3 21 15:01 send_kill_all
-rwxr-xr-x   1 tetsuyai  staff    64  3 21 15:01 start_all
-rwxr-xr-x   1 tetsuyai  staff    65  3 21 15:01 status_all
-rwxr-xr-x   1 tetsuyai  staff    63  3 21 15:01 stop_all
-rwxr-xr-x   1 tetsuyai  staff    62  3 21 15:01 use_all

この時点で既にmysqldは起動しています。接続するには~/sandboxes/msb_5_5_10に移動して./useコマンドを実行します。./useコマンドはMySQL Clientを起動するコマンドです。デフォルトのユーザ名とパスワード、rootのパスワードはそれぞれ“msandbox”になっています。

$ cd sandboxes/msb_5_5_10
$ ./use
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.10 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql [localhost] {msandbox} ((none)) > show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql [localhost] {msandbox} ((none)) > use test;
Database changed
mysql [localhost] {msandbox} (test) > show tables;
Empty set (0.00 sec)

MySQL Clientにrootでログインしてみました。

$ ./use -uroot -pmsandbox
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.10 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql [localhost] {root} ((none)) >

サンドボックスの中では通常のMySQLと同様に、DDLDMLを実行できます。

mysql [localhost] {msandbox} (test) > CREATE TABLE accounts(id int, name VARCHAR(256));
Query OK, 0 rows affected (0.02 sec)

mysql [localhost] {msandbox} (test) > SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| accounts       |
+----------------+
1 row in set (0.00 sec)

mysql [localhost] {msandbox} (test) > DESC accounts;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | YES  |     | NULL    |       |
| name  | varchar(256) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.03 sec)

mysql [localhost] {msandbox} (test) > INSERT INTO accounts VALUES(1, 'tetsuyai');
Query OK, 1 row affected (0.00 sec)

mysql [localhost] {msandbox} (test) > SELECT * FROM accounts WHERE id = 1;
+------+----------+
| id   | name     |
+------+----------+
|    1 | tetsuyai |
+------+----------+
1 row in set (0.00 sec)

サンドボックスを初期化したくなったら、./clearコマンドを実行します。初期化の確認はないので、コマンドの実行には注意してください。同時にmysqldも停止するので、もう一度サンドボックスに入るためには、./startコマンドでmysqldを起動します。

$ ./clear
$ ./start
.. sandbox server started
$ ./use
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.10 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql [localhost] {msandbox} ((none)) > use test;
Database changed
mysql [localhost] {msandbox} (test) > show tables;
Empty set (0.00 sec)

mysqldを起動すると、データディレクトリにpidファイル、/tmpにソケットファイルが作成されます。

$ ls -la  ~/sandboxes/msb_5_5_10/data
-rw-rw----    1 tetsuyai  staff         5  3 21 21:34 mysql_sandbox5510.pid
$ ls -la /tmp/
srwxrwxrwx   1 tetsuyai  wheel    0  3 21 21:35 mysql_sandbox5510.sock

さらにsbtoolという名前のツールを使うと、

  • サーバのクローン(クローン元と先のsandboxが必要)
  • サーバの移動
  • ポートの変更
  • ポートの表示
  • sandboxの削除

といったことができます*1

-o portsサンドボックスが使うポートを表示します。--only_usedオプションを指定すると、現在使用中のポートだけが表示されます。

$ sbtool -o ports
 5510  1
$ sbtool -o ports --only_used
 5510  1

-o infoはサンドボックスの環境設定を表示します。

$ sbtool -o info
$ports = {
           '5510' => 1
         };
$all_info = {
              '5510' => {
                          'opt' => {
                                     'tmpdir' => '/Users/tetsuyai/sandboxes/msb_5_5_10/tmp',
                                     'datadir' => '/Users/tetsuyai/sandboxes/msb_5_5_10/data',
                                     'port' => '5510',
                                     'basedir' => '/Users/tetsuyai/opt/mysql/5.5.10',
                                     'socket' => '/tmp/mysql_sandbox5510.sock',
                                     'prompt' => '\'mysql [\\h] {\\u} (\\d) > \'',
                                     'pid_file' => '/Users/tetsuyai/sandboxes/msb_5_5_10/data/mysql_sandbox5510.pid',
                                     'password' => 'msandbox',
                                     'user' => 'tetsuyai',
                                     'log_error' => 'msandbox.err'
                                   },
                          'conf' => undef
                        }
            };

*1:他にもいろいろなことができます。引数なしでsbtoolコマンドを実行するとヘルプが表示されるので、そちらで確認してください。