2020年5月29日金曜日

テーブルの使用容量確認

時々テーブルのデータが蓄積しすぎでディスク容量を圧迫されることがあります。その時以下のコマンドを使えばテーブル毎の容量、行数など情報をみることができます。

SELECT
  table_name,
  engine,
  table_rows,
  (data_length+index_length)/1024/1024/1024 as allGB,
  (data_length)/1024/1024/1024 as dataGB,
  (index_length)/1024/1024/1024 as indexGB
FROM
  information_schema.tables
WHERE
  table_schema=database()
ORDER BY
  (data_length+index_length) DESC;
+--------------------+--------+------------+-----------------+-----------------+----------------+
| TABLE_NAME         | ENGINE | TABLE_ROWS | allGB           | dataGB          | indexGB        |
+--------------------+--------+------------+-----------------+-----------------+----------------+
user           | InnoDB |   33543611 | 17.791976928711 | 16.137695312500 | 1.654281616211 |
| product       | InnoDB |      43979 |  0.893157958984 |  0.893157958984 | 0.000000000000 |
| comment | InnoDB |          0 |  0.000244140625 |  0.000015258789 | 0.000228881836 |
| category | InnoDB |          0 |  0.000244140625 |  0.000015258789 | 0.000228881836 |
+--------------------+--------+------------+-----------------+-----------------+----------------+

先程テーブル単位のデータ容量を確認できましたが、以下のクエリーを使えばデータベース単位での容量も確認できます。

SELECT
    table_schema, sum(data_length) /1024/1024/1024 AS GB
FROM
    information_schema.tables 
GROUP BY
    table_schema
ORDER BY      
    sum(data_length+index_length) DESC;
+---------------------+-----------------+
| table_schema        | GB              |
+---------------------+-----------------+
| bbs                 | 81.270685018040 |
user             | 13.613167230971 |
| product             |  1.102905860171 |
| test                |  0.629666061141 |
| log          |  0.017431590706 |
| auth                |  0.003235515207 |
| information_schema  |  0.000000000000 |
+---------------------+-----------------+


2020年5月20日水曜日

MACで複数サーバに別タブを開いて一括SSHする

同時に複数サーバにSSHする必要な時があります。その都度新しいターミナルを開いて一台ずつログインするのは手間です。

以下を使えば新しいタブを開いて自動的にSSHできます。

#!/bin/bash
 
# 一台目
osascript <<EOF
tell app "Terminal"
  tell application "System Events" to keystroke "t" using command down
end tell
tell application "Terminal"
  do script "ssh -oStrictHostKeyChecking=no -o ServerAliveInterval=30 -i ssh接続用キー ログインユーザー@サーバIP" in front window
end tell
EOF
 
# 二台目
osascript <<EOF
tell app "Terminal"
  tell application "System Events" to keystroke "t" using command down
end tell
tell application "Terminal"
  do script "ssh -oStrictHostKeyChecking=no -o ServerAliveInterval=30 -i ssh接続用キー ログインユーザー@サーバIP" in front window
end tell
EOF

解説

tell app “Terminal”
tell application “System Events” to keystroke “t” using command down
end tell

ターミナルで「⌘」+「t」を打つ。新しいタブを開くようになります。

 

do script “xxx”

開いたターミナルの中に、スクリプトを実行

-oStrictHostKeyChecking

初めてのサーバーに接続するとき、「Are you sure you want to continue connecting (yes/no)?」を聞かれるので、こちらで設定すると聞かれなくなります。