LoginSignup
3
4

MySQL 8.0インストール

Posted at

はじめに

RPM パッケージを使用した Oracle Linux 8 への MySQL 8.0.32 インストール手順を紹介します。

本記事は、MySQL8.0の下記マニュアルを参考に作成しました。
2.5 Linux に MySQL をインストールする

筆者の環境は、OCIのAlways Freeサービスを使用しています。
コンピュートインスタンスのスペックは下記です。

#
シェイプ VM.Standard.E2.1.Micro
OCPU数 1
NW帯域幅(Gbps) 0.48
メモリ(GB) 1
ストレージ(GB) 47

メモリ1GBと心許ない環境ですが、今のところ支障なく使用できています。

MySQL Yum リポジトリの追加

MySQL Developer Zone のDownload MySQL Yum Repository にアクセスし、使用しているプラットフォーム用のRPMパッケージをダウンロードします。

Oracle Linux 8の場合は下記をダウンロードします。

  • Red Hat Enterprise Linux 8 / Oracle Linux 8 (Architecture Independent)
    (mysql80-community-release-el8-4.noarch.rpm)

MySQL Yum リポジトリをシステムのリポジトリリストに追加します。

[opc@mdb01 ~]$ sudo yum install mysql80-community-release-el8-4.noarch.rpm
Ksplice for Oracle Linux 8 (x86_64)                                                                                                                             10 MB/s | 2.1 MB     00:00
MySQL 8.0 for Oracle Linux 8 (x86_64)                                                                                                                           12 MB/s | 2.8 MB     00:00
MySQL 8.0 Tools Community for Oracle Linux 8 (x86_64)                                                                                                          3.3 MB/s | 426 kB     00:00
MySQL 8.0 Connectors Community for Oracle Linux 8 (x86_64)                                                                                                     245 kB/s |  28 kB     00:00
Oracle Software for OCI users on Oracle Linux 8 (x86_64)                                                                                                        31 MB/s |  74 MB     00:02
Oracle Linux 8 BaseOS Latest (x86_64)                                                                                                                           26 MB/s |  57 MB     00:02
Oracle Linux 8 Application Stream (x86_64)                                                                                                                      25 MB/s |  43 MB     00:01
Oracle Linux 8 Addons (x86_64)                                                                                                                                  17 MB/s | 6.9 MB     00:00
Latest Unbreakable Enterprise Kernel Release 7 for Oracle Linux 8 (x86_64)                                                                                      22 MB/s |  14 MB     00:00
依存関係が解決しました。
===============================================================================================================================================================================================
 パッケージ                                                 アーキテクチャー                        バージョン                             リポジトリー                                  サイズ
===============================================================================================================================================================================================
インストール:
 mysql80-community-release                                  noarch                                  el8-4                                  @commandline                                   14 k

トランザクションの概要
===============================================================================================================================================================================================
インストール  1 パッケージ

合計サイズ: 14 k
インストール後のサイズ: 8.6 k
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                                                                                       1/1
  インストール中   : mysql80-community-release-el8-4.noarch                                                                                                                                1/1
  検証             : mysql80-community-release-el8-4.noarch                                                                                                                                1/1

インストール済み:
  mysql80-community-release-el8-4.noarch

完了しました!

MySQL Yum リポジトリが正常に追加されたことを確認します。

[opc@mdb01 ~]$ yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community       MySQL Connectors Community
mysql-tools-community            MySQL Tools Community
mysql80-community                MySQL 8.0 Community Server

デフォルトで有効になっている MySQL モジュール無効にします。

[opc@mdb01 ~]$ sudo dnf module disable mysql
メタデータの期限切れの最終確認: 3:02:40 時間前の 2023年04月9日 11時38分27秒 に実施しました。
依存関係が解決しました。
===============================================================================================================================================================================================
 パッケージ                                    アーキテクチャー                             バージョン                                     リポジトリー                                  サイズ
===============================================================================================================================================================================================
モジュールの無効化:
 mysql

トランザクションの概要
===============================================================================================================================================================================================

これでよろしいですか? [y/N]: y
完了しました!

MySQL をインストールします。

[opc@mdb01 ~]$ sudo yum install mysql-community-server
メタデータの期限切れの最終確認: 3:04:36 時間前の 2023年04月9日 11時38分27秒 に実施しました。
依存関係が解決しました。
===============================================================================================================================================================================================
 パッケージ                                                 アーキテクチャー                   バージョン                                  リポジトリー                                  サイズ
===============================================================================================================================================================================================
インストール:
 mysql-community-server                                     x86_64                             8.0.32-1.el8                                mysql80-community                              64 M
依存関係のインストール:
 mysql-community-client                                     x86_64                             8.0.32-1.el8                                mysql80-community                              16 M
 mysql-community-client-plugins                             x86_64                             8.0.32-1.el8                                mysql80-community                             2.5 M
 mysql-community-common                                     x86_64                             8.0.32-1.el8                                mysql80-community                             656 k
 mysql-community-icu-data-files                             x86_64                             8.0.32-1.el8                                mysql80-community                             2.1 M
 mysql-community-libs                                       x86_64                             8.0.32-1.el8                                mysql80-community                             1.5 M

