Skip to content

Instantly share code, notes, and snippets.

@nakunaru
Last active August 29, 2015 14:10
Show Gist options
  • Save nakunaru/1e0a0ebbfca48d67e112 to your computer and use it in GitHub Desktop.
Save nakunaru/1e0a0ebbfca48d67e112 to your computer and use it in GitHub Desktop.
MySQLでアクセス可能なテーブルのリストを取得する

MySQLでアクセス可能なテーブルのリストを取得する

権限の種類に関わらず(Select, Insert, ... etc)アクセス可能なテーブル/view

information_schema.tables はアクセス可能なものだけを返してくれるので、そのまま検索する。

ディクショナリ情報込み

SELECT *
FROM   information_schema.tables;

ディクショナリ情報以外に絞り込み

SELECT *
FROM   information_schema.tables
WHERE  table_schema NOT IN ('mysql', 'information_schema', 'performance_schema');

特定の権限を持つテーブル/View

mysql.tablesにテーブル名と、保有する権限名が格納されているが、 グローバル・レベルで付与された権限、DBレベルで付与された権限は含まれないため、 information_schema.tables, mysql.user, mysql.db, mysql.tables_privから必要な情報を取得し絞り込む。

SELECT table_schema,
       table_name,
       table_comment
FROM
      (SELECT it.table_schema,
              it.table_name,
              it.table_comment,
              (select select_priv
               from   mysql.user mu
               where  mu.user = 'カレント・ユーザー名'
               and    mu.host = 'ホスト名') as g_level,
              (select select_priv
               from   mysql.db md
               where  md.user = 'カレント・ユーザー名'
               and    md.host = 'ホスト名'
               and    md.db = it.table_schema) as db_level,
              (select table_priv
               from   mysql.tables_priv mt
               where  mt.user = 'カレント・ユーザー名'
               and    mt.host = 'ホスト名'
               and    mt.table_priv = 'Select'
               and    mt.table_name = it.table_name) as t_level
        FROM   information_schema.tables it
        WHERE  it.table_schema NOT IN('information_schema', 'mysql', 'performance_schema')) all_tables
WHERE   g_level = 'Y'
OR      db_level = 'Y'
OR      t_level = 'Select';

カレント・ユーザー名 : 現在のセッションのユーザー名
ホスト名 : 現在のセッションのホスト名
mt.table_priv='Select' SELECT select_priv t_level = 'Select' : 探したい権限に合わせる

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment