Mac OS X Lion に homebrew で MySQL をインストールする

Mac OS X 10.7 に MySQL をインストールする。homebrew はすでにインストール済みとする。

% brew install mysql
==> Installing mysql dependency: cmake
==> Downloading
https://downloads.sf.net/project/machomebrew/Bottles/cmake-2.8.6-bottle.tar.gz
######################################################################## 100.0%
==> Pouring cmake-2.8.6.bottle.tar.gz
Warning: m4 macros were installed to "share/aclocal".
Homebrew does not append "/usr/local/share/aclocal"
to "/usr/share/aclocal/dirlist". If an autoconf script you use
requires these m4 macros, you'll need to add this path manually.
==> Summary
/usr/local/Cellar/cmake/2.8.6: 628 files, 28M
==> Installing mysql dependency: readline
==> Downloading http://ftpmirror.gnu.org/readline/readline-6.2.tar.gz
######################################################################## 100.0%
==> Downloading patches
######################################################################## 100.0%
==> Patching
patching file vi_mode.c
patching file callback.c
==> ./configure --prefix=/usr/local/Cellar/readline/6.2.1
--mandir=/usr/local/Cellar/readline/6.2.1/share/man
--infodir=/usr/local/Cellar/re
==> make install
==> Caveats
This formula is keg-only, so it was not symlinked into /usr/local.

OS X provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

Generally there are no consequences of this for you.
If you build your own software and it requires this formula, you'll need
to add its lib & include paths to your build variables:

   LDFLAGS  -L/usr/local/Cellar/readline/6.2.1/lib
   CPPFLAGS -I/usr/local/Cellar/readline/6.2.1/include
==> Summary
/usr/local/Cellar/readline/6.2.1: 28 files, 1.6M, built in 20 seconds
==> Installing mysql dependency: pidof
==> Downloading http://www.nightproductions.net/downloads/pidof_source.tar.gz
######################################################################## 100.0%
==> make all
==> /usr/bin/gzip pidof.1
/usr/local/Cellar/pidof/0.1.4: 2 files, 20K, built in 2 seconds
==> Installing mysql
==> Downloading
http://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.15.tar.gz
######################################################################## 100.0%
==> Patching
patching file scripts/mysqld_safe.sh
Hunk #1 succeeded at 555 (offset 172 lines).
patching file scripts/mysql_config.sh
Hunk #1 succeeded at 137 (offset 5 lines).
patching file configure.cmake
==> cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/Cellar/mysql/5.5.15
-DMYSQL_DATADIR=/usr/local/var/mysql
-DINSTALL_MANDIR=/usr/local/Cellar/my
==> make
==> make install
==> Caveats
Set up databases to run AS YOUR USER ACCOUNT with:
   unset TMPDIR
   mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

To set up base tables in another folder, or use a different user to run
mysqld, view the help for mysqld_install_db:
   mysql_install_db --help

and view the MySQL documentation:
 * http://dev.mysql.com/doc/refman/5.5/en/mysql-install-db.html
 * http://dev.mysql.com/doc/refman/5.5/en/default-privileges.html

To run as, for instance, user "mysql", you may need to `sudo`:
   sudo mysql_install_db ...options...

Start mysqld manually with:
   mysql.server start

   Note: if this fails, you probably forgot to run the first two steps up above

A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

To connect:
   mysql -uroot

To launch on startup:
* if this is your first install:
   mkdir -p ~/Library/LaunchAgents
   cp /usr/local/Cellar/mysql/5.5.15/com.mysql.mysqld.plist
~/Library/LaunchAgents/
   launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

* if this is an upgrade and you already have the com.mysql.mysqld.plist loaded:
   launchctl unload -w ~/Library/LaunchAgents/com.mysql.mysqld.plist
   cp /usr/local/Cellar/mysql/5.5.15/com.mysql.mysqld.plist
~/Library/LaunchAgents/
   launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

You may also need to edit the plist to use the correct "UserName".

Warning: m4 macros were installed to "share/aclocal".
Homebrew does not append "/usr/local/share/aclocal"
to "/usr/share/aclocal/dirlist". If an autoconf script you use
requires these m4 macros, you'll need to add this path manually.
==> Summary
/usr/local/Cellar/mysql/5.5.15: 6276 files, 217M, built in 4.7 minutes

