SSブログ

homebrewでMySQL5.1を入れてみた [mac]

前回(Macにrbenvでruby1.9.3を入れてみた。)Rubyの環境を作って次はRails・・の前にMySQLの環境を作る。

Mac用のMySQLはパッケージで提供されているが、今回はせっかくなのでhomebrewで入れてみる。
ちなみに現時点でのMySQLの最新バージョンは5.6なのですが、大人の事情により5.1を入れる事にします。

まずはインストールできるバージョンを確認(2013/03/22時点)
% brew versions mysql
5.6.10   git checkout aa3db07 /usr/local/Library/Formula/mysql.rb
5.5.29   git checkout 336c976 /usr/local/Library/Formula/mysql.rb
(中略)
5.1.56   git checkout feae63a /usr/local/Library/Formula/mysql.rb
(中略)


どうやら5.6系、5.5系、5.1系がインストール出来る模様。

今回は5.1系のインスールなので、その中で最新である5.1.56をインストールする。
% cd /usr/local/Library/Formula
% git checkout feae63a /usr/local/Library/Formula/mysql.rb
% brew info mysql
mysql: stable 5.1.56
http://dev.mysql.com/doc/refman/5.1/en/
Depends on: readline
Not installed
https://github.com/mxcl/homebrew/commits/master/Library/Formula/mysql.rb
==> Options
--universal
 Make mysql a universal binary
--with-bench
 Keep benchmark app when installing.
--with-embedded
 Build the embedded server.
--client-only
 Only install client tools, not the server.
--with-utf8-default
 Set the default character set to utf8
--with-tests
 Keep tests when installing.
==> Caveats
Set up databases with:
    unset TMPDIR
    mysql_install_db

If this is your first install, automatically load on login with:
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/Cellar/mysql/5.1.56/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.1.56/com.mysql.mysqld.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

Note on upgrading:
    We overwrite any existing com.mysql.mysqld.plist in ~/Library/LaunchAgents
    if we are upgrading because previous versions of this brew created the
    plist with a version specific program argument.

Or start manually with:
    mysql.server start

% brew install mysql
==> Downloading http://mysql.mirrors.pair.com/Downloads/MySQL-5.1/mysql-5.1.56.tar.gz

curl: (22) The requested URL returned error: 404
Error: Download failed: http://mysql.mirrors.pair.com/Downloads/MySQL-5.1/mysql-5.1.56.tar.gz


------------------------------------------------------------------------------
追記:2013/03/29
このあと色々とトラブルに見舞われるのですが、そもそも初期設定をしてなかったのが問題でした。。。
コンソールログはちゃんと見ましょうという話ですね。
> Set up databases with:
> unset TMPDIR
> mysql_install_db

以降の対応はちゃんと設定していれば必要のない部分なのですが、自戒の念と似たようなハマり方をしてしまった方の為に残しておきます。
------------------------------------------------------------------------------

いきなり躓く。404エラー・・・?
調べると他バージョンを入れるformulaを追加しないとダメらしい。

参考:macportsからHomebrewに乗り換えてMySQL5.1をインストールするまでの流れ

"brew tap"コマンドを利用してMySQL5.1用のformulaをインストールする。
% brew tap homebrew/versions      
Cloning into '/usr/local/Library/Taps/homebrew-versions'…
remote: Counting objects: 518, done.
remote: Compressing objects: 100% (212/212), done.
remote: Total 518 (delta 317), reused 503 (delta 306)
Receiving objects: 100% (518/518), 107.34 KiB | 45 KiB/s, done.
Resolving deltas: 100% (317/317), done.
Tapped 48 formula


