ウェブサイト検索

MYSQL/MariaDB 管理用の 20 の mysqladmin コマンド


mysqladmin は、MySQL/MariaDB サーバーに付属するコマンドライン データベース管理ユーティリティであり、データベース管理者がいくつかの基本的な操作を実行するために使用します。 >MySQL のタスク(root パスワードの設定、root パスワードの変更、mysql プロセスの監視、権限のリロード、データベースの作成/削除、サーバーのステータスの確認、使用状況の統計の表示、実行中のクエリの強制終了など)。

mysqladmin を使用するコマンドと一般的な構文は次のとおりです。

mysqladmin [options] command [command-arg] [command [command-arg]] ...

MySQL/MariaDB サーバーがインストールされていない場合、または古いバージョンの MySQL サーバーを使用している場合は、次のコマンドを使用して MySQL バージョンをインストールまたは更新することをお勧めします。次の記事:

  • RHEL ベースのディストリビューションに MySQL をインストールする方法
  • RHEL および Debian システムに MariaDB をインストールする方法

この記事では、システム/データベース管理者が日常業務で使用する非常に便利な「mysqladmin」コマンドをいくつかまとめました。これらのタスクを実行するには、MySQL/MariaDB サーバーがシステムにインストールされている必要があります。

1. MySQL ルートパスワードを設定する方法

MySQL/MariaDB サーバーを新規インストールしている場合は、root ユーザーとして接続するためにパスワードは必要ありません。 root ユーザーのMySQL パスワードを設定するには、次のコマンドを使用します。

mysqladmin -u root password YOURNEWPASSWORD

警告: mysqladmin を使用して新しい MYSQL パスワードを設定すると、脆弱であるとみなされる必要があります。一部のシステムでは、システム上のアクティブなプロセスのステータスを知るために他のユーザーが実行する可能性のある ps コマンドなどのシステム ステータス プログラムにパスワードが表示されることがあります。

2. MySQL ルートパスワードを変更する方法

MySQL の root パスワードを変更または更新する場合は、次のコマンドを入力する必要があります。たとえば、古いパスワードが 123456 で、それを新しいパスワード xyz123 に変更したいとします。

mysqladmin -u root -p123456 password 'xyz123'

3. MySQLサーバーのステータスを確認する方法

MySQL サーバーが稼働しているかどうかを確認するには、次のコマンドを使用します。

mysqladmin -u root -p ping

Enter password:
mysqld is alive

4. 実行している MySQL バージョンを確認する方法

次のコマンドは、MySQL のバージョンと現在の実行ステータスを表示します。

mysqladmin -u root -p version

Enter password: 
mysqladmin  Ver 9.1 Distrib 10.3.32-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab, and others.

Server version		10.3.32-MariaDB
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/var/lib/mysql/mysql.sock
Uptime:			18 min 6 sec

Threads: 6  Questions: 20  Slow queries: 0  Opens: 18  Flush tables: 1  
Open tables: 11  Queries per second avg: 0.018

5. MySQL サーバーの現在のステータスを確認する方法

MySQL サーバーの現在のステータスを確認するには、次のコマンドを使用します。 mysqladmin コマンドは、 実行中のスレッドクエリ稼働時間のステータスを表示します。

mysqladmin -u root -p status

Enter password: 
Uptime: 1185  Threads: 6  Questions: 21  Slow queries: 0  Opens: 18  Flush tables: 1
Open tables: 11  Queries per second avg: 0.017

6. MySQL ステータス変数とその値を確認する方法

MySQL サーバーの変数と値のすべての実行ステータスを確認するには、次のコマンドを入力します。出力は以下のようになります。

mysqladmin -u root -p extended-status

Enter password: 
+--------------------------------------------------------------+
| Variable_name                          | Value               |
+--------------------------------------------------------------+
| Aborted_clients                        | 0                   |
| Aborted_connects                       | 2                   |
| Access_denied_errors                   | 2                   |
| Acl_column_grants                      | 0                   |
| Acl_database_grants                    | 0                   |
| Acl_function_grants                    | 0                   |
| Acl_procedure_grants                   | 0                   |
| Acl_package_spec_grants                | 0                   |
| Acl_package_body_grants                | 0                   |
| Acl_proxy_users                        | 2                   |
| Acl_role_grants                        | 0                   |
| Acl_roles                              | 0                   |
| Acl_table_grants                       | 0                   |
| Acl_users                              | 4                   |
| Aria_pagecache_blocks_not_flushed      | 0                   |
| Aria_pagecache_blocks_unused           | 15706               |
| Aria_pagecache_blocks_used             | 0                   |
| Aria_pagecache_read_requests           | 0                   |
| Aria_pagecache_reads                   | 0                   |
| Aria_pagecache_write_requests          | 0                   |

