LoginSignup
2
2

More than 3 years have passed since last update.

MySQLをHomebrewでインストールして初期設定まで行ったけど、MySQLがどうしても起動しなかったときの対応

Posted at

この記事は

MySQLの環境構築でけっこう手こずったので、手順とエラー対応をメモ。
トライしたことを書き出すことで、自分の中で整理したい。

手順

HomebrewでMySQLをインストール

ターミナルにて、Homebrewを最新にアップデートしてMySQLをインストールします。
インストールが成功すると下記のように出力されます。
(usernameにそれぞれのPC名が入ります)
(余談だけど、初心者だとこういう「自分と出力がちょっと違う!なぜ!?」ってなりがちな気がする。hogehogeとか最初は「へぇ???」だったw)

#Homebrewのアップデート
$ brew update

#MySQLのインストール
$ brew install mysql

Already downloaded: /Users/username/Library/Caches/Homebrew/downloads/*****--mysql-8.0.19.catalina.bottle.2.tar.gz
==> Pouring mysql-8.0.19.catalina.bottle.2.tar.gz
==> /usr/local/Cellar/mysql/8.0.19/bin/mysqld --initialize-insecure --user=username --basedir=/usr/local/Cellar/mysql/8.0.19 --datadir=/usr/local/var/mysql --tmpdir=/tmp
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mysql/8.0.19: 286 files, 289.2MB

ここで上記出力を見ると、

We've installed your MySQL database without a root password

とあります。
HomebrewでMySQLをインストールするとrootパスワードは設定されないとのこと。
rootパスワード設定は次のステップで行います。

  • インストールされていれば、下記でバージョンが確認できます。
#バージョン確認
$ mysql --version

mysql  Ver 8.0.19 for osx10.15 on x86_64 (Homebrew)

MySQLの初期設定

rootパスワードが設定されていない等の問題があるため、セキュリティ設定を行います。(使えるのは使えるが)
いくつかYse or No を聞かれますが、特別なことがない限りYesでOKです。

ちなみに、セキュリティレベルを問われるところでの選択肢の内容は下記のとおりです。
勉強用に使うのであれば、LOWでOKです。

 ・LOW:8文字以上
 ・MEDIUM:8文字以上 + 数字・アルファベットの大文字と小文字・特殊文字を含む
 ・STRONG:8文字以上 + 数字・アルファベットの大文字と小文字・特殊文字を含む + 辞書ファイルでのチェック

#MySQLのセキュリティ設定
$ mysql_secure_installation

 Securing the MySQL server deployment.

Enter password for user root: 初期パスワードを入力する

The existing password for the user account root has expired. Please set a new password.

New password: 新しいパスワードを入力する

Re-enter new password: 再度同じ新しいパスワードを入力する

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: ポリシーに沿った新しいパスワードを入力

Re-enter new password: 再度新しいパスワードを入力する

Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user, a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y Success.

- Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

MySQLの起動

以下のコマンドで起動されます。

"MySQLの起動
$ mysql.server start

Starting MySQL
 SUCCESS! 

発生したエラーと対応

けっこう手こずったのでまとめておきます。
まとめてるけど、自分の場合のエラー原因はまだわかっていません。

MySQLが起動しない

  • きっかけは未だにわからないけれど、起動しなくなった。
$ mysql.server start

Starting MySQL
. ERROR! The server quit without updating PID file (/usr/local/var/mysql/マック名local.pid).

【トライ1】PIDファイルの作成

とりあえずPIDファイルを確認。
すると以下のようなPIDファイルが存在しないとのことだったので、PIDファイルの作成を実行。
しかしながら、起動できず。

# PIDファイルについて確認
$ ls -la /usr/local/var/mysql/XXXXXXXX.local.pid
# PIDがファイルが存在しないとのこと
ls: /usr/local/var/mysql/XXXXXXXX.local.pid: No such file or directory

#PIDファイルの作成
$ touch /usr/local/var/mysql/****.local.pid

【トライ2】権限をなおす

アクセス権限がなくてえらーが出るとの情報があったので、以下をトライ。
それでも起動できず。

$ sudo chown -R _mysql:_mysql /usr/local/var/mysql

【トライ3】MySQLの再インストール

もうこれしかないかなと思って、アンインストール→再インストール
調べると/usr/local/var/mysqlは消えないみたいなので、そちらは手動で削除。
が、これでも起動失敗。。。

#手動での削除
$ $ sudo rm -rf /usr/local/var/mysql
#MySQLのアンインストール
$ brew uninstall mysql
#MySQLの再インストール
$ brew install mysql

【トライ4】PC再起動→MySQL再起動

結局これで起動できた。
わからないままではちょっと、、なので原因調べます。

参考記事

以下の記事を参考にしました。ありがとうございます。
- https://reasonable-code.com/mysql-install/
- https://qiita.com/hkusu/items/cda3e8461e7a46ecf25d#%E6%89%8B%E9%A0%86
- https://teratail.com/questions/19778
- https://qiita.com/akizora001/items/4d63c75548520a70b845
- https://qiita.com/mogetarou/items/e34ca51d3756d55d7800

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2