ウェブサイト検索

MariaDB データベース サーバーのインストール、安全性、パフォーマンス チューニングの方法


データベース サーバーは、今日のアプリケーションに必要なネットワーク インフラストラクチャの重要なコンポーネントです。 (必要な場合に) データを保存、取得、更新、削除する機能がなければ、Web アプリとデスクトップ アプリの有用性と範囲は非常に限られてしまいます。

さらに、データベース サーバーを (期待どおりに動作するように) インストール、管理、構成する方法を知ることは、すべてのシステム管理者が備えておく必要のある必須のスキルです。

この記事では、MariaDB データベース サーバーをインストールして保護する方法を簡単に確認し、その後、その構成方法について説明します。

MariaDB サーバーのインストールとセキュリティ保護

CentOS 7.x では、MySQL が MariaDB に置き換わりました。MySQL は現在も (MariaDB とともに) Ubuntu に含まれています。同じことがopenSUSEにも当てはまります。

簡潔にするために、このチュートリアルでは MariaDB のみを使用しますが、名前と開発哲学が異なることに加えて、両方のリレーショナル データベース管理システム (RDBMS) も使用することに注意してください。略してstrong>)はほぼ同じです。

これは、クライアント側のコマンドが MySQLMariaDB の両方で同じであり、構成ファイルの名前と場所が同じであることを意味します。

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 root ユーザーのパスワードの入力を求められることに注意してください。

上記のパッケージをインストールしたら、データベース サービスが実行中であり、起動時に開始するようにアクティブ化されていることを確認します (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 スクリプトを実行します。このプロセスにより、次のことが可能になります。

  1. RDBMS root ユーザーのパスワードを設定/リセットする
  2. 匿名ログインを削除します (これにより、有効なアカウントを持つユーザーのみが RDBMS にログインできるようになります)
  3. localhost 以外のマシンの root アクセスを無効にする
  4. テスト データベース (誰でもアクセスできる) を削除します。
  5. 1 ~ 4 に関連付けられた変更を有効にします。

このプロセスの詳細については、「RHEL/CentOS/Fedora および Debian/Ubuntu への MariaDB データベースのインストール」の「インストール後のセクション」を参照してください。

MariaDB サーバーの構成

デフォルトの構成オプションは、指定された順序で次のファイルから読み取られます: /etc/mysql/my.cnf/etc/my.cnf、および ~ /.my.cnf

ほとんどの場合、/etc/my.cnf のみが存在します。このファイルにサーバー全体の設定を設定します (各ユーザーの ~/.my.cnf 内の同じ設定で上書きできます)。

my.cnf について最初に注意する必要があるのは、設定がカテゴリ (またはグループ) に編成されており、各カテゴリ名が角括弧で囲まれていることです。

サーバー システムの設定は [mysqld] セクションに記載されています。通常、以下の表には最初の 2 つの設定のみが表示されます。残りはその他の頻繁に使用されるオプションです (指定されている場合は、デフォルト値を選択したカスタム値に変更します)。

Setting and description

デフォルト値

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.

ソケット=/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).

We will change this to instruct the service to listen only on its main address (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.

We will replace the default value(3306) with 20500 (but we need to make sure nothing else is using that port):
port=20500

While some people will argue that security through obscurity is not good practice, changing the default application ports for higher ones is a rudimentary -yet effective- method to discourage port scans.

ポート=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.

We will replace the default value with 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.

Unless you require hostnames to determine permissions, it is advisable to disable this variable (in order to speed up connections and queries) by setting its value to 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.

You should choose a query cache size that matches your needs based on 1) the number of repetitive queries, and 2) the approximate number of records those repetitive queries are expected to return. We will set this value to 100 MB for the time being:

query_cache_size=100M

query_cache_size=0 (デフォルトでは無効であることを意味します)

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.

Again, this depends on the number of connections you are expecting. We can safely set this value to half the number of max_connections:

thread_cache_size=15

thread_cache_size=0 (デフォルトでは無効)

CentOS では、MariaDB が非標準ポート (20500) でリッスンできるように SELinux に指示する必要があります。 ) サービスを再起動する前に:

# 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 の公式ドキュメントを参照するか、MariaDB のパフォーマンス チューニングのヒントを参照してください。

お見逃しなく: MariaDB のパフォーマンス チューニングと最適化に役立つ 15 のヒント

いつものように、この記事に関してご質問やコメントがございましたら、お気軽にお知らせください。他に使用したいサーバー設定はありますか?以下のコメントフォームを使用して、コミュニティの他のメンバーと自由に共有してください。

関連記事: