ubuntu(virtual box)にMySQLをインストールしてみた

参照サイト
https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04-ja
https://qiita.com/houtarou/items/a44ce783d09201fc28f5
https://charlie1012.hatenablog.jp/entry/2015/09/05/170000
https://www.javadrive.jp/mysql/insert/index1.html


# インストール・設定
## MySQLをインストール
```terminal:terminal
sudo apt update
sudo apt install mysql-server mysql-client
```

# セキュリティスクリプトを実行する
```terminal:terminal
sudo mysql_secure_installation
```
```terminal:mysql_secure_installation
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

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: y
```
- パスワードの強度チェックをするか選択

```terminal:mysql_secure_installation
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
```
- パスワード強度を選択する

```terminal:mysql_secure_installation
Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.

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.
```
- 匿名ユーザーを削除するか選択する

```terminal:mysql_secure_installation
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.
```
- rootがリモートからログイン不可にするか選択する

```terminal:mysql_secure_installation
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.
```
- デフォルトでインストールされているテスト用DBとその権限を削除するか選択

```terminal:mysql_secure_installation
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を設定する
### rootでログインする
```terminal:terminal
sudo mysql -u root
```
### rootのパスワードを設定する
```terminal:terminal
ALTER USER 'root'@'localhost' IDENTIFIED BY '1';
```


### データベースを作成する
```terminal:terminal
CREATE DATABASE DB1;
```

### ユーザーを作成・権限付与する
```terminal:terminal
CREATE USER 'user'@'localhost' IDENTIFIED BY '1';
GRANT ALL ON DB1.* TO 'user'@'localhost';
```

### rootをログアウトする
```terminal:terminal
exit 
```

## テーブルを作成する
### 作成したユーザーでログインする
```terminal:terminal
sudo mysql -u user -p DB1;
```


### テーブルを作成する
```terminal:terminal
CREATE TABLE `addressBook` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `age` int(3) NOT NULL,
  `address` varchar(255),
  PRIMARY KEY (`id`)
);
```


### テーブルを確認する
```terminal:terminal
SHOW TABLES;
+---------------+
| Tables_in_DB1 |
+---------------+
| addressBook   |
+---------------+
1 row in set (0.00 sec)


DESC addressBook;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int          | NO   | PRI | NULL    | auto_increment |
| name    | varchar(255) | NO   |     | NULL    |                |
| age     | int          | NO   |     | NULL    |                |
| address | varchar(255) | NO   |     | 　　    |                |
+---------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
```

## テーブルにデータを挿入する
### INSERTを実行する
```terminal:terminal
INSERT INTO addressBook 
(name,age,address)
VALUES  
('towamz', 20, 'Tokyo');
```

### SELECTを実行する
```terminal:terminal
SELECT * FROM addressBook;
+----+--------+-----+---------+
| id | name   | age | address |
+----+--------+-----+---------+
|  1 | towamz |  20 | Tokyo   |
+----+--------+-----+---------+
1 row in set (0.00 sec)
```