💽

Ubuntuに最新のMariaDBを入れる

2021/02/18に公開

Ubuntuに最新のMariaDBを入れるときの備忘録的なものです。
aptで入れてしまうと少し古いのが入ってしまうのでそれが嫌だっていう人向けの記事です。

プロキシ環境下の人向け

環境

OS: Ubuntu Server 20.04.2 LTS

MariaDBのパッケージリポジトリの追加とインストール

参考: MariaDB Package Repository Setup and Usage - MariaDB Knowledge Base

# scriptを回すために必要らしい
$ sudo apt install apt-transport-https
$ curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash

[info] Repository file successfully written to /etc/apt/sources.list.d/mariadb.list
[info] Adding trusted package signing keys...
[info] Running apt-get update...
[info] Done adding trusted package signing keys

このコマンドだけでインストールしてくれるのでめっちゃ便利ですね。
上の参考サイトを見るといろいろなオプションを指定することができます。

sudo apt updateをすると、MariaDBのパッケージも拾ってきていることがわかります。

$ sudo apt update
…
Get:X https://dlm.mariadb.com/repo/maxscale/latest/ubuntu focal InRelease 
Hit:X https://downloads.mariadb.com/MariaDB/mariadb-10.5/repo/ubuntu focal InRelease
Hit:X https://downloads.mariadb.com/Tools/ubuntu focal InRelease

MariaDBのサーバーパッケージをインストールします。

$ sudo apt install mariadb-server

MariaDBが動作しているかの確認

MariaDBはsystemdで動いているのでいつものコマンドで確認できます。

$ systemctl status mariadb

● mariadb.service - MariaDB 10.5.8 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/mariadb.service.d
             └─migrated-from-my.cnf-settings.conf
     Active: active (running) since dddd YYYY-MM-DD hh:mm:ss JST; Xmin XXs ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 12599 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 10 (limit: 4486)
     Memory: 69.5M
     CGroup: /system.slice/mariadb.service
             └─12599 /usr/sbin/mariadbd

MariaDBの初期設定

参考: Ubuntu 20.04 : MariaDB インストールと設定 : Server World

mysql_secure_installationを管理者権限で実行します。

$ sudo mysql_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)
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'.

# Unix_socketを使うかどうか
Switch to unix_socket authentication [Y/n] n
 ... skipping.

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

# rootパスワードを変えるかどうか
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.

# rootのリモートログインを無効にするかどうか
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
"/etc/mysql/mariadb.conf.d/50-server.cnf"
 - 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!

MariaDBへのログイン

以下のコマンドでログインできます。

$ mariadb -u root -p
Enter password: # さっき決めたrootパスワード

Discussion