その他

MySQLのインストールでエラー [ERROR] –initialize specified but the data directory has files in it. Aborting.

新しいMacにしてMySQLをインストールしようとしたのですが、前にインストールしたデータが残っていたみたいで思いの外ハマりまくったので、備忘録として残しておきたいと思います。

解決法

homebrewでMySQLをインストールして、mysql.server startしても下記エラーでハマっていました。いろんな記事を試してもうまく行かず、MySQLをインストールし直すことにしました。

$ mysql.server start
Starting MySQL
..... ERROR! The server quit without updating PID file (/opt/homebrew/var/mysql/MacBook-Pro.local.pid).

brew uninstallするだけではだめで、そこら中に点在するMySQL関連のファイルを削除しないといけなかったです。この記事の通りに削除して再度インストールすることで僕の場合はOKでした。

https://qiita.com/akiko-pusu/items/aef52b723da2cb5dc596

また、ずっと下記エラーが出ていてインストールできませんでした。

2022-01-02T06:33:45.154465Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-01-02T06:33:45.155700Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2022-01-02T06:33:45.155714Z 0 [ERROR] Aborting

Warning: The post-install step did not complete successfully
You can try again using:
  brew postinstall mysql@5.7

これは「[ERROR] –initialize specified but the data directory has files in it. Aborting.」のキーワードでググって、下記記事を参考に/opt/homebrew/var/mysql/以下のファイルを削除して、brew postinstall mysql@5.7することでインストールが再度まで実行できました。

$ cd /opt/homebrew/var
$ rm -fr mysql
$ brew postinstall mysql@5.7
==> Postinstalling mysql@5.7
==> /opt/homebrew/Cellar/mysql@5.7/5.7.36/bin/mysqld --initialize-insecure --user=sakaishun --basedir=/opt/homebrew/Cellar/mysql@5.7/5.7.36 --datadir=/opt/homebrew/var/mysql --tmpdir=/tmp
$ mysql --version
mysql  Ver 14.14 Distrib 5.7.36, for osx10.17 (x86_64) using  EditLine wrapper
$ mysql.server start
Starting MySQL
 SUCCESS! 

また、直接関係はないかもですが、docker-composeを立ち上げっぱなしにしていて3306のポートが空いたままになっていたのも原因の一つかもしれないです。

文章にしてみるとこれだけですが、試行錯誤ですごく時間かかりました。MySQLもちゃんと勉強しないとです。

ABOUT ME
酒井 駿
名古屋工業大学大学院卒業後、豊田合成(株)で品質管理を経験し、その後スタートアップ・マネーフォワードを経て、2024年11月に株式会社EGGHEAD創業。 製造業とエンジニアリング、両方の現場の知見を活かし、製造業における生成AIを活用した業務改善やシステム開発を支援します。