Edited at

"$ brew install mysql" でエラー

More than 5 years have passed since last update.

MacにMySQLをインストールしようとしてちょっとつまずいたのでメモ。


参考にさせて頂いた記事

Mac OS X 開発環境構築手順:DB実行環境(MySQL)インストール - Shinya’s Daily Report

http://d.hatena.ne.jp/absj31/20121020/1350743258

"$ brew install mysql" で一発楽ちん。

この記事だと、何事もなくインストール出来てるんだけど、実際にやってみたらエラーが出た!!


発生したエラー

MacBook-Pro13:server u1$ brew install mysql

==> Installing mysql dependency: cmake
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/cmake-2.8.9
######################################################################## 100.0%
==> Pouring cmake-2.8.9.mountainlion.bottle.tar.gz
/usr/local/Cellar/cmake/2.8.9: 662 files, 32M
==> Installing mysql
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/mysql-5.5.2
######################################################################## 100.0%
==> Pouring mysql-5.5.27.mountainlion.bottle.tar.gz
==> 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.27/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

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

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

Warning: Could not link mysql. Unlinking...
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
You can try again using `brew link mysql'
==> Summary
/usr/local/Cellar/mysql/5.5.27: 6385 files, 222M
MacBook-Pro13:server u1$ brew link mysql
Linking /usr/local/Cellar/mysql/5.5.27... Warning: Could not link mysql. Unlinking...

Error: Could not symlink file: /usr/local/Cellar/mysql/5.5.27/include/typelib.h
/usr/local/include is not writable. You should change its permissions.


解決方法

エラーメッセージを見れば分かるけど、 /usr/local/include の書き込み権限がないと怒られているので権限を設定すれば良い。


書き込み権限がない状態

MacBook-Pro13:~ u1$ ls -all /usr/local

total 24
drwxrwxr-x 14 root admin 476 3 7 00:53 .
drwxr-xr-x@ 13 root wheel 442 11 8 22:36 ..
drwxr-xr-x 14 u1 admin 476 3 7 01:30 .git
-rw-r--r-- 1 u1 admin 177 11 8 21:42 .gitignore
-rw-r--r-- 1 u1 admin 493 3 7 00:53 CONTRIBUTING.md
drwxr-xr-x 6 u1 admin 204 3 7 00:36 Cellar
drwxr-xr-x 9 u1 admin 306 3 7 00:53 Library
-rw-r--r-- 1 u1 admin 1084 3 7 00:53 README.md
drwxr-xr-x 59 u1 admin 2006 3 7 01:28 bin
drwxr-xr-x 47 root wheel 1598 3 7 01:28 include
drwxr-xr-x 13 root wheel 442 3 7 01:28 lib
drwxr-xr-x 6 u1 admin 204 3 7 01:28 opt
drwxr-xr-x 8 u1 admin 272 3 7 01:28 share
drwxr-xr-x 4 u1 admin 136 3 7 01:33 var


書き込み権限を与える

MacBook-Pro13:~ u1$ cd /usr/local

MacBook-Pro13:local u1$ sudo chmod g+w include
MacBook-Pro13:local u1$ sudo chown :admin include
MacBook-Pro13:local u1$ ls -all
total 24
drwxrwxr-x 14 root admin 476 3 7 00:53 .
drwxr-xr-x@ 13 root wheel 442 11 8 22:36 ..
drwxr-xr-x 14 u1 admin 476 3 7 01:30 .git
-rw-r--r-- 1 u1 admin 177 11 8 21:42 .gitignore
-rw-r--r-- 1 u1 admin 493 3 7 00:53 CONTRIBUTING.md
drwxr-xr-x 6 u1 admin 204 3 7 00:36 Cellar
drwxr-xr-x 9 u1 admin 306 3 7 00:53 Library
-rw-r--r-- 1 u1 admin 1084 3 7 00:53 README.md
drwxr-xr-x 59 u1 admin 2006 3 7 01:28 bin
drwxrwxr-x 47 root admin 1598 3 7 01:28 include
drwxr-xr-x 13 root wheel 442 3 7 01:28 lib
drwxr-xr-x 6 u1 admin 204 3 7 01:28 opt
drwxr-xr-x 8 u1 admin 272 3 7 01:28 share
drwxr-xr-x 4 u1 admin 136 3 7 01:33 var

この後、再度インストールしたら、今度は /usr/local/lib の書き込み権限がないと怒られたので、同様に権限を与える。そうしたらMySQLのインストールうまくいきました!!


おまけ:"$ brew doctor" が便利

実は最初エラーが出た時に、どこがエラーメッセージかちゃんと読めてなくて、どう対応していいか悩んでたりした。その時に見つけたコマンドが、"$ brew doctor" !!

Homebrewの正常か確認してくれます。


やってみたらこんな警告が出た

MacBook-Pro13:local u1$ brew doctor

Warning: /usr/local/include isn't writable.
This can happen if you "sudo make install" software that isn't managed
by Homebrew.

If a brew tries to write a header file to this directory, the install will
fail during the link step.

You should probably `chown` /usr/local/include
Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:

mysql
Warning: Your compilers are different from the standard versions for your Xcode.
If you have Xcode 4.3 or newer, you should install the Command Line Tools for
Xcode from within Xcode's Download preferences.
Otherwise, you should reinstall Xcode.

これで書き込み権限がないって言ってる事に気づいたわけ。助かった!


警告全部消すとこうなります

MacBook-Pro13:local u1$ brew doctor

Your system is raring to brew.

よし!!