MySQL

## install
https://prog-8.com/docs/mysql-env

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
```
mysql@8.0 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.


To start mysql@8.0 now and restart at login:
```
brew services start mysql@8.0
```
---
```
// mysqlがインストールされているかどうかを確認
ryamashita@ryamashita ~ % brew list| grep mysql                     
mysql@8.0

// PATHを通す
ryamashita@ryamashita ~ % echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc

// 反映させる
ryamashita@ryamashita ~ % source ~/.zshrc

// version確認
ryamashita@ryamashita ~ % mysql --version                                
mysql  Ver 8.0.35 for macos14.0 on arm64 (Homebrew)
```

## 起動
```
brew services start mysql@8.0
```

## password
**mysql_secure_installation**

```
ryamashita@ryamashita ~ % mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

// コマンドを実行すると最初にVALIDATE PASSWORD PLUGINという強固なパスワード設定を助けるプラグインを使用するかどうかを質問されます。
// 今回はローカル環境のみでの使用を想定しているので、何も入力せずEnterキーを押して次に進みます。
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: 
Please set the password for root here.

New password: DB-pass99
Re-enter new password: DB-pass99
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) : 

 ... skipping.


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) : 

 ... skipping.
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) : 

 ... skipping.
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) : 

 ... skipping.
All done! 
ryamashita@ryamashita ~ % 

```

## Login
mysql --user=root --password
```
ryamashita@ryamashita ~ % mysql --user=root --password
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.35 Homebrew

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

```

https://prog-8.com/docs/mysql-database-setup

## DB一覧表示
SHOW databases;
```
mysql> SHOW databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> 
```

## DB作成
CREATE DATABASE DB名;
```
// まず使用するDBを指定する
mysql> USE practice;
Database changed

// 作成
mysql> CREATE DATABASE practice;
Query OK, 1 row affected (0.00 sec)

mysql> 
```

## Table作成
```
mysql> CREATE TABLE users (id INT AUTO_INCREMENT, name TEXT, PRIMARY KEY (id));
Query OK, 0 rows affected (0.01 sec)

mysql> 
```
## Table一覧表示
SHOW tables;
```
mysql> SHOW tables;
+--------------------+
| Tables_in_practice |
+--------------------+
| users              |
+--------------------+
1 row in set (0.00 sec)

mysql> 
```

## Table定義を表示
```
mysql> DESCRIBE users;
+-------+------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra          |
+-------+------+------+-----+---------+----------------+
| id    | int  | NO   | PRI | NULL    | auto_increment |
| name  | text | YES  |     | NULL    |                |
+-------+------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

mysql> 
```

## Table削除
```
mysql> DROP TABLE Users;
Query OK, 0 rows affected (0.03 sec)

mysql> 
```

## Logout
exit;

## MySQLの停止
brew services stop mysql@8.0