...

7. すべての MySQL サーバーの変数と値を確認するにはどうすればよいですか?

MySQL サーバーの実行中の変数と値をすべて表示するには、次のコマンドを使用します。

mysqladmin  -u root -p variables

Enter password:
+--------------------------------------------+-----------------------------+
| Variable_name                              | Value                       |
+--------------------------------------------+-----------------------------+
| auto_increment_increment                   | 1                           |
| auto_increment_offset                      | 1                           |
| autocommit                                 | ON                          |
| automatic_sp_privileges                    | ON                          |
| back_log                                   | 50                          |
| basedir                                    | /usr                        |
| big_tables                                 | OFF                         |
| binlog_cache_size                          | 32768                       |
| binlog_direct_non_transactional_updates    | OFF                         |
| binlog_format                              | STATEMENT                   |
| binlog_stmt_cache_size                     | 32768                       |
| bulk_insert_buffer_size                    | 8388608                     |
| character_set_client                       | latin1                      |
| character_set_connection                   | latin1                      |
| character_set_database                     | latin1                      |
| character_set_filesystem                   | binary                      |
| character_set_results                      | latin1                      |
| character_set_server                       | latin1                      |
| character_set_system                       | utf8                        |
| character_sets_dir                         | /usr/share/mysql/charsets/  |
| collation_connection                       | latin1_swedish_ci           |
+---------------------------------------------------+----------------------+
...

8. MySQLサーバーのアクティブスレッドを確認する方法

次のコマンドは、MySQL データベース クエリの実行中のプロセスをすべて表示します。

mysqladmin -u root -p processlist

Enter password: 
+----+-------------+-----------+----+---------+------+--------------------------+------------------+----------+
| Id | User        | Host      | db | Command | Time | State                    | Info             | Progress |
+----+-------------+-----------+----+---------+------+--------------------------+------------------+----------+
| 2  | system user |           |    | Daemon  |      | InnoDB purge coordinator |                  | 0.000    |
| 1  | system user |           |    | Daemon  |      | InnoDB purge worker      |                  | 0.000    |
| 4  | system user |           |    | Daemon  |      | InnoDB purge worker      |                  | 0.000    |
| 3  | system user |           |    | Daemon  |      | InnoDB purge worker      |                  | 0.000    |
| 5  | system user |           |    | Daemon  |      | InnoDB shutdown handler  |                  | 0.000    |
| 20 | root        | localhost |    | Query   | 0    | Init                     | show processlist | 0.000    |
+----+-------------+-----------+----+---------+------+--------------------------+------------------+----------+

9. MySQL サーバーでデータベースを作成する方法

MySQL サーバーに新しいデータベースを作成するには、以下に示すコマンドを使用します。

mysqladmin -u root -p create tecmint
Enter password: 

mysql -u root -p

Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 10.3.32-MariaDB MariaDB Server

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

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

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| tecmint |
+--------------------+
4 rows in set (0.001 sec)

10. MySQL サーバーでデータベースを削除する方法

MySQL サーバーにデータベースを削除するには、次のコマンドを使用します。確認を求められるので「y」を押します。

mysqladmin -u root -p drop tecmint

Enter password: 
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.

Do you really want to drop the 'tecmint' database [y/N] y
Database "tecmint" dropped

11. MySQL 権限をリロード/リフレッシュするにはどうすればよいですか?

reload コマンドはサーバーに付与テーブルを再ロードするように指示し、refresh コマンドはすべてのテーブルをフラッシュしてログ ファイルを再度開きます。

mysqladmin -u root -p reload
mysqladmin -u root -p refresh

12. MySQL サーバーを安全にシャットダウンする方法

MySQL サーバーを安全にシャットダウンするには、次のコマンドを入力します。

mysqladmin -u root -p shutdown

Enter password:

次のコマンドを使用して、MySQL サーバーを起動/停止することもできます。

systemctl stop mysqld
systemctl start mysqld
Or
systemctl stop mariadb
systemctl start mariadb

13. いくつかの便利な MySQL フラッシュ コマンド

以下に、いくつかの便利なフラッシュ コマンドとその説明を示します。

  • flush-hosts: ホスト キャッシュからすべてのホスト情報をフラッシュします。
  • flush-tables: すべてのテーブルをフラッシュします。
  • flush-threads: すべてのスレッド キャッシュをフラッシュします。
  • flush-logs: すべての情報ログをフラッシュします。
  • flush-privileges: 付与テーブルをリロードします (リロードと同じ)。
  • flush-status: ステータス変数をクリアします。

