{"id":1015854,"date":"2024-12-27T12:11:31","date_gmt":"2024-12-27T04:11:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1015854.html"},"modified":"2024-12-27T12:11:33","modified_gmt":"2024-12-27T04:11:33","slug":"python%e5%a6%82%e4%bd%95%e6%9c%ac%e5%9c%b0%e5%a4%87%e4%bb%bdmysql","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1015854.html","title":{"rendered":"python\u5982\u4f55\u672c\u5730\u5907\u4efdmysql"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095559\/401d9254-4bc0-409e-95b9-182d3676eb3a.webp\" alt=\"python\u5982\u4f55\u672c\u5730\u5907\u4efdmysql\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528MySQL\u9a71\u52a8\u548c\u7cfb\u7edf\u547d\u4ee4\u6765\u5b9e\u73b0\u672c\u5730\u5907\u4efdMySQL\u6570\u636e\u5e93\u3001\u4f7f\u7528<code>mysqldump<\/code>\u547d\u4ee4\u3001\u5c06\u5907\u4efd\u6587\u4ef6\u538b\u7f29\u5b58\u50a8\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u8c03\u7528\u7cfb\u7edf\u7684<code>mysqldump<\/code>\u547d\u4ee4\u6765\u5bfc\u51fa\u6570\u636e\u5e93\uff0c\u7136\u540e\u5c06\u5bfc\u51fa\u7684\u6587\u4ef6\u8fdb\u884c\u538b\u7f29\u4ee5\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>mysqldump<\/code>\u5de5\u5177\u662f\u5907\u4efdMySQL\u6570\u636e\u5e93\u7684\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u751f\u6210\u6570\u636e\u5e93\u7684SQL\u8f6c\u50a8\u6587\u4ef6\uff0c\u8fd9\u4e9b\u6587\u4ef6\u53ef\u4ee5\u7528\u6765\u91cd\u5efa\u6570\u636e\u5e93\u3002Python\u53ef\u4ee5\u901a\u8fc7\u5b50\u8fdb\u7a0b\u8c03\u7528\u8fd9\u4e2a\u5de5\u5177\u6765\u5b9e\u73b0\u81ea\u52a8\u5316\u5907\u4efd\u3002\u4e3a\u4e86\u786e\u4fdd\u5907\u4efd\u6587\u4ef6\u7684\u5b89\u5168\u6027\u548c\u5b58\u50a8\u6548\u7387\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5728\u5907\u4efd\u540e\u5bf9\u6587\u4ef6\u8fdb\u884c\u538b\u7f29\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684Python\u5e93\u548c\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86MySQL\uff0c\u5e76\u4e14\u5df2\u914d\u7f6e\u597d\u73af\u5883\u53d8\u91cf\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u4ece\u547d\u4ee4\u884c\u8bbf\u95ee<code>mysqldump<\/code>\u3002\u6b64\u5916\uff0c\u4f60\u8fd8\u9700\u8981\u5b89\u88c5Python\u7684MySQL\u9a71\u52a8\uff0c\u4f8b\u5982<code>mysql-connector-python<\/code>\uff0c\u4ee5\u53ca\u7528\u4e8e\u538b\u7f29\u6587\u4ef6\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mysql-connector-python<\/p>\n<p>pip install zipfile36<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Python\u8c03\u7528<code>mysqldump<\/code><\/h3>\n<\/p>\n<p><p><code>mysqldump<\/code>\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5b9e\u7528\u7a0b\u5e8f\uff0c\u7528\u4e8e\u4eceMySQL\u6570\u636e\u5e93\u5bfc\u51fa\u6570\u636e\u3002\u5b83\u80fd\u591f\u751f\u6210\u6570\u636e\u5e93\u7684SQL\u811a\u672c\uff0c\u8fd9\u4e9b\u811a\u672c\u53ef\u4ee5\u7528\u6765\u91cd\u5efa\u6570\u636e\u5e93\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Python\u7684<code>subprocess<\/code>\u6a21\u5757\u6765\u8c03\u7528\u8fd9\u4e2a\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def backup_database(host, user, password, database_name, backup_file_path):<\/p>\n<p>    dump_command = f&quot;mysqldump -h {host} -u {user} -p{password} {database_name} &gt; {backup_file_path}&quot;<\/p>\n<p>    subprocess.run(dump_command, shell=True, check=True)<\/p>\n<p>    print(f&quot;Backup of database &#39;{database_name}&#39; completed successfully!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u538b\u7f29\u5907\u4efd\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>zipfile<\/code>\u6a21\u5757\u5c06\u5907\u4efd\u6587\u4ef6\u8fdb\u884c\u538b\u7f29\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>import os<\/p>\n<p>def compress_backup(file_path, compressed_file_path):<\/p>\n<p>    with zipfile.ZipFile(compressed_file_path, &#39;w&#39;, zipfile.ZIP_DEFLATED) as zipf:<\/p>\n<p>        zipf.write(file_path, os.path.basename(file_path))<\/p>\n<p>    print(f&quot;Backup file compressed successfully to &#39;{compressed_file_path}&#39;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u793a\u4f8b\uff1a\u81ea\u52a8\u5316\u5907\u4efd\u6d41\u7a0b<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u5b8c\u6574\u7684Python\u811a\u672c\u6765\u81ea\u52a8\u5316\u5907\u4efd\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>import zipfile<\/p>\n<p>import os<\/p>\n<p>def backup_database(host, user, password, database_name, backup_file_path):<\/p>\n<p>    dump_command = f&quot;mysqldump -h {host} -u {user} -p{password} {database_name} &gt; {backup_file_path}&quot;<\/p>\n<p>    subprocess.run(dump_command, shell=True, check=True)<\/p>\n<p>    print(f&quot;Backup of database &#39;{database_name}&#39; completed successfully!&quot;)<\/p>\n<p>def compress_backup(file_path, compressed_file_path):<\/p>\n<p>    with zipfile.ZipFile(compressed_file_path, &#39;w&#39;, zipfile.ZIP_DEFLATED) as zipf:<\/p>\n<p>        zipf.write(file_path, os.path.basename(file_path))<\/p>\n<p>    print(f&quot;Backup file compressed successfully to &#39;{compressed_file_path}&#39;&quot;)<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    host = &quot;localhost&quot;<\/p>\n<p>    user = &quot;your_username&quot;<\/p>\n<p>    password = &quot;your_password&quot;<\/p>\n<p>    database_name = &quot;your_database&quot;<\/p>\n<p>    backup_file_path = &quot;backup.sql&quot;<\/p>\n<p>    compressed_file_path = &quot;backup.zip&quot;<\/p>\n<p>    backup_database(host, user, password, database_name, backup_file_path)<\/p>\n<p>    compress_backup(backup_file_path, compressed_file_path)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5907\u4efd\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u4f8b\u5982\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\u3001\u78c1\u76d8\u7a7a\u95f4\u4e0d\u8db3\u7b49\u3002\u4e3a\u4e86\u63d0\u9ad8\u811a\u672c\u7684\u5065\u58ee\u6027\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>Configure logging<\/strong><\/h2>\n<p>logging.basicConfig(filename=&#39;backup.log&#39;, level=logging.INFO, <\/p>\n<p>                    format=&#39;%(asctime)s:%(levelname)s:%(message)s&#39;)<\/p>\n<p>def backup_database(host, user, password, database_name, backup_file_path):<\/p>\n<p>    try:<\/p>\n<p>        dump_command = f&quot;mysqldump -h {host} -u {user} -p{password} {database_name} &gt; {backup_file_path}&quot;<\/p>\n<p>        subprocess.run(dump_command, shell=True, check=True)<\/p>\n<p>        logging.info(f&quot;Backup of database &#39;{database_name}&#39; completed successfully!&quot;)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        logging.error(f&quot;Error backing up database: {e}&quot;)<\/p>\n<p>        raise<\/p>\n<p>def compress_backup(file_path, compressed_file_path):<\/p>\n<p>    try:<\/p>\n<p>        with zipfile.ZipFile(compressed_file_path, &#39;w&#39;, zipfile.ZIP_DEFLATED) as zipf:<\/p>\n<p>            zipf.write(file_path, os.path.basename(file_path))<\/p>\n<p>        logging.info(f&quot;Backup file compressed successfully to &#39;{compressed_file_path}&#39;&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        logging.error(f&quot;Error compressing backup file: {e}&quot;)<\/p>\n<p>        raise<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b9a\u65f6\u5907\u4efd\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u81ea\u52a8\u5316\u5907\u4efd\u8fc7\u7a0b\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\uff0c\u4f8b\u5982Linux\u7684<code>cron<\/code>\u6216Windows\u7684\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\uff0c\u6765\u5b9a\u671f\u8fd0\u884cPython\u5907\u4efd\u811a\u672c\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5e93\u7684\u5b9a\u671f\u5907\u4efd\u548c\u6570\u636e\u5b89\u5168\u3002<\/p>\n<\/p>\n<p><h4>\u5728Linux\u4e0a\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1<\/h4>\n<\/p>\n<ol>\n<li>\u6253\u5f00<code>crontab<\/code>\u7f16\u8f91\u5668\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">crontab -e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6dfb\u52a0\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\uff0c\u4f8b\u5982\u6bcf\u5929\u51cc\u66682\u70b9\u8fd0\u884c\u5907\u4efd\u811a\u672c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">0 2 * * * \/usr\/bin\/python3 \/path\/to\/your\/backup_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5728Windows\u4e0a\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1<\/h4>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u4efb\u52a1\uff0c\u8bbe\u7f6e\u89e6\u53d1\u5668\u548c\u64cd\u4f5c\u3002<\/li>\n<li>\u5728\u64cd\u4f5c\u4e2d\uff0c\u9009\u62e9\u542f\u52a8\u7a0b\u5e8f\uff0c\u5e76\u9009\u62e9Python\u89e3\u91ca\u5668\u548c\u811a\u672c\u8def\u5f84\u3002<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u8fdb\u884cMySQL\u6570\u636e\u5e93\u5907\u4efd\u662f\u4e00\u4e2a\u7b80\u5355\u800c\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528<code>mysqldump<\/code>\u547d\u4ee4\u548cPython\u7684\u538b\u7f29\u529f\u80fd\uff0c\u53ef\u4ee5\u81ea\u52a8\u5316\u5907\u4efd\u8fc7\u7a0b\uff0c\u5e76\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u5b58\u50a8\u6548\u7387\u3002\u6dfb\u52a0\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\uff0c\u53ef\u4ee5\u63d0\u9ad8\u811a\u672c\u7684\u5065\u58ee\u6027\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b9a\u671f\u81ea\u52a8\u5316\u5907\u4efd\uff0c\u8fdb\u4e00\u6b65\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5907\u4efdMySQL\u6570\u636e\u5e93\uff1f<\/strong><br \/>\u4f7f\u7528Python\u5907\u4efdMySQL\u6570\u636e\u5e93\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6700\u5e38\u89c1\u7684\u662f\u4f7f\u7528<code>mysqldump<\/code>\u547d\u4ee4\u3002\u53ef\u4ee5\u901a\u8fc7<code>subprocess<\/code>\u6a21\u5757\u5728Python\u4e2d\u8c03\u7528\u6b64\u547d\u4ee4\uff0c\u6216\u8005\u4f7f\u7528<code>mysql-connector<\/code>\u7b49\u5e93\u8fde\u63a5\u5230\u6570\u636e\u5e93\u5e76\u6267\u884cSQL\u547d\u4ee4\u6765\u5bfc\u51fa\u6570\u636e\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e5f\u53ef\u4ee5\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\uff0c\u65b9\u4fbf\u5907\u4efd\u548c\u540e\u7eed\u5206\u6790\u3002<\/p>\n<p><strong>\u5907\u4efdMySQL\u6570\u636e\u5e93\u65f6\u6709\u54ea\u4e9b\u91cd\u8981\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u5907\u4efdMySQL\u6570\u636e\u5e93\u65f6\uff0c\u786e\u4fdd\u9009\u62e9\u5408\u9002\u7684\u5907\u4efd\u65f6\u95f4\uff0c\u907f\u514d\u5728\u9ad8\u5cf0\u671f\u8fdb\u884c\u64cd\u4f5c\u4ee5\u51cf\u5c11\u5bf9\u7cfb\u7edf\u6027\u80fd\u7684\u5f71\u54cd\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u6d4b\u8bd5\u5907\u4efd\u6587\u4ef6\u7684\u5b8c\u6574\u6027\u4e5f\u5f88\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u5728\u9700\u8981\u6062\u590d\u6570\u636e\u65f6\u80fd\u591f\u987a\u5229\u8fdb\u884c\u3002\u786e\u4fdd\u9009\u62e9\u5408\u9002\u7684\u5b58\u50a8\u4f4d\u7f6e\uff0c\u907f\u514d\u5c06\u5907\u4efd\u6587\u4ef6\u4e0e\u4e3b\u6570\u636e\u5e93\u5b58\u50a8\u5728\u540c\u4e00\u7269\u7406\u4f4d\u7f6e\uff0c\u4ee5\u63d0\u9ad8\u6570\u636e\u5b89\u5168\u6027\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u5907\u4efd\u7684MySQL\u6570\u636e\u5e93\u81ea\u52a8\u5316\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u811a\u672c\u7ed3\u5408\u5b9a\u65f6\u4efb\u52a1\uff08\u5982Linux\u7684Cron\u6216Windows\u7684\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\uff09\u6765\u81ea\u52a8\u5316MySQL\u6570\u636e\u5e93\u7684\u5907\u4efd\u8fc7\u7a0b\u3002\u901a\u8fc7\u7f16\u5199\u811a\u672c\u5e76\u8bbe\u7f6e\u5b9a\u65f6\u6267\u884c\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5e93\u5b9a\u671f\u5907\u4efd\uff0c\u4ece\u800c\u964d\u4f4e\u6570\u636e\u4e22\u5931\u7684\u98ce\u9669\u3002\u8fd8\u53ef\u4ee5\u5c06\u5907\u4efd\u6587\u4ef6\u4e0a\u4f20\u5230\u4e91\u5b58\u50a8\uff0c\u4ee5\u8fdb\u4e00\u6b65\u589e\u5f3a\u6570\u636e\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528MySQL\u9a71\u52a8\u548c\u7cfb\u7edf\u547d\u4ee4\u6765\u5b9e\u73b0\u672c\u5730\u5907\u4efdMySQL\u6570\u636e\u5e93\u3001\u4f7f\u7528mysqldump\u547d\u4ee4\u3001\u5c06 [&hellip;]","protected":false},"author":3,"featured_media":1015857,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1015854"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1015854"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1015854\/revisions"}],"predecessor-version":[{"id":1015859,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1015854\/revisions\/1015859"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1015857"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1015854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1015854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1015854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}