それを利用してMySQLのインストールに再チャレンジ。
% brew install mysql51
==> Downloading http://mysql.mirrors.pair.com/Downloads/MySQL-5.1/mysql-5.1.67.tar.gz
######################################################################## 100.0%
==> Patching
patching file scripts/mysqld_safe.sh
Hunk #1 succeeded at 411 (offset 27 lines).
patching file scripts/mysql_config.sh
Hunk #1 succeeded at 133 (offset 1 line).
==> ./configure --without-docs --without-debug --prefix=/usr/local/Cellar/mysql51/5.1.67 --localstatedir=/usr/local/var/mysql --sysco
==> make install
==> Caveats
Set up databases with:
    unset TMPDIR
    mysql_install_db

If this is your first install, automatically load on login with:
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/Cellar/mysql51/5.1.67/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/mysql51/5.1.67/com.mysql.mysqld.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

Note on upgrading:
    We overwrite any existing com.mysql.mysqld.plist in ~/Library/LaunchAgents
    if we are upgrading because previous versions of this brew created the
    plist with a version specific program argument.

Or start manually with:
    mysql.server start

This article may help in troubleshooting MySQL installs:
    http://cloudbacon.com/2011/03/20/fixing-mysql-in-homebrew/

To have launchd start mysql51 at login:
    ln -sfv /usr/local/opt/mysql51/*.plist ~/Library/LaunchAgents
Then to load mysql51 now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql51.plist
==> Summary
 /usr/local/Cellar/mysql51/5.1.67: 221 files, 42M, built in 4.1 minutes

% unset TMPDIR
% mysql_install_db 
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/mysql51/5.1.67/bin/mysqladmin -u root password 'new-password'
/usr/local/Cellar/mysql51/5.1.67/bin/mysqladmin -u root -h **************************.local password 'new-password'

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

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

Please report any problems with the /usr/local/Cellar/mysql51/5.1.67/bin/mysqlbug script!


インストールはうまくいったっぽい。念のため動作も確認しておく。
% mysql --version
mysql Ver 14.14 Distrib 5.1.67, for apple-darwin12.2.1 (i386) using readline 6.2
% mysql.server start
Starting MySQL
. SUCCESS! 
% mysql.server stop 
Shutting down MySQL


今度は大丈夫そう。

てか、"brew tap"で"Formula"追加しないとダメなんだな。
もっと使い方を勉強せねば。。。

参考:
Homebrew 0.9
これは便利!Homebrewに追加されたtapコマンドはリポジトリを追加して簡単にフォーミュラを増やせる


気を取り直してMySQLの設定の続き。
DBの設定を行うためにmy.cnfを作成する。

"usr/local/Cellar/mysql51/5.1.67/share/mysql"配下に設定ファイルの雛形があるので、それをベースにして"my.cnf"を作る。
# support-filesフォルダでなく、share/mysqlフォルダ配下なので注意
% cd /usr/local/Cellar/mysql51/5.1.67/share/mysql
% ls | grep .cnf 
my-huge.cnf
my-innodb-heavy-4G.cnf
my-large.cnf
my-medium.cnf
my-small.cnf
% cp /usr/local/Cellar/mysql51/5.1.67/share/mysql/my-huge.cnf /usr/local/var/mysql/my.cnf


細かいの設定は今のところいいんで、まずは以下の設定を行う。
・defaultエンコードをutf-8にする
・innodbを有効にする
・バイナリログを無効化にする
% vim /usr/local/var/mysql/my.cnf
=========================================
[client]
default-charcter-set=utf8      # utf8に変更
[mysqld]
character-set-server=utf8     # utf8に変更

#log-bin=mysql-bin                 # コメント化

# innodb_・・・のコメントを外して設定を有効に。値は一旦そのままで。
innodb_data_home_dir = /usr/local/var/mysql
innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend
innodb_log_group_home_dir = /usr/local/var/mysql
innodb_buffer_pool_size = 384M
innodb_additional_mem_pool_size = 20M
innodb_log_file_size = 100M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
=========================================


設定し終わったら、もう一度MySQLを起動する。
% mysql.server start
Starting MySQL
. ERROR! Manager of pid-file quit without updating file.


あら。。。
ログを見たら以下の内容が。
% less /usr/local/var/mysql/[自分のローカルマシン名].local.err
InnoDB: Error: data file /usr/local/var/mysql/ibdata1 is of a different size
InnoDB: 640 pages (rounded down to MB)
InnoDB: than specified in the .cnf file 128000 pages!
InnoDB: Could not open or create data files.
InnoDB: If you tried to add new data files, and it failed here,
InnoDB: you should now edit innodb_data_file_path in my.cnf back
InnoDB: to what it was, and remove the new ibdata files InnoDB created
InnoDB: in this failed attempt. InnoDB only wrote those files full of
InnoDB: zeros, but did not yet use them in any way. But be careful: do not
InnoDB: remove old data files which contain your precious data!


data fileのサイズが違うって怒られている。
どうやら設定変更する前に起動テストをしてしまったのが原因みたい。
# 初回起動時に設定を元にdata fileが作られるので。

data fileを削除して、改めて起動してみる。
% rm /usr/local/var/mysql/ibdata1
% mysql.server start                       
Starting MySQL
...... SUCCESS!


今度は大丈夫。
最後にrootユーザを登録する。
% mysqladmin -u root password 'hogehoge'
% mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)


って、あら。何度か登録やパスワード変更を行うがログインできず。
mysqlのプロセス動いているし、mysql.sockもある。う〜ん。
mysql自体には問題ないと踏んで、中身を確認するために権限テーブルをスキップして起動してみる。
% mysql.server start --skip-grant-tables
% mysql -u root mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.67 Source distribution

Copyright (c) 2000, 2012, 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> select user,host from user where user='root';

+------+------------------------------+
| user | host |
+------+------------------------------+
| root | 127.0.0.1 |
| root | localhost |
+------+------------------------------+
2 rows in set (0.00 sec)


userテーブルにもrootユーザがいる。パスワードの設定に失敗した・・・?

mysql> update user set password=PASSWORD('hogehoge') where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

直接updateでパスワードを更新しようとしたら・・・データが0件?
何だろ?データが壊れているのかな。

一回userテーブルのレコードを削除して、手動で登録してみる事にする。
mysql> delete from user;
Query OK, 3 rows affected (0.00 sec)

mysql> insert into user (user, host, password) values('root', 'localhost', PASSWORD('hogehoge'));

mysql> update user set
    -> Select_priv='Y',
    -> Insert_priv='Y',
    -> Update_priv='Y',
    -> Delete_priv='Y',
    -> Create_priv='Y',
    -> Drop_priv='Y',
    -> Reload_priv='Y',
    -> Shutdown_priv='Y',
    -> Process_priv='Y',
    -> File_priv='Y',
    -> Grant_priv='Y',
    -> References_priv='Y',
    -> Index_priv='Y',
    -> Alter_priv='Y',
    -> Show_db_priv='Y',
    -> Super_priv='Y',
    -> Create_tmp_table_priv='Y',
    -> Lock_tables_priv='Y',
    -> Execute_priv='Y',
    -> Repl_slave_priv='Y',
    -> Repl_client_priv='Y',
    -> Create_view_priv='Y',
    -> Show_view_priv='Y',
    -> Create_routine_priv='Y',
    -> Alter_routine_priv='Y',
    -> Create_user_priv='Y',
    -> Event_priv='Y',
    -> Trigger_priv='Y'
    -> where User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0


登録できた。改めてもう1度ログインしてみる。
% mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.67 Source distribution

Copyright (c) 2000, 2012, 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>


やっと上手くいった。。。ほぼ1日潰れた(嘆)
よく考えたら職場でもローカルではパッケージのMySQL入れているんで、パッケージで入れれば良かった。

なお、最後のrootユーザに関する部分は友達が同じように環境作った時は発生しなかったらしいので、色々とハマっている時に変に設定をいぢったからかも。

もし同様の現象が出てしまった方は参考してくれれば。
nice!(0)  コメント(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。