CentOS Stream 9
Sponsored Link

MySQL 8.0 : インストール2022/07/06

MySQL をインストールして、データベースサーバーを構築します。
[1] MySQL をインストールして起動します。
[root@www ~]#
dnf -y install mysql-server
[root@www ~]#
vi /etc/my.cnf.d/charset.cnf
# 新規作成
# デフォルトの文字コードを設定
# 絵文字等 4 バイト長の文字を扱う場合は [utf8mb4]
character-set-server = utf8mb4

default-character-set = utf8mb4

[root@www ~]#
systemctl enable --now mysqld

[2] Firewalld を有効にしている 且つ リモートホストからも MySQL を利用する場合はサービスの許可が必要です。MySQL は [3306/TCP] を使用します。
[root@www ~]#
firewall-cmd --add-service=mysql

[root@www ~]#
firewall-cmd --runtime-to-permanent

[3] MySQL の初期設定です。
[root@www ~]#

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

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
Please set the password for root here.

# MySQL root パスワードを設定
New password:

Re-enter new password:

# 入力したパスワードで良いかの確認
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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

# 匿名ユーザーを削除するか否か
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

# root ユーザーのリモートログインを無効とするか否か
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

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

# テストデータベースを削除するか否か
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...

 - Removing privileges on test database...

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

All done!

# MySQL に root ユーザーで接続

[root@www ~]#
mysql -u root -p

Enter password:    
# 設定したパスワードで認証

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.28 Source distribution

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

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

# ユーザー情報一覧表示
mysql> select user,host from mysql.user; 
| user             | host      |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
4 rows in set (0.00 sec)

# データベース一覧表示
mysql> show databases; 
| Database           |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
4 rows in set (0.00 sec)

# テストデータベース作成
mysql> create database test_database; 
Query OK, 1 row affected (0.11 sec)

# テストデータベースにテストテーブル作成
mysql> create table test_database.test_table (id int, name varchar(50), address varchar(50), primary key (id)); 
Query OK, 0 rows affected (0.42 sec)

# テストテーブルにデータ投入
mysql> insert into test_database.test_table(id, name, address) values("001", "CentOS", "Hiroshima"); 
Query OK, 1 row affected (0.04 sec)

# テストテーブル表示
mysql> select * from test_database.test_table; 
| id | name   | address   |
|  1 | CentOS | Hiroshima |
1 row in set (0.00 sec)

# テストデータベース削除
mysql> drop database test_database; 
Query OK, 1 row affected (0.27 sec)

mysql> exit
[4] MySQL の既存のデータを全て消去して初期化する場合は、以下のように実行します。
[root@www ~]#
systemctl stop mysqld

[root@www ~]#
rm -rf /var/lib/mysql/*

[root@www ~]#
mysqld --initialize --user=mysql

[root@www ~]#
systemctl start mysqld
# root パスワードは一時割り当てされるためログで確認

[root@www ~]#
grep password /var/log/mysql/mysqld.log

2022-07-06T06:58:13.839600Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Password