{"id":183806,"date":"2024-11-10T07:53:18","date_gmt":"2024-11-09T23:53:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183806\/"},"modified":"2024-11-10T07:53:19","modified_gmt":"2024-11-09T23:53:19","slug":"%e4%bd%bf%e7%94%a8msql%e5%af%a6%e7%8f%be%e9%ab%98%e6%95%88%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8a%80%e5%b7%a7-msql-%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183806\/","title":{"rendered":"\u4f7f\u7528MSQL\u5be6\u73fe\u9ad8\u6548\u9023\u63a5\u6578\u64da\u5eab\u6280\u5de7 (msql \u9023\u63a5\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8msql%e5%af%a6%e7%8f%be%e9%ab%98%e6%95%88%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8a%80%e5%b7%a7-PmIJpjvtgL\">\u4f7f\u7528MSQL\u5be6\u73fe\u9ad8\u6548\u9023\u63a5\u6578\u64da\u5eab\u6280\u5de7<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u7684\u9ad8\u6548\u9023\u63a5\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002MSQL\uff08MySQL\uff09\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u958b\u6e90\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u4e00\u4e9b\u4f7f\u7528MSQL\u9023\u63a5\u6578\u64da\u5eab\u7684\u6280\u5de7\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u6548\u7387\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8%e6%8c%81%e4%b9%85%e9%80%a3%e6%8e%a5-PmIJpjvtgL\">1. \u4f7f\u7528\u6301\u4e45\u9023\u63a5<\/h2>\n<p>\u6301\u4e45\u9023\u63a5\u662f\u4e00\u7a2e\u53ef\u4ee5\u5728\u591a\u500b\u8acb\u6c42\u4e4b\u9593\u4fdd\u6301\u9023\u63a5\u7684\u6280\u8853\u3002\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u6bcf\u6b21\u8acb\u6c42\u90fd\u91cd\u65b0\u5efa\u7acb\u9023\u63a5\u7684\u958b\u92b7\u3002\u4f7f\u7528\u6301\u4e45\u9023\u63a5\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u9ad8\u6d41\u91cf\u7684\u74b0\u5883\u4e2d\u3002<\/p>\n<pre><code>mysqli_connect('localhost', 'username', 'password', 'database', 3306, null, MYSQLI_USE_SSL);<\/code><\/pre>\n<p>\u5728PHP\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528mysqli\u64f4\u5c55\u4f86\u5be6\u73fe\u6301\u4e45\u9023\u63a5\u3002\u53ea\u9700\u5728\u4e3b\u6a5f\u540d\u7a31\u524d\u52a0\u4e0a\u201cp:\u201d\u5373\u53ef\u3002<\/p>\n<pre><code>$connection = mysqli_connect('p:localhost', 'username', 'password', 'database');<\/code><\/pre>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0-PmIJpjvtgL\">2. \u4f7f\u7528\u9023\u63a5\u6c60<\/h2>\n<p>\u9023\u63a5\u6c60\u662f\u4e00\u7a2e\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u7684\u6280\u8853\uff0c\u901a\u904e\u91cd\u7528\u73fe\u6709\u7684\u9023\u63a5\u4f86\u6e1b\u5c11\u9023\u63a5\u7684\u5efa\u7acb\u548c\u95dc\u9589\u6642\u9593\u3002\u9019\u5c0d\u65bc\u9700\u8981\u983b\u7e41\u8a2a\u554f\u6578\u64da\u5eab\u7684\u61c9\u7528\u7a0b\u5e8f\u7279\u5225\u6709\u7528\u3002<\/p>\n<p>\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Apache DBCP\u6216HikariCP\u7b49\u5eab\u4f86\u5be6\u73fe\u9023\u63a5\u6c60\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528HikariCP\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>HikariConfig config = new HikariConfig();\nconfig.setJdbcUrl(\"jdbc:mysql:\/\/localhost:3306\/database\");\nconfig.setUsername(\"username\");\nconfig.setPassword(\"password\");\nHikariDataSource dataSource = new HikariDataSource(config);<\/code><\/pre>\n<h2 id=\"3-%e5%84%aa%e5%8c%96%e6%9f%a5%e8%a9%a2-PmIJpjvtgL\">3. \u512a\u5316\u67e5\u8a62<\/h2>\n<p>\u67e5\u8a62\u7684\u6548\u7387\u76f4\u63a5\u5f71\u97ff\u5230\u6578\u64da\u5eab\u7684\u6027\u80fd\u3002\u4f7f\u7528\u7d22\u5f15\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u67e5\u8a62\u901f\u5ea6\u3002\u78ba\u4fdd\u5c0d\u7d93\u5e38\u67e5\u8a62\u7684\u5b57\u6bb5\u5efa\u7acb\u7d22\u5f15\uff0c\u4e26\u907f\u514d\u5728\u67e5\u8a62\u4e2d\u4f7f\u7528SELECT *\uff0c\u800c\u662f\u9078\u64c7\u9700\u8981\u7684\u5b57\u6bb5\u3002<\/p>\n<pre><code>SELECT column1, column2 FROM table WHERE condition;<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u4f7f\u7528EXPLAIN\u8a9e\u53e5\u53ef\u4ee5\u5e6b\u52a9\u5206\u6790\u67e5\u8a62\u7684\u57f7\u884c\u8a08\u5283\uff0c\u5f9e\u800c\u9032\u4e00\u6b65\u512a\u5316\u67e5\u8a62\u3002<\/p>\n<pre><code>EXPLAIN SELECT column1, column2 FROM table WHERE condition;<\/code><\/pre>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8%e4%ba%8b%e5%8b%99-PmIJpjvtgL\">4. \u4f7f\u7528\u4e8b\u52d9<\/h2>\n<p>\u4e8b\u52d9\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002\u5728\u9032\u884c\u591a\u500b\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u4f7f\u7528\u4e8b\u52d9\u53ef\u4ee5\u907f\u514d\u90e8\u5206\u64cd\u4f5c\u6210\u529f\u800c\u5176\u4ed6\u64cd\u4f5c\u5931\u6557\u7684\u60c5\u6cc1\u3002<\/p>\n<pre><code>mysqli_begin_transaction($connection);\ntry {\n    mysqli_query($connection, \"INSERT INTO table1 ...\");\n    mysqli_query($connection, \"INSERT INTO table2 ...\");\n    mysqli_commit($connection);\n} catch (Exception $e) {\n    mysqli_rollback($connection);\n}<\/code><\/pre>\n<h2 id=\"5-%e5%ae%9a%e6%9c%9f%e7%b6%ad%e8%ad%b7%e6%95%b8%e6%93%9a%e5%ba%ab-PmIJpjvtgL\">5. \u5b9a\u671f\u7dad\u8b77\u6578\u64da\u5eab<\/h2>\n<p>\u5b9a\u671f\u7dad\u8b77\u6578\u64da\u5eab\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\uff0c\u5305\u62ec\u6e05\u7406\u4e0d\u5fc5\u8981\u7684\u6578\u64da\u3001\u91cd\u5efa\u7d22\u5f15\u548c\u66f4\u65b0\u7d71\u8a08\u4fe1\u606f\u3002\u9019\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u5e6b\u52a9\u6578\u64da\u5eab\u4fdd\u6301\u6700\u4f73\u6027\u80fd\u3002<\/p>\n<pre><code>OPTIMIZE TABLE table_name;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-PmIJpjvtgL\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528MSQL\u9023\u63a5\u6578\u64da\u5eab\u7684\u6280\u5de7\u4e0d\u50c5\u80fd\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u9084\u80fd\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002\u901a\u904e\u6301\u4e45\u9023\u63a5\u3001\u9023\u63a5\u6c60\u3001\u512a\u5316\u67e5\u8a62\u3001\u4f7f\u7528\u4e8b\u52d9\u4ee5\u53ca\u5b9a\u671f\u7dad\u8b77\u6578\u64da\u5eab\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\uff0c\u63d0\u5347\u6574\u9ad4\u6548\u7387\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u4f7f\u7528MSQL\u5be6\u73fe\u9ad8\u6548\u9023\u63a5\u6578\u64da\u5eab\u7684\u6280\u5de7\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u512a\u5316\u67e5\u8a62\u6027\u80fd\uff0c\u7c21\u5316\u958b\u767c\u6d41\u7a0b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-183806","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183806","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=183806"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183806\/revisions"}],"predecessor-version":[{"id":183807,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183806\/revisions\/183807"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}