{"id":96968,"date":"2024-10-20T13:52:17","date_gmt":"2024-10-20T05:52:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96968\/"},"modified":"2024-10-20T13:52:18","modified_gmt":"2024-10-20T05:52:18","slug":"python-mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%b5%84%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96968\/","title":{"rendered":"Python MySQL\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7d44\u4ef6"},"content":{"rendered":"<h1 id=\"python-mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%b5%84%e4%bb%b6-ehYpcitDLX\">Python MySQL\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7d44\u4ef6<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u6027\u80fd\u548c\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002\u5c24\u5176\u662f\u5728\u4f7f\u7528Python\u9032\u884c\u958b\u767c\u6642\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u6210\u70ba\u4e86\u4e00\u500b\u91cd\u8981\u7684\u8ab2\u984c\u3002\u672c\u6587\u5c07\u63a2\u8a0ePython\u4e2dMySQL\u6578\u64da\u5eab\u7684\u9023\u63a5\u6c60\u7d44\u4ef6\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u5176\u5de5\u4f5c\u539f\u7406\u53ca\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-ehYpcitDLX\">\u4ec0\u9ebc\u662f\u9023\u63a5\u6c60\uff1f<\/h2>\n<p>\u9023\u63a5\u6c60\u662f\u4e00\u7a2e\u7528\u65bc\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u7684\u6280\u8853\u3002\u5b83\u7684\u4e3b\u8981\u76ee\u7684\u662f\u91cd\u7528\u5df2\u7d93\u5efa\u7acb\u7684\u6578\u64da\u5eab\u9023\u63a5\uff0c\u5f9e\u800c\u6e1b\u5c11\u6bcf\u6b21\u8acb\u6c42\u6642\u6240\u9700\u7684\u9023\u63a5\u958b\u92b7\u3002\u9019\u5c0d\u65bc\u9ad8\u4e26\u767c\u7684\u61c9\u7528\u7a0b\u5f0f\u5c24\u70ba\u91cd\u8981\uff0c\u56e0\u70ba\u6bcf\u6b21\u5efa\u7acb\u548c\u95dc\u9589\u9023\u63a5\u90fd\u6703\u6d88\u8017\u5927\u91cf\u8cc7\u6e90\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-ehYpcitDLX\">\u70ba\u4ec0\u9ebc\u4f7f\u7528\u9023\u63a5\u6c60\uff1f<\/h2>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347\uff1a<\/strong>\u901a\u904e\u91cd\u7528\u9023\u63a5\uff0c\u6e1b\u5c11\u4e86\u5efa\u7acb\u548c\u95dc\u9589\u9023\u63a5\u7684\u6642\u9593\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u7ba1\u7406\uff1a<\/strong>\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\uff0c\u907f\u514d\u904e\u591a\u7684\u9023\u63a5\u5c0e\u81f4\u8cc7\u6e90\u8017\u76e1\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc\uff1a<\/strong>\u4f7f\u7528\u9023\u63a5\u6c60\u53ef\u4ee5\u7c21\u5316\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u4ee3\u78bc\uff0c\u8b93\u958b\u767c\u8005\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\u3002<\/li>\n<\/ul>\n<h2 id=\"python%e4%b8%ad%e4%bd%bf%e7%94%a8mysql%e9%80%a3%e6%8e%a5%e6%b1%a0-ehYpcitDLX\">Python\u4e2d\u4f7f\u7528MySQL\u9023\u63a5\u6c60<\/h2>\n<p>\u5728Python\u4e2d\uff0c\u6709\u591a\u500b\u5eab\u53ef\u4ee5\u5be6\u73feMySQL\u7684\u9023\u63a5\u6c60\u529f\u80fd\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f`mysql-connector-python`\u548c`SQLAlchemy`\u3002\u4ee5\u4e0b\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528\u9019\u5169\u500b\u5eab\u4f86\u5be6\u73fe\u9023\u63a5\u6c60\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8mysql-connector-python-ehYpcitDLX\">\u4f7f\u7528mysql-connector-python<\/h3>\n<pre><code>import mysql.connector\nfrom mysql.connector import pooling\n\n# \u5275\u5efa\u9023\u63a5\u6c60\ndbconfig = {\n    \"database\": \"your_database\",\n    \"user\": \"your_username\",\n    \"password\": \"your_password\",\n    \"host\": \"localhost\"\n}\n\nconnection_pool = pooling.MySQLConnectionPool(pool_name=\"mypool\",\n                                              pool_size=5,\n                                              **dbconfig)\n\n# \u7372\u53d6\u9023\u63a5\nconnection = connection_pool.get_connection()\n\n# \u4f7f\u7528\u9023\u63a5\ncursor = connection.cursor()\ncursor.execute(\"SELECT * FROM your_table\")\nresults = cursor.fetchall()\n\n# \u95dc\u9589\u9023\u63a5\ncursor.close()\nconnection.close()\n<\/code><\/pre>\n<h3 id=\"%e4%bd%bf%e7%94%a8sqlalchemy-ehYpcitDLX\">\u4f7f\u7528SQLAlchemy<\/h3>\n<pre><code>from sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\n\n# \u5275\u5efa\u5f15\u64ce\nengine = create_engine(\"mysql+mysqlconnector:\/\/user:password@localhost\/your_database\", pool_size=5)\n\n# \u5275\u5efa\u6703\u8a71\nSession = sessionmaker(bind=engine)\nsession = Session()\n\n# \u4f7f\u7528\u6703\u8a71\nresults = session.execute(\"SELECT * FROM your_table\").fetchall()\n\n# \u95dc\u9589\u6703\u8a71\nsession.close()\n<\/code><\/pre>\n<h2 id=\"%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%9a%84%e9%85%8d%e7%bd%ae%e9%81%b8%e9%a0%85-ehYpcitDLX\">\u9023\u63a5\u6c60\u7684\u914d\u7f6e\u9078\u9805<\/h2>\n<p>\u5728\u914d\u7f6e\u9023\u63a5\u6c60\u6642\uff0c\u6709\u5e7e\u500b\u91cd\u8981\u7684\u9078\u9805\u9700\u8981\u8003\u616e\uff1a<\/p>\n<ul>\n<li><strong>pool_size\uff1a<\/strong>\u5b9a\u7fa9\u9023\u63a5\u6c60\u4e2d\u53ef\u7528\u7684\u6700\u5927\u9023\u63a5\u6578\u3002<\/li>\n<li><strong>max_overflow\uff1a<\/strong>\u5b9a\u7fa9\u8d85\u51fapool_size\u7684\u6700\u5927\u9023\u63a5\u6578\u3002<\/li>\n<li><strong>timeout\uff1a<\/strong>\u5b9a\u7fa9\u9023\u63a5\u7684\u8d85\u6642\u6642\u9593\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ehYpcitDLX\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Python\u9032\u884cMySQL\u6578\u64da\u5eab\u7684\u958b\u767c\u6642\uff0c\u9023\u63a5\u6c60\u662f\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u7d44\u4ef6\u3002\u5b83\u4e0d\u50c5\u80fd\u63d0\u9ad8\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\uff0c\u9084\u80fd\u6709\u6548\u7ba1\u7406\u8cc7\u6e90\u3002\u7121\u8ad6\u662f\u4f7f\u7528`mysql-connector-python`\u9084\u662f`SQLAlchemy`\uff0c\u90fd\u80fd\u8f15\u9b06\u5be6\u73fe\u9023\u63a5\u6c60\u7684\u529f\u80fd\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u61c9\u7528\u7a0b\u5f0f\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u9023\u63a5\u6c60\u914d\u7f6e\u5c07\u5927\u5927\u63d0\u5347\u6574\u9ad4\u6027\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684Python\u61c9\u7528\u7a0b\u5f0f\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Python MySQL\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7d44\u4ef6\uff0c\u63d0\u5347\u6578\u64da\u5eab\u6027\u80fd\u8207\u6548\u7387\uff0c\u7c21\u5316\u9023\u63a5\u7ba1\u7406\uff0c\u9069\u5408\u9ad8\u4e26\u767c\u61c9\u7528\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-96968","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96968","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=96968"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96968\/revisions"}],"predecessor-version":[{"id":96969,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96968\/revisions\/96969"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}