10 mysql-fix-orphan-privileges.php - Suggest SQL commands to clean up unused records in system tables which hold permission data
17 error_reporting(E_ALL
& ~E_DEPRECATED
);
18 ini_set('display_errors', true);
22 echo "Usage: {$argv[0]} host[:port] user password
24 This tool outputs sql commands to delete orphan records in privilege-related
25 tables, which belong to nonexiststent mysql users. This tool does not modify
26 any privileges, just suggests commands.
28 Connecting user must have SELECT privilege for `user`, `db`, `tables_priv`
29 and `columns_priv` tables in mysql database. You need DELETE privilege for
30 those last three tables in order to apply changes.
35 if(!mysql_connect($argv[1], $argv[2], $argv[3])) die();
36 mysql_select_db("mysql");
39 function table($query)
42 $result = mysql_query($query);
43 if(!$result) return NULL;
44 while($row = mysql_fetch_assoc($result))
48 mysql_free_result($result);
52 foreach(array("db", "tables_priv", "columns_priv") as $table)
54 $grants[$table] = table("SELECT user, host FROM $table");
59 foreach($grants as $table => $users)
61 foreach($users as $user_data)
63 $user = $user_data["user"];
64 $host = $user_data["host"];
65 $user_safe = mysql_escape_string($user);
66 $host_safe = mysql_escape_string($host);
68 $result = mysql_query("SELECT 1 FROM user WHERE user='$user_safe' AND host='$host_safe' LIMIT 1");
69 if($result and mysql_num_rows($result) == 0)
71 echo "DELETE FROM $table WHERE user='$user_safe' AND host='$host_safe';\n";
73 mysql_free_result($result);
77 echo "FLUSH PRIVILEGES;\n";