CentOS7.9 にMariaDB10を導入する

## 初めに
Wordpress導入にあたっての前提の環境作成の一部です
[CentOS7.4 にWordpress5.8を導入する](https://qiita.com/ymachida/private/062b8ab41adcfb7413c6)

## 環境
* CentOS 7.9
* MariaDB 10.6

## MariaDb導入

CentOS7のリポジトリだとバージョンが古いのでリポジトリ追加

```
# curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash
```

MariaDBインストール

```
# yum install MariaDB-server MariaDB-client MariaDB-devel
```
MariaDBの起動

```
# systemctl start mariadb
```

サービスの起動状態確認

```
# systemctl status mariadb
mariadb.service - MariaDB 10.6.5 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/mariadb.service.d
           `-migrated-from-my.cnf-settings.conf
   Active: active (running) since Tue 2021-11-23 13:27:12 JST; 7s ago
     Docs: man:mariadbd(8)
           https://mariadb.com/kb/en/library/systemd/
  Process: 1631 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 1602 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1 (code=exited, status=0/SUCCESS)
  Process: 1598 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
 Main PID: 1612 (mariadbd)
   Status: "Taking your SQL requests now..."
   CGroup: /system.slice/mariadb.service
           `-1612 /usr/sbin/mariadbd

Nov 23 13:27:12 hogesv mariadbd[1612]: 2021-11-23 13:27:12 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
Nov 23 13:27:12 hogesv  mariadbd[1612]: 2021-11-23 13:27:12 0 [Note] InnoDB: 10.6.5 started; log sequence number 41349; transaction id 14
Nov 23 13:27:12 hogesv  mariadbd[1612]: 2021-11-23 13:27:12 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
Nov 23 13:27:12 hogesv  mariadbd[1612]: 2021-11-23 13:27:12 0 [Note] Plugin 'FEEDBACK' is disabled.
Nov 23 13:27:12 hogesv  mariadbd[1612]: 2021-11-23 13:27:12 0 [Note] InnoDB: Buffer pool(s) load completed at 211123 13:27:12
Nov 23 13:27:12 hogesv  mariadbd[1612]: 2021-11-23 13:27:12 0 [Note] Server socket created on IP: '0.0.0.0'.
Nov 23 13:27:12 hogesv  mariadbd[1612]: 2021-11-23 13:27:12 0 [Note] Server socket created on IP: '::'.
Nov 23 13:27:12 hogesv  mariadbd[1612]: 2021-11-23 13:27:12 0 [Note] /usr/sbin/mariadbd: ready for connections.
Nov 23 13:27:12 hogesv  mariadbd[1612]: Version: '10.6.5-MariaDB'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MariaDB Server
Nov 23 13:27:12 hogesv  systemd[1]: Started MariaDB 10.6.5 database server.
```

サーバ起動時にサービスが自動開始するように設定

```
# systemctl enable mariadb
```

DBの初期設定を実施

```
# mariadb-secure-installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] Y
Enabled successfully!
Reloading privilege tables..
 ... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? [Y/n] Y
 ... Success!

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? [Y/n] Y
 ... Success!

By default, MariaDB 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? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
```
DB文字セットの確認　文字セット(charset)のutf8mb4化

```
# mysql -u root -p

MariaDB [(none)]> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8mb3                    |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.002 sec)

MariaDB [(none)]> show variables like 'coll%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.001 sec)
```
設定ファイルのバックアップ(念のため。。。)

```
# cp /etc/my.cnf.d/server.cnf /etc/my.cnf.d/server.cnf.bak
```
文字セット(charset)を「utf8mb4」へ変更

```
# vi /etc/my.cnf.d/server.cnf

変更・追加(すでにある場合は変更)
[mariadb]
character-set-server = utf8mb4
[client-mariadb]
default-character-set = utf8mb4
```

サービスを再起動し変更内容を適用

```
# systemctl restart mariadb
```
変更結果確認

```
# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.6.5-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8mb3                    |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.001 sec)

MariaDB [(none)]> show variables like 'coll%';
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+
3 rows in set (0.001 sec)
```
DB作成

```
> create database hoge_db default character set utf8mb4;
```
ユーザ作成、作成DBに権限付与

```
> GRANT ALL ON hoge_db.* TO hoge@localhost IDENTIFIED BY 'hogehoge';
```
これで完了です！！