0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MySQL8から5.7にバージョンダウンしたらハマり散らかした話

0
Posted at

MySQLを8系から5系にバージョンダウンしたら、沼にハマりました。。。
どなたかの役に立てたら…という思いで投稿します。

環境

MacOS
MySQLはhomebrewからインストール

実行していきます

MySQL8のアンインストール

# @8.0でバージョンを指定(しなくても動作するかもです)
$ brew uninstall mysql@8.0

MySQL5.7のインストール

# どのパッケージ(バージョン)が利用可能か確認する。5.7はインストール可能。
$ brew search mysql

# バージョン指定でインストール(自身の環境はバージョン指定しないとエラーになりました)
$ brew install mysql@5.7

MySQLを起動

$ mysql.server start

第一の沼

なぜかアンインストールしたMySQL8が起動している

# mysqlにログイン
$ mysql -u root

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
# バージョンが8系になってる!
Server version: 8.0.11 Homebrew ←ココ

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

え、なぜ?どうゆうこと?
一旦exitで抜けて、バージョンを確認

$ mysql --version
mysql  Ver 14.14 Distrib 5.7.40, for osx10.17 (x86_64) using  EditLine wrapper

ちゃんと5.7がインストールされてる。。

第一の沼突破

MySQL8のプロセスが残っていることが原因。
参考:https://zenn.dev/ogakuzuko/articles/1d9d20fb5cbef8

# プロセスを確認
$ ps aux | grep mysql

# MySQL8のプロセスをkillする(grep以外のmysqlプロセス全てkillでOK)
$ sudo kill -9 〇〇〇〇 ←PID番号を入力

# MySQL起動
$ service.start mysql

第二の沼

MySQLが起動できない…
こいつを解決するのに本当に本当に苦労しました。

# MySQL起動
$ service.start mysql

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

MySQL嫌い…
MySQL5.7は元々このエラーの状態でした。さっきまでMySQLを起動できていたのは、MySQL8のプロセスのおかげのようです。

解決までの手順

/opt/homebrew/var/mysql/〇〇noMacBook-Pro.local.pidのあるディレクトリに移動

$ cd /opt/homebrew/var/mysql

# 該当のファイルがないのでファイルを作成
$ touch 〇〇noMacBook-Pro.local.pid

$ service.start mysql
# ↑再度MySQL起動するも、うまくいかないし作成したファイルが消えてる

$ cd /opt/homebrew/var/mysql
〇〇noMacBook-Pro.local.err #←?errorファイルがある?

エラーファイルを確認

# tailコマンドで末尾2000行を抽出
$ tail -2000 〇〇noMacBook-Pro.local.err
...一部省略
.
.
2023-01-11T09:53:24.585121Z 0 [ERROR] Can't start server : Bind on unix socket: Address already in use
2023-01-11T09:53:24.585130Z 0 [ERROR] Do you already have another mysqld server running on socket: /tmp/mysql.sock ?
2023-01-11T09:53:24.585142Z 0 [ERROR] Aborting

怪しい記述発見!
このエラーは、他のMySQLサーバー動いてないですか?というもので、/tmp/mysql.sockが存在していることにより起きているエラーでした。

ソケットとはプロセスやネットワーク間の通信機構です。MySQLはこのソケットを通じてサーバーとクライアントが接続されます。

/tmp/mysql.sockとは、UNIXドメインソケット(ファイルシステムソケット)というUNIX特有のソケット。ローカルシステム内の通信を行う場合に用いられます。
ソケットは通常、ポート番号を指定し利用するのが一般的ですが、MySQLではこのファイルを使って通信をおこなっているようです。

参考:https://urashita.com/archives/32590#:~:text=ソケットについてはポート番号,場合に用いられます。

第二の沼突破&解決

参考:https://qiita.com/PoohSunny/items/4df890dde4879c2cd29b

# ファイルを削除
$ sudo rm /tmp/mysql.sock

# MySQL起動→成功!
$ mysql.server start 
Starting MySQL
 SUCCESS!

# MySQLにログイン
$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
# ちゃんと5.7になってる!
Server version: 5.7.40 Homebrew

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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.

無事起動&ログインできました😭
このエラーで丸一日溶けました。。。

教訓

  • ログを正確に読める者がプログラミングを制す
  • Linuxのコマンドを使いこなせないと時間が余計にかかる
  • MySQLはくそ最高
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?