CLOVER🍀

That was when it all began.

MySQLをaptリポジトリーからインストールする時に、MySQL 8.0が選択されるように設定する

これは、なにをしたくて書いたもの?

MySQLの次のLTSである、MySQL 8.4.0がリリースされました。

MySQL :: MySQL 8.4 Release Notes :: Changes in MySQL 8.4.0 (2024-04-30, LTS Release)

ふだんの自分はMySQLをインストールする時はaptリポジトリーを使っているのですが、そのままだとMySQL 8.4をインストールしようとします。
※デフォルトが8.4に向いているからです

MySQL :: A Quick Guide to Using the MySQL APT Repository

8.4.0はリリースされて日が浅いので、まだしばらくは8.0のままにしておきたいのでaptリポジトリーを使いつつMySQL 8.0をインストール
する方法をまとめておきます。

環境

今回の環境はこちら。Ubuntu Linux 22.04 LTSです。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:        22.04
Codename:       jammy


$ uname -srvmpio
Linux 5.15.0-105-generic #115-Ubuntu SMP Mon Apr 15 09:52:04 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

MySQLのaptリポジトリーを追加する

mysql-apt-configの0.8.30-1以降は、MySQL 8.4がデフォルトになっているようです(0.8.29-1まではMySQL 8.0がデフォルト)。

MySQL :: Download MySQL APT Repository

まずはaptリポジトリーを追加します。

$ curl -LO https://dev.mysql.com/get/mysql-apt-config_0.8.30-1_all.deb
$ sudo dpkg -i mysql-apt-config_0.8.30-1_all.deb

最初に対話形式で、インストールするMySQLのメジャーバージョンを聞かれます。

デフォルトはMySQL 8.4になっていますね。

これは選択することができます。

とりあえず、この段階ではMySQL 8.4のままにしておきます。

パッケージの情報を更新します。

$ sudo apt update

mysql-serverパッケージを確認すると、MySQL 8.4を見ています。

$ apt show mysql-server
Package: mysql-server
Version: 8.4.0-1ubuntu22.04
Priority: optional
Section: database
Source: mysql-community
Maintainer: MySQL Release Engineering <mysql-build@oss.oracle.com>
Installed-Size: 77.8 kB
Depends: mysql-community-server (= 8.4.0-1ubuntu22.04)
Homepage: http://www.mysql.com/
Download-Size: 67.0 kB
APT-Sources: http://repo.mysql.com/apt/ubuntu jammy/mysql-8.4-lts amd64 Packages
Description: MySQL Server meta package depending on latest version
 The MySQL(TM) software delivers a very fast, multi-threaded, multi-user,
 and robust SQL (Structured Query Language) database server. MySQL Server
 is intended for mission-critical, heavy-load production systems as well
 as for embedding into mass-deployed software. MySQL is a trademark of
 Oracle. This is a meta package that depends on the latest mysql server
 package available in the repository.

N: 追加レコードが 2 件あります。表示するには '-a' スイッチを付けてください。

ちなみに、apt updateしない状態だとUbuntu LinuxリポジトリーにあるMySQLを見たままになるので、パッケージ情報の更新は必須ですね。

$ apt show mysql-server
Package: mysql-server
Version: 8.0.36-0ubuntu0.22.04.1
Priority: optional
Section: database
Source: mysql-8.0
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian MySQL Maintainers <pkg-mysql-maint@lists.alioth.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 35.8 kB
Depends: mysql-server-8.0
Homepage: http://dev.mysql.com/
Task: lamp-server
Download-Size: 9,460 B
APT-Sources: https://mirrors.edge.kernel.org/ubuntu jammy-updates/main amd64 Packages
Description: MySQL database server (metapackage depending on the latest version)
 This is an empty package that depends on the current "best" version of
 mysql-server (currently mysql-server-8.0), as determined by the MySQL
 maintainers. Install this package if in doubt about which MySQL
 version you need. That will install the version recommended by the
 package maintainers.
 .
 MySQL is a fast, stable and true multi-user, multi-threaded SQL database
 server. SQL (Structured Query Language) is the most popular database query
 language in the world. The main goals of MySQL are speed, robustness and
 ease of use.

N: 追加レコードが 1 件あります。表示するには '-a' スイッチを付けてください。

で、この状態からMySQL 8.0を指すように切り替えるには以下のコマンドを実行します。

$ sudo dpkg-reconfigure mysql-apt-config

すると再びインストールするMySQLのバージョンを選択できるようになります。

決定すると、上書きの確認を求められるのでy

ファイル'/usr/share/keyrings/mysql-apt-config.gpg'は既に存在します。 上書きしますか? (y/N) y

MySQL 8.0にしてみましょう。

この直後にapt showmysql-serverを確認してみると、Ubuntu Linuxリポジトリーの情報を参照するようになっています。

$ apt show mysql-server
Package: mysql-server
Version: 8.0.36-0ubuntu0.22.04.1
Priority: optional
Section: database
Source: mysql-8.0
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian MySQL Maintainers <pkg-mysql-maint@lists.alioth.debian.org>