トランザクションの概要
===============================================================================================================================================================================================
インストール  6 パッケージ

ダウンロードサイズの合計: 87 M
インストール後のサイズ: 410 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(1/6): mysql-community-common-8.0.32-1.el8.x86_64.rpm                                                                                                           19 MB/s | 656 kB     00:00
(2/6): mysql-community-client-plugins-8.0.32-1.el8.x86_64.rpm                                                                                                   22 MB/s | 2.5 MB     00:00
(3/6): mysql-community-icu-data-files-8.0.32-1.el8.x86_64.rpm                                                                                                   13 MB/s | 2.1 MB     00:00
(4/6): mysql-community-libs-8.0.32-1.el8.x86_64.rpm                                                                                                             12 MB/s | 1.5 MB     00:00
(5/6): mysql-community-client-8.0.32-1.el8.x86_64.rpm                                                                                                           19 MB/s |  16 MB     00:00
(6/6): mysql-community-server-8.0.32-1.el8.x86_64.rpm                                                                                                           26 MB/s |  64 MB     00:02
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
合計                                                                                                                                                            33 MB/s |  87 MB     00:02
MySQL 8.0 Community Server                                                                                                                                     3.0 MB/s | 3.1 kB     00:00
GPG 鍵 0x3A79BD29 をインポート中:
 Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 Fingerprint: 859B E8D7 C586 F538 430B 19C2 467B 942D 3A79 BD29
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
これでよろしいですか? [y/N]: y
鍵のインポートに成功しました
MySQL 8.0 Community Server                                                                                                                                     1.9 MB/s | 1.9 kB     00:00
GPG 鍵 0x5072E1F5 をインポート中:
 Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 Fingerprint: A4A9 4068 76FC BD3C 4567 70C8 8C71 8D3B 5072 E1F5
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
これでよろしいですか? [y/N]: y
鍵のインポートに成功しました
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                                                                                       1/1
  インストール中   : mysql-community-common-8.0.32-1.el8.x86_64                                                                                                                            1/6
  インストール中   : mysql-community-client-plugins-8.0.32-1.el8.x86_64                                                                                                                    2/6
  インストール中   : mysql-community-libs-8.0.32-1.el8.x86_64                                                                                                                              3/6
  scriptletの実行中: mysql-community-libs-8.0.32-1.el8.x86_64                                                                                                                              3/6
  インストール中   : mysql-community-client-8.0.32-1.el8.x86_64                                                                                                                            4/6
  インストール中   : mysql-community-icu-data-files-8.0.32-1.el8.x86_64                                                                                                                    5/6
  scriptletの実行中: mysql-community-server-8.0.32-1.el8.x86_64                                                                                                                            6/6
  インストール中   : mysql-community-server-8.0.32-1.el8.x86_64                                                                                                                            6/6
  scriptletの実行中: mysql-community-server-8.0.32-1.el8.x86_64                                                                                                                            6/6
  検証             : mysql-community-client-8.0.32-1.el8.x86_64                                                                                                                            1/6
  検証             : mysql-community-client-plugins-8.0.32-1.el8.x86_64                                                                                                                    2/6
  検証             : mysql-community-common-8.0.32-1.el8.x86_64                                                                                                                            3/6
  検証             : mysql-community-icu-data-files-8.0.32-1.el8.x86_64                                                                                                                    4/6
  検証             : mysql-community-libs-8.0.32-1.el8.x86_64                                                                                                                              5/6
  検証             : mysql-community-server-8.0.32-1.el8.x86_64                                                                                                                            6/6

インストール済み:
  mysql-community-client-8.0.32-1.el8.x86_64 mysql-community-client-plugins-8.0.32-1.el8.x86_64 mysql-community-common-8.0.32-1.el8.x86_64 mysql-community-icu-data-files-8.0.32-1.el8.x86_64
  mysql-community-libs-8.0.32-1.el8.x86_64   mysql-community-server-8.0.32-1.el8.x86_64

完了しました!

MySQL Server の起動

systemctlコマンドを使用して、MySQLサーバを起動します。

[opc@mdb01 ~]$ sudo systemctl start mysqld

状態を確認します。

[opc@mdb01 ~]$ sudo systemctl status mysqld

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-04-09 15:01:41 JST; 36s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 693087 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 693263 (mysqld)
   Status: "Server is operational"
    Tasks: 39 (limit: 5565)
   Memory: 459.7M
   CGroup: /system.slice/mysqld.service
           mq693263 /usr/sbin/mysqld

 4月 9 15:01:13 mdb01 systemd[1]: Starting MySQL Server...
 4月 9 15:01:41 mdb01 systemd[1]: Started MySQL Server.

何らかのエラーが発生し、起動に失敗した場合は、MySQLサーバのログファイル、あるいはsystemctl status mysqldコマンドで原因を調査します。

