OpenWrt で MariaDB を使えるようにする

WordPress をインストールするために、MySQL 互換で MySQL よりは軽量の(とはいってもそれなりに ROM 容量は食うが)MariaDB を使えるようにする。

前提状況:USB フラッシュドライブ

データ領域として USB フラッシュの外部ドライブを用意(👉 OpenWrt での USB フラッシュドライブ)し、/mnt/data にマウントしていることを前提としている。

アプリケーション自体(MariaDB)のインストール


opkg install mariadb-server
  • 設定は /etc/mysql にある
  • my.cnf はサーバー・クライアントに共通のグローバル設定用
  • conf.d/50-server.cnf はサーバー専用のグローバル設定
  • conf.d/60-galera.cnf は Galera Cluster 用のグローバル設定(Galera Cluster を使わない限り無関係)
  • ユーザー別のローカル設定用は ~/.my.cnf を作成する

ということなので、基本的に /etc/mysql/conf.d/50-server.cnf を編集して設定を調整することになる。

MariaDB の設定

デフォルトの /etc/mysql/conf.d/50-server.cnf(抜粋)
#
# * Basic Settings
#
user  = mariadb
pid-file = /var/run/mysqld/mysqld.pid
socket  = /var/run/mysqld/mysqld.sock
port  = 3306
basedir  = /usr
# Don't put this on flash memory
# Figure out where you are going to put the databases and run
# mysql_install_db --force
datadir  = /mnt/data/mysql
# tmpdir should also not go on flash memory
tmpdir  = /tmp
lc-messages-dir = /usr/share/mariadb

skip-external-locking

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
  • /var/run/mysqld(ディレクトリー)はデフォルトでは存在しなかったので、作成(mkdir)しておく
  • /mnt/data は USB フラッシュドライブの設定段階で既に用意したので、さらにその下に /mnt/data/mysql ディレクトリー(owner: mariadb)を作成する
  • このまま mysql_install_db を実行すると、Linux Native AIO interface がサポートされていないというエラーが出ると思うので(致命的ではなく、放っておいても結局は無効化されるだけである)、明示的に避けるためには Linux Native AIO を無効化しておく(設定ファイルに innodb_use_native_aio = 0 を追加)。
  • スワップ(👉 USB フラッシュドライブ)が有効だと、メモリー不足から来る InnoDB のエラーが解消され、MariaDB-server が正常に動くようになった。

以上の調整が終ったら、mysql_install_db --force を実行する。

データベース(MySQL 互換システム)のインストール

mysql_install_db --force を実行して成功すると、以下のメッセージが出力される:

Installing MariaDB/MySQL system tables in '/mnt/data/mysql' ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:

'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h OpenWrt password 'new-password'

Alternatively you can run:
'/usr/bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/mnt/data/mysql'

You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/
  • メッセージにあるように、インストール後の、MariaDB のシステム全体を管理するアカウントは root(パスワードは未設定;OS 側の root とは関係がないが、sock を使う場合には OS のアカウント名と関係する場合もあるようだ)。公開用データベースとして運用する場合は、root パスワードを設定し、test 用データベースと匿名ユーザーを削除することが推奨されている。
  • ただし、メッセージにある、mysql_secure_installation の実行には mariadb-server-extra のインストールが、mysqladmin の実行には mariadb-client と mariadb-client-extra のインストールが必要である。
  • デフォルトで匿名ユーザーは存在するとは言っても、見た限り、何ら権限が付与されていない状態なので、ストレートにセキュリティを脅かすというほどのものでもなさそうである。まあ、無意味なエントリーが存在することにはなるので、気になるなら行を削除すればいい。mysql_secure_installation を使わずとも直接操作できる。test データベースの削除も同様。
  • WordPress をインストールする場合は、別途 WordPress 専用のデータベースを作成し、そのデータベース専用のユーザーとパスワードを用意することになる。

データベースの root のパスワードに関して、自分の場合、今回 MariaDB を運用する OpenWrt ルーターは、WordPress/WWW サーバー専用なので、データベース自体はローカル運用のみで直接ネットワークに開放するわけではない(/etc/mysql/conf.d/50-server.cnf の bind-address = 127.0.0.1)。そのため、WordPress プログラムを除き、root アカウントでルーターに SSH ログインした場合のみ、データベースに直接アクセスすることを想定している。なので、データベース側の root アカウントをパスワードで別途プロテクトする必要はない。OS 側の root アカウントの段階でプロテクトするものとし、それが破られれば、諦める他ないというスタンス。

mysqld(サーバー)の設定・稼動

/etc/config/mysqld

config mysqld 'general'
-       option enabled '0'
+       option enabled '1'
        option log_stderr '1'
        option log_stdout '1'
        option options ''

enabled を '0' から '1' に変更する。

service mysqld start するなり、ルーターを再起動するなりすれば、mysqld が有効に動くようになるはず。正常にセットアップできていれば 何らかの MariaDB 用クライアントからのアクセスが可能となる。


MariaDB クライアント

自分の場合は少しでも OpenWrt ルーターの ROM 容量の消費を抑えるために MariaDB クライアントのインストールは避け、PC(Mac)にインストールした MariaDB クライアントアプリ(例えば macOS の Sequel Pro)から SSH ログイン経由で OpenWrt の MariaDB サーバーにアクセスしてテストしたりした。下のスクリーンショットでは、root @ 127.0.0.1(パスワード無し)としてデータベース mysql にログインしている。

しかし、ROM 容量消費のことを別にすれば、サーバーの存在する OpenWrt ルーター自体にクライアントが存在して操作できるのが一番確実なことに違いない。その場合は、mariadb-client をインストールすればよい:


opkg install mariadb-client

また、クライアントをインストールした場合、他に mysqldump コマンドなども使いたければ、さらに mariadb-client-extra もインストールする。

コメント

このブログの人気の投稿

清水俊史『上座部仏教における聖典論の研究』

シークエンスパパともの先見の明

シークエンスパパとも 本物の霊能力