0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

個人的備忘録:MySQLの接続上限エラーを防ぐ max_connections の設定と確認方法

Posted at

はじめに

本記事では、MySQLサーバにおける同時接続数(max_connections)を500に設定する方法について解説します。

個人の備忘録程度の走り書きとなっておりますが、温かい目で見守っていただければ幸いです。

接続数が不足してアプリケーション側でエラーが発生するなど、実運用において重要なパラメータのひとつです。

書こうと思ったきっかけ

MySQLへの接続が集中し "Too many connections" エラーが発生するケースのことを考えてみました。その対応として接続上限の調整を行い、エラーの解消する必要があると思いましたので、備忘録として整理しておきます。

実行内容

稼働中のサーバで以下のように接続数上限を変更しました:

set global max_connections=500;

設定後、以下のコマンドで反映されたかを確認します:

show global variables like '%connection%';

表示結果の例:

+--------------------------+-----------------+
| Variable_name            | Value           |
+--------------------------+-----------------+
| character_set_connection | utf8            |
| collation_connection     | utf8_general_ci |
| max_connections          | 500             |
| max_user_connections     | 0               |
+--------------------------+-----------------+
4 rows in set (0.00 sec)

永続化のための設定(自分調べ)

このままだとサーバ再起動時に設定が元に戻ってしまうため、永続的な変更も行います。

設定ファイル(例:/etc/my.cnf)に以下の行を追加または編集します:

[mysqld]
max_connections = 500

設定を反映させるには、mysqld の再起動が必要です:

sudo systemctl restart mysqld

または MariaDB を使用している場合:

sudo systemctl restart mariadb

まとめ

  • set global コマンドで一時的に接続数を変更可能
  • my.cnf に追記することで永続化
  • サーバ再起動後も反映されるようにするには設定ファイル編集が必要

運用中のシステムで接続数エラーが発生していた場合、今回の設定で解消できる可能性がありますので、参考にしてみてください...!

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?