{"id":1756,"date":"2025-02-18T00:07:56","date_gmt":"2025-02-17T16:07:56","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1756"},"modified":"2025-02-18T00:07:56","modified_gmt":"2025-02-17T16:07:56","slug":"mysql-%e5%88%9b%e5%bb%ba%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%88%e9%99%84%e5%b8%a6%e5%ae%9e%e4%be%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/18\/mysql-%e5%88%9b%e5%bb%ba%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%88%e9%99%84%e5%b8%a6%e5%ae%9e%e4%be%8b%ef%bc%89\/","title":{"rendered":"MySQL \u521b\u5efa\u6570\u636e\u5e93\uff08\u9644\u5e26\u5b9e\u4f8b\uff09"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f <strong>MySQL \u521b\u5efa\u6570\u636e\u5e93\u7684\u4e0d\u540c\u65b9\u6cd5<\/strong>\uff0c\u5305\u62ec\u547d\u4ee4\u884c\u3001MySQLi \u6269\u5c55\u3001PDO \u6269\u5c55\u548c <code>mysqladmin<\/code> \u5de5\u5177\u7b49\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u9644\u5e26\u5b9e\u4f8b\uff0c\u5e2e\u52a9\u4f60\u7406\u89e3\u5982\u4f55\u901a\u8fc7\u4e0d\u540c\u65b9\u5f0f\u5728 MySQL \u4e2d\u521b\u5efa\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 MySQL \u547d\u4ee4\u884c\u521b\u5efa\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u57fa\u672c SQL \u8bed\u6cd5<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE DATABASE database_name;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5b9e\u4f8b\uff1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>my_blog<\/code> \u7684\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE DATABASE my_blog;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6307\u5b9a\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE DATABASE my_blog\nCHARACTER SET utf8\nCOLLATE utf8_general_ci;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u4f7f\u7528 <code>mysqladmin<\/code> \u5de5\u5177\u521b\u5efa\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mysqladmin<\/code> \u662f MySQL \u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u7528\u4e8e\u6267\u884c MySQL \u670d\u52a1\u5668\u7ba1\u7406\u4efb\u52a1\uff0c\u5305\u62ec\u521b\u5efa\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u57fa\u672c\u547d\u4ee4\u683c\u5f0f<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>mysqladmin -u username -p create database_name<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5b9e\u4f8b\uff1a\u4f7f\u7528 <code>mysqladmin<\/code> \u521b\u5efa <code>my_blog<\/code> \u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>mysqladmin -u root -p create my_blog<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>-u<\/code>\uff1a\u6307\u5b9a\u7528\u6237\u540d\uff08\u4f8b\u5982 <code>root<\/code>\uff09\u3002<\/li>\n\n\n\n<li><code>-p<\/code>\uff1a\u8f93\u5165\u5bc6\u7801\u540e\u521b\u5efa\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><code>create my_blog<\/code>\uff1a\u521b\u5efa\u540d\u4e3a <code>my_blog<\/code> \u7684\u6570\u636e\u5e93\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528 MySQLi \u6269\u5c55\u5728 PHP \u4e2d\u521b\u5efa\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MySQLi \u662f PHP \u7528\u4e8e\u4e0e MySQL \u6570\u636e\u5e93\u4ea4\u4e92\u7684\u6269\u5c55\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5b9e\u4f8b\uff1a\u4f7f\u7528 MySQLi \u6269\u5c55\u521b\u5efa\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u521b\u5efa\u8fde\u63a5\n$connection = mysqli_connect(\"localhost\", \"username\", \"password\");\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\nif (!$connection) {\n    die(\"Connection failed: \" . mysqli_connect_error());\n}\n\n\/\/ \u521b\u5efa\u6570\u636e\u5e93\n$sql = \"CREATE DATABASE my_blog\";\nif (mysqli_query($connection, $sql)) {\n    echo \"Database created successfully\";\n} else {\n    echo \"Error creating database: \" . mysqli_error($connection);\n}\n\n\/\/ \u5173\u95ed\u8fde\u63a5\nmysqli_close($connection);\n?&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7 <code>mysqli_connect()<\/code> \u521b\u5efa\u4e0e MySQL \u7684\u8fde\u63a5\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>mysqli_query()<\/code> \u6267\u884c <code>CREATE DATABASE<\/code> SQL \u8bed\u53e5\u3002<\/li>\n\n\n\n<li>\u68c0\u67e5\u662f\u5426\u6210\u529f\u5e76\u8f93\u51fa\u7ed3\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4f7f\u7528 PDO \u6269\u5c55\u5728 PHP \u4e2d\u521b\u5efa\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PDO \u662f PHP \u4e2d\u7684\u6570\u636e\u5e93\u62bd\u8c61\u5c42\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5b9e\u4f8b\uff1a\u4f7f\u7528 PDO \u6269\u5c55\u521b\u5efa\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\ntry {\n    \/\/ \u521b\u5efa PDO \u5b9e\u4f8b\u5e76\u8fde\u63a5 MySQL\n    $pdo = new PDO(\"mysql:host=localhost\", \"username\", \"password\");\n\n    \/\/ \u8bbe\u7f6e PDO \u9519\u8bef\u6a21\u5f0f\u4e3a\u5f02\u5e38\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n\n    \/\/ \u521b\u5efa\u6570\u636e\u5e93\n    $sql = \"CREATE DATABASE my_blog\";\n    $pdo-&gt;exec($sql);\n    echo \"Database created successfully\";\n} catch (PDOException $e) {\n    echo \"Error creating database: \" . $e-&gt;getMessage();\n}\n\n\/\/ \u5173\u95ed\u8fde\u63a5\n$pdo = null;\n?&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>new PDO()<\/code> \u8fde\u63a5\u5230 MySQL\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>exec()<\/code> \u6267\u884c <code>CREATE DATABASE<\/code> \u547d\u4ee4\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7 <code>PDOException<\/code> \u6355\u83b7\u4efb\u4f55\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u901a\u8fc7 <code>SHOW DATABASES<\/code> \u67e5\u770b\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u521b\u5efa\u6570\u636e\u5e93\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>SHOW DATABASES<\/code> \u67e5\u770b\u6240\u6709\u53ef\u7528\u7684\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>SQL \u547d\u4ee4<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW DATABASES;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u5207\u6362\u5230\u65b0\u521b\u5efa\u7684\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u6570\u636e\u5e93\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>USE<\/code> \u547d\u4ee4\u5207\u6362\u5230\u8be5\u6570\u636e\u5e93\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>SQL \u547d\u4ee4<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>USE my_blog;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u5220\u9664\u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u5220\u9664\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>DROP DATABASE<\/code> \u547d\u4ee4\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>SQL \u547d\u4ee4<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP DATABASE my_blog;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684 <strong>MySQL \u521b\u5efa\u6570\u636e\u5e93<\/strong> \u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u547d\u4ee4\u884c<\/strong>\uff1a\u4f7f\u7528 <code>CREATE DATABASE<\/code> \u8bed\u53e5\u3002<\/li>\n\n\n\n<li><strong>mysqladmin \u5de5\u5177<\/strong>\uff1a\u4f7f\u7528 <code>mysqladmin -u username -p create database_name<\/code> \u6765\u521b\u5efa\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><strong>MySQLi \u6269\u5c55<\/strong>\uff1a\u5728 PHP \u4e2d\u901a\u8fc7 <code>mysqli_query()<\/code> \u6267\u884c SQL \u521b\u5efa\u6570\u636e\u5e93\u547d\u4ee4\u3002<\/li>\n\n\n\n<li><strong>PDO \u6269\u5c55<\/strong>\uff1a\u5728 PHP \u4e2d\u901a\u8fc7 <code>exec()<\/code> \u6267\u884c SQL \u521b\u5efa\u6570\u636e\u5e93\u547d\u4ee4\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bcf\u79cd\u65b9\u6cd5\u7684\u9009\u62e9\u53d6\u51b3\u4e8e\u4f60\u7684\u4f7f\u7528\u573a\u666f\uff0c\u4f8b\u5982\u5728\u811a\u672c\u4e2d\u521b\u5efa\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9 MySQLi \u6216 PDO\uff1b\u5728\u547d\u4ee4\u884c\u4e2d\u5feb\u901f\u7ba1\u7406\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9 <code>mysqladmin<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f MySQL \u521b\u5efa\u6570\u636e\u5e93\u7684\u4e0d\u540c\u65b9\u6cd5\uff0c\u5305\u62ec\u547d\u4ee4\u884c\u3001MySQLi \u6269\u5c55\u3001PDO \u6269\u5c55\u548c mysqladmin [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1756","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1756"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1756\/revisions"}],"predecessor-version":[{"id":1757,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1756\/revisions\/1757"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}