RockyLinux9におけるMySQLのインストール

# MySQLのインストール手順

## 背景

MySQLはよく利用するソフトウェアですが、初期設定を行う際に毎回調べるのが手間となるので、備忘録のためにまとめておきます。
最近では、個人の開発環境であればdockerなどを利用しますが、Linux環境に直接インストールすることもあるため、数少ないタイミングで記録を残しておこうと思います。

## 前提条件

* Host: Windows11
* Guest: RockyLinux9
    * MySQL8.4

## 作業の流れ

### インストール

* リポジトリの登録
```
$ curl -L -O https://dev.mysql.com/get/mysql84-community-release-el9-1.noarch.rpm

$ sudo yum localinstall mysql84-community-release-el9-1.noarch.rpm
Last metadata expiration check: 0:18:29 ago on Fri Oct 18 22:04:30 2024.
Dependencies resolved.
========================================================================================================================
 Package                                  Architecture          Version               Repository                   Size
========================================================================================================================
Installing:
 mysql84-community-release                noarch                el9-1                 @commandline                 13 k

Transaction Summary
========================================================================================================================
Install  1 Package

Total size: 13 k
Installed size: 14 k
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                1/1
  Installing       : mysql84-community-release-el9-1.noarch                                                         1/1
  Verifying        : mysql84-community-release-el9-1.noarch                                                         1/1

Installed:
  mysql84-community-release-el9-1.noarch

Complete!
```

* パッケージのインストール
```
$ sudo yum install -y mysql-community-server
Last metadata expiration check: 0:00:21 ago on Fri Oct 18 22:25:40 2024.
Dependencies resolved.
========================================================================================================================
 Package                              Architecture Version                          Repository                     Size
========================================================================================================================
Installing:
 mysql-community-server               x86_64       8.4.3-1.el9                      mysql-8.4-lts-community        50 M
Installing dependencies:
 libtirpc                             x86_64       1.3.3-8.el9_4                    baseos                         93 k
 mysql-community-client               x86_64       8.4.3-1.el9                      mysql-8.4-lts-community       3.1 M
 mysql-community-client-plugins       x86_64       8.4.3-1.el9                      mysql-8.4-lts-community       1.5 M
 mysql-community-common               x86_64       8.4.3-1.el9                      mysql-8.4-lts-community       575 k
 mysql-community-icu-data-files       x86_64       8.4.3-1.el9                      mysql-8.4-lts-community       2.3 M
 mysql-community-libs                 x86_64       8.4.3-1.el9                      mysql-8.4-lts-community       1.5 M
 net-tools                            x86_64       2.0-0.62.20160912git.el9         baseos                        292 k

Transaction Summary
========================================================================================================================
Install  8 Packages

Total download size: 59 M
Installed size: 331 M
Downloading Packages:
(1/8): mysql-community-common-8.4.3-1.el9.x86_64.rpm                                    9.2 MB/s | 575 kB     00:00
(2/8): mysql-community-client-plugins-8.4.3-1.el9.x86_64.rpm                             19 MB/s | 1.5 MB     00:00
(3/8): mysql-community-client-8.4.3-1.el9.x86_64.rpm                                     25 MB/s | 3.1 MB     00:00
(4/8): mysql-community-libs-8.4.3-1.el9.x86_64.rpm                                       30 MB/s | 1.5 MB     00:00
(5/8): mysql-community-icu-data-files-8.4.3-1.el9.x86_64.rpm                             32 MB/s | 2.3 MB     00:00
(6/8): libtirpc-1.3.3-8.el9_4.x86_64.rpm                                                478 kB/s |  93 kB     00:00
(7/8): net-tools-2.0-0.62.20160912git.el9.x86_64.rpm                                    631 kB/s | 292 kB     00:00
(8/8): mysql-community-server-8.4.3-1.el9.x86_64.rpm                                     95 MB/s |  50 MB     00:00
------------------------------------------------------------------------------------------------------------------------
Total                                                                                    53 MB/s |  59 MB     00:01
MySQL 8.4 LTS Community Server                                                          3.0 MB/s | 3.1 kB     00:00
Importing GPG key 0xA8D3785C:
 Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 Fingerprint: BCA4 3417 C3B4 85DD 128E C6D4 B7B3 B788 A8D3 785C
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2023
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                1/1
  Installing       : mysql-community-common-8.4.3-1.el9.x86_64                                                      1/8
  Installing       : mysql-community-client-plugins-8.4.3-1.el9.x86_64                                              2/8
  Installing       : mysql-community-libs-8.4.3-1.el9.x86_64                                                        3/8
  Running scriptlet: mysql-community-libs-8.4.3-1.el9.x86_64                                                        3/8
  Installing       : mysql-community-client-8.4.3-1.el9.x86_64                                                      4/8
  Installing       : net-tools-2.0-0.62.20160912git.el9.x86_64                                                      5/8
  Running scriptlet: net-tools-2.0-0.62.20160912git.el9.x86_64                                                      5/8
  Installing       : libtirpc-1.3.3-8.el9_4.x86_64                                                                  6/8
  Installing       : mysql-community-icu-data-files-8.4.3-1.el9.x86_64                                              7/8
  Running scriptlet: mysql-community-server-8.4.3-1.el9.x86_64                                                      8/8
  Installing       : mysql-community-server-8.4.3-1.el9.x86_64                                                      8/8
  Running scriptlet: mysql-community-server-8.4.3-1.el9.x86_64                                                      8/8
  Verifying        : mysql-community-client-8.4.3-1.el9.x86_64                                                      1/8
  Verifying        : mysql-community-client-plugins-8.4.3-1.el9.x86_64                                              2/8
  Verifying        : mysql-community-common-8.4.3-1.el9.x86_64                                                      3/8
  Verifying        : mysql-community-icu-data-files-8.4.3-1.el9.x86_64                                              4/8
  Verifying        : mysql-community-libs-8.4.3-1.el9.x86_64                                                        5/8
  Verifying        : mysql-community-server-8.4.3-1.el9.x86_64                                                      6/8
  Verifying        : libtirpc-1.3.3-8.el9_4.x86_64                                                                  7/8
  Verifying        : net-tools-2.0-0.62.20160912git.el9.x86_64                                                      8/8

Installed:
  libtirpc-1.3.3-8.el9_4.x86_64                                  mysql-community-client-8.4.3-1.el9.x86_64
  mysql-community-client-plugins-8.4.3-1.el9.x86_64              mysql-community-common-8.4.3-1.el9.x86_64
  mysql-community-icu-data-files-8.4.3-1.el9.x86_64              mysql-community-libs-8.4.3-1.el9.x86_64
  mysql-community-server-8.4.3-1.el9.x86_64                      net-tools-2.0-0.62.20160912git.el9.x86_64

Complete!
```

