{"id":1643,"date":"2025-02-14T23:16:15","date_gmt":"2025-02-14T15:16:15","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1643"},"modified":"2025-02-14T23:19:08","modified_gmt":"2025-02-14T15:19:08","slug":"mysql-%e5%ae%9e%e6%88%98%e9%a1%b9%e7%9b%ae%ef%bc%9a%e6%9e%84%e5%bb%ba%e9%ab%98%e6%95%88%e6%95%b0%e6%8d%ae%e5%ba%93%e7%b3%bb%e7%bb%9f%e4%b8%8e%e5%ba%94%e7%94%a8%e9%9b%86%e6%88%90%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/14\/mysql-%e5%ae%9e%e6%88%98%e9%a1%b9%e7%9b%ae%ef%bc%9a%e6%9e%84%e5%bb%ba%e9%ab%98%e6%95%88%e6%95%b0%e6%8d%ae%e5%ba%93%e7%b3%bb%e7%bb%9f%e4%b8%8e%e5%ba%94%e7%94%a8%e9%9b%86%e6%88%90%e6%8a%80%e5%b7%a7\/","title":{"rendered":"MySQL \u5b9e\u6218\u9879\u76ee\uff1a\u6784\u5efa\u9ad8\u6548\u6570\u636e\u5e93\u7cfb\u7edf\u4e0e\u5e94\u7528\u96c6\u6210\u6280\u5de7"},"content":{"rendered":"\r\n<p class=\"wp-block-paragraph\">\u7ee7\u7eed\u6df1\u5165\u5b66\u4e60 <strong>MySQL \u5b9e\u6218\u9879\u76ee<\/strong>\uff0c\u8fd9\u4e00\u90e8\u5206\u5c06\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u9879\u76ee\u6848\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u9ad8\u6548\u5730\u4f7f\u7528 MySQL\uff0c\u4ece\u6570\u636e\u5e93\u8bbe\u8ba1\u3001\u6027\u80fd\u4f18\u5316\u5230\u6545\u969c\u6392\u67e5\uff0c\u6db5\u76d6\u6574\u4e2a\u9879\u76ee\u5f00\u53d1\u7684\u8fc7\u7a0b\u3002<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">1. <strong>\u9879\u76ee\u80cc\u666f\u4e0e\u9700\u6c42\u5206\u6790<\/strong><\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u8981\u4e3a\u4e00\u4e2a\u7535\u5b50\u5546\u52a1\u7f51\u7ad9\u8bbe\u8ba1\u6570\u636e\u5e93\u7cfb\u7edf\uff0c\u8be5\u7cfb\u7edf\u9700\u8981\u5904\u7406\u5546\u54c1\u4fe1\u606f\u3001\u8ba2\u5355\u7ba1\u7406\u3001\u7528\u6237\u8d26\u6237\u3001\u652f\u4ed8\u8bb0\u5f55\u7b49\u4e1a\u52a1\u3002\u8be5\u7cfb\u7edf\u9700\u8981\u652f\u6301\u9ad8\u5e76\u53d1\u7684\u8bfb\u5199\u64cd\u4f5c\uff0c\u4e14\u8981\u6c42\u6570\u636e\u4e00\u81f4\u6027\u548c\u9ad8\u53ef\u7528\u6027\u3002<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">\u9700\u6c42\u5206\u6790\uff1a<\/h4>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><strong>\u7528\u6237\u7ba1\u7406<\/strong>\uff1a\u5305\u62ec\u7528\u6237\u7684\u6ce8\u518c\u3001\u767b\u5f55\u3001\u4fe1\u606f\u4fee\u6539\u7b49\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u5546\u54c1\u7ba1\u7406<\/strong>\uff1a\u5305\u62ec\u5546\u54c1\u7684\u5206\u7c7b\u3001\u5e93\u5b58\u3001\u4ef7\u683c\u7b49\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u8ba2\u5355\u7ba1\u7406<\/strong>\uff1a\u5305\u62ec\u521b\u5efa\u8ba2\u5355\u3001\u652f\u4ed8\u3001\u8ba2\u5355\u72b6\u6001\u7ba1\u7406\u7b49\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u652f\u4ed8\u8bb0\u5f55<\/strong>\uff1a\u8bb0\u5f55\u6bcf\u4e2a\u8ba2\u5355\u7684\u652f\u4ed8\u60c5\u51b5\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u9ad8\u5e76\u53d1\u652f\u6301<\/strong>\uff1a\u80fd\u591f\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u6570\u636e\u4e00\u81f4\u6027<\/strong>\uff1a\u4fdd\u8bc1\u4e8b\u52a1\u7684ACID\u7279\u6027\uff0c\u907f\u514d\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">2. <strong>\u6570\u636e\u5e93\u8bbe\u8ba1\u4e0e\u5efa\u6a21<\/strong><\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u6839\u636e\u4e1a\u52a1\u9700\u6c42\uff0c\u8bbe\u8ba1\u6570\u636e\u5e93\u8868\u7ed3\u6784\uff0c\u786e\u4fdd\u6570\u636e\u7684\u89c4\u8303\u5316\uff0c\u540c\u65f6\u8003\u8651\u6027\u80fd\u4f18\u5316\u3002<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">2.1. <strong>ER \u56fe\u8bbe\u8ba1<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u8bbe\u8ba1\u5b9e\u4f53-\u5173\u7cfb\u56fe\uff08ER\u56fe\uff09\uff0c\u660e\u786e\u8868\u4e0e\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u6211\u4eec\u9700\u8981\u7684\u57fa\u672c\u5b9e\u4f53\u6709\uff1a<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><strong>\u7528\u6237\u8868\uff08users\uff09<\/strong><\/li>\r\n\r\n\r\n\r\n<li><strong>\u5546\u54c1\u8868\uff08products\uff09<\/strong><\/li>\r\n\r\n\r\n\r\n<li><strong>\u8ba2\u5355\u8868\uff08orders\uff09<\/strong><\/li>\r\n\r\n\r\n\r\n<li><strong>\u8ba2\u5355\u9879\u8868\uff08order_items\uff09<\/strong><\/li>\r\n\r\n\r\n\r\n<li><strong>\u652f\u4ed8\u8bb0\u5f55\u8868\uff08payments\uff09<\/strong><\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">2.2. <strong>\u8868\u7ed3\u6784\u8bbe\u8ba1<\/strong><\/h4>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><strong>\u7528\u6237\u8868<\/strong>\uff08users\uff09\uff1a<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>  CREATE TABLE users (\r\n      user_id INT AUTO_INCREMENT PRIMARY KEY,\r\n      username VARCHAR(50) NOT NULL UNIQUE,\r\n      password_hash VARCHAR(255) NOT NULL,\r\n      email VARCHAR(100) NOT NULL UNIQUE,\r\n      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\r\n  );<\/code><\/pre>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><strong>\u5546\u54c1\u8868<\/strong>\uff08products\uff09\uff1a<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>  CREATE TABLE products (\r\n      product_id INT AUTO_INCREMENT PRIMARY KEY,\r\n      name VARCHAR(100) NOT NULL,\r\n      description TEXT,\r\n      price DECIMAL(10, 2) NOT NULL,\r\n      stock INT NOT NULL DEFAULT 0,\r\n      category_id INT,\r\n      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\r\n  );<\/code><\/pre>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><strong>\u8ba2\u5355\u8868<\/strong>\uff08orders\uff09\uff1a<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>  CREATE TABLE orders (\r\n      order_id INT AUTO_INCREMENT PRIMARY KEY,\r\n      user_id INT NOT NULL,\r\n      total_amount DECIMAL(10, 2) NOT NULL,\r\n      status ENUM('pending', 'paid', 'shipped', 'delivered', 'canceled') DEFAULT 'pending',\r\n      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\r\n      updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\r\n      FOREIGN KEY (user_id) REFERENCES users(user_id)\r\n  );<\/code><\/pre>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><strong>\u8ba2\u5355\u9879\u8868<\/strong>\uff08order_items\uff09\uff1a<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>  CREATE TABLE order_items (\r\n      order_item_id INT AUTO_INCREMENT PRIMARY KEY,\r\n      order_id INT NOT NULL,\r\n      product_id INT NOT NULL,\r\n      quantity INT NOT NULL,\r\n      price DECIMAL(10, 2) NOT NULL,\r\n      FOREIGN KEY (order_id) REFERENCES orders(order_id),\r\n      FOREIGN KEY (product_id) REFERENCES products(product_id)\r\n  );<\/code><\/pre>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><strong>\u652f\u4ed8\u8bb0\u5f55\u8868<\/strong>\uff08payments\uff09\uff1a<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>  CREATE TABLE payments (\r\n      payment_id INT AUTO_INCREMENT PRIMARY KEY,\r\n      order_id INT NOT NULL,\r\n      payment_amount DECIMAL(10, 2) NOT NULL,\r\n      payment_method ENUM('credit_card', 'paypal', 'bank_transfer') NOT NULL,\r\n      payment_status ENUM('pending', 'completed', 'failed') DEFAULT 'pending',\r\n      paid_at TIMESTAMP,\r\n      FOREIGN KEY (order_id) REFERENCES orders(order_id)\r\n  );<\/code><\/pre>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">2.3. <strong>\u4f18\u5316\u8868\u8bbe\u8ba1<\/strong><\/h4>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><strong>\u5206\u533a\u8bbe\u8ba1<\/strong>\uff1a\u5bf9\u4e8e\u8ba2\u5355\u8868\u548c\u652f\u4ed8\u8bb0\u5f55\u8868\uff0c\u8003\u8651\u5230\u6570\u636e\u91cf\u5e9e\u5927\uff0c\u53ef\u4ee5\u4f7f\u7528\u5206\u533a\u6765\u4f18\u5316\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u6309 <code>created_at<\/code> \u65f6\u95f4\u8fdb\u884c\u5206\u533a\uff0c\u786e\u4fdd\u67e5\u8be2\u6027\u80fd\u3002<\/li>\r\n\r\n\r\n\r\n<li><strong>\u7d22\u5f15\u8bbe\u8ba1<\/strong>\uff1a\u4e3a\u7ecf\u5e38\u67e5\u8be2\u7684\u5b57\u6bb5\u6dfb\u52a0\u7d22\u5f15\uff0c\u5982 <code>user_id<\/code>\u3001<code>order_id<\/code>\u3001<code>product_id<\/code>\u3002<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>  CREATE INDEX idx_user_id ON orders(user_id);\r\n  CREATE INDEX idx_order_id ON order_items(order_id);\r\n  CREATE INDEX idx_product_id ON order_items(product_id);<\/code><\/pre>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">3. <strong>\u4e8b\u52a1\u5904\u7406\u4e0e\u6570\u636e\u4e00\u81f4\u6027<\/strong><\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u5728\u7535\u5b50\u5546\u52a1\u7cfb\u7edf\u4e2d\uff0c\u8ba2\u5355\u7684\u521b\u5efa\u3001\u652f\u4ed8\u7b49\u64cd\u4f5c\u9700\u8981\u786e\u4fdd\u539f\u5b50\u6027\uff0c\u4e00\u81f4\u6027\u3001\u9694\u79bb\u6027\u548c\u6301\u4e45\u6027\uff08ACID\uff09\u3002\u4e3a\u4e86\u786e\u4fdd\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <strong>\u4e8b\u52a1\uff08Transactions\uff09<\/strong> \u6765\u7ba1\u7406\u64cd\u4f5c\u3002<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">3.1. <strong>\u8ba2\u5355\u521b\u5efa\u4e0e\u652f\u4ed8\u6d41\u7a0b\u4e2d\u7684\u4e8b\u52a1<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u5728\u7528\u6237\u521b\u5efa\u8ba2\u5355\u548c\u652f\u4ed8\u65f6\uff0c\u591a\u4e2a\u64cd\u4f5c\u9700\u8981\u4f5c\u4e3a\u4e00\u4e2a\u4e8b\u52a1\u6765\u6267\u884c\u3002\u5982\u679c\u5176\u4e2d\u4efb\u4f55\u4e00\u6b65\u5931\u8d25\uff0c\u6574\u4e2a\u4e8b\u52a1\u4f1a\u56de\u6eda\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>START TRANSACTION;\r\n\r\n-- \u521b\u5efa\u8ba2\u5355\r\nINSERT INTO orders (user_id, total_amount, status) VALUES (1, 100.00, 'pending');\r\nSET @order_id = LAST_INSERT_ID();\r\n\r\n-- \u63d2\u5165\u8ba2\u5355\u9879\r\nINSERT INTO order_items (order_id, product_id, quantity, price) VALUES (@order_id, 1, 2, 50.00);\r\n\r\n-- \u66f4\u65b0\u5546\u54c1\u5e93\u5b58\r\nUPDATE products SET stock = stock - 2 WHERE product_id = 1;\r\n\r\n-- \u5904\u7406\u652f\u4ed8\r\nINSERT INTO payments (order_id, payment_amount, payment_method, payment_status) VALUES (@order_id, 100.00, 'credit_card', 'completed');\r\n\r\nCOMMIT;<\/code><\/pre>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">3.2. <strong>\u5e76\u53d1\u63a7\u5236<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u5f53\u591a\u4e2a\u7528\u6237\u540c\u65f6\u64cd\u4f5c\u540c\u4e00\u5546\u54c1\u6216\u8ba2\u5355\u65f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u7ade\u4e89\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u591a\u4e2a\u7528\u6237\u540c\u65f6\u8d2d\u4e70\u540c\u4e00\u5546\u54c1\uff0c\u53ef\u80fd\u5bfc\u81f4\u8d85\u5356\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5e94\u8be5\u4f7f\u7528 <strong>\u9501<\/strong> \u6765\u63a7\u5236\u5e76\u53d1\uff0c\u907f\u514d\u51fa\u73b0\u4e0d\u4e00\u81f4\u7684\u6570\u636e\u3002<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>-- \u4f7f\u7528\u60b2\u89c2\u9501\u9632\u6b62\u8d85\u5356\r\nSELECT stock FROM products WHERE product_id = 1 FOR UPDATE;\r\n\r\n-- \u5728\u4e8b\u52a1\u7ed3\u675f\u540e\u91ca\u653e\u9501\r\nCOMMIT;<\/code><\/pre>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">4. <strong>\u6027\u80fd\u4f18\u5316<\/strong><\/h3>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">4.1. <strong>\u67e5\u8be2\u4f18\u5316<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u6839\u636e\u5b9e\u9645\u7684\u67e5\u8be2\u9700\u6c42\uff0c\u4f18\u5316 SQL \u67e5\u8be2\u548c\u8868\u8bbe\u8ba1\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u8ba2\u5355\u67e5\u8be2\u65f6\uff0c\u53ef\u4ee5\u6839\u636e <code>status<\/code> \u548c <code>user_id<\/code> \u52a0\u4e0a\u590d\u5408\u7d22\u5f15\u3002<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>CREATE INDEX idx_status_user_id ON orders(status, user_id);<\/code><\/pre>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">4.2. <strong>\u7f13\u5b58\u673a\u5236<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u51cf\u5c11\u6570\u636e\u5e93\u7684\u67e5\u8be2\u8d1f\u8f7d\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 <strong>\u7f13\u5b58<\/strong> \u6280\u672f\uff08\u5982 Redis\u3001Memcached\uff09\u7f13\u5b58\u4e00\u4e9b\u9891\u7e41\u67e5\u8be2\u7684\u6570\u636e\uff0c\u4f8b\u5982\u5546\u54c1\u4fe1\u606f\u3001\u7528\u6237\u8d44\u6599\u7b49\uff0c\u907f\u514d\u6bcf\u6b21\u90fd\u67e5\u8be2\u6570\u636e\u5e93\u3002<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">4.3. <strong>\u6570\u636e\u5e93\u5206\u7247<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u5f53\u6570\u636e\u91cf\u8fbe\u5230\u4e00\u5b9a\u89c4\u6a21\u65f6\uff0c\u8003\u8651\u4f7f\u7528 <strong>\u5206\u7247<\/strong> \u6280\u672f\u5c06\u6570\u636e\u5206\u5e03\u5230\u4e0d\u540c\u7684\u670d\u52a1\u5668\u4e0a\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6309 <code>user_id<\/code> \u6216 <code>order_id<\/code> \u8fdb\u884c\u6c34\u5e73\u5206\u7247\uff0c\u5c06\u6570\u636e\u5206\u6563\u5b58\u50a8\u5230\u4e0d\u540c\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\u4e2d\uff0c\u4ee5\u51cf\u5c11\u5355\u4e00\u6570\u636e\u5e93\u7684\u8d1f\u8f7d\u3002<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">4.4. <strong>\u6570\u636e\u5e93\u8fde\u63a5\u6c60<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u5728\u9ad8\u5e76\u53d1\u7684\u73af\u5883\u4e0b\uff0c\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u6c60\uff08\u5982 <strong>HikariCP<\/strong>\uff09\u6765\u590d\u7528\u8fde\u63a5\uff0c\u51cf\u5c11\u9891\u7e41\u5efa\u7acb\u548c\u65ad\u5f00\u6570\u636e\u5e93\u8fde\u63a5\u5e26\u6765\u7684\u6027\u80fd\u5f00\u9500\u3002<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">5. <strong>\u9ad8\u53ef\u7528\u6027\u4e0e\u5bb9\u9519<\/strong><\/h3>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">5.1. <strong>\u4e3b\u4ece\u590d\u5236<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <strong>MySQL \u4e3b\u4ece\u590d\u5236<\/strong> \u6765\u5b9e\u73b0\u6570\u636e\u7684\u9ad8\u53ef\u7528\u6027\u3002\u4e3b\u6570\u636e\u5e93\u8d1f\u8d23\u5199\u64cd\u4f5c\uff0c\u4ece\u6570\u636e\u5e93\u8d1f\u8d23\u8bfb\u64cd\u4f5c\uff0c\u8fd9\u6837\u53ef\u4ee5\u5206\u62c5\u6570\u636e\u5e93\u7684\u8d1f\u8f7d\u3002<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>-- \u5728\u4e3b\u670d\u52a1\u5668\u4e0a\u542f\u7528\u4e8c\u8fdb\u5236\u65e5\u5fd7\r\nlog-bin = mysql-bin\r\n\r\n-- \u914d\u7f6e\u4ece\u670d\u52a1\u5668\u8fde\u63a5\u5230\u4e3b\u670d\u52a1\u5668\r\nCHANGE MASTER TO MASTER_HOST='master_host', MASTER_USER='replication_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=  106;<\/code><\/pre>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">5.2. <strong>\u8bfb\u5199\u5206\u79bb<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <strong>\u8bfb\u5199\u5206\u79bb<\/strong> \u7b56\u7565\uff0c\u901a\u8fc7\u8d1f\u8f7d\u5747\u8861\u5c06\u8bfb\u8bf7\u6c42\u5206\u53d1\u5230\u4ece\u670d\u52a1\u5668\uff0c\u5c06\u5199\u8bf7\u6c42\u53d1\u9001\u5230\u4e3b\u670d\u52a1\u5668\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u6269\u5c55\u6027\u548c\u5904\u7406\u80fd\u529b\u3002<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">5.3. <strong>\u81ea\u52a8\u6545\u969c\u8f6c\u79fb<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u5f53\u4e3b\u6570\u636e\u5e93\u51fa\u73b0\u6545\u969c\u65f6\uff0c\u80fd\u591f\u81ea\u52a8\u5c06\u4e00\u4e2a\u4ece\u6570\u636e\u5e93\u63d0\u5347\u4e3a\u65b0\u7684\u4e3b\u6570\u636e\u5e93\uff0c\u786e\u4fdd\u7cfb\u7edf\u7684\u9ad8\u53ef\u7528\u6027\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7 <strong>MySQL Router<\/strong> \u6216\u7b2c\u4e09\u65b9\u5de5\u5177\uff08\u5982 <strong>MHA<\/strong> \u6216 <strong>Orchestrator<\/strong>\uff09\u6765\u5b9e\u73b0\u3002<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">6. <strong>\u65e5\u5fd7\u4e0e\u76d1\u63a7<\/strong><\/h3>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">6.1. <strong>\u6162\u67e5\u8be2\u65e5\u5fd7<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u542f\u7528\u6162\u67e5\u8be2\u65e5\u5fd7\uff0c\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u8bc6\u522b\u548c\u4f18\u5316\u6027\u80fd\u74f6\u9888\u3002<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>slow_query_log = 1\r\nslow_query_log_file = \/var\/log\/mysql\/mysql-slow.log\r\nlong_query_time = 2<\/code><\/pre>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">6.2. <strong>\u6027\u80fd\u6a21\u5f0f<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 MySQL \u7684 <strong>Performance Schema<\/strong> \u8fdb\u884c\u6027\u80fd\u76d1\u63a7\uff0c\u67e5\u770b\u6570\u636e\u5e93\u7684\u8fd0\u884c\u60c5\u51b5\uff0c\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u6027\u80fd\u95ee\u9898\u3002<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>SELECT * FROM performance_schema.events_statements_summary_by_digest;<\/code><\/pre>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">6.3. <strong>\u5916\u90e8\u76d1\u63a7\u5de5\u5177<\/strong><\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u50cf <strong>Prometheus<\/strong> \u548c <strong>Grafana<\/strong> \u8fd9\u6837\u7684\u76d1\u63a7\u5de5\u5177\u5b9e\u65f6\u76d1\u63a7 MySQL \u7684\u6027\u80fd\uff0c\u5e76\u8bbe\u5b9a\u544a\u8b66\u89c4\u5219\uff0c\u786e\u4fdd\u6570\u636e\u5e93\u7684\u5065\u5eb7\u72b6\u6001\u3002<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u672c\u90e8\u5206\u7684\u5b66\u4e60\uff0c\u4f60\u5c06\u80fd\u591f\u7406\u89e3\u5982\u4f55\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u8bbe\u8ba1 MySQL \u6570\u636e\u5e93\uff0c\u5982\u4f55\u4f18\u5316\u67e5\u8be2\u548c\u5b58\u50a8\u8bbe\u8ba1\uff0c\u5982\u4f55\u5904\u7406\u4e8b\u52a1\u5e76\u4fdd\u8bc1\u6570\u636e\u4e00\u81f4\u6027\uff0c\u4ee5\u53ca\u5982\u4f55\u63d0\u9ad8 MySQL \u7684\u6027\u80fd\u548c\u9ad8\u53ef\u7528\u6027\u3002\u6700\u7ec8\uff0c\u901a\u8fc7\u672c\u5b9e\u6218\u9879\u76ee\uff0c\u4f60\u5c06\u80fd\u591f\u719f\u7ec3\u638c\u63e1 MySQL \u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u5e94\u7528\u6280\u5de7\uff0c\u89e3\u51b3\u9879\u76ee\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\u3002<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>\u7ee7\u7eed\u6df1\u5165\u5b66\u4e60 MySQL \u5b9e\u6218\u9879\u76ee\uff0c\u8fd9\u4e00\u90e8\u5206\u5c06\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u9879\u76ee\u6848\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u9ad8\u6548\u5730\u4f7f\u7528 MySQ [&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-1643","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1643","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=1643"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1643\/revisions"}],"predecessor-version":[{"id":1645,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1643\/revisions\/1645"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}