A database server is an critical component of the network infrastructure necessary for today’s applications. Without the ability to store, retrieve, update, and delete data (when needed), the usefulness and scope of web and desktop apps becomes very limited.

インストール、管理、およびデータベースサーバーを構成する方法を知っていること(期待どおりに動作するように)は、すべてのシステム管理者が持っている必須のスキルです。
この記事では、MariaDBデータベースサーバーをインストールしてセキュリティを確保する方法について簡単に説明し、その後、構成方法について説明します。
MariaDBサーバーのインストールとセキュリティの確保
CentOS 7.xでは、MariaDBがMySQLに置き換わり、Ubuntu(およびMariaDB)にはまだMySQLが見つかることができます。openSUSEでも同様です。
簡潔にするため、このチュートリアルではMariaDBのみを使用しますが、異なる名前と開発哲学を持っていることに注意してください。リレーショナルデータベース管理システム(略してRDBMS)はほぼ同一です。
これは、クライアント側のコマンドがMySQLとMariaDBの両方で同じであり、構成ファイルの名前と場所も同じであることを意味します。
MariaDBをインストールするには、次のようにします:
--------------- On CentOS/RHEL 7 and Fedora 23 --------------- # yum update && yum install mariadb mariadb-server # CentOS --------------- On Debian and Ubuntu --------------- $ sudo aptitude update && sudo aptitude install mariadb-client mariadb-server --------------- On openSUSE --------------- # zypper update && zypper install mariadb mariadb-tools # openSUSE
Ubuntuでは、RDBMSルートユーザーのパスワードを入力するように求められます。
上記のパッケージがインストールされたら、データベースサービスが実行され、起動時に起動するようにアクティブ化されていることを確認してください(CentOSおよびopenSUSEでは、この操作を手動で実行する必要がありますが、Ubuntuではインストールプロセスがすでに対処しています):
--------------- On CentOS/RHEL 7 and Fedora 23 --------------- # systemctl start mariadb && systemctl enable mariadb --------------- On openSUSE --------------- # systemctl start mysql && systemctl enable mysql
その後、mysql_secure_installation
スクリプトを実行します。このプロセスにより、以下が可能になります:
- RDBMSルートユーザーのパスワードを設定/リセット
- 匿名ログインを削除(有効なアカウントを持つユーザーのみがRDBMSにログインできるようにする)
- localhost以外のマシンからのrootアクセスを無効にする
- 誰でもアクセスできるテストデータベースを削除する
- 1から4に関連する変更を有効にする
このプロセスの詳細な説明については、Install MariaDB Database in RHEL/CentOS/Fedora and Debian/Ubuntuの「Post installation」セクションを参照してください。
MariaDBサーバーの設定
デフォルトの設定オプションは、次の順序で指定されたファイルから読み込まれます:/etc/mysql/my.cnf
、/etc/my.cnf
、および~/.my.cnf
。
ほとんどの場合、/etc/my.cnf
のみが存在します。ここにサーバー全体の設定を行います(ユーザーごとに~/.my.cnf
で同じ設定を上書きできます)。
my.cnf
について最初に注意するべきことは、設定がカテゴリ(またはグループ)に整理されていることで、各カテゴリ名が角かっこで囲まれていることです。
サーバーシステムの設定は、通常、[mysqld]
セクションに記載されており、通常、以下の表の最初の2つの設定のみが見つかります。残りは他の頻繁に使用されるオプションです(指定されている場合、デフォルト値を選択したカスタム値に変更します):
Setting and description | Default value |
datadir is the directory where the data files are stored. | datadir=/var/lib/mysql |
socket indicates the name and location of the socket file that is used for local client connections. Keep in mind that a socket file is a resource that is utilized to pass information between applications. | socket=/var/lib/mysql/mysql.sock |
bind_address is the address where the database server will listen on for TCP/IP connections. If you need your server to listen on more than one IP address, leave out this setting (0.0.0.0 which means it will listen on all IP addresses assigned to this specific host).
このサービスがメインアドレス(192.168.0.13)のみでリッスンするように変更します。 bind_address=192.168.0.13 |
bind_address=0.0.0.0 |
port represents the port where the database server will be listening.
デフォルト値(3306)を20500に置き換えます(ただし、他にそのポートを使用していないことを確認する必要があります): 一部の人々は、セキュリティを曖昧にすることは良い慣行ではないと主張するかもしれませんが、デフォルトのアプリケーションポートをより高い値に変更することは、ポートスキャンを妨げるための初歩的で効果的な方法です。 |
port=3306 |
innodb_buffer_pool_size is the buffer pool (in bytes) of memory that is allocated for data and indexes that are accessed frequently when using Innodb (which is the default in MariaDB) or XtraDB as storage engine.
デフォルト値を256 MBに置き換えます: innodb_buffer_pool_size=256M |
innodb_buffer_pool_size=134217728 |
skip_name_resolve indicates whether hostnames will be resolved or not on incoming connections. If set to 1, as we will do in this guide, only IP addresses.
許可を決定するためにホスト名が必要ない場合は、この変数を無効にすることがお勧めされます(接続とクエリの高速化のために、値を1に設定します): skip_name_resolve=1 |
skip_name_resolve=0 |
query_cache_size represents the size (in bytes) available to the query cache in disk, where the results of SELECT queries are stored for future use when an identical query (to the same database and using the same protocol and same character set) is performed.
繰り返しクエリの数と、それらの繰り返しクエリが返す予定のレコードのおおよその数に基づいて、ニーズに合ったクエリキャッシュサイズを選択する必要があります。当分の間、この値を100 MBに設定します: query_cache_size=100M |
query_cache_size=0 (which means it is disabled by default) |
max_connections is the maximum number of simultaneous client connections to the server. We will set this value to 30: max_connections=30Each connection will use a thread, and thus will consume memory. Take this fact into account while setting max_connections. |
max_connections=151 |
thread_cache_size indicates the numbers of threads that the server allocates for reuse after a client disconnects and frees thread(s) previously in use. In this situation, it is cheaper (performance-wise) to reuse a thread than instantiating a new one.
再び、予想される接続数に依存します。この値を安全に最大接続数の半分に設定できます: thread_cache_size=15 |
thread_cache_size=0 (disabled by default) |
CentOSでは、SELinuxに、サービスを再起動する前に非標準ポート(20500)でMariaDBがリッスンできるように許可する必要があります:
# yum install policycoreutils-python # semanage port -a -t mysqld_port_t -p tcp 20500
その後、MariaDBサービスを再起動します。
MariaDBパフォーマンスの調整
私たちの特定のニーズに合わせて構成をチェックおよび調整するために、mysqltuner(データベースサーバーのパフォーマンスを向上させ、安定性を高めるための提案を提供するスクリプト)をインストールできます:
# wget https://github.com/major/MySQLTuner-perl/tarball/master # tar xzf master
次に、tarballから抽出したフォルダに移動してください(正確なバージョンはお使いの場合によって異なる場合があります):
# cd major-MySQLTuner-perl-7dabf27
そして実行してください(管理者のMariaDBアカウントの資格情報を入力するように求められます)
# ./mysqltuner.pl
スクリプトの出力自体は非常に興味深いですが、変更する変数が推奨値とともにリストされている最下部にスキップしましょう:

query_cache_type
設定は、クエリキャッシュが無効(0)または有効(1)かを示します。この場合、mysqltunerは無効にするようアドバイスしています。
では、なぜ今無効にするようアドバイスされているのでしょうか?その理由は、クエリキャッシュが主に高読み取り/低書き込みのシナリオで有用であるため(私たちがデータベースサーバーをインストールしたばかりであるため、私たちの場合ではないからです)。
警告:本番サーバーの構成を変更する前に、mysqltunerによって提案された設定が既存の設定に悪影響を与えないことを確認するために、専門のデータベース管理者に相談することを強くお勧めします。
要約
公式のMariaDBドキュメントを参照するか、MariaDBのパフォーマンスチューニングのヒント:
お見逃しなく: 15の有用なMariaDBパフォーマンスチューニングと最適化のヒント
を参照してください。いつものように、この記事に関するご質問やコメントがあれば、お知らせください。他に使用したいサーバーの設定はありますか?以下のコメントフォームを使用して、コミュニティ全体と共有してください。
Source:
https://www.tecmint.com/install-secure-performance-tuning-mariadb-database-server/