* サービスの起動

```
$ sudo systemctl start mysqld
$ sudo systemctl status mysqld
● mysqld.service - MySQL Server
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)
     Active: active (running) since Fri 2024-10-18 22:26:39 JST; 13s ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
    Process: 54425 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
   Main PID: 54495 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 23136)
     Memory: 442.7M
        CPU: 1.434s
     CGroup: /system.slice/mysqld.service
             └─54495 /usr/sbin/mysqld

Oct 18 22:26:36 localhost.localdomain systemd[1]: Starting MySQL Server...
Oct 18 22:26:39 localhost.localdomain systemd[1]: Started MySQL Server.
```

### 初期設定

* 以下のログからrootで接続するための一時的なパスワードを確認する
```
$ sudo cat /var/log/mysqld.log
...
2024-10-18T13:26:36.192848Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-10-18T13:26:37.193161Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: *********
2024-10-18T13:26:38.859775Z 0 [System] [MY-015018] [Server] MySQL Server Initialization - end.
...
```

* rootユーザーで接続して、独自のパスワードに変更する

```
$ mysql -uroot -p -A
mysql> SET PASSWORD = '********'
```

### 最後に

RockyLinuxの環境に、MySQLをインストールすることができました。
rootユーザーでのログイン確認までは行いましたが、実際に利用する際には、rootユーザーでのアクセスは制限するべきです。
ユーザーの追加、DBの追加など、実際に利用する際には設定が必要になりますが、そのあたりの設定は、SQLを使って操作するため本記事には含めないようにしたいと思います。