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)?」を聞かれるので、こちらで設定すると聞かれなくなります。

2020年5月17日日曜日

kubernetesのcronjobをすぐ実行する方法

方法1

一時実行用ジョブを作成

kubectl create job ジョブ名 --from=cronjob/元のcronjob名
例↓ 例えば元のcronjobの名前はlog_rotate、すぐ実行してほしい名前はonetime_job
kubectl create job onetime_job --from=cronjob/log_rotate

上記のコマンドで「onetime_job」というジョブが作成られてそしてすぐ実行されます。

方法2

ステップ1、cronjobの実行履歴を確認
kubectl get pod | grep cronjobの名前

上記のコマンドで実行済みのジョブ履歴を表示することができます。

ステップ2、実行履歴からイメージを取得
kubectl describe pod ステップ1で取得したpod名
ステップ3、テスト用PODを作成
kubectl run test --image=ステップ2で取得したイメージ名 --restart=Never --command sleep infinity
ステップ4、PODにログインして手動でジョブを実行
kubectl exec -it test bash
# ジョブを実行
ステップ5、最後テストPODを削除
kubectl delete pod test