〜省略〜

なので、再度apt update

$ sudo apt update

これでインストールしたaptリポジトリーのMySQL 8.0を参照するようになりました。

$ apt show mysql-server
Package: mysql-server
Version: 8.0.37-1ubuntu22.04
Priority: optional
Section: database
Source: mysql-community
Maintainer: MySQL Release Engineering <mysql-build@oss.oracle.com>

〜省略〜

CLIで設定する

ここまでは対話形式で設定していましたが、コンテナ作成時などCLIで設定したい場合はこのままだと不便ですね。

CLIで設定できるようにしてみたいと思います。

開始時点は、aptリポジトリーの設定はMySQL 8.4に向けている(デフォルト)の状態とします。

現在の設定を確認します。

$ sudo debconf-get-selections | grep mysql
mysql-apt-config        mysql-apt-config/repo-url       string  http://repo.mysql.com/apt
# Choices: MySQL Server & Cluster (Currently selected: mysql-8.4-lts), MySQL Tools & Connectors (Currently selected: Enabled), MySQL Preview Packages (Currently selected: Disabled), Ok
mysql-apt-config        mysql-apt-config/select-product select  Ok
mysql-apt-config        mysql-apt-config/preview-component      string
# Choices: mysql-8.0, mysql-innovation, mysql-8.4-lts, mysql-cluster-8.0, mysql-cluster-innovation, mysql-cluster-8.4-lts, None
mysql-apt-config        mysql-apt-config/select-server  select  mysql-8.4-lts
mysql-apt-config        mysql-apt-config/select-preview select  Disabled
mysql-apt-config        mysql-apt-config/repo-codename  select  jammy
mysql-apt-config        mysql-apt-config/unsupported-platform   select  abort
mysql-apt-config        mysql-apt-config/repo-distro    select  ubuntu
mysql-apt-config        mysql-apt-config/select-tools   select  Enabled
mysql-apt-config        mysql-apt-config/tools-component        string  mysql-tools

debconf-get-selectionsがインストールされていない場合は、こちらのコマンドで。

$ sudo apt install debconf-utils

MySQL 8.0を選択。

$ echo 'mysql-apt-config mysql-apt-config/select-server select mysql-8.0' | sudo debconf-set-selections

このあとはdpkg-reconfigureで設定するのかなと思ったのですが、そのまま返ってこなくなったので…

$ sudo dpkg-reconfigure --frontend noninteractive mysql-apt-config

mysql-apt-configを上書きインストールしました。

$ sudo DEBIAN_FRONTEND=noninteractive dpkg -i mysql-apt-config_0.8.30-1_all.deb

これでもパッケージ情報を更新すると

$ sudo apt update

MySQL 8.0が選択されるようになっています。

$ sudo apt show mysql-server | grep 8.0

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Version: 8.0.37-1ubuntu22.04
Depends: mysql-community-server (= 8.0.37-1ubuntu22.04)
APT-Sources: http://repo.mysql.com/apt/ubuntu jammy/mysql-8.0 amd64 Packages

こうなると、最初からdebconf-set-selectionsで設定しておくとよさそうですね。

ちなみに、8.4に戻す場合はこちら。

$ echo 'mysql-apt-config mysql-apt-config/select-server select mysql-8.4-lts' | sudo debconf-set-selections
$ sudo DEBIAN_FRONTEND=noninteractive dpkg -i mysql-apt-config_0.8.30-1_all.deb
$ sudo apt update

オマケ:インストール時のrootユーザーのパスワードを設定する

mysql-serverをインストールすると、以下のような設定が追加されます。

$ sudo debconf-get-selections | grep mysql-community-server
mysql-community-server  mysql-community-server/re-root-pass     password
mysql-community-server  mysql-community-server/root-pass        password
mysql-community-server  mysql-community-server/data-dir note
mysql-community-server  mysql-server/lowercase-table-names      select
mysql-community-server  mysql-community-server/root-pass-mismatch       error
mysql-community-server  mysql-community-server/remove-data-dir  boolean false
mysql-community-server  mysql-server/default-auth-override      select  Use Strong Password Encryption (RECOMMENDED)

このうち、rootのパスワードの設定は以下の2つです。ここでは「mypassword」で設定しています。

$ echo 'mysql-community-server mysql-community-server/root-pass password mypassword' | sudo debconf-set-selections
$ echo 'mysql-community-server mysql-community-server/re-root-pass password mypassword' | sudo debconf-set-selections

mysql-community-serverの他にもmysql-server-8.0という項目もありましたが、ちょっと違いそうです。

あとはMySQLサーバーをインストールすればOKです。

$ sudo DEBIAN_FRONTEND=noninteractive apt install mysql-server

おわりに

MySQLをaptリポジトリーからインストールする時に、MySQL 8.0が選択されるようにしてみました。

CLIでできるようにも調べたりしたのですが、こちらはけっこう大変でした…。
Dockerコンテナ作成時やインストールスクリプトを書く時になどに使えたりすると思うので、覚えておきましょう。