White Box技術部

WEB開発のあれこれ(と何か)

MacPortsでMySQL5.7をインストールする

homebrewが猛威を振っていますが、MacPortsおじさんの私は元気です。

MySQL 5.7をインストールする

仮想環境にはMySQL 5.7を入れたりしていましたが、そういえばMacには入れてなかったのでインストール作業をしてみました。

環境情報

とりあえずport install

まずはsudo port selfupdateして、portを最新にしつつ(これで2.3.4になりました)、 mysqlの5.7がリポジトリに存在することをport search mysql | grep ^mysqlで探して、 おもむろに以下のインストールコマンドを叩きます。

$ sudo port install mysql57

すると以下のようなエラーが発生しました。

--->  Configuring mysql57
Error: The file /opt/local/lib/libwrap.dylib does not exist, though it was
Error: expected to have been provided by one of mysql57's dependencies. Try
Error: rebuilding the port that should have provided that file by running
Error: 
Error:     sudo port -n upgrade --force <portname>
Error: 
Error: org.macports.configure for port mysql57 returned: missing required file
To report a bug, follow the instructions in the guide:
    http://guide.macports.org/#project.tickets
Error: Processing of port mysql57 failed

depsチェック

mysql57をインストールするときに期待する依存ライブラリないというようなことを言われているので、 おもむろに以下のコマンドを叩いて依存ライブラリを確認します。

$ port deps mysql57 

すると以下のような結果になりました。

Full Name: mysql57 @5.7.11_1
Build Dependencies:   cmake
Library Dependencies: zlib
Runtime Dependencies: mysql_select

port installedで確認すると、どれもインストールされているので少し調べ、 その調べた感じだとcmakeがちゃんと入ってるか?みたいなことが書かれていたのでチェックしましたが、 ちゃんと現時点の最新のcmake @3.5.1_0が入っていました。

libwrap.dylibとは?

次にエラーになっているこのファイルが何なのか調べたら、 TCP Wrapperだったみたいなので、以下を実行してインストールし、 その後mysql57のインストールを実行したところ、いい感じに進みました。

$ sudo port install tcp_wrappers

mysql57-serverのインストール

とはいえ、以下のようなメッセージが出ており、mysql57-serverのインストールが別途必要なことをここで知ります。

--->  Computing dependencies for mysql57
--->  Configuring mysql57
--->  Building mysql57
--->  Staging mysql57 into destroot
--->  Installing mysql57 @5.7.11_1
The mysql57 client has been installed.
To install the mysql57 server, install the mysql57-server port.
--->  Activating mysql57 @5.7.11_1

On activation if no /opt/local/etc/mysql57/my.cnf file exists one
will be created which loads
/opt/local/etc/mysql57/macports-default.cnf.

If a /opt/local/etc/mysql57/my.cnf file exists MacPorts does not
touch it and any changes you make to /opt/local/etc/mysql57/my.cnf
will be preserved (e.g., during port upgrades, deactivations or
activations). /opt/local/etc/mysql57/my.cnf is a good place to
customize your mysql57 installation.

Any changes made to /opt/local/etc/mysql57/macports-default.cnf
will be lost during port upgrades, deactivations or activations so you
are advised to not make changes here. Currently
/opt/local/etc/mysql57/macports-default.cnf contains only one
directive; to disable networking. With disabled networking it is
possible to install and have running all the MacPorts mysql ports
simultaneously.

ちなみにこの時点のmy.cnfはmacports-default.cnfを見るという設定しか無いですし、 macports-default.cnfはコメント除くと以下だけです。

[mysqld]
skip-networking

実行結果

インストールコマンドを実行すると以下のようになります。

$ sudo port install mysql57-server
--->  Computing dependencies for mysql57-server
--->  Fetching archive for mysql57-server
--->  Attempting to fetch mysql57-server-5.7.11_0.darwin_14.noarch.tbz2 from https://packages.macports.org/mysql57-server
--->  Attempting to fetch mysql57-server-5.7.11_0.darwin_14.noarch.tbz2.rmd160 from https://packages.macports.org/mysql57-server
--->  Installing mysql57-server @5.7.11_0
--->  Activating mysql57-server @5.7.11_0

If this is a new install you might want to run:

$ sudo /opt/local/lib/mysql57/bin/mysqld --initialize --user=_mysql
$ /opt/local/lib/mysql57/bin/mysql_secure_installation

--->  Cleaning mysql57-server
--->  Updating database of binaries
--->  Scanning binaries for linking errors
--->  No broken files found.

これでインストールはおしまいですね。
あとは上のログにもあるようにmysqldを立ち上げて、Initializeコマンドを叩けばOKです。

でも今日はもうやりません。

後始末

これらの流れでmysqlコマンドにはPATHが通らなかったので、適当に/opt/local/lib/mysql57/binをPATHに通しておきましょう。

bashなら.bashrcに以下を追記してsource ~/.bashrcですね。

  • ~/.bashrc
export PATH=$PATH:/opt/local/lib/mysql57/bin

ついでにMacPortsも綺麗にする

MySQLのインストール前にport outdatedで更新が必要なパッケージがどのくらいが見て、 sudo port upgrade outdatedをしてからのほうが、依存性で怒られる可能性が減っていいと思いますが、 upgradeの際に-uオプションを付けていないと、過去バージョンのパッケージも残っている場合があるので、 port installedで確認して特に問題なければ(過去バージョンが必要なければ)、 以下のコマンドでささっと使ってないパッケージを削除してしまいましょう。

$ sudo port uninstall inactive

お疲れ様でした。

結論

MySQLなんてそうそう簡単にバージョン上げたりしないので、MySQLの公式サイトからdmgファイルを落としてきて入れるのが一番いいのではないでしょうか。

MySQL :: Download MySQL Community Server

追記

試しにdmgでインストールしてみたところ、 PATHは通ってないし、Initializeコマンドの説明も出てこなかったので、思っていたのとは違いました。 もう少しサポートしてくれるのかなと期待していたので。

ちなみにインストールが終了するとダイアログが表示され、デフォルトパスワードが設定されたことがわかります。

2016-04-01T05:55:51.394996Z 1 [Note] A temporary password is generated for root@localhost: XXXXXXXXX(←パスワード)

If you lose this password, please consult the section How to Reset the Root Password in the MySQL reference manual.

インストール先などの情報は以下の通りで、rootのファイルになっています。

  • インストール先:/usr/local/mysql(/usr/local/mysql-5.7.11-osx10.9-x86_64のエイリアス
  • my.cnfサンプル:/usr/local/mysql/support-files/my-default.cnf

my-default.cnfはMacPorts版より詳しい説明がある気がします。

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES