{"id":105,"date":"2016-04-26T22:18:50","date_gmt":"2016-04-26T14:18:50","guid":{"rendered":"http:\/\/pony.blog.myhz.com\/?p=105"},"modified":"2023-06-02T21:08:05","modified_gmt":"2023-06-02T13:08:05","slug":"mysql%e6%8e%88%e6%9d%83","status":"publish","type":"post","link":"https:\/\/lnmp.ivan.xin\/?p=105","title":{"rendered":"mysql\u6388\u6743"},"content":{"rendered":"<blockquote><p>\u67e5\u8be2mysql\u7248\u672c<br \/>\nmysql&gt; SELECT version();<br \/>\nmysql 8 \u5fc5\u987b\u52063\u6b65\u6765\u5b9e\u73b0\u8bbe\u7f6e\u7528\u6237\u6743\u9650\uff1a<br \/>\n\uff081\uff09\u5148\u521b\u5efa\u7528\u6237<br \/>\n\uff082\uff09\u518d\u5bf9\u8be5\u7528\u6237\u5206\u914d\u7528\u6237\u6743\u9650<br \/>\n\uff083\uff09\u6700\u540e\u5237\u65b0\u6743\u9650<br \/>\nmysql&gt; create user test@'localhost' identified by '123456';<br \/>\nmysql&gt; grant all privileges on test.* to test@'localhost';<br \/>\nmysql&gt; flush privileges;<\/p><\/blockquote>\n<p>\u6dfb\u52a0\u4e00\u4e2a\u672c\u5730\u7528\u6237\uff0c\u540d\u4e3ausername\u5bc6\u7801\u4e3apassword<br \/>\nGRANT ALL PRIVILEGES ON *.* TO username@localhost IDENTIFIED BY 'password' WITH GRANT OPTION;<\/p>\n<p>\u6dfb\u52a0\u4e00\u4e2a\u8fdc\u7a0b\u7528\u6237\uff0c\u540d\u4e3ausername\u5bc6\u7801\u4e3apassword<br \/>\nGRANT create,select,insert,update,delete ON *.* TO username@\"%\" IDENTIFIED BY 'password' WITH GRANT OPTION;<br \/>\nGRANT ALL PRIVILEGES ON database.* TO username@\"111.111.111.111\" IDENTIFIED BY 'password' WITH GRANT OPTION;<br \/>\nGRANT ALL PRIVILEGES ON *.* TO username@\"%\" IDENTIFIED BY 'password' WITH GRANT OPTION;<\/p>\n<p>mysql\u521b\u5efa\u548c\u5220\u9664\u7528\u6237\u65b9\u6cd5<br \/>\n\u5173\u952e\u5b57: mysql\u521b\u5efa\u548c\u5220\u9664\u7528\u6237\u65b9\u6cd5<br \/>\n\u547d\u4ee4\u65b9\u5f0f\u7684.\u6ce8\u610f\u6bcf\u884c\u540e\u8fb9\u90fd\u8ddf\u4e2a \uff1b \u8868\u793a\u4e00\u4e2a\u547d\u4ee4\u8bed\u53e5\u7ed3\u675f.<br \/>\n\u683c\u5f0f\uff1agrant select on \u6570\u636e\u5e93.* to \u7528\u6237\u540d@\u767b\u5f55\u4e3b\u673a identified by \"\u5bc6\u7801\"<\/p>\n<p>\u4f8b1\u3001\u589e\u52a0\u4e00\u4e2a\u7528\u6237test1\u5bc6\u7801\u4e3aabc\uff0c\u8ba9\u4ed6\u53ef\u4ee5\u5728\u4efb\u4f55\u4e3b\u673a\u4e0a\u767b\u5f55\uff0c\u5e76\u5bf9\u6240\u6709\u6570\u636e\u5e93\u6709\u67e5\u8be2\u3001\u63d2\u5165\u3001\u4fee\u6539\u3001\u5220\u9664\u7684\u6743\u9650\u3002\u9996\u5148\u7528\u4ee5root\u7528\u6237\u8fde\u5165MYSQL\uff0c\u7136\u540e\u952e\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<br \/>\ngrant select,insert,update,delete on *.* to test1@\"%\" Identified by \"abc\";<br \/>\n\u4f46\u4f8b1\u589e\u52a0\u7684\u7528\u6237\u662f\u5341\u5206\u5371\u9669\u7684\uff0c\u4f60\u60f3\u5982\u67d0\u4e2a\u4eba\u77e5\u9053test1\u7684\u5bc6\u7801\uff0c\u90a3\u4e48\u4ed6\u5c31\u53ef\u4ee5\u5728internet\u4e0a\u7684\u4efb\u4f55\u4e00\u53f0\u7535\u8111\u4e0a\u767b\u5f55\u4f60\u7684mysql\u6570\u636e\u5e93\u5e76\u5bf9\u4f60\u7684\u6570\u636e\u53ef\u4ee5\u4e3a\u6240\u6b32\u4e3a\u4e86\uff0c\u89e3\u51b3\u529e\u6cd5\u89c1\u4f8b2\u3002<br \/>\n\u4f8b2\u3001\u589e\u52a0\u4e00\u4e2a\u7528\u6237test2\u5bc6\u7801\u4e3aabc,\u8ba9\u4ed6\u53ea\u53ef\u4ee5\u5728localhost\u4e0a\u767b\u5f55\uff0c\u5e76\u53ef\u4ee5\u5bf9\u6570\u636e\u5e93mydb\u8fdb\u884c\u67e5\u8be2\u3001\u63d2\u5165\u3001\u4fee\u6539\u3001\u5220\u9664\u7684\u64cd\u4f5c\uff08localhost\u6307\u672c\u5730\u4e3b\u673a\uff0c\u5373MYSQL\u6570\u636e\u5e93\u6240\u5728\u7684\u90a3\u53f0\u4e3b\u673a\uff09\uff0c\u8fd9\u6837\u7528\u6237\u5373\u4f7f\u7528\u77e5\u9053test2\u7684\u5bc6\u7801\uff0c\u4ed6\u4e5f\u65e0\u6cd5\u4eceinternet\u4e0a\u76f4\u63a5\u8bbf\u95ee\u6570\u636e\u5e93\uff0c\u53ea\u80fd\u901a\u8fc7MYSQL\u4e3b\u673a\u4e0a\u7684web\u9875\u6765\u8bbf\u95ee\u4e86\u3002<br \/>\ngrant select,insert,update,delete on mydb.* to test2@localhost identified by \"abc\";<br \/>\n\u5982\u679c\u4f60\u4e0d\u60f3test2\u6709\u5bc6\u7801\uff0c\u53ef\u4ee5\u518d\u6253\u4e00\u4e2a\u547d\u4ee4\u5c06\u5bc6\u7801\u6d88\u6389\u3002<br \/>\ngrant select,insert,update,delete on mydb.* to test2@localhost identified by \"\";<\/p>\n<p>\u64a4\u6743\u5e76\u5220\u9664\u7528\u6237<br \/>\n\u8981\u53d6\u6d88\u4e00\u4e2a\u7528\u6237\u7684\u6743\u9650\uff0c\u4f7f\u7528REVOKE\u8bed\u53e5\u3002REVOKE\u7684\u8bed\u6cd5\u975e\u5e38\u7c7b\u4f3c\u4e8eGRANT\u8bed\u53e5\uff0c\u9664\u4e86TO\u7528FROM\u53d6\u4ee3\u5e76\u4e14\u6ca1\u6709INDETIFED BY\u548cWITH GRANT OPTION\u5b50\u53e5\uff1a<br \/>\nREVOKE privileges (columns) ON what FROM user<br \/>\n\u4f8b\u5982\uff1arevoke usage on *.* from username@localhost;<\/p>\n<p>grant, revoke \u7528\u6237\u6743\u9650\u540e\uff0c\u8be5\u7528\u6237\u53ea\u6709\u91cd\u65b0\u8fde\u63a5 MySQL \u6570\u636e\u5e93\uff0c\u6743\u9650\u624d\u80fd\u751f\u6548\u3002<\/p>\n<p>user\u90e8\u5206\u5fc5\u987b\u5339\u914d\u539f\u6765GRANT\u8bed\u53e5\u7684\u4f60\u60f3\u64a4\u6743\u7684\u7528\u6237\u7684user\u90e8\u5206\u3002privileges\u90e8\u5206\u4e0d\u9700\u5339\u914d\uff0c\u4f60\u53ef\u4ee5\u7528GRANT\u8bed\u53e5\u6388\u6743\uff0c\u7136\u540e\u7528REVOKE\u8bed\u53e5\u53ea\u64a4\u9500\u90e8\u5206\u6743\u9650\u3002<br \/>\nREVOKE\u8bed\u53e5\u53ea\u5220\u9664\u6743\u9650\uff0c\u800c\u4e0d\u5220\u9664\u7528\u6237\u3002\u5373\u4f7f\u4f60\u64a4\u9500\u4e86\u6240\u6709\u6743\u9650\uff0c\u5728user\u8868\u4e2d\u7684\u7528\u6237\u8bb0\u5f55\u4f9d\u7136\u4fdd\u7559\uff0c\u8fd9\u610f\u5473\u7740\u7528\u6237\u4ecd\u7136\u53ef\u4ee5\u8fde\u63a5\u670d\u52a1\u5668\u3002\u8981\u5b8c\u5168\u5220\u9664\u4e00\u4e2a\u7528\u6237\uff0c\u4f60\u5fc5\u987b\u7528\u4e00\u6761DELETE\u8bed\u53e5\u660e\u786e\u4eceuser\u8868\u4e2d\u5220\u9664\u7528\u6237\u8bb0\u5f55\uff1a<\/p>\n<p>use user;<br \/>\nDELETE FROM user WHERE User='user_name' and Host='host_name';<\/p>\n<p>FLUSH PRIVILEGES;<br \/>\nDELETE\u8bed\u53e5\u5220\u9664\u7528\u6237\u8bb0\u5f55\uff0c\u800cFLUSH\u8bed\u53e5\u544a\u8bc9\u670d\u52a1\u5668\u91cd\u8f7d\u6388\u6743\u8868\u3002\uff08\u5f53\u4f60\u4f7f\u7528GRANT\u548cREVOKE\u8bed\u53e5\u65f6\uff0c\u8868\u81ea\u52a8\u91cd\u8f7d\uff0c\u800c\u4f60\u76f4\u63a5\u4fee\u6539\u6388\u6743\u8868\u65f6\u4e0d\u662f\u3002\uff09<br \/>\n\u5206\u6790\u7528\u6237\u6388\u6743\uff1a<br \/>\nSELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;<\/p>\n<p>select User, Host from user;<br \/>\nshow grants for username@localhost;<br \/>\ndelete from user where User='username' and Host='localhost';<br \/>\nFLUSH PRIVILEGES;<br \/>\nshow grants for username@localhost;<br \/>\nselect * from mysql.user where user='username'\\G<br \/>\nselect * from mysql.db where user='username'\\G<\/p>\n<p>=========================================<\/p>\n<p>\u7b80\u5355\u8bf4\uff0cmysql\u5e93\u662fmysql\u7cfb\u7edf\u7684\u7ba1\u7406\u6570\u636e\u5e93\uff0c\u5b83\u91cc\u9762\u5b58\u653e\u4e86\u7528\u6237\u4fe1\u606f\u548c\u914d\u7f6e\u4fe1\u606f\uff0c\u5176\u4e2d\u6700\u4e3b\u8981\u7684\u4e24\u4e2a\uff1a<br \/>\nmysql.user\uff1a\u7528\u6237\u5e93\uff0c\u91cc\u9762\u6709\u5168\u5c40\u7684\u6743\u9650\u3002<br \/>\nmysql.db \uff1a\u6309\u5e93\u6765\u6307\u5b9a\u7684\u6743\u9650<\/p>\n<p>but:\u7528grant\u628a\u4e00\u4e2a\u7528\u6237\u52a0\u4e86\u597d\u51e0\u4e2a\u5e93\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u7136\u540e\u4e0d\u7ba1\u662f\u7528revoke\u5220\u9664\u6743\u9650\uff0c\u8fd8\u662f\u7528delete\u5220\u9664\u7528\u6237\u540e\uff0c\u7136\u540e\u518d\u968f\u4fbf\u628a\u8fd9\u4e2a\u7528\u6237\u8d4b\u4e88\u67d0\u4e00\u4e2a\u5e93\u7684\u6743\u9650\u540e\uff0c\u8fd9\u4e2a\u7528\u6237\u4ee5\u524d\u6240\u6709\u7684\u6743\u9650\u90fd\u51fa\u6765\u4e86\u3002<br \/>\n\u6ca1\u529e\u6cd5\u89e3\u51b3\uff0c\u53ea\u80fd\u4ecemysql.db\u4e2d\u7528\u547d\u4ee4\u5220\u9664\u6570\u636e<br \/>\ndelete from mysql.db where User='username' and Host='173.234.50.142';<br \/>\ndelete from mysql.user where User='username' and Host='173.234.50.142';<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u8be2mysql\u7248\u672c mysql&gt; SELECT version(); mysql 8 \u5fc5\u987b\u52063\u6b65\u6765\u5b9e\u73b0\u8bbe...<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15330],"tags":[],"class_list":["post-105","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=\/wp\/v2\/posts\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=105"}],"version-history":[{"count":2,"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":740,"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions\/740"}],"wp:attachment":[{"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}