【MySQL】ローカルDB環境構築

# 手順

## 1. 準備
```terminal
$ brew update
$ brew search mysql
```
`mysql@5.7`がインストール出来ることを確認


## 2. MySQLをインストール
```terminal
$ brew install mysql@5.7
```

## 3. パスを通す
```terminal
$ echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc
```

## 4. 反映させる
```terminal
$ source .zshrc
```

## 5. 確認
```terminal
$ mysql --version
```

```terminal:例
(base) sample@SampleMBP ~ % mysql --version
mysql  Ver 14.14 Distrib 5.7.39, for osx10.17 (x86_64) using  EditLine wrapper
```

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

```terminal:例
mysql> update mysql.user set password=password('任意のパスワードを入力') where user = 'root';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 0
 
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
```

## 7. 設定を入力

```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!
```

## 8. ログイン
```terminal
$ mysql -u root -p
```

```terminal
sample@SampleMBP ~ % mysql --user=root --password
 Enter password: 
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 12
 Server version: 5.6.51 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.
```

- ログインした状態のまま`status`コマンドを実行し確認

```terminal
mysql> status
--------------
mysql  Ver 14.14 Distrib 5.7.39, for osx10.17 (x86_64) using  EditLine wrapper

Connection id:		6
Current database:
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.6.51 Homebrew
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8
Db     characterset:	utf8
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/tmp/mysql.sock
Uptime:			10 min 35 sec

Threads: 2  Questions: 16  Slow queries: 0  Opens: 67  Flush tables: 1  Open tables: 60  Queries per second avg: 0.025
--------------
```

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

# MySQL起動停止コマンド

## 起動
```terminal
$ mysql.server start
```

## 停止
```terminal
$ mysql.server stop
```

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

---
### MySQLをGUIアプリケーションで管理できるようにしたい場合は以下を参照

https://qiita.com/rexid/items/a1e0e1012bd5664fdbf0