MacBookAirでHomebrewを使ったMySQL 5.5.14のインストール

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

MacにMySQLをインストール

MacBookAirにMySQLをインストールしたのでメモを共有します。インストールにはHomebrewというパッケージ管理アプリを用います。

  • OS : Mac OS X Lion 10.7.1
  • DB : MySQL 5.5.14

Homebrewのインストール

Homebrewのインストールはとても簡単です。ワンライナーで書けます。素敵です。

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

MySQLのインストール

Homebrewを使えばインストールもコマンド1回です。3分ほど待ちますとインストール完了です。

$ brew install mysql

データベースをセットアップする

brewでMySQLをインストールすると、いろいろと注意書きが表示されています。これに沿って設定をします。まずはデータベースの作成です。

$ mysql_install_db --verbose -user='whoami' --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

MySQLの起動用にmy.cnfを設定します。必要に応じて記述します。

[mysqld]
max_allowed_packet=64M
character-set-server = utf8
innodb_file_per_table
[mysql]
default-character-set = utf8

最後に起動と接続確認をします。

$ mysql.server start
Starting MySQL
 SUCCESS!
$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.14 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql>

とりあえずの接続まで確認できました。

自動機能の設定とパスワードの設定

Mac起動時に自動実行するように設定します。

$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/mysql/5.5.14/com.mysql.mysqld.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

MySQLに繋げるためにパスワードを設定します。

$ mysqladmin -u root -h 127.0.0.1 password 'パスワード'
$ mysql -u root -h 127.0.0.1 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.14 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>

まとめ

Linuxでは、yumなど大変便利なパッケージ管理アプリがありますが、Macでも同様のソフトがないかなぁと思っていました。MacPortsよりも簡単に使えるHomebrewは非常に簡単にパッケージ管理ができます。brewコマンドを使いこなしてMac使いになりましょう!