これらのコマンドを確認してみましょう。

mysqladmin -u root -p flush-hosts
mysqladmin -u root -p flush-tables
mysqladmin -u root -p flush-threads
mysqladmin -u root -p flush-logs
mysqladmin -u root -p flush-privileges
mysqladmin -u root -p flush-status

14. スリープ中の MySQL クライアント プロセスを強制終了するにはどうすればよいですか?

次のコマンドを使用して、スリープ状態のMySQL クライアント プロセスを特定します。

mysqladmin -u root -p processlist

Enter password:
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host      | db | Command | Time | State | Info             |
+----+------+-----------+----+---------+------+-------+------------------+
| 5  | root | localhost |    | Sleep   | 14   |       |			 |
| 8  | root | localhost |    | Query   | 0    |       | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+

ここで、 以下に示すようにkillプロセス ID を指定して次のコマンドを実行します。

mysqladmin -u root -p kill 5

Enter password:
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host      | db | Command | Time | State | Info             |
+----+------+-----------+----+---------+------+-------+------------------+
| 12 | root | localhost |    | Query   | 0    |       | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+

複数のプロセスを強制終了したい場合は、以下に示すようにカンマ区切りでプロセス IDを渡します。

mysqladmin -u root -p kill 5,10

15. 複数の mysqladmin コマンドを一緒に実行する方法

複数の「mysqladmin」コマンドを一緒に実行したい場合、コマンドは次のようになります。

mysqladmin  -u root -p processlist status version

Enter password: 
+----+-------------+-----------+----+---------+------+--------------------------+------------------+----------+
| Id | User        | Host      | db | Command | Time | State                    | Info             | Progress |
+----+-------------+-----------+----+---------+------+--------------------------+------------------+----------+
| 1  | system user |           |    | Daemon  |      | InnoDB purge worker      |                  | 0.000    |
| 2  | system user |           |    | Daemon  |      | InnoDB purge coordinator |                  | 0.000    |
| 4  | system user |           |    | Daemon  |      | InnoDB purge worker      |                  | 0.000    |
| 3  | system user |           |    | Daemon  |      | InnoDB purge worker      |                  | 0.000    |
| 5  | system user |           |    | Daemon  |      | InnoDB shutdown handler  |                  | 0.000    |
| 9  | root        | localhost |    | Query   | 0    | Init                     | show processlist | 0.000    |
+----+-------------+-----------+----+---------+------+--------------------------+------------------+----------+
Uptime: 173  Threads: 6  Questions: 4  Slow queries: 0  Opens: 18  Flush tables: 1  Open tables: 11  Queries per second avg: 0.023
mysqladmin  Ver 9.1 Distrib 10.3.32-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab, and others.

Server version		10.3.32-MariaDB
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/var/lib/mysql/mysql.sock
Uptime:			2 min 53 sec

Threads: 6  Questions: 4  Slow queries: 0  Opens: 18  Flush tables: 1  Open tables: 11  Queries per second avg: 0.023

16. リモート MySQL サーバーに接続する方法

リモートの MySQL サーバーに接続するには、リモートの IP アドレスを指定した -h (host) を使用します。機械。

mysqladmin -h 172.16.25.126 -u root -p

17. リモート MySQL サーバーでコマンドを実行する方法

リモートのMySQL サーバーのステータスを確認したいとします。その場合のコマンドは次のようになります。

mysqladmin -h 172.16.25.126 -u root -p status

18. スレーブサーバーで MySQL レプリケーションを開始/停止する方法

スレーブサーバーで MySQL レプリケーションを開始/停止するには、次のコマンドを使用します。

mysqladmin  -u root -p start-slave
mysqladmin  -u root -p stop-slave

19. MySQL サーバーのデバッグ情報をログに保存する方法

これは、使用中のロック、使用済みメモリ、およびクエリの使用状況に関するデバッグ情報を、イベント スケジューラに関する情報を含むMySQL ログ ファイルに書き込むようにサーバーに指示します。

mysqladmin  -u root -p debug

Enter password:

20. mysqladmin のオプションと使用法を表示する方法

myslqadmin コマンドの詳細なオプションと使用法を確認するには、以下に示す help コマンドを使用します。利用可能なオプションのリストが表示されます。

mysqladmin --help

この記事では、ほぼすべての「mysqladmin」コマンドとその例を含めるように最善を尽くしました。それでも何か見逃している場合は、コメントでお知らせください。友達と共有するのを忘れてください。