{"id":511,"date":"2017-03-16T10:18:05","date_gmt":"2017-03-16T15:18:05","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=511"},"modified":"2017-03-16T10:18:05","modified_gmt":"2017-03-16T15:18:05","slug":"quiz-drop-non-global-users","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=511","title":{"rendered":"Quiz: Drop non-global users"},"content":{"rendered":"<p>Somebody asked on Freenode. I don&#8217;t know why they wanted it. How would you drop all MySQL users who do not have &#8220;GRANT ALL ON *.* &#8230; WITH GRANT OPTION&#8221;? That is, drop any users who have &#8216;N&#8217; in any of the privilege columns in `mysql`.`user`.<\/p>\n<p>My solution shown below. Did you think of a different approach?<\/p>\n<p><spoiler 'My solution' 'Hide'>I used SQL to build SQL which built more SQL. Get the list of &#8216;priv&#8217; columns from information_schema.columns. Build a query that looks for &#8216;N&#8217; in any of those columns (it feels funny to search for a constant in a list of fields instead of a field in a list of constants, but it&#8217;s perfectly legal). Use the results to build the DROP USER statement.<\/p>\n<pre>mysql -BNe \"SELECT CONCAT('SELECT CONCAT(''DROP USER '', QUOTE(user), ''@'', QUOTE(Host), '';'') FROM mysql.user WHERE ''N'' IN (', GROUP_CONCAT(column_name), ')') FROM information_schema.columns WHERE table_schema = 'mysql' AND table_name = 'user' AND column_name LIKE '%priv';\" | mysql -BN | mysql<\/pre>\n<p><\/spoiler><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Somebody asked on Freenode. I don&#8217;t know why they wanted it. How would you drop all MySQL users who do not have &#8220;GRANT ALL ON *.* &#8230; WITH GRANT OPTION&#8221;? That is, drop any users who have &#8216;N&#8217; in any of the privilege columns in `mysql`.`user`. My solution shown below. Did you think of a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4,11],"tags":[],"class_list":["post-511","post","type-post","status-publish","format-standard","hentry","category-mysql","category-one-liners"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-8f","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=511"}],"version-history":[{"count":4,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/511\/revisions"}],"predecessor-version":[{"id":515,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/511\/revisions\/515"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}