{"id":63663,"date":"2025-04-29T14:50:44","date_gmt":"2025-04-29T06:50:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/63663\/"},"modified":"2025-04-29T14:50:44","modified_gmt":"2025-04-29T06:50:44","slug":"linux%e6%80%8e%e4%b9%88%e6%89%a7%e8%a1%8csql-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/63663\/","title":{"rendered":"linux\u600e\u4e48\u6267\u884csql"},"content":{"rendered":"<blockquote><p>\n  \u5728 linux \u7cfb\u7edf\u4e2d\u6267\u884c sql \u8bed\u53e5\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 mysql \u547d\u4ee4\u884c\u5de5\u5177\uff08\u5b89\u88c5 mysql \u5ba2\u6237\u7aef\u3001\u767b\u5f55 mysql \u670d\u52a1\u5668\u3001\u6267\u884c sql \u8bed\u53e5\uff09\u4f7f\u7528 python mysqldb \u5e93\uff08\u5bfc\u5165\u5e93\u3001\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884c sql \u8bed\u53e5\uff09\u4f7f\u7528 php mysqli \u5e93\uff08\u5bfc\u5165\u5e93\u3001\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884c sql \u8bed\u53e5\uff09\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/04\/2024060410122123497.jpg\" class=\"aligncenter\" title=\"linux\u600e\u4e48\u6267\u884csql\u63d2\u56fe\" alt=\"linux\u600e\u4e48\u6267\u884csql\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728 Linux \u4e2d\u6267\u884c SQL<\/strong><\/p>\n<p>\u5728 Linux \u7cfb\u7edf\u4e2d\u6267\u884c SQL \u8bed\u53e5\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177<\/strong><\/p>\n<p><strong>1.1. \u5b89\u88c5 MySQL \u5ba2\u6237\u7aef<\/strong><\/p>\n<pre>sudo apt-get install mysql-client<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>1.2. \u767b\u5f55 MySQL \u670d\u52a1\u5668<\/strong><\/p>\n<pre>mysql -u [\u7528\u6237\u540d] -p<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>1.3. \u6267\u884c SQL \u8bed\u53e5<\/strong><\/p>\n<pre>mysql&gt; SELECT * FROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 Python \u5e93<\/strong><\/p>\n<p><strong>2.1. \u5b89\u88c5 Python MySQLdb \u5e93<\/strong><\/p>\n<pre>sudo pip install mysqlclient<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2.2. \u5bfc\u5165\u5e93\u5e76\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/p>\n<pre>import mysql.connector\n\nmydb = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"username\",\n    password=\"password\",\n    database=\"database_name\",\n)\n\nmycursor = mydb.cursor()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2.3. \u6267\u884c SQL \u8bed\u53e5<\/strong><\/p>\n<pre>mycursor.execute(\"SELECT * FROM table_name\")\nresults = mycursor.fetchall()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a\u4f7f\u7528 PHP \u5e93<\/strong><\/p>\n<p><strong>3.1. \u5b89\u88c5 PHP MySQLi \u5e93<\/strong><\/p>\n<pre>sudo apt-get install php7.4-mysqli<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3.2. \u5bfc\u5165\u5e93\u5e76\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/p>\n<pre>&lt;?php\n\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"database_name\";\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . $conn-&gt;connect_error);\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3.3. \u6267\u884c SQL \u8bed\u53e5<\/strong><\/p>\n<pre>$sql = \"SELECT * FROM table_name\";\n$result = $conn-&gt;query($sql);\n\n\/\/ \u5faa\u73af\u67e5\u8be2\u7ed3\u679c\nif ($result-&gt;num_rows &gt; 0) {\n    \/\/ \u8f93\u51fa\u6570\u636e\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"id: \" . $row[\"id\"] . \" - Name: \" . $row[\"name\"] . \"&lt;br&gt;\";\n    }\n} else {\n    echo \"0 results\";\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u6267\u884csql\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 linux \u7cfb\u7edf\u4e2d\u6267\u884c sql \u8bed\u53e5\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 mysql \u547d\u4ee4\u884c\u5de5\u5177\uff08\u5b89\u88c5 mysql \u5ba2\u6237\u7aef\u3001\u767b\u5f55 mysql \u670d\u52a1\u5668\u3001\u6267\u884c sql \u8bed\u53e5\uff09\u4f7f\u7528 python mysqldb \u5e93\uff08\u5bfc\u5165\u5e93\u3001\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884c sql \u8bed\u53e5\uff09\u4f7f\u7528 php mysqli \u5e93\uff08\u5bfc\u5165\u5e93\u3001\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884c sql \u8bed\u53e5\uff09 \u5728 Linux \u4e2d\u6267\u884c SQL \u5728 Linux \u7cfb\u7edf\u4e2d\u6267\u884c SQL \u8bed\u53e5\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a \u65b9\u6cd5 1\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177 1.1. \u5b89\u88c5 MySQL \u5ba2\u6237\u7aef sudo apt-get install mysql-client \u767b\u5f55\u540e\u590d\u5236 1.2. \u767b\u5f55 MySQL \u670d\u52a1\u5668 mysql -u [\u7528\u6237\u540d] -p \u767b\u5f55\u540e\u590d\u5236 1.3. \u6267\u884c SQL [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-63663","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=63663"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63663\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=63663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=63663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=63663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}