カテゴリー
ソフトウェア

AmazonLinux2にMariaDBをインストールする

はじめに

MariaDBはMySQLの後継にあたる、オープンソースソフトウェアです。今回、このMariaDBをAmazonLinux2にセットアップする手順を投稿します。

DBサーバーとDBクライアントは独立したパッケージとなっています。1台のホストでスタンドアロンで動作するようなアプリケーションの場合はDBサーバーもDBクライアントも必要となります。DBクライアント(接続元ホスト)は別にあり、このホストではDBサーバー機能だけでよい場合ならDBサーバーのインストールだけで大丈夫でしょう。逆に、アプリケーションが稼働するホストにはDBサーバーはなく、別ホストにある既存のDBサーバーにアクセスしに行く必要がある場合にはDBクライアントのインストールが必要です。

今回、AmazonLinux2を使用しました。AmazonLinux2のデフォルトのリポジトリではMariaDBのバージョンは5.5と古いのですが、今回は簡単のためこのバージョンのままの手順を紹介します。

DBサーバーインストール

このホストにDBサーバーをインストールします。

パッケージインストール

$ sudo yum install mariadb-server

MariaDB設定調整

セクション[mysqld]のみに追記します。MariaDB専用のオプションを指定するにはセクション[mariadb]に記述すればいいようですが、今回はそのような設定はありませんのでセクション[mariadb]にはなにも記述しません。

今回のこの設定は、t2.nanoインスタンス(メインメモリ512MB)でも動作するよう、かなりコンパクトなリソース割り当てとしています。

sudo vi /etc/my.cnf.d/server.cnf

#
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]
character-set-server = utf8
skip-character-set-client-handshake

table-definition-cache = 200
innodb-buffer-pool-size = 67108864
max-connections = 20

log-error = /var/log/mariadb/mariadb.log

# this is only for embedded server
[embedded]

# This group is only read by MariaDB-5.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mysqld-5.5]

# These two groups are only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

[mariadb-5.5]

MariaDBログファイル出力先ディレクトリ設定

$ sudo mkdir -p /var/log/mariadb
$ sudo chown mysql:mysql /var/log/mariadb

LogRotate設定調整

DBサーバーのエラーログは /var/log/mariadb/mariadb.log に記録されるように設定しました。このファイルの肥大化を避けるため、logrotateコマンドにより定期的にファイルが分割されるようにします。

$ sudo cp /etc/logrotate.d/mariadb /etc/logrotate.d/mariadb.disabled
$ sudo vi /etc/logrotate.d/mariadb
$ sudo diff /etc/logrotate.d/mariadb.disabled /etc/logrotate.d/mariadb

23,38c23,38
< #/var/log/mariadb/mariadb.log {
< #        create 640 mysql mysql
< #        notifempty
< #	daily
< #        rotate 3
< #        missingok
< #        compress
< #    postrotate
< #	# just if mysqld is really running
< #	if test -x /usr/bin/mysqladmin && \
< #	   /usr/bin/mysqladmin ping &>/dev/null
< #	then
< #	   /usr/bin/mysqladmin flush-logs
< #	fi
< #    endscript
< #}
---
> /var/log/mariadb/mariadb.log {
>         create 640 mysql mysql
>         notifempty
> 	daily
>         rotate 3
>         missingok
>         compress
>     postrotate
> 	# just if mysqld is really running
> 	if test -x /usr/bin/mysqladmin && \
> 	   /usr/bin/mysqladmin ping &>/dev/null
> 	then
> 	   /usr/bin/mysqladmin flush-logs
> 	fi
>     endscript
> }

DBサーバー起動

MariaDBの手動での起動、起動状況の確認、そしてOS起動時に自動的に起動するように設定します。

$ sudo systemctl start mariadb
$ sudo systemctl status mariadb
$ sudo systemctl enable mariadb

DBクライアントインストール

このホストにDBクライアントをインストールします。DBクライアントは単体で利用される事よりもJavaやPythonなどのフレームワークを利用して接続される事の方が多いはずです。アプリケーション側のライブラリがこのDBクライアントを認識またはビルドできるように、mariadb-develパッケージをインストールします。

パッケージインストール

$ sudo yum install mariadb-devel

MariaDB設定調整

セクション[client]のみに追記します。MariaDB専用のオプションを指定するにはセクション[client-mariadb]に記述すればいいようですが、今回はそのような設定はありませんのでセクション[client-mariadb]にはなにも記述しません。

$ sudo vi /etc/my.cnf.d/client.cnf

/etc/my.cnf.d/client.cnf

#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#


[client]
default-character-set = utf8

# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]

DBサーバー・DBクライアント接続確認

この段階でDBサーバーが可動しているようなら、接続テストを実施してみましょう。また、mysqlコマンドにより文字コードがどのように設定されているか確認します。

$ mysql -h localhost

DBサーバーに、DBクライアントの接続権限が設定されてれば下記のようなメッセージが表示されて、mysqlコマンドによる接続に成功します。

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

次に、下記のコマンドを入力・実行して、この接続に関する文字コードの扱いを確認します。


MariaDB [(none)]> SHOW VARIABLES LIKE '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

今回の場合、DBサーバーもDBクライアントも文字コードはutf8を使うように設定されていることが確認できます。

終わりに

今回は最低限の設定のみ紹介しました。MariaDBまたはMySQLのデータベース作成、ユーザー作成、データベース権限設定などはこちらの投稿を参照してください。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください