{"id":199614,"date":"2025-05-06T09:48:35","date_gmt":"2025-05-06T01:48:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/199614\/"},"modified":"2025-05-06T09:48:35","modified_gmt":"2025-05-06T01:48:35","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8sql%e8%af%ad%e5%8f%a5%e5%9c%a8mysql%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e6%9d%83%e9%99%90%e5%92%8c%e7%94%a8%e6%88%b7%e7%ae%a1%e7%90%86%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/199614\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u5728MySQL\u4e2d\u8fdb\u884c\u6570\u636e\u6743\u9650\u548c\u7528\u6237\u7ba1\u7406\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u5728MySQL\u4e2d\u8fdb\u884c\u6570\u636e\u6743\u9650\u548c\u7528\u6237\u7ba1\u7406\uff1f<\/h1>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u5728MySQL\u4e2d\u8fdb\u884c\u6570\u636e\u6743\u9650\u548c\u7528\u6237\u7ba1\u7406\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p>\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u5728MySQL\u4e2d\u8fdb\u884c\u6570\u636e\u6743\u9650\u548c\u7528\u6237\u7ba1\u7406\uff1f<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u6570\u636e\u6743\u9650\u548c\u7528\u6237\u7ba1\u7406\u662f\u6570\u636e\u5e93\u7ba1\u7406\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u73af\u8282\u3002\u5728MySQL\u6570\u636e\u5e93\u4e2d\uff0c\u901a\u8fc7SQL\u8bed\u53e5\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u6743\u9650\u7684\u63a7\u5236\u548c\u7528\u6237\u7ba1\u7406\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u5728MySQL\u4e2d\u8fdb\u884c\u6570\u636e\u5e93\u6743\u9650\u548c\u7528\u6237\u7ba1\u7406\u3002<\/p>\n<p>\u4e00\u3001\u6570\u636e\u6743\u9650\u7ba1\u7406<\/p>\n<ol>\n<li>\u521b\u5efa\u7528\u6237\u5e76\u6388\u6743<br \/>\u5728MySQL\u4e2d\uff0c\u901a\u8fc7CREATE USER\u8bed\u53e5\u53ef\u4ee5\u521b\u5efa\u65b0\u7528\u6237\uff0c\u5e76\u4f7f\u7528GRANT\u8bed\u53e5\u5bf9\u7528\u6237\u8fdb\u884c\u6388\u6743\u3002<\/li>\n<\/ol>\n<pre>CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';<\/pre>\n<p>\u4e0a\u8ff0\u8bed\u53e5\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3ausername\u7684\u7528\u6237\uff0c\u5e76\u6307\u5b9a\u5bc6\u7801\u4e3apassword\u3002\u5176\u4e2d\uff0c&#8217;localhost&#8217;\u8868\u793a\u8be5\u7528\u6237\u53ea\u80fd\u4ece\u672c\u5730\u8bbf\u95ee\u6570\u636e\u5e93\u3002\u5982\u679c\u60f3\u5141\u8bb8\u8fdc\u7a0b\u8bbf\u95ee\uff0c\u53ef\u4ee5\u4f7f\u7528&#8217;%&#8217;\u3002<\/p>\n<pre>GRANT privileges ON database.table TO 'username'@'localhost';<\/pre>\n<p>\u4e0a\u8ff0\u8bed\u53e5\u4e2d\uff0cprivileges\u8868\u793a\u6388\u6743\u7684\u6743\u9650\uff0c\u53ef\u4ee5\u662fSELECT\u3001INSERT\u3001UPDATE\u7b49\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528ALL\u8868\u793a\u6240\u6709\u6743\u9650\u3002database.table\u8868\u793a\u8981\u6388\u6743\u7684\u6570\u636e\u5e93\u548c\u8868\u540d\u3002<\/p>\n<ol>\n<li>\u64a4\u9500\u7528\u6237\u6743\u9650<br \/>\u5982\u679c\u60f3\u64a4\u9500\u7528\u6237\u7684\u67d0\u4e2a\u6743\u9650\uff0c\u53ef\u4ee5\u4f7f\u7528REVOKE\u8bed\u53e5\u3002<\/li>\n<\/ol>\n<pre>REVOKE privilege ON database.table FROM 'username'@'localhost';<\/pre>\n<p>\u4e0a\u8ff0\u8bed\u53e5\u4e2d\uff0cprivilege\u3001database\u548ctable\u4e0e\u6388\u6743\u8bed\u53e5\u4e2d\u7684\u542b\u4e49\u76f8\u540c\u3002<\/p>\n<ol>\n<li>\u5237\u65b0\u6743\u9650<br \/>\u5728MySQL\u4e2d\u4fee\u6539\u7528\u6237\u6743\u9650\u540e\uff0c\u9700\u8981\u4f7f\u7528FLUSH PRIVILEGES\u8bed\u53e5\u5237\u65b0\u6743\u9650\uff0c\u4f7f\u4fee\u6539\u7acb\u5373\u751f\u6548\u3002<\/li>\n<\/ol>\n<pre>FLUSH PRIVILEGES;<\/pre>\n<p>\u4e8c\u3001\u7528\u6237\u7ba1\u7406<\/p>\n<ol>\n<li>\u67e5\u770b\u7528\u6237\u5217\u8868<br \/>\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u4e2d\u7684\u7528\u6237\u5217\u8868\u3002<\/li>\n<\/ol>\n<pre>SELECT user, host FROM mysql.user;<\/pre>\n<ol>\n<li>\u4fee\u6539\u7528\u6237\u5bc6\u7801<br \/>\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\u53ef\u4ee5\u4fee\u6539\u7528\u6237\u7684\u5bc6\u7801\u3002<\/li>\n<\/ol>\n<pre>SET PASSWORD FOR 'username'@'localhost' = PASSWORD('newpassword');<\/pre>\n<p>\u4e0a\u8ff0\u8bed\u53e5\u4e2d\uff0c&#8217;username&#8217;@&#8217;localhost&#8217;\u8868\u793a\u8981\u4fee\u6539\u5bc6\u7801\u7684\u7528\u6237\uff0cnewpassword\u8868\u793a\u65b0\u7684\u5bc6\u7801\u3002<\/p>\n<ol>\n<li>\u5220\u9664\u7528\u6237<br \/>\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\u53ef\u4ee5\u5220\u9664\u7528\u6237\u3002<\/li>\n<\/ol>\n<pre>DROP USER 'username'@'localhost';<\/pre>\n<p>\u4e0a\u8ff0\u8bed\u53e5\u4e2d\uff0c&#8217;username&#8217;@&#8217;localhost&#8217;\u8868\u793a\u8981\u5220\u9664\u7684\u7528\u6237\u3002<\/p>\n<p>\u4e09\u3001\u793a\u4f8b<br \/>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u521b\u5efa\u7528\u6237\u3001\u6388\u6743\u3001\u4fee\u6539\u5bc6\u7801\u548c\u5220\u9664\u7528\u6237\u3002<\/p>\n<pre>-- \u521b\u5efa\u7528\u6237\nCREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';\n\n-- \u6388\u4e88SELECT\u6743\u9650\nGRANT SELECT ON testdb.* TO 'newuser'@'localhost';\n\n-- \u4fee\u6539\u5bc6\u7801\nSET PASSWORD FOR 'newuser'@'localhost' = PASSWORD('newpassword');\n\n-- \u5220\u9664\u7528\u6237\nDROP USER 'newuser'@'localhost';<\/pre>\n<p>\u603b\u7ed3\uff1a<br \/>\u901a\u8fc7\u4e0a\u8ff0\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5728MySQL\u4e2d\u4f7f\u7528SQL\u8bed\u53e5\u8fdb\u884c\u6570\u636e\u6743\u9650\u548c\u7528\u6237\u7ba1\u7406\u975e\u5e38\u7b80\u5355\u3002\u901a\u8fc7CREATE USER\u3001GRANT\u3001REVOKE\u3001SET PASSWORD\u548cDROP USER\u7b49\u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u7528\u6237\u3001\u6388\u6743\u3001\u64a4\u9500\u6743\u9650\u3001\u4fee\u6539\u5bc6\u7801\u548c\u5220\u9664\u7528\u6237\u3002\u8fd9\u4e9b\u529f\u80fd\u4e3a\u6570\u636e\u5e93\u7ba1\u7406\u5458\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ba1\u7406\u5de5\u5177\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5e93\u7684\u5b89\u5168\u548c\u53ef\u9760\u8fd0\u884c\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u5728MySQL\u4e2d\u8fdb\u884c\u6570\u636e\u6743\u9650\u548c\u7528\u6237\u7ba1\u7406\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8emysql,\u6570\u636e\u6743\u9650,\u7528\u6237\u7ba1\u7406\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u5728MySQL\u4e2d&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-199614","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=199614"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199614\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=199614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=199614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=199614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}