MySQL5.7からMySQL8へ移行する


# Todo
[MySQL@5.7をインストール](https://qiita.com/rexid/items/c3ddc93a871a263487cf)している為、バージョンを8にする

# 事前準備

下記コマンドでプロセスを切る
```terminal
$ ps aux | grep mysqld
$ kill -9 表示されたPID
```

# 手順

## 1. 古いMySQLをアンインストール
```terminal
$ brew uninstall mysql
$ brew uninstall mysql@5.7
```

## 2. MySQLをインストールし再起動
```terminal
$ brew install mysql
$ ps aux | grep mysqld # 一応確認
$ mysql.server restart
```

```terminal:例
(base) sample@SampleMBP ~ % mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
(base) sample@SampleMBP ~ % sudo mysql.server start
Starting MySQL
.Logging to '/usr/local/var/mysql/SampleMBP.err'.
 ERROR! The server quit without updating PID file (/usr/local/var/mysql/SampleMBP.pid).


(base) sample@SampleMBP ~ % brew uninstall mysql
Uninstalling /usr/local/Cellar/mysql/8.0.30_1... (312 files, 296.2MB)
(base) sample@SampleMBP ~ % brew uninstall mysql@5.7
Uninstalling /usr/local/Cellar/mysql@5.7/5.7.39... (320 files, 233.6MB)
(base) sample@SampleMBP ~ % brew install mysql
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/manifests/8.0.30_1-1
Already downloaded: /Users/sample/Library/Caches/Homebrew/downloads/ef6fd578319bdb4275333285a66de1a37d953b18d0233d48b9775c8d8203e473--mysql-8.0.30_1-1.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/blobs/sha256:7387b47a62ea6f7da931bcbe1e46045649e3c90a3d6a0376516536d586e99471
Already downloaded: /Users/sample/Library/Caches/Homebrew/downloads/bb45378a1ad0c55eae9b2abe7b0f26f9c96192c3aed0e9aca52822a16e262303--mysql--8.0.30_1.monterey.bottle.1.tar.gz
==> Pouring mysql--8.0.30_1.monterey.bottle.1.tar.gz
==> 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 -u root

To restart mysql after an upgrade:
  brew services restart mysql
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/mysql/bin/mysqld_safe --datadir=/usr/local/var/mysql
==> Summary
🍺  /usr/local/Cellar/mysql/8.0.30_1: 312 files, 296.2MB
==> Running `brew cleanup mysql`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
(base) sample@SampleMBP ~ % ps aux | grep mysqld
sample              37571   0.0  0.0 408637584   1792 s000  S+    1:02AM   0:00.00 grep mysqld
(base) sample@SampleMBP ~ % mysql.server restart
Shutting down MySQL
. SUCCESS!
Starting MySQL
. SUCCESS!
```

## 3. パスワードの設定
```terminal
$ mysql -u root
```

```terminal
mysql> update mysql.user set password=password('任意のパスワード') where user = 'root';
mysql> USE mysql;
mysql> ALTER USER 'root'@'localhost' identified BY '任意のパスワード';
```

```terminal:例
(base) sample@SampleMBP ~ % mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.30 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.

mysql> update mysql.user set password=password('任意のパスワード') where user = 'root';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('任意のパスワード') where user = 'root'' at line 1
mysql> USE mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> ALTER USER 'root'@'localhost' identified BY '任意のパスワード';
Query OK, 0 rows affected (0.01 sec)
```

## 4. MySQLの設定

```terminal
$ mysql_secure_installation
```

:::note warn
設定はご自身で判断してください
:::

### 例

①パスワードの強度を設定：設定しないので`ENTER`キーを押下
※今回はローカル環境でのみ使う為

:::note alert
本番サーバ環境など、セキュリティがより求められる場面では`y`を入力し、MEDIUMやSTRONGを選択する
:::

```terminal
VALIDATE PASSWORD COMPONENT 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 component?

Press y|Y for Yes, any other key for No:
Using existing password for root.
```

②rootユーザのパスワードを変更するかどうか：変更しないので`ENTER`

```terminal
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
```

③匿名ユーザは必要としないので削除する：`y`
```terminal
 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
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.
```

④リモートからのrootユーザでのログインを禁止する：`y`

```terminal
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.
```

⑤デフォルトで作成されているtestデータベースの削除：`y`
```terminal
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
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.
```

⑥これまでの設定を今すぐ更新するかどうか：`y`
```terminal
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!
```


## 5. 再起動
```terminal
$ mysql.server restart
```

## 6. 確認
```terminal
$ mysql -u root -p
```

```terminal
mysql> status
```

```terminal:例
(base) sample@samplenoMBP ~ % mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.30 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.
```

```terminal:例
mysql> status
--------------
mysql  Ver 8.0.30 for macos12.6 on x86_64 (Homebrew)

Connection id:		8
Current database:
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		8.0.30 Homebrew
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	utf8mb4
Conn.  characterset:	utf8mb4
UNIX socket:		/tmp/mysql.sock
Binary data as:		Hexadecimal
Uptime:			56 sec

Threads: 2  Questions: 5  Slow queries: 0  Opens: 117  Flush tables: 3  Open tables: 38  Queries per second avg: 0.089
--------------
```

抜ける時は`exit`
```terminal
mysql> exit
```

:::note
完了
:::

# MySQLをGUIアプリケーションで管理したい場合
https://qiita.com/rexid/items/a1e0e1012bd5664fdbf0