[root@mdb01 ~]# cat /var/log/mysqld.log

MySQLサーバの初回起動時に下記の初期化処理が行われます。

  • サーバーが初期化されます。
  • SSL 証明書およびキーファイルがデータディレクトリに生成されます。
  • validate_password がインストールされ、有効になっています。
  • スーパーユーザーアカウントroot@localhost が作成されます。

MySQLの管理者であるrootアカウントの初期パスワードは、ログファイル( /var/log/mysqld.log )に出力されています。下記のようにtemporary passwordをキーワードにして確認可能です。

[root@mdb01 ~]# grep 'temporary password' /var/log/mysqld.log
2023-04-09T06:01:29.672108Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: <初期パスワード>

mysqlクライアントを起動し、初期パスワードを使用してログインします。

[root@mdb01 ~]# mysql -uroot -p
Enter password: <初期パスワードを入力>

ALTER USER文を使用して、初期パスワードを変更します。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '変更後のパスワード';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

前述の通り、validate_passwordが有効化されていますため、下記のパスワードポリシに従う必要があります。

  • パスワードに大文字、小文字、数字および特殊文字を 1 文字以上含める
  • パスワードの合計長は 8 文字以上

PRMパッケージを使用してインストールした場合のデータベースファイルは、/var/lib/mysql に格納されます。初回起動後のデータベースファイルディレクトリの状態は下記となります。

[root@mdb01 ~]# ls -l /var/lib/mysql
total 90568
-rw-r-----. 1 mysql mysql       56 Apr 9 15:01  auto.cnf
-rw-r-----. 1 mysql mysql      157 Apr 9 15:01  binlog.000001
-rw-r-----. 1 mysql mysql       16 Apr 9 15:01  binlog.index
-rw-------. 1 mysql mysql     1676 Apr 9 15:01  ca-key.pem
-rw-r--r--. 1 mysql mysql     1112 Apr 9 15:01  ca.pem
-rw-r--r--. 1 mysql mysql     1112 Apr 9 15:01  client-cert.pem
-rw-------. 1 mysql mysql     1676 Apr 9 15:01  client-key.pem
-rw-r-----. 1 mysql mysql   196608 Apr 9 15:03 '#ib_16384_0.dblwr'
-rw-r-----. 1 mysql mysql  8585216 Apr 9 15:01 '#ib_16384_1.dblwr'
-rw-r-----. 1 mysql mysql     5554 Apr 9 15:01  ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Apr 9 15:01  ibdata1
-rw-r-----. 1 mysql mysql 12582912 Apr 9 15:01  ibtmp1
drwxr-x---. 2 mysql mysql     4096 Apr 9 15:01 '#innodb_redo'
drwxr-x---. 2 mysql mysql      187 Apr 9 15:01 '#innodb_temp'
drwxr-x---. 2 mysql mysql      143 Apr 9 15:01  mysql
-rw-r-----. 1 mysql mysql 25165824 Apr 9 15:01  mysql.ibd
srwxrwxrwx. 1 mysql mysql        0 Apr 9 15:01  mysql.sock
-rw-------. 1 mysql mysql        7 Apr 9 15:01  mysql.sock.lock
drwxr-x---. 2 mysql mysql     8192 Apr 9 15:01  performance_schema
-rw-------. 1 mysql mysql     1676 Apr 9 15:01  private_key.pem
-rw-r--r--. 1 mysql mysql      452 Apr 9 15:01  public_key.pem
-rw-r--r--. 1 mysql mysql     1112 Apr 9 15:01  server-cert.pem
-rw-------. 1 mysql mysql     1676 Apr 9 15:01  server-key.pem
drwxr-x---. 2 mysql mysql       28 Apr 9 15:01  sys
-rw-r-----. 1 mysql mysql 16777216 Apr 9 15:03  undo_001
-rw-r-----. 1 mysql mysql 16777216 Apr 9 15:03  undo_002

また、上記ディレクトリの場所は、設定ファイル(/etc/my.cnf)のdata_dir変数に定義されています。

[opc@mdb01 ~]$ cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

MySQL Server の停止

mysqladminコマンドを使用して、MySQLサーバを停止します。

[root@mdb01 ~]# mysqladmin -u root -p shutdown
Enter password:

MySQL Server が停止した事を確認します。

[root@mdb01 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Wed 2023-04-09 15:43:17 JST; 19min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 693263 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 693087 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 693263 (code=exited, status=0/SUCCESS)
   Status: "Server shutdown complete"

Apr 9 15:01:13 mdb01 systemd[1]: Starting MySQL Server...
Apr 9 15:01:41 mdb01 systemd[1]: Started MySQL Server.
Apr 9 15:43:17 mdb01 systemd[1]: mysqld.service: Succeeded.

当環境のようにsystemdを使用している場合は、systemctlコマンドにて停止可能です。

[root@mdb01 ~]# systemctl stop mysqld
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4