インストールが完了したら、インストールログに表示された設定を実行する。
まずは、データベースを初期化する。データベースのデータを配置するディレクトリを変更する場合は、コマンドに渡すオプションを変更する。今回は初期の設定で実施する。

% unset TMPDIR
% mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
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:

/usr/local/Cellar/mysql/5.5.15/bin/mysqladmin -u root password 'new-password'
/usr/local/Cellar/mysql/5.5.15/bin/mysqladmin -u root -h MacBook-Air.local password 'new-password'

Alternatively you can run:
/usr/local/Cellar/mysql/5.5.15/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 /usr/local/Cellar/mysql/5.5.15 ; /usr/local/Cellar/mysql/5.5.15/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/Cellar/mysql/5.5.15/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/local/Cellar/mysql/5.5.15/scripts/mysqlbug script!

初期化が済んだら、次に MySQL の管理者ユーザーのパスワードが設定されていないので、MySQL を起動してパスワードを新たに設定する。
この一連の作業は、MySQL を新規にインストールする度に行う作業になる。

% mysql.server start
Starting MySQL
.. SUCCESS! 

そして、MySQL にログインをする。

% mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.15 Source distribution

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> 

MySQL を利用するユーザー権限の確認をする。

mysql> USE MySQL;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SELECT `User`, `Host`, `Password` FROM `USER`;
+----+---------------+----------+
| User | Host                     | Password     |
+----+---------------+----------+
| root | localhost               |                   |
| root | MacBook-Air.local |                   |
| root | 127.0.0.1              |                   |
| root | ::1                         |                   |
|        | localhost               |                    |
|        | MacBook-Air.local |                    |
+---+----------------+----------+
6 rows in set (0.00 sec)

匿名でもログインできるユーザーを削除し、root ユーザーのパスワードを新たに設定する。
まずは、匿名ユーザーを削除する。

mysql> DELETE FROM `User` WHERE `User` = '';
Query OK, 2 rows affected (0.01 sec)

mysql> SELECT `User`, `Host`, `Password` FROM `USER`;
+----+---------------+----------+
| User | Host                      | Password   |
+----+---------------+----------+
| root | localhost               |                   |
| root | MacBook-Air.local |                   |
| root | 127.0.0.1              |                   |
| root | ::1                         |                   |
+---+----------------+----------+
4 rows in set (0.00 sec)

そして、root ユーザーにパスワードを設定する。

mysql> SET PASSWORD FOR 'root'@'localhost' = password('yourpassword');
Query OK, 0 rows affected (0.00 sec)

mysql> SET PASSWORD FOR 'root'@'MacBook-Air.local' = password('yourpassword');
Query OK, 0 rows affected (0.00 sec)

mysql> SET PASSWORD FOR 'root'@'127.0.0.1' = password('yourpassword');
Query OK, 0 rows affected (0.00 sec)

mysql> SET PASSWORD FOR 'root'@'::1' = password('yourpassword');
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT `User`, `Host`, `Password` FROM `USER`;
+- --+---------------+----------------------------------------------+
| User | Host                     | Password                                                                      |
+----+---------------+----------------------------------------------+
| root | localhost               | *04G0E553996615211F49D24A263ACACD723FB599  |
| root | MacBook-Air.local | *04G0E553996615211F49D24A263ACACD723FB599  |
| root | 127.0.0.1              | *04G0E553996615211F49D24A263ACACD723FB599  |
| root | ::1                         | *04G0E553996615211F49D24A263ACACD723FB599  |
+------+-------------+----------------------------------------------+
4 rows in set (0.00 sec)

これで最初の設定は完了となる。この他に MySQL で必要な設定は利用目的に沿った my.cnf の設定となる。

Perl の DBD::mysql をインストールする

perlbrew でインストールした Perl を利用し、cpanm でDBD::mysql をインストールする。
これはあっさりとインストールできた。

% cpanm DBD::mysql
--> Working on DBD::mysql
Fetching http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.020.tar.gz
... OK
Configuring DBD-mysql-4.020 ... OK
Building and testing DBD-mysql-4.020 ... OK
Successfully installed DBD-mysql-4.020
1 distribution installed