{"id":72,"date":"2025-12-14T00:37:29","date_gmt":"2025-12-13T16:37:29","guid":{"rendered":"https:\/\/blog.senru.site\/?p=72"},"modified":"2025-12-14T01:02:39","modified_gmt":"2025-12-13T17:02:39","slug":"%e8%84%9a%e6%9c%ac%e9%aa%8c%e8%af%81%e7%b3%bb%e7%bb%9f-%e6%95%b0%e6%8d%ae%e5%ba%93%e8%a1%a8%e5%88%9b%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/blog.senru.site\/index.php\/2025\/12\/14\/%e8%84%9a%e6%9c%ac%e9%aa%8c%e8%af%81%e7%b3%bb%e7%bb%9f-%e6%95%b0%e6%8d%ae%e5%ba%93%e8%a1%a8%e5%88%9b%e5%bb%ba\/","title":{"rendered":"\u811a\u672c\u9a8c\u8bc1\u7cfb\u7edf-\u6570\u636e\u5e93\u8868\u521b\u5efa"},"content":{"rendered":"\n<p class=\"has-medium-font-size wp-block-paragraph\">\u5e73\u65f6\u6709\u63a5\u89e6\u4e00\u4e9b\u642d\u5efa\u79c1\u670d\u6e38\u620f\u7684\u5185\u5bb9\uff0c\u7a0d\u7a0d\u7cbe\u901a\u540e\u4e5f\u662f\u5f04\u4e86\u4e9b\u4e00\u952e\u642d\u5efa\u7684\u811a\u672c\u3002\u4e3a\u4e86\u9632\u6b62\u6ee5\u7528\u641e\u4e86\u4e2a\u9a8c\u8bc1\u7cfb\u7edf<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>\u811a\u672c\u7f51\u7ad9\uff1a<a href=\"https:\/\/ym.senru.site\/\">https:\/\/ym.senru.site\/<\/a><\/strong><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e00\u3001\u57fa\u7840\u8868\u7ed3\u6784<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u811a\u672c\u8868 (allowed_scripts)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE <code>allowed_scripts<\/code> (<br><code>id<\/code> int(11) NOT NULL AUTO_INCREMENT,<br><code>script_id<\/code> varchar(100) NOT NULL,<br><code>script_name<\/code> varchar(255) NOT NULL,<br><code>max_daily_usage<\/code> int(11) DEFAULT 3,<br><code>description<\/code> text DEFAULT NULL,<br><code>is_active<\/code> tinyint(1) DEFAULT 1,<br><code>created_at<\/code> timestamp NULL DEFAULT CURRENT_TIMESTAMP,<br>PRIMARY KEY (<code>id<\/code>),<br>UNIQUE KEY <code>script_id<\/code> (<code>script_id<\/code>)<br>) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u811a\u672c\u4f7f\u7528\u8bb0\u5f55\u8868 (script_usage)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE <code>script_usage<\/code> (<br><code>id<\/code> int(11) NOT NULL AUTO_INCREMENT,<br><code>script_id<\/code> varchar(100) NOT NULL,<br><code>script_name<\/code> varchar(255) NOT NULL,<br><code>client_ip<\/code> varchar(45) NOT NULL,<br><code>usage_date<\/code> date NOT NULL,<br><code>usage_time<\/code> timestamp NULL DEFAULT CURRENT_TIMESTAMP,<br>PRIMARY KEY (<code>id<\/code>),<br>KEY <code>script_id<\/code> (<code>script_id<\/code>),<br>KEY <code>client_ip<\/code> (<code>client_ip<\/code>),<br>KEY <code>usage_date<\/code> (<code>usage_date<\/code>)<br>) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u6bcf\u65e5\u767d\u540d\u5355\u8868 (daily_whitelist)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE <code>daily_whitelist<\/code> (<br><code>id<\/code> int(11) NOT NULL AUTO_INCREMENT,<br><code>ip_address<\/code> varchar(45) NOT NULL,<br><code>description<\/code> varchar(255) DEFAULT NULL,<br><code>expires_at<\/code> date NOT NULL,<br><code>created_at<\/code> timestamp NULL DEFAULT CURRENT_TIMESTAMP,<br>PRIMARY KEY (<code>id<\/code>),<br>UNIQUE KEY <code>ip_address<\/code> (<code>ip_address<\/code>)<br>) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u6c38\u4e45\u767d\u540d\u5355\u8868 (permanent_whitelist)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE <code>permanent_whitelist<\/code> (<br><code>id<\/code> int(11) NOT NULL AUTO_INCREMENT,<br><code>ip_address<\/code> varchar(45) NOT NULL,<br><code>description<\/code> varchar(255) DEFAULT NULL,<br><code>created_at<\/code> timestamp NULL DEFAULT CURRENT_TIMESTAMP,<br>PRIMARY KEY (<code>id<\/code>),<br>UNIQUE KEY <code>ip_address<\/code> (<code>ip_address<\/code>)<br>) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u7cfb\u7edf\u8bbe\u7f6e\u8868 (system_settings)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE <code>system_settings<\/code> (<br><code>id<\/code> int(11) NOT NULL AUTO_INCREMENT,<br><code>setting_key<\/code> varchar(100) NOT NULL,<br><code>setting_value<\/code> text DEFAULT NULL,<br><code>description<\/code> varchar(255) DEFAULT NULL,<br><code>updated_at<\/code> timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,<br><code>created_at<\/code> timestamp NULL DEFAULT CURRENT_TIMESTAMP,<br>PRIMARY KEY (<code>id<\/code>),<br>UNIQUE KEY <code>setting_key<\/code> (<code>setting_key<\/code>)<br>) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. \u7cfb\u7edf\u65e5\u5fd7\u8868 (system_logs)&nbsp;<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE <code>system_logs<\/code> (<br><code>id<\/code> int(11) NOT NULL AUTO_INCREMENT,<br><code>action<\/code> varchar(100) NOT NULL,<br><code>description<\/code> text DEFAULT NULL,<br><code>ip_address<\/code> varchar(45) DEFAULT NULL,<br><code>created_at<\/code> timestamp NULL DEFAULT CURRENT_TIMESTAMP,<br>PRIMARY KEY (<code>id<\/code>),<br>KEY <code>created_at<\/code> (<code>created_at<\/code>),<br>KEY <code>action<\/code> (<code>action<\/code>)<br>) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e8c\u3001\u5b8c\u6574\u7684\u6570\u636e\u5e93\u521d\u59cb\u5316\u811a\u672c<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1. init_database_final.sql\u811a\u672c\u4ee3\u7801<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u811a\u672c\u9a8c\u8bc1\u7cfb\u7edf - \u6700\u7ec8\u7248\u6570\u636e\u5e93\u521d\u59cb\u5316\u811a\u672c\n-- \u4fdd\u5b58\u4e3a\uff1ainit_database_final.sql\n\nSET NAMES utf8mb4;\nSET FOREIGN_KEY_CHECKS = 0;\n\n-- 1. \u521b\u5efa\u7cfb\u7edf\u8bbe\u7f6e\u8868\nDROP TABLE IF EXISTS `system_settings`;\nCREATE TABLE `system_settings` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `setting_key` varchar(100) NOT NULL,\n  `setting_value` text DEFAULT NULL,\n  `description` varchar(255) DEFAULT NULL,\n  `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `setting_key` (`setting_key`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n\n-- \u63d2\u5165\u9ed8\u8ba4\u7cfb\u7edf\u8bbe\u7f6e\nINSERT INTO `system_settings` (`setting_key`, `setting_value`, `description`) VALUES\n('max_daily_usage_default', '3', '\u9ed8\u8ba4\u6bcf\u65e5\u4f7f\u7528\u6b21\u6570\u9650\u5236'),\n('auto_cleanup_days', '30', '\u81ea\u52a8\u6e05\u7406\u591a\u5c11\u5929\u524d\u7684\u6570\u636e'),\n('system_name', '\u811a\u672c\u9a8c\u8bc1\u7cfb\u7edf', '\u7cfb\u7edf\u540d\u79f0'),\n('admin_email', 'admin@example.com', '\u7ba1\u7406\u5458\u90ae\u7bb1'),\n('enable_notifications', '1', '\u662f\u5426\u542f\u7528\u901a\u77e5'),\n('retention_period', '365', '\u6570\u636e\u4fdd\u7559\u5929\u6570'),\n('theme', 'default', '\u7cfb\u7edf\u4e3b\u9898'),\n('language', 'zh-CN', '\u7cfb\u7edf\u8bed\u8a00'),\n('api_rate_limit', '100', 'API\u901f\u7387\u9650\u5236\uff08\u6bcf\u5206\u949f\uff09'),\n('maintenance_mode', '0', '\u7ef4\u62a4\u6a21\u5f0f\u5f00\u5173'),\n('log_retention_days', '90', '\u65e5\u5fd7\u4fdd\u7559\u5929\u6570');\n\n-- 2. \u521b\u5efa\u7cfb\u7edf\u65e5\u5fd7\u8868\nDROP TABLE IF EXISTS `system_logs`;\nCREATE TABLE `system_logs` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `action` varchar(100) NOT NULL,\n  `description` text DEFAULT NULL,\n  `ip_address` varchar(45) DEFAULT NULL,\n  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n  PRIMARY KEY (`id`),\n  KEY `created_at` (`created_at`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n\n-- 3. \u68c0\u67e5\u5e76\u521b\u5efa\u5176\u4ed6\u8868\uff08\u5982\u679c\u4e0d\u5b58\u5728\uff09\n\n-- 3.1 \u811a\u672c\u8868\nDROP TABLE IF EXISTS `allowed_scripts`;\nCREATE TABLE `allowed_scripts` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `script_id` varchar(100) NOT NULL,\n  `script_name` varchar(255) NOT NULL,\n  `max_daily_usage` int(11) DEFAULT 3,\n  `description` text DEFAULT NULL,\n  `is_active` tinyint(1) DEFAULT 1,\n  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `script_id` (`script_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n\n-- 3.2 \u811a\u672c\u4f7f\u7528\u8bb0\u5f55\u8868\nDROP TABLE IF EXISTS `script_usage`;\nCREATE TABLE `script_usage` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `script_id` varchar(100) NOT NULL,\n  `script_name` varchar(255) NOT NULL,\n  `client_ip` varchar(45) NOT NULL,\n  `usage_date` date NOT NULL,\n  `usage_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n  PRIMARY KEY (`id`),\n  KEY `script_id` (`script_id`),\n  KEY `client_ip` (`client_ip`),\n  KEY `usage_date` (`usage_date`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n\n-- 3.3 \u6bcf\u65e5\u767d\u540d\u5355\u8868\nDROP TABLE IF EXISTS `daily_whitelist`;\nCREATE TABLE `daily_whitelist` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `ip_address` varchar(45) NOT NULL,\n  `description` varchar(255) DEFAULT NULL,\n  `expires_at` date NOT NULL,\n  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `ip_address` (`ip_address`),\n  KEY `expires_at` (`expires_at`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n\n-- 3.4 \u6c38\u4e45\u767d\u540d\u5355\u8868\nDROP TABLE IF EXISTS `permanent_whitelist`;\nCREATE TABLE `permanent_whitelist` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `ip_address` varchar(45) NOT NULL,\n  `description` varchar(255) DEFAULT NULL,\n  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `ip_address` (`ip_address`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n\n-- 4. \u6dfb\u52a0\u5fc5\u8981\u7684\u7d22\u5f15\uff08\u4f7f\u7528\u5b89\u5168\u7684\u65b9\u5f0f\uff09\n-- \u68c0\u67e5\u5e76\u521b\u5efa\u7d22\u5f15\u7684\u5b58\u50a8\u8fc7\u7a0b\nDROP PROCEDURE IF EXISTS `CreateIndexIfNotExists`;\nDELIMITER \/\/\nCREATE PROCEDURE `CreateIndexIfNotExists`(\n    IN tableName VARCHAR(64),\n    IN indexName VARCHAR(64),\n    IN indexColumns VARCHAR(255)\n)\nBEGIN\n    DECLARE indexExists INT DEFAULT 0;\n    \n    -- \u68c0\u67e5\u7d22\u5f15\u662f\u5426\u5b58\u5728\n    SELECT COUNT(1) INTO indexExists\n    FROM information_schema.STATISTICS \n    WHERE TABLE_SCHEMA = DATABASE() \n    AND TABLE_NAME = tableName \n    AND INDEX_NAME = indexName;\n    \n    -- \u5982\u679c\u7d22\u5f15\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\n    IF indexExists = 0 THEN\n        SET @sql = CONCAT('CREATE INDEX ', indexName, ' ON `', tableName, '`(', indexColumns, ')');\n        PREPARE stmt FROM @sql;\n        EXECUTE stmt;\n        DEALLOCATE PREPARE stmt;\n    END IF;\nEND\/\/\nDELIMITER ;\n\n-- \u521b\u5efa\u6240\u6709\u5fc5\u8981\u7684\u7d22\u5f15\nCALL CreateIndexIfNotExists('script_usage', 'idx_script_usage_script_date', 'script_id, usage_date');\nCALL CreateIndexIfNotExists('allowed_scripts', 'idx_allowed_scripts_active', 'is_active');\nCALL CreateIndexIfNotExists('daily_whitelist', 'idx_daily_whitelist_expires', 'expires_at');\nCALL CreateIndexIfNotExists('system_logs', 'idx_system_logs_action', 'action');\n\n-- \u5220\u9664\u4e34\u65f6\u5b58\u50a8\u8fc7\u7a0b\nDROP PROCEDURE IF EXISTS `CreateIndexIfNotExists`;\n\n-- 5. \u8bb0\u5f55\u521d\u59cb\u5316\u65e5\u5fd7\nINSERT INTO `system_logs` (`action`, `description`, `ip_address`) \nVALUES ('system', '\u6570\u636e\u5e93\u521d\u59cb\u5316\u5b8c\u6210', 'system');\n\nSET FOREIGN_KEY_CHECKS = 1;\n\n-- \u663e\u793a\u521b\u5efa\u7ed3\u679c\nSELECT '\u6570\u636e\u5e93\u8868\u521b\u5efa\u5b8c\u6210!' AS `message`;\nSELECT \n    TABLE_NAME AS `\u8868\u540d`,\n    TABLE_ROWS AS `\u884c\u6570`,\n    CREATE_TIME AS `\u521b\u5efa\u65f6\u95f4`\nFROM information_schema.TABLES \nWHERE TABLE_SCHEMA = DATABASE() \nORDER BY TABLE_NAME;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u4f7f\u7528MySQL\u547d\u4ee4\u884c\u6267\u884c\u521d\u59cb\u5316\u811a\u672c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># \u8fde\u63a5\u5230\u6570\u636e\u5e93\nmysql -h \u6570\u636e\u5e93\u5730\u5740 -P \u7aef\u53e3 -u \u7528\u6237\u540d -p \u6570\u636e\u5e93\u540d\n\n# \u6267\u884c\u521d\u59cb\u5316\u811a\u672c\nmysql&gt; source init_database_final.sql;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u4f7f\u7528PHP\u811a\u672c\u6267\u884c\u6267\u884c\u521d\u59cb\u5316\u811a\u672c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ init_db.php - \u6570\u636e\u5e93\u521d\u59cb\u5316\u811a\u672c\n$db_host = '\u6570\u636e\u5e93\u5730\u5740:\u7aef\u53e3';\n$db_name = '\u6570\u636e\u5e93\u540d';\n$db_user = '\u7528\u6237\u540d';\n$db_pass = '\u6570\u636e\u5e93\u5bc6\u78014';\n\ntry {\n    $db = new PDO(\"mysql:host=$db_host;dbname=$db_name;charset=utf8mb4\", $db_user, $db_pass);\n    $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n    \n    \/\/ \u8bfb\u53d6SQL\u6587\u4ef6\n    $sql = file_get_contents('init_database_final.sql');\n    \n    \/\/ \u5206\u5272SQL\u8bed\u53e5\n    $statements = explode(';', $sql);\n    \n    foreach ($statements as $statement) {\n        $statement = trim($statement);\n        if (!empty($statement)) {\n            try {\n                $db-&gt;exec($statement);\n            } catch (PDOException $e) {\n                \/\/ \u5ffd\u7565\u91cd\u590d\u521b\u5efa\u8868\u7684\u9519\u8bef\n                if (strpos($e-&gt;getMessage(), 'already exists') === false) {\n                    echo \"\u9519\u8bef: \" . $e-&gt;getMessage() . \"\\n\";\n                }\n            }\n        }\n    }\n    \n    echo \"\u6570\u636e\u5e93\u521d\u59cb\u5316\u6210\u529f\uff01\\n\";\n    \n} catch (PDOException $e) {\n    die(\"\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25: \" . $e-&gt;getMessage());\n}\n?&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u6267\u884c\nphp init_db.php<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5e73\u65f6\u6709\u63a5\u89e6\u4e00\u4e9b\u642d\u5efa\u79c1\u670d\u6e38\u620f\u7684\u5185\u5bb9\uff0c\u7a0d\u7a0d\u7cbe\u901a\u540e\u4e5f\u662f\u5f04\u4e86\u4e9b\u4e00\u952e\u642d\u5efa\u7684\u811a\u672c\u3002\u4e3a\u4e86\u9632\u6b62\u6ee5\u7528\u641e\u4e86\u4e2a\u9a8c\u8bc1\u7cfb\u7edf \u811a\u672c\u7f51\u7ad9\uff1ah [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-72","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.senru.site\/index.php\/wp-json\/wp\/v2\/posts\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.senru.site\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.senru.site\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.senru.site\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.senru.site\/index.php\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":5,"href":"https:\/\/blog.senru.site\/index.php\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":80,"href":"https:\/\/blog.senru.site\/index.php\/wp-json\/wp\/v2\/posts\/72\/revisions\/80"}],"wp:attachment":[{"href":"https:\/\/blog.senru.site\/index.php\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.senru.site\/index.php\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.senru.site\/index.php\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}