{"id":42416,"date":"2025-06-08T15:27:51","date_gmt":"2025-06-08T07:27:51","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/42416.html"},"modified":"2025-06-08T15:27:51","modified_gmt":"2025-06-08T07:27:51","slug":"python-mysql%e8%bf%9c%e7%a8%8b%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/42416.html","title":{"rendered":"Python\u2014\u2014MySQL\u8fdc\u7a0b\u63a7\u5236"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"MySQL%E8%BF%90%E7%A8%8B%E6%8E%A7%E5%88%B6-toc\" style=\"margin-left:0px\">MySQL\u8fd0\u7a0b\u63a7\u5236<\/p>\n<p id=\"1.%20%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C-toc\" style=\"margin-left:40px\">1. \u51c6\u5907\u5de5\u4f5c<\/p>\n<p id=\"2.%20%E8%BF%9E%E6%8E%A5MySQL%E6%95%B0%E6%8D%AE%E5%BA%93-toc\" style=\"margin-left:40px\">2. \u8fde\u63a5MySQL\u6570\u636e\u5e93<\/p>\n<p id=\"%E4%BD%BF%E7%94%A8mysql-connector-toc\" style=\"margin-left:80px\">\u4f7f\u7528mysql-connector<\/p>\n<p id=\"%E4%BD%BF%E7%94%A8PyMySQL-toc\" style=\"margin-left:80px\">\u4f7f\u7528PyMySQL<\/p>\n<p id=\"3.%20%E5%9F%BA%E6%9C%ACCRUD%E6%93%8D%E4%BD%9C-toc\" style=\"margin-left:40px\">3. \u57fa\u672cCRUD\u64cd\u4f5c<\/p>\n<p id=\"%E5%88%9B%E5%BB%BA%E8%A1%A8-toc\" style=\"margin-left:80px\">\u521b\u5efa\u8868<\/p>\n<p id=\"%E6%8F%92%E5%85%A5%E6%95%B0%E6%8D%AE-toc\" style=\"margin-left:80px\">\u63d2\u5165\u6570\u636e<\/p>\n<p id=\"%E6%9F%A5%E8%AF%A2%E6%95%B0%E6%8D%AE-toc\" style=\"margin-left:80px\">\u67e5\u8be2\u6570\u636e<\/p>\n<p id=\"%E6%9B%B4%E6%96%B0%E6%95%B0%E6%8D%AE-toc\" style=\"margin-left:80px\">\u66f4\u65b0\u6570\u636e<\/p>\n<p id=\"%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE-toc\" style=\"margin-left:80px\">\u5220\u9664\u6570\u636e<\/p>\n<p id=\"4.%20%E9%AB%98%E7%BA%A7%E6%93%8D%E4%BD%9C-toc\" style=\"margin-left:40px\">4. \u9ad8\u7ea7\u64cd\u4f5c<\/p>\n<p id=\"%E4%BA%8B%E5%8A%A1%E5%A4%84%E7%90%86-toc\" style=\"margin-left:80px\">\u4e8b\u52a1\u5904\u7406<\/p>\n<p id=\"%E4%BD%BF%E7%94%A8ORM%E6%A1%86%E6%9E%B6%20-%20SQLAlchemy-toc\" style=\"margin-left:80px\">\u4f7f\u7528ORM\u6846\u67b6 &#8211; SQLAlchemy<\/p>\n<p id=\"5.%20%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5-toc\" style=\"margin-left:40px\">5. \u6700\u4f73\u5b9e\u8df5<\/p>\n<p id=\"6.%20%E5%B8%B8%E8%A7%81%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86-toc\" style=\"margin-left:40px\">6. \u5e38\u89c1\u9519\u8bef\u5904\u7406<\/p>\n<p id=\"%E8%BF%9E%E6%8E%A5%E6%B1%A0-toc\" style=\"margin-left:0px\">\u8fde\u63a5\u6c60<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E8%BF%9E%E6%8E%A5%E6%B1%A0%E7%9A%84%E4%BD%9C%E7%94%A8-toc\" style=\"margin-left:80px\">\u4e00\u3001\u8fde\u63a5\u6c60\u7684\u4f5c\u7528<\/p>\n<p id=\"%E4%BA%8C%E3%80%81%E4%BC%98%E5%8A%BF%E4%B8%8E%E5%8A%A3%E5%8A%BF-toc\" style=\"margin-left:80px\">\u4e8c\u3001\u4f18\u52bf\u4e0e\u52a3\u52bf<\/p>\n<p id=\"%E4%B8%89%E3%80%81%E9%83%A8%E7%BD%B2%E4%B8%8E%E4%BD%BF%E7%94%A8-toc\" style=\"margin-left:80px\">\u4e09\u3001\u90e8\u7f72\u4e0e\u4f7f\u7528<\/p>\n<p id=\"1.%20%E5%B8%B8%E7%94%A8%E5%BA%93%E5%8F%8A%E5%AE%89%E8%A3%85-toc\" style=\"margin-left:120px\">1. \u5e38\u7528\u5e93\u53ca\u5b89\u88c5<\/p>\n<p id=\"2.%20%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B-toc\" style=\"margin-left:120px\">2. \u57fa\u7840\u4f7f\u7528\u793a\u4f8b<\/p>\n<p id=\"3.%20%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E5%BB%BA%E8%AE%AE-toc\" style=\"margin-left:120px\">3. \u751f\u4ea7\u73af\u5883\u5efa\u8bae<\/p>\n<p id=\"%E5%9B%9B%E3%80%81%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E6%8A%80%E5%B7%A7-toc\" style=\"margin-left:80px\">\u56db\u3001\u6027\u80fd\u4f18\u5316\u6280\u5de7<\/p>\n<p id=\"%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86%C2%A0-toc\" style=\"margin-left:0px\">\u4e8b\u52a1\u7ba1\u7406\u00a0<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E4%BA%8B%E5%8A%A1%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5-toc\" style=\"margin-left:80px\">\u4e00\u3001\u4e8b\u52a1\u6838\u5fc3\u6982\u5ff5<\/p>\n<p id=\"%E4%BA%8C%E3%80%81%E5%85%B8%E5%9E%8B%E9%97%AE%E9%A2%98%E5%9C%BA%E6%99%AF-toc\" style=\"margin-left:80px\">\u4e8c\u3001\u5178\u578b\u95ee\u9898\u573a\u666f<\/p>\n<p id=\"%E4%B8%89%E3%80%81%E9%80%89%E5%9E%8B%E5%BB%BA%E8%AE%AE-toc\" style=\"margin-left:80px\">\u4e09\u3001\u9009\u578b\u5efa\u8bae<\/p>\n<p id=\"%E5%9B%9B%E3%80%81Python%E9%85%8D%E7%BD%AE%E7%A4%BA%E4%BE%8B-toc\" style=\"margin-left:80px\">\u56db\u3001Python\u914d\u7f6e\u793a\u4f8b<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 id=\"MySQL%E8%BF%90%E7%A8%8B%E6%8E%A7%E5%88%B6\">MySQL\u8fd0\u7a0b\u63a7\u5236<\/h2>\n<p>MySQL\u662f\u6700\u6d41\u884c\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e4b\u4e00&#xff0c;Python\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4e0eMySQL\u8fdb\u884c\u4ea4\u4e92\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u64cd\u4f5cMySQL\u7684\u5e38\u7528\u65b9\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<h3 id=\"1.%20%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C\">1. \u51c6\u5907\u5de5\u4f5c<\/h3>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d&#xff0c;\u4f60\u9700\u8981&#xff1a;<\/p>\n<li>\u5b89\u88c5MySQL\u670d\u52a1\u5668<\/li>\n<li>\u5b89\u88c5Python\u7684MySQL\u8fde\u63a5\u5e93<\/li>\n<p>\u63a8\u8350\u4f7f\u7528mysql-connector-python\u6216PyMySQL\u5e93&#xff1a;<\/p>\n<p>pip install mysql-connector-python<br \/>\n# \u6216<br \/>\npip install pymysql  <\/p>\n<h3 id=\"2.%20%E8%BF%9E%E6%8E%A5MySQL%E6%95%B0%E6%8D%AE%E5%BA%93\">2. \u8fde\u63a5MySQL\u6570\u636e\u5e93<\/h3>\n<h4 id=\"%E4%BD%BF%E7%94%A8mysql-connector\">\u4f7f\u7528mysql-connector<\/h4>\n<p>import mysql.connector <\/p>\n<p># \u521b\u5efa\u8fde\u63a5<br \/>\nconn &#061; mysql.connector.connect(<br \/>\n host&#061;&#034;localhost&#034;,<br \/>\n user&#061;&#034;your_username&#034;,<br \/>\n password&#061;&#034;your_password&#034;,<br \/>\n database&#061;&#034;your_database&#034;<br \/>\n) <\/p>\n<p># \u521b\u5efa\u6e38\u6807<br \/>\ncursor &#061; conn.cursor() <\/p>\n<p># \u6267\u884cSQL\u67e5\u8be2<br \/>\ncursor.execute(&#034;SELECT * FROM your_table&#034;) <\/p>\n<p># \u83b7\u53d6\u7ed3\u679c<br \/>\nresults &#061; cursor.fetchall()<br \/>\nfor row in results:<br \/>\nprint(row) <\/p>\n<p># \u5173\u95ed\u8fde\u63a5<br \/>\ncursor.close()<br \/>\nconn.close()  <\/p>\n<h4 id=\"%E4%BD%BF%E7%94%A8PyMySQL\">\u4f7f\u7528PyMySQL<\/h4>\n<p>import pymysql <\/p>\n<p># \u521b\u5efa\u8fde\u63a5<br \/>\nconn &#061; pymysql.connect(<br \/>\n host&#061;&#039;localhost&#039;,<br \/>\n user&#061;&#039;your_username&#039;,<br \/>\n password&#061;&#039;your_password&#039;,<br \/>\n db&#061;&#039;your_database&#039;,<br \/>\n charset&#061;&#039;utf8mb4&#039;,<br \/>\n cursorclass&#061;pymysql.cursors.DictCursor<br \/>\n) <\/p>\n<p># \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u81ea\u52a8\u7ba1\u7406\u8fde\u63a5<br \/>\nwith conn:<br \/>\n\u00a0    with conn.cursor() as cursor:<br \/>\n\u00a0        # \u6267\u884cSQL\u67e5\u8be2<br \/>\n\u00a0        sql &#061; &#034;SELECT * FROM your_table&#034;<br \/>\n\u00a0        cursor.execute(sql)<\/p>\n<p>\u00a0        # \u83b7\u53d6\u7ed3\u679c<br \/>\n\u00a0        results &#061; cursor.fetchall()<br \/>\n\u00a0        for row in results:<br \/>\n\u00a0            print(row)  <\/p>\n<h3 id=\"3.%20%E5%9F%BA%E6%9C%ACCRUD%E6%93%8D%E4%BD%9C\" style=\"background-color:transparent\">3. \u57fa\u672cCRUD\u64cd\u4f5c<\/h3>\n<h4 id=\"%E5%88%9B%E5%BB%BA%E8%A1%A8\">\u521b\u5efa\u8868<\/h4>\n<p>cursor.execute(&#034;&#034;&#034;<br \/>\nCREATE TABLE IF NOT EXISTS users (<br \/>\n\u00a0    id INT AUTO_INCREMENT PRIMARY KEY,<br \/>\n\u00a0    name VARCHAR(255) NOT NULL,<br \/>\n\u00a0    email VARCHAR(255) NOT NULL UNIQUE,<br \/>\n\u00a0    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP<br \/>\n )<br \/>\n&#034;&#034;&#034;)  <\/p>\n<h4 id=\"%E6%8F%92%E5%85%A5%E6%95%B0%E6%8D%AE\">\u63d2\u5165\u6570\u636e<\/h4>\n<p># \u5355\u6761\u63d2\u5165<br \/>\nsql &#061; &#034;INSERT INTO users (name, email) VALUES (%s, %s)&#034;<br \/>\nval &#061; (&#034;John Doe&#034;, &#034;john&#064;example.com&#034;)<br \/>\ncursor.execute(sql, val) <\/p>\n<p># \u591a\u6761\u63d2\u5165<br \/>\nsql &#061; &#034;INSERT INTO users (name, email) VALUES (%s, %s)&#034;<br \/>\nval &#061; [<br \/>\n\u00a0    (&#034;Jane Smith&#034;, &#034;jane&#064;example.com&#034;),<br \/>\n\u00a0    (&#034;Bob Johnson&#034;, &#034;bob&#064;example.com&#034;)<br \/>\n]<br \/>\ncursor.executemany(sql, val) <\/p>\n<p># \u63d0\u4ea4\u4e8b\u52a1<br \/>\nconn.commit()  <\/p>\n<h4 id=\"%E6%9F%A5%E8%AF%A2%E6%95%B0%E6%8D%AE\">\u67e5\u8be2\u6570\u636e<\/h4>\n<p># \u67e5\u8be2\u6240\u6709\u8bb0\u5f55<br \/>\ncursor.execute(&#034;SELECT * FROM users&#034;)<br \/>\nrows &#061; cursor.fetchall() <\/p>\n<p># \u67e5\u8be2\u5355\u6761\u8bb0\u5f55<br \/>\ncursor.execute(&#034;SELECT * FROM users WHERE id &#061; %s&#034;, (1,))<br \/>\nrow &#061; cursor.fetchone() <\/p>\n<p># \u5e26\u6761\u4ef6\u7684\u67e5\u8be2<br \/>\ncursor.execute(&#034;SELECT name, email FROM users WHERE name LIKE %s&#034;, (&#034;%John%&#034;,))<br \/>\nrows &#061; cursor.fetchall()  <\/p>\n<h4 id=\"%E6%9B%B4%E6%96%B0%E6%95%B0%E6%8D%AE\">\u66f4\u65b0\u6570\u636e<\/h4>\n<p>sql &#061; &#034;UPDATE users SET name &#061; %s WHERE id &#061; %s&#034;<br \/>\nval &#061; (&#034;John Smith&#034;, 1)<br \/>\ncursor.execute(sql, val)<br \/>\nconn.commit()  <\/p>\n<h4 id=\"%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE\">\u5220\u9664\u6570\u636e<\/h4>\n<p>sql &#061; &#034;DELETE FROM users WHERE id &#061; %s&#034;<br \/>\nval &#061; (1,)<br \/>\ncursor.execute(sql, val)<br \/>\nconn.commit()  <\/p>\n<h3 id=\"4.%20%E9%AB%98%E7%BA%A7%E6%93%8D%E4%BD%9C\">4. \u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<h4 id=\"%E4%BA%8B%E5%8A%A1%E5%A4%84%E7%90%86\">\u4e8b\u52a1\u5904\u7406<\/h4>\n<p>try:<br \/>\n # \u5f00\u59cb\u4e8b\u52a1<br \/>\n conn.start_transaction()<\/p>\n<p> # \u6267\u884c\u591a\u4e2aSQL\u64cd\u4f5c<br \/>\n cursor.execute(sql1, val1)<br \/>\n cursor.execute(sql2, val2)<\/p>\n<p> # \u63d0\u4ea4\u4e8b\u52a1 conn.commit()<br \/>\nexcept Exception as e:<br \/>\n # \u53d1\u751f\u9519\u8bef\u65f6\u56de\u6eda<br \/>\n conn.rollback()<br \/>\n print(f&#034;Transaction failed: {e}&#034;)  <\/p>\n<h4 id=\"%E4%BD%BF%E7%94%A8ORM%E6%A1%86%E6%9E%B6%20-%20SQLAlchemy\">\u4f7f\u7528ORM\u6846\u67b6 &#8211; SQLAlchemy<\/h4>\n<p>from sqlalchemy import create_engine, Column, Integer, String<br \/>\nfrom sqlalchemy.ext.declarative import declarative_base<br \/>\nfrom sqlalchemy.orm import sessionmaker <\/p>\n<p># \u521b\u5efa\u5f15\u64ce<br \/>\nengine &#061; create_engine(&#039;mysql&#043;pymysql:\/\/user:password&#064;localhost\/dbname&#039;) <\/p>\n<p># \u58f0\u660e\u57fa\u7c7b<br \/>\nBase &#061; declarative_base() <\/p>\n<p># \u5b9a\u4e49\u6a21\u578b<br \/>\nclass User(Base):<br \/>\n __tablename__ &#061; &#039;users&#039;<br \/>\n\u00a0<br \/>\n\u00a0id &#061; Column(Integer, primary_key&#061;True)<br \/>\n name &#061; Column(String(255))<br \/>\n email &#061; Column(String(255), unique&#061;True) <\/p>\n<p># \u521b\u5efa\u8868<br \/>\nBase.metadata.create_all(engine) <\/p>\n<p># \u521b\u5efa\u4f1a\u8bdd<br \/>\nSession &#061; sessionmaker(bind&#061;engine)<br \/>\nsession &#061; Session() <\/p>\n<p># \u6dfb\u52a0\u65b0\u7528\u6237<br \/>\nnew_user &#061; User(name&#061;&#039;Alice&#039;, email&#061;&#039;alice&#064;example.com&#039;)<br \/>\nsession.add(new_user)<br \/>\nsession.commit() <\/p>\n<p># \u67e5\u8be2\u7528\u6237<br \/>\nusers &#061; session.query(User).filter_by(name&#061;&#039;Alice&#039;).all()<br \/>\nfor user in users:<br \/>\n print(user.name, user.email)  <\/p>\n<h3 id=\"5.%20%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5\">5. \u6700\u4f73\u5b9e\u8df5<\/h3>\n<li>\u200c\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\u200c&#xff1a;\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb<\/li>\n<li>\u200c\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u200c&#xff1a;\u786e\u4fdd\u8fde\u63a5\u548c\u6e38\u6807\u6b63\u786e\u5173\u95ed<\/li>\n<li>\u200c\u5904\u7406\u5f02\u5e38\u200c&#xff1a;\u6355\u83b7\u5e76\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\u7684\u5f02\u5e38<\/li>\n<li>\u200c\u8fde\u63a5\u6c60\u200c&#xff1a;\u5728\u9ad8\u5e76\u53d1\u5e94\u7528\u4e2d\u4f7f\u7528\u8fde\u63a5\u6c60<\/li>\n<li>\u200c\u7d22\u5f15\u4f18\u5316\u200c&#xff1a;\u4e3a\u5e38\u7528\u67e5\u8be2\u5b57\u6bb5\u6dfb\u52a0\u7d22\u5f15<\/li>\n<h3 id=\"6.%20%E5%B8%B8%E8%A7%81%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86\">6. \u5e38\u89c1\u9519\u8bef\u5904\u7406<\/h3>\n<p>try:<br \/>\n conn &#061; mysql.connector.connect(**config)<br \/>\n cursor &#061; conn.cursor()<br \/>\n cursor.execute(&#034;SELECT * FROM non_existent_table&#034;)<br \/>\nexcept mysql.connector.Error as err:<br \/>\n print(f&#034;Error: {err}&#034;)<br \/>\nfinally: if &#039;conn&#039; in locals() and conn.is_connected():<br \/>\n cursor.close()<br \/>\n conn.close() <\/p>\n<h2 id=\"%E8%BF%9E%E6%8E%A5%E6%B1%A0\">\u8fde\u63a5\u6c60<\/h2>\n<h4 id=\"%E4%B8%80%E3%80%81%E8%BF%9E%E6%8E%A5%E6%B1%A0%E7%9A%84%E4%BD%9C%E7%94%A8\">\u4e00\u3001\u8fde\u63a5\u6c60\u7684\u4f5c\u7528<\/h4>\n<p>\u00a0 \u00a0 \u6570\u636e\u5e93\u8fde\u63a5\u6c60\u662f\u4e00\u79cd\u9884\u5148\u521b\u5efa\u5e76\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u7684\u6280\u672f&#xff0c;\u4e3b\u8981\u89e3\u51b3\u9891\u7e41\u521b\u5efa\/\u9500\u6bc1\u8fde\u63a5\u7684\u6027\u80fd\u635f\u8017\u95ee\u9898\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\u8fde\u63a5\u590d\u7528&#xff0c;\u5e94\u7528\u7a0b\u5e8f\u4ece\u6c60\u4e2d\u83b7\u53d6\u8fde\u63a5&#xff0c;\u4f7f\u7528\u540e\u5f52\u8fd8\u800c\u975e\u76f4\u63a5\u5173\u95ed\u3002<\/p>\n<h4 id=\"%E4%BA%8C%E3%80%81%E4%BC%98%E5%8A%BF%E4%B8%8E%E5%8A%A3%E5%8A%BF\">\u4e8c\u3001\u4f18\u52bf\u4e0e\u52a3\u52bf<\/h4>\n<p>\u4f18\u52bf&#xff1a;<\/p>\n<li>\n<p>\u6027\u80fd\u63d0\u5347&#xff1a;\u51cf\u5c11\u8fde\u63a5\u521b\u5efa\/\u9500\u6bc1\u7684TCP\u4e09\u6b21\u63e1\u624b\u548c\u8ba4\u8bc1\u5f00\u9500&#xff0c;\u964d\u4f4e\u5ef6\u8fdf<\/p>\n<\/li>\n<li>\n<p>\u8d44\u6e90\u63a7\u5236&#xff1a;\u901a\u8fc7max_connections\u9650\u5236\u6700\u5927\u8fde\u63a5\u6570&#xff0c;\u9632\u6b62\u6570\u636e\u5e93\u8fc7\u8f7d<\/p>\n<\/li>\n<li>\n<p>\u54cd\u5e94\u52a0\u901f&#xff1a;\u521d\u59cb\u5316\u65f6\u9884\u5efa\u8fde\u63a5&#xff0c;\u4e1a\u52a1\u8bf7\u6c42\u53ef\u76f4\u63a5\u4f7f\u7528<\/p>\n<\/li>\n<li>\n<p>\u6cc4\u6f0f\u9632\u62a4&#xff1a;\u8d85\u65f6\u56de\u6536\u673a\u5236\u907f\u514d\u8fde\u63a5\u957f\u671f\u5360\u7528<\/p>\n<\/li>\n<p>\u52a3\u52bf&#xff1a;<\/p>\n<li>\n<p>\u9700\u8981\u5408\u7406\u914d\u7f6e\u53c2\u6570&#xff08;\u5982\u6700\u5927\/\u6700\u5c0f\u8fde\u63a5\u6570&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u8fde\u63a5\u72b6\u6001\u7ef4\u62a4\u589e\u52a0\u590d\u6742\u5ea6<\/p>\n<\/li>\n<li>\n<p>\u4e0d\u9002\u7528\u4e8e\u8d85\u77ed\u751f\u547d\u5468\u671f\u5e94\u7528<\/p>\n<\/li>\n<h4 id=\"%E4%B8%89%E3%80%81%E9%83%A8%E7%BD%B2%E4%B8%8E%E4%BD%BF%E7%94%A8\">\u4e09\u3001\u90e8\u7f72\u4e0e\u4f7f\u7528<\/h4>\n<h5 id=\"1.%20%E5%B8%B8%E7%94%A8%E5%BA%93%E5%8F%8A%E5%AE%89%E8%A3%85\">1. \u5e38\u7528\u5e93\u53ca\u5b89\u88c5<\/h5>\n<p># SQLAlchemy&#xff08;\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93&#xff09;<br \/>\npip install sqlalchemy<\/p>\n<p># DBUtils&#xff08;\u901a\u7528\u8fde\u63a5\u6c60&#xff09;<br \/>\npip install dbutils<\/p>\n<p># Psycopg2&#xff08;PostgreSQL\u4e13\u7528&#xff09;<br \/>\npip install psycopg2-binary <\/p>\n<h5 id=\"2.%20%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B\">2. \u57fa\u7840\u4f7f\u7528\u793a\u4f8b<\/h5>\n<p>SQLAlchemy\u8fde\u63a5\u6c60\u914d\u7f6e&#xff1a;<\/p>\n<p>from sqlalchemy import create_engine<\/p>\n<p># \u5e26\u8fde\u63a5\u6c60\u7684\u914d\u7f6e&#xff08;\u8fde\u63a5\u6c60\u5927\u5c0f5-10&#xff09;<br \/>\nengine &#061; create_engine(<br \/>\n    &#034;mysql&#043;pymysql:\/\/user:pass&#064;host\/db&#034;,<br \/>\n    pool_size&#061;5,<br \/>\n    max_overflow&#061;5,<br \/>\n    pool_recycle&#061;3600<br \/>\n) <\/p>\n<p>DBUtils\u8fde\u63a5\u6c60\u793a\u4f8b&#xff1a;<\/p>\n<p>from dbutils.pooled_db import PooledDB<br \/>\nimport pymysql<\/p>\n<p>pool &#061; PooledDB(<br \/>\n    creator&#061;pymysql,<br \/>\n    maxconnections&#061;10,<br \/>\n    host&#061;&#039;localhost&#039;,<br \/>\n    user&#061;&#039;root&#039;,<br \/>\n    database&#061;&#039;test&#039;<br \/>\n)<br \/>\nconn &#061; pool.connection()  # \u83b7\u53d6\u8fde\u63a5 <\/p>\n<h5 id=\"3.%20%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E5%BB%BA%E8%AE%AE\">3. \u751f\u4ea7\u73af\u5883\u5efa\u8bae<\/h5>\n<li>\n<p>\u6839\u636eQPS\u8bbe\u7f6epool_size&#xff08;\u5efa\u8bae&#061;\u5e73\u5747\u5e76\u53d1\u91cf\u00d71.2&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u542f\u7528pool_pre_ping\u81ea\u52a8\u68c0\u6d4b\u5931\u6548\u8fde\u63a5<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528with\u8bed\u53e5\u786e\u4fdd\u8fde\u63a5\u5f52\u8fd8<\/p>\n<\/li>\n<li>\n<p>\u76d1\u63a7\u8fde\u63a5\u6c60\u4f7f\u7528\u7387&#xff08;\u5982SQLAlchemy\u7684pool.status()&#xff09;<\/p>\n<\/li>\n<h4 id=\"%E5%9B%9B%E3%80%81%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E6%8A%80%E5%B7%A7\">\u56db\u3001\u6027\u80fd\u4f18\u5316\u6280\u5de7<\/h4>\n<li>\n<p>\u4e0d\u540c\u4e1a\u52a1\u4f7f\u7528\u72ec\u7acb\u8fde\u63a5\u6c60\u9694\u79bb\u8d44\u6e90<\/p>\n<\/li>\n<li>\n<p>\u52a8\u6001\u8c03\u6574\u8fde\u63a5\u6570&#xff08;\u5982SQLAlchemy\u7684pool_events&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u914d\u5408\u8fde\u63a5\u6c60\u4f7f\u7528ORM\u7684Session\u7f13\u5b58\u673a\u5236<\/p>\n<\/li>\n<h2 id=\"%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86%C2%A0\">\u4e8b\u52a1\u7ba1\u7406\u00a0<\/h2>\n<h4 id=\"%E4%B8%80%E3%80%81%E4%BA%8B%E5%8A%A1%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5\">\u4e00\u3001\u4e8b\u52a1\u6838\u5fc3\u6982\u5ff5<\/h4>\n<li>\n<p>\u200cACID\u7279\u6027\u200c<\/p>\n<ul>\n<li>\u200c\u539f\u5b50\u6027(Atomicity)\u200c&#xff1a;\u4e8b\u52a1\u662f\u4e0d\u53ef\u5206\u5272\u7684\u5de5\u4f5c\u5355\u5143<\/li>\n<li>\u200c\u4e00\u81f4\u6027(Consistency)\u200c&#xff1a;\u4e8b\u52a1\u524d\u540e\u6570\u636e\u5e93\u72b6\u6001\u4fdd\u6301\u4e00\u81f4<\/li>\n<li>\u200c\u9694\u79bb\u6027(Isolation)\u200c&#xff1a;\u5e76\u53d1\u4e8b\u52a1\u4e92\u4e0d\u5e72\u6270<\/li>\n<li>\u200c\u6301\u4e45\u6027(Durability)\u200c&#xff1a;\u4e8b\u52a1\u63d0\u4ea4\u540e\u7ed3\u679c\u6c38\u4e45\u751f\u6548<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u200c\u9694\u79bb\u7ea7\u522b\u200c<\/p>\n<ul>\n<li>READ_UNCOMMITTED&#xff08;\u53ef\u80fd\u8bfb\u53d6\u672a\u63d0\u4ea4\u6570\u636e&#xff09;<\/li>\n<li>READ_COMMITTED&#xff08;\u907f\u514d\u810f\u8bfb&#xff09;<\/li>\n<li>REPEATABLE_READ&#xff08;\u907f\u514d\u4e0d\u53ef\u91cd\u590d\u8bfb&#xff09;<\/li>\n<li>SERIALIZABLE&#xff08;\u5b8c\u5168\u4e32\u884c\u5316&#xff09;<\/li>\n<\/ul>\n<\/li>\n<hr \/>\n<p>\u56db\u5927\u9694\u79bb\u7ea7\u522b\u5bf9\u6bd4<\/p>\n<table>\n<tr>\u9694\u79bb\u7ea7\u522b\u810f\u8bfb\u4e0d\u53ef\u91cd\u590d\u8bfb\u5e7b\u8bfb\u9501\u673a\u5236\u7279\u70b9<\/tr>\n<tbody>\n<tr>\n<td>READ UNCOMMITTED<\/td>\n<td>\u2713<\/td>\n<td>\u2713<\/td>\n<td>\u2713<\/td>\n<td>\u65e0\u8bfb\u9501&#xff0c;\u4ec5\u5199\u9501\u51b2\u7a81<\/td>\n<\/tr>\n<tr>\n<td>READ COMMITTED<\/td>\n<td>\u2717<\/td>\n<td>\u2713<\/td>\n<td>\u2713<\/td>\n<td>\u8bfb\u540e\u7acb\u5373\u91ca\u653e\u5171\u4eab\u9501<\/td>\n<\/tr>\n<tr>\n<td>REPEATABLE READ<\/td>\n<td>\u2717<\/td>\n<td>\u2717<\/td>\n<td>\u2713*<\/td>\n<td>\u6301\u6709\u8bfb\u9501\u81f3\u4e8b\u52a1\u7ed3\u675f<\/td>\n<\/tr>\n<tr>\n<td>SERIALIZABLE<\/td>\n<td>\u2717<\/td>\n<td>\u2717<\/td>\n<td>\u2717<\/td>\n<td>\u8303\u56f4\u9501\u9632\u6b62\u5e7b\u8bfb<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>*\u6ce8&#xff1a;MySQL\u7684InnoDB\u901a\u8fc7MVCC\u673a\u5236\u5728REPEATABLE READ\u4e0b\u53ef\u907f\u514d\u5e7b\u8bfb<\/p>\n<h4 id=\"%E4%BA%8C%E3%80%81%E5%85%B8%E5%9E%8B%E9%97%AE%E9%A2%98%E5%9C%BA%E6%99%AF\">\u4e8c\u3001\u5178\u578b\u95ee\u9898\u573a\u666f<\/h4>\n<li>\u200c\u810f\u8bfb\u200c&#xff1a;\u4e8b\u52a1A\u8bfb\u53d6\u4e8b\u52a1B\u672a\u63d0\u4ea4\u7684\u4fee\u6539&#xff0c;B\u56de\u6eda\u5bfc\u81f4A\u83b7\u5f97\u65e0\u6548\u6570\u636e<\/li>\n<li>\u200c\u4e0d\u53ef\u91cd\u590d\u8bfb\u200c&#xff1a;\u4e8b\u52a1A\u4e24\u6b21\u8bfb\u53d6\u540c\u8bb0\u5f55&#xff0c;\u56e0\u4e8b\u52a1B\u63d0\u4ea4\u4fee\u6539\u5bfc\u81f4\u7ed3\u679c\u4e0d\u4e00\u81f4<\/li>\n<li>\u200c\u5e7b\u8bfb\u200c&#xff1a;\u4e8b\u52a1A\u6309\u6761\u4ef6\u67e5\u8be2&#xff0c;\u4e8b\u52a1B\u65b0\u589e\u7b26\u5408\u6761\u4ef6\u8bb0\u5f55\u5bfc\u81f4A\u4e24\u6b21\u7ed3\u679c\u96c6\u4e0d\u540c<\/li>\n<h4 id=\"%E4%B8%89%E3%80%81%E9%80%89%E5%9E%8B%E5%BB%BA%E8%AE%AE\">\u4e09\u3001\u9009\u578b\u5efa\u8bae<\/h4>\n<li>\u200c\u5b9e\u65f6\u5206\u6790\u7cfb\u7edf\u200c&#xff1a;READ UNCOMMITTED&#xff08;\u5bb9\u5fcd\u810f\u8bfb\u6362\u53d6\u6027\u80fd&#xff09;<\/li>\n<li>\u200c\u652f\u4ed8\u7cfb\u7edf\u200c&#xff1a;REPEATABLE READ&#xff08;\u4fdd\u8bc1\u91d1\u989d\u4e00\u81f4\u6027&#xff09;<\/li>\n<li>\u200c\u7968\u52a1\u7cfb\u7edf\u200c&#xff1a;SERIALIZABLE&#xff08;\u675c\u7edd\u8d85\u5356\u98ce\u9669&#xff09;<\/li>\n<li>\u200c\u5e38\u89c4OLTP\u200c&#xff1a;READ COMMITTED&#xff08;\u5e73\u8861\u6027\u80fd\u4e0e\u4e00\u81f4\u6027&#xff09;<\/li>\n<h4 id=\"%E5%9B%9B%E3%80%81Python%E9%85%8D%E7%BD%AE%E7%A4%BA%E4%BE%8B\">\u56db\u3001Python\u914d\u7f6e\u793a\u4f8b<\/h4>\n<p># PostgreSQL\u8bbe\u7f6e\u9694\u79bb\u7ea7\u522b<br \/>\nimport psycopg2<br \/>\nconn &#061; psycopg2.connect(dsn)<br \/>\nconn.set_isolation_level(<br \/>\n psycopg2.extensions.ISOLATION_LEVEL_REPEATABLE_READ )  <\/p>\n<p>\u4e0d\u540c\u6570\u636e\u5e93\u5bf9\u9694\u79bb\u7ea7\u522b\u7684\u5b9e\u73b0\u5b58\u5728\u5dee\u5f02&#xff0c;\u5982Oracle\u9ed8\u8ba4READ COMMITTED\u800cMySQL\u9ed8\u8ba4REPEATABLE READ38&#xff0c;\u5b9e\u9645\u5f00\u53d1\u9700\u7ed3\u5408\u5177\u4f53\u6570\u636e\u5e93\u7279\u6027\u8c03\u6574<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e17\u6b21\uff0c\u6536\u85cf23\u6b21\u3002\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u662f\u4e00\u79cd\u9884\u5148\u521b\u5efa\u5e76\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u7684\u6280\u672f\uff0c\u4e3b\u8981\u89e3\u51b3\u9891\u7e41\u521b\u5efa\/\u9500\u6bc1\u8fde\u63a5\u7684\u6027\u80fd\u635f\u8017\u95ee\u9898\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\u8fde\u63a5\u590d\u7528\uff0c\u5e94\u7528\u7a0b\u5e8f\u4ece\u6c60\u4e2d\u83b7\u53d6\u8fde\u63a5\uff0c\u4f7f\u7528\u540e\u5f52\u8fd8\u800c\u975e\u76f4\u63a5\u5173\u95ed\u3002\u4e0d\u540c\u6570\u636e\u5e93\u5bf9\u9694\u79bb\u7ea7\u522b\u7684\u5b9e\u73b0\u5b58\u5728\u5dee\u5f02\uff0c\u5982Oracle\u9ed8\u8ba4READ COMMITTED\u800cMySQL\u9ed8\u8ba4REPEATABLE READ38\uff0c\u5b9e\u9645\u5f00\u53d1\u9700\u7ed3\u5408\u5177\u4f53\u6570\u636e\u5e93\u7279\u6027\u8c03\u6574\u3002MySQL\u662f\u6700\u6d41\u884c\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e4b\u4e00\uff0cPython\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4e0eMySQL\u8fdb\u884c\u4ea4\u4e92\u3002\uff1a\u51cf\u5c11\u8fde\u63a5\u521b\u5efa\/\u9500\u6bc1\u7684TCP\u4e09\u6b21\u63e1\u624b\u548c\u8ba4\u8bc1\u5f00\u9500\uff0c\u964d\u4f4e\u5ef6\u8fdf\u3002\u4e0d\u9002\u7528\u4e8e\u8d85\u77ed\u751f\u547d\u5468\u671f\u5e94\u7528\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[114,81,190],"topic":[],"class_list":["post-42416","post","type-post","status-publish","format-standard","hentry","category-server","tag-mysql","tag-python","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python\u2014\u2014MySQL\u8fdc\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/42416.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python\u2014\u2014MySQL\u8fdc\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e17\u6b21\uff0c\u6536\u85cf23\u6b21\u3002\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u662f\u4e00\u79cd\u9884\u5148\u521b\u5efa\u5e76\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u7684\u6280\u672f\uff0c\u4e3b\u8981\u89e3\u51b3\u9891\u7e41\u521b\u5efa\/\u9500\u6bc1\u8fde\u63a5\u7684\u6027\u80fd\u635f\u8017\u95ee\u9898\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\u8fde\u63a5\u590d\u7528\uff0c\u5e94\u7528\u7a0b\u5e8f\u4ece\u6c60\u4e2d\u83b7\u53d6\u8fde\u63a5\uff0c\u4f7f\u7528\u540e\u5f52\u8fd8\u800c\u975e\u76f4\u63a5\u5173\u95ed\u3002\u4e0d\u540c\u6570\u636e\u5e93\u5bf9\u9694\u79bb\u7ea7\u522b\u7684\u5b9e\u73b0\u5b58\u5728\u5dee\u5f02\uff0c\u5982Oracle\u9ed8\u8ba4READ COMMITTED\u800cMySQL\u9ed8\u8ba4REPEATABLE READ38\uff0c\u5b9e\u9645\u5f00\u53d1\u9700\u7ed3\u5408\u5177\u4f53\u6570\u636e\u5e93\u7279\u6027\u8c03\u6574\u3002MySQL\u662f\u6700\u6d41\u884c\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e4b\u4e00\uff0cPython\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4e0eMySQL\u8fdb\u884c\u4ea4\u4e92\u3002\uff1a\u51cf\u5c11\u8fde\u63a5\u521b\u5efa\/\u9500\u6bc1\u7684TCP\u4e09\u6b21\u63e1\u624b\u548c\u8ba4\u8bc1\u5f00\u9500\uff0c\u964d\u4f4e\u5ef6\u8fdf\u3002\u4e0d\u9002\u7528\u4e8e\u8d85\u77ed\u751f\u547d\u5468\u671f\u5e94\u7528\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/42416.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-08T07:27:51+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/42416.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/42416.html\",\"name\":\"Python\u2014\u2014MySQL\u8fdc\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-08T07:27:51+00:00\",\"dateModified\":\"2025-06-08T07:27:51+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/42416.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/42416.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/42416.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\u2014\u2014MySQL\u8fdc\u7a0b\u63a7\u5236\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python\u2014\u2014MySQL\u8fdc\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/42416.html","og_locale":"zh_CN","og_type":"article","og_title":"Python\u2014\u2014MySQL\u8fdc\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e17\u6b21\uff0c\u6536\u85cf23\u6b21\u3002\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u662f\u4e00\u79cd\u9884\u5148\u521b\u5efa\u5e76\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u7684\u6280\u672f\uff0c\u4e3b\u8981\u89e3\u51b3\u9891\u7e41\u521b\u5efa\/\u9500\u6bc1\u8fde\u63a5\u7684\u6027\u80fd\u635f\u8017\u95ee\u9898\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\u8fde\u63a5\u590d\u7528\uff0c\u5e94\u7528\u7a0b\u5e8f\u4ece\u6c60\u4e2d\u83b7\u53d6\u8fde\u63a5\uff0c\u4f7f\u7528\u540e\u5f52\u8fd8\u800c\u975e\u76f4\u63a5\u5173\u95ed\u3002\u4e0d\u540c\u6570\u636e\u5e93\u5bf9\u9694\u79bb\u7ea7\u522b\u7684\u5b9e\u73b0\u5b58\u5728\u5dee\u5f02\uff0c\u5982Oracle\u9ed8\u8ba4READ COMMITTED\u800cMySQL\u9ed8\u8ba4REPEATABLE READ38\uff0c\u5b9e\u9645\u5f00\u53d1\u9700\u7ed3\u5408\u5177\u4f53\u6570\u636e\u5e93\u7279\u6027\u8c03\u6574\u3002MySQL\u662f\u6700\u6d41\u884c\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e4b\u4e00\uff0cPython\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4e0eMySQL\u8fdb\u884c\u4ea4\u4e92\u3002\uff1a\u51cf\u5c11\u8fde\u63a5\u521b\u5efa\/\u9500\u6bc1\u7684TCP\u4e09\u6b21\u63e1\u624b\u548c\u8ba4\u8bc1\u5f00\u9500\uff0c\u964d\u4f4e\u5ef6\u8fdf\u3002\u4e0d\u9002\u7528\u4e8e\u8d85\u77ed\u751f\u547d\u5468\u671f\u5e94\u7528\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/42416.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-08T07:27:51+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/42416.html","url":"https:\/\/www.wsisp.com\/helps\/42416.html","name":"Python\u2014\u2014MySQL\u8fdc\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-08T07:27:51+00:00","dateModified":"2025-06-08T07:27:51+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/42416.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/42416.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/42416.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python\u2014\u2014MySQL\u8fdc\u7a0b\u63a7\u5236"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/42416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=42416"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/42416\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=42416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=42416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=42416"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=42416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}