{"id":235,"date":"2020-02-29T16:06:43","date_gmt":"2020-02-29T08:06:43","guid":{"rendered":"http:\/\/118.25.47.203\/?page_id=235"},"modified":"2026-09-04T10:02:12","modified_gmt":"2026-09-04T02:02:12","slug":"four","status":"publish","type":"page","link":"https:\/\/blog.ywdevops.cn\/index.php\/four\/","title":{"rendered":"mysql\u5e38\u7528\u547d\u4ee4"},"content":{"rendered":"\n<p class=\"has-text-align-center wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">select\u547d\u4ee4<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>help select;                                        \u67e5\u770bselect\u7528\u6cd5\nselect distinct \u5217\u540d from tablename;                \u68c0\u7d22\u5217\u4e2d\u4e0d\u540c\u7684\u503c\uff0c\u53ef\u4ee5\u8e22\u51fa\u91cd\u590d\u503c\nselect * from table_name where name like \"hello%\";  \u67e5\u8be2name\u5b57\u6bb5\u4ee5hello\u5f00\u5934\u7684\u6570\u636e\uff0chello*\u4e5f\u884c\nselect * from table_name where name like \"%hello\";  \u67e5\u8be2name\u5b57\u6bb5\u4ee5hello\u7ed3\u5c3e\u7684\u6570\u636e\nselect * from table_name where name like \"%hello%\"; \u67e5\u8be2name\u5b57\u6bb5\u4ee5\u5305\u542bhello\u7684\u6570\u636e\nselect prod_id from products limit 3;               \u67e5\u8be2\u524d3\u884c\u6570\u636e\nselect prod_id from products limit 3,3;             \u4ece\u7b2c3\u884c\u5f00\u59cb\u83b7\u53d63\u6761\u6570\u636e(\u4e0d\u5305\u62ec\u7b2c3\u884c)\nselect prod_id from products order by prod_id;      \u5bf9\u5217\u540d\u8fdb\u884c\u6392\u5e8f\uff0c\u6392\u5e8f\u591a\u4e2a\u5c31\u7528\u9017\u53f7\u5206\u9694\nselect prod_id from products order by prod_id desc; \u964d\u5e8f\uff0c\u5728\u5347\u5e8f\u8bed\u53e5\u540e\u52a0desc\u5173\u952e\u5b57\u5373\u53ef\nselect id,age from abc orderby id desc,age;         \u5bf9id\u8fdb\u884c\u964d\u5e8f\uff0c\u7136\u540e\u5bf9id\u5bf9\u5e94\u7684age\u8fdb\u884c\u5347\u5e8f\nselect price from abc order by price desc limit 1;  \u83b7\u53d6\u6700\u5927\u503c(\u5148\u964d\u5e8f\uff0c\u7136\u540e\u83b7\u53d6\u7b2c\u4e00\u6761\u6570\u636e)\nselect name from abc where id = 10;                 \u83b7\u53d6id=10\u5bf9\u5e94\u7684name\u7684\u503c\nselect id,name from abc where id &gt; 10 order by id;  \u83b7\u53d6id\u5927\u4e8e10\u7684name\u503c\u5e76\u5bf9id\u8fdb\u884c\u6392\u5e8f\uff0corder by \u8981\u653e\u5728where \u540e\u9762\nselect id,name from abc where id &lt;&gt; 100;            \u83b7\u53d6id\u4e0d\u7b49\u4e8e100\u7684\u503c\uff0c&lt;&gt;\u4e0e!=\u7b49\u4ef7\nselect id,name from abc where id between 10 and 20; \u83b7\u53d6id\u8303\u56f4\u572810-20\u4e4b\u95f4\u7684\u503c\nselect id,name from abc where name is null;         \u7a7a\u503c\u68c0\u67e5\nselect id,name from abc where id &gt; 10 and id &lt;100;  \u68c0\u7d22\u540c\u65f6\u6ee1\u8db3\u4e24\u4e2a\u6761\u4ef6\u7684\u6570\u636e\uff0c\u4f18\u5148\u7ea7\u9ad8\u4e8eor\nselect id,name from abc where id &gt; 10 or id &lt; 5 ;   \u68c0\u7d22\u6ee1\u8db3\u5176\u4e2d\u4e00\u4e2a\u6761\u4ef6\u7684\u6570\u636e\uff0c\u4f18\u5148\u7ea7\u4f4e\u4e8eand\nselect id,name from abc where id in (10,5);         in\u5173\u952e\u5b57\u4e0e\u4e0a\u9762\u7684or\u7528\u6cd5\u76f8\u540c\uff0c\u90fd\u8868\u793a\u6216\nselect id,name from abc where id not in (10,5);     \u83b7\u53d6id\u4e0d\u5339\u914d10\u548c5\u7684\u6570\u636e\nselect id,name from abc where name like \"gong%\";    \u83b7\u53d6\u6240\u6709\u4ee5gong\u5f00\u5934\u7684name\u7684\u503c,\u53ef\u5339\u914d\u591a\u4e2a\nselect id,name from abc where name like \"%gong%\";   \u83b7\u53d6\u5305\u542bgong\u7684name\u7684\u503c,\u5339\u914d\u591a\u4e2a\nselect id,name from abc where name like \"g%n\";      \u83b7\u53d6\u4ee5g\u5f00\u5934\u4ee5n\u7ed3\u5c3e\u7684name\u7684\u503c\uff0c\u5339\u914d\u591a\u4e2a\nselect id,name from abc where name like \"_ong\";     \u5339\u914dong\u524d\u9762\u7684\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u7684name\u503c\nselect id,name from abc where name regexp \"gong\";   \u6b63\u5219\u5339\u914d\u5305\u542bgong\u7684name\u7684\u503c\uff0c\u4e0d\u533a\u5206\u5927\u5c0f\u5199\nselect name from abc where name regexp binary \"gong\";\u6b63\u5219\u5339\u914d\uff0c\u5e76\u533a\u5206\u5927\u5c0f\u5199(\u52a0\u4e86binary\u5173\u952e\u5b57) \nselect name from abc where name regexp \"gong | guan\";\u5339\u914dgong\u6216\u8005guan\u7684\u884c\nselect name from abc where name regexp \"&#91;123]gong\"; \u5339\u914d\u5355\u4e00\u5b57\u7b261\u30012\u30013\u5f00\u5934\u540e\u9762\u4e3agong\u7684\u5185\u5bb9\nselect name from abc where name regexp \"&#91;1-3]gong\"; \u4e0e\u4e0a\u4e00\u6761\u547d\u4ee4\u76f8\u540c\uff0c\u7b80\u5316\u5199\u6cd5&#91;1-3]\nselect name from abc where name regexp \"\\\\.\";       \u5339\u914d\u5305\u542b.\u7684\u5185\u5bb9\uff0c\u901a\u8fc7\\\\\u8f6c\u4e49\u624d\u80fd\u83b7\u53d6\u5230  \nselect name from abc where name regexp \"\\\\(&#91;0-9] sticks?\\\\)\"; \u5339\u914d(n stick)\u548c (n sticks),n\u4e3a0-9\nselect id from abc where id regexp \"&#91;&#91;:digit:]]{3}\"; \u5339\u914d\u4efb\u610f3\u4e2a\u6570\u5b57\nselect id from abc where id regexp \"&#91;0-9]&#91;0-9]&#91;0-9]\";\u4e0e\u4e0a\u6761\u547d\u4ee4\u6548\u679c\u76f8\u540c\nselect id from abc where id regexp \"^&#91;0-9]\\\\.\";      \u5339\u914d\u4ee5\u6570\u5b57\u6216\u8005.\u5f00\u5934\u7684\u884c\nselect name from abc where name regexp \"gong$\";      \u5339\u914d\u4ee5gong\u7ed3\u5c3e\u7684name\u7684\u503c\nselect Concat(name,'(',address,')') from abc;        \u901a\u8fc7Concat()\u62fc\u63a5\uff0c\u8fd4\u56dename(address)\u503c\nselect Concat(name,'(',address,')') as new_namefrom abc; \u62fc\u63a5\u540e\u7684\u65b0\u5217\u540d\u4e3anew_name,as\u6307\u5b9a\u522b\u540d\nselect RTrim(name) from abc;                         \u53bb\u6389\u5b57\u6bb5\u53f3\u8fb9\u7684\u7a7a\u683c\nselect LTrim(name) from abc;                         \u53bb\u6389\u5b57\u6bb5\u5de6\u8fb9\u7684\u7a7a\u683c\nselect Trim(name)  from abc;                         \u53bb\u6389\u5b57\u6bb5\u4e24\u8fb9\u7684\u7a7a\u683c\nselect Upper(name) AS name_upper from abc;           \u5c06\u83b7\u53d6\u7684\u7ed3\u679c\u5168\u90e8\u8f6c\u6362\u4e3a\u5927\u5199\nselect Lower(name) AS name_lower from abc;           \u5c06\u83b7\u53d6\u7684\u7ed3\u679c\u5168\u90e8\u8f6c\u6362\u4e3a\u5c0f\u5199\nselect Length(name)AS length_name from abc;          \u83b7\u53d6\u5b57\u6bb5\u7684\u957f\u5ea6\nselect CurTime();                                    \u8fd4\u56de\u5f53\u524d\u65f6\u95f4\nselect CurDate();                                    \u8fd4\u56de\u5f53\u524d\u65e5\u671f\nselect Data(order_date) from orders;                 \u8fd4\u56de\u65e5\u671f\u65f6\u95f4\u5b57\u6bb5\u4e2d\u7684\u65e5\u671f\u90e8\u5206\nselect Day(order_date)  from orders;                 \u8fd4\u56de\u65e5\u671f\u65f6\u95f4\u5b57\u6bb5\u4e2d\u7684\u5929\u6570\u90e8\u5206\nselect Hour(order_date) from orders;                 \u8fd4\u56de\u65e5\u671f\u65f6\u95f4\u5b57\u6bb5\u4e2d\u7684\u5c0f\u65f6\u90e8\u5206\nselect Minute(order_date) from orders;               \u8fd4\u56de\u65e5\u671f\u65f6\u95f4\u5b57\u6bb5\u4e2d\u7684\u5206\u949f\u90e8\u5206\nselect Month(order_date) from orders;                \u8fd4\u56de\u65e5\u671f\u65f6\u95f4\u5b57\u6bb5\u4e2d\u7684\u6708\u4efd\u90e8\u5206\nselect Second(order_date) from orders;               \u8fd4\u56de\u65e5\u671f\u65f6\u95f4\u5b57\u6bb5\u4e2d\u7684\u79d2\u90e8\u5206\nselect Time(order_date) from orders;                 \u8fd4\u56de\u65e5\u671f\u65f6\u95f4\u5b57\u6bb5\u4e2d\u7684\u65f6\u95f4\u90e8\u5206\nselect Year(order_date) from orders;                 \u8fd4\u56de\u65e5\u671f\u65f6\u95f4\u5b57\u6bb5\u4e2d\u7684\u5e74\u4efd\u90e8\u5206\nselect Now();                                        \u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\nselect avg(age) as age_avg from abc;                 \u83b7\u53d6\u5e74\u9f84\u7684\u5e73\u5747\u503c\nselect count(age)as age_count from abc;              \u83b7\u53d6\u5e74\u9f84\u5217\u7684\u884c\u6570\nselect min(age)  as age_min   from abc;              \u83b7\u53d6\u5e74\u9f84\u7684\u6700\u5c0f\u503c\nselect max(age)  as age_max   from abc;              \u83b7\u53d6\u5e74\u9f84\u7684\u6700\u5927\u503c\nselect sum(age)  as age_sum   from abc;              \u83b7\u53d6\u5e74\u9f84\u7684\u548c\nselect database();                                   \u67e5\u770b\u5f53\u524d\u6240\u5728\u6570\u636e\u5e93\nselect id,count(*)as id_count from abc group by id;  \u5bf9id\u8fdb\u884c\u5206\u7ec4\u540e\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u7684\u603b\u6570\nselect id,count(*)as id_count from abc group by id having count(*) &gt; 10; \u8fc7\u6ee4\u5206\u7ec4\n#group by \u8981\u653e\u5728where\u4e4b\u540e\uff0corder by \u4e4b\u524d\uff0c\u5426\u5219\u62a5\u9519\n#having\u4e0ewhere\u533a\u522b\u662f\uff1awhere\u5728\u5206\u7ec4\u524d\u8fc7\u6ee4\u884c\uff0chaving\u5728\u5206\u7ec4\u540e\u8fdb\u884c\u8fc7\u6ee4\nselect cust_name from customers where cust_id in (select cust_id from orders where order_num in (select order_num from orderitems where prod_id = \"TNT2\"));\u5b50\u67e5\u8be2\uff0c\u9996\u5148\u6839\u636e\u4ea7\u54c1prod_id\u6765\u68c0\u7d22\u51fa\u8ba2\u5355\u4fe1\u606forder_num,\u7136\u540e\u6839\u636e\u8ba2\u5355\u4fe1\u606forder_num\u6765\u68c0\u7d22\u51fa\u5ba2\u6237id\u4e3acust_id,\u6700\u540e\u6839\u636ecust_id\u6765\u83b7\u53d6\u5ba2\u6237\u4fe1\u606fcust_name\nselect vend_name,prod_name from products,vendors where vendors.vend_id = products.vend_id; \u7b49\u503c\u8054\u7ed3\u67e5\u8be2\uff0cvend_name\u548cprod_name\u5206\u522b\u4f4d\u4e8eproducts\u548cvendors\u8868\u4e2d\uff0cvend_id\u662fvendors\u8868\u7684\u4e3b\u952e\u5e76\u4e14\u662fproducts\u8868\u7684\u5916\u952e\uff0c\u56e0\u6b64\u53ef\u4f7f\u7528\u8054\u7ed3\u67e5\u8be2\u540c\u65f6\u83b7\u53d6\u4e24\u4e2a\u8868\u7684\u5b57\u6bb5\nselect vend_name,vend_address,prod_name from products inner join vendors on vendors.vend_id = products.vend_id; \u4e0e\u4e0a\u6761\u8bed\u53e5\u4f5c\u7528\u76f8\u540c\uff0cwhere\u66ff\u6362\u4e3aon\uff0c\u4f7f\u7528inner join\uff0c\u8fd9\u662f\u63a8\u8350\u7684\u7528\u6cd5\n#\u6ce8\uff1a\u8054\u7ed3\u591a\u4e2a\u8868\u53ef\u4ee5\u4f7f\u7528AND(\u5728where\u6216\u8005on\u540e\u9762\u7684\u591a\u4e2a\u6761\u4ef6\u4e4b\u95f4)\uff0c\u8054\u7ed3\u4f1a\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\uff0c\u6ce8\u610f\u4f7f\u7528\nselect tb1.column tc1,table2.column tc2 from table1 tb1 inner join table2 tb2 on tb1.column = tb2.column; \u901a\u8fc7inner join\u5b9e\u73b0\u591a\u8868\u67e5\u8be2,tb1\u548ctb2\u662f\u8868table1\u548ctable2\u7684\u522b\u540d,tc1\u548ctc2\u662f\u67e5\u8be2\u540e\u6307\u5b9a\u7684\u5217\u522b\u540d\nselect c.* from customers AS c;                    \u83b7\u53d6\u8868customers\u7684\u6240\u6709\u5217,c\u662f\u8868\u522b\u540d\nselect id,price from where id &gt; 5 union select id,price from where price &gt;10; \u7ec4\u5408\u67e5\u8be2\nselect current_user();                             \u67e5\u8be2\u5f53\u524d\u8fde\u63a5MySQL\u7684\u7528\u6237\nselect decode((select password from test WHERE id = 1),\u2019123456789\u2032); \u89e3\u5bc6\uff0c\u901a\u8fc7\u52a0\u5bc6\u5b57\u7b26\u4e32\uff0c\u67e5\u8be2\u540e\u5c31\u662f\u52a0\u5bc6\u4e4b\u524d\u7684\u5bc6\u7801\nSHOW VARIABLES LIKE 'server_uuid'; \u67e5\u770bserver_uuid\nshow slave hosts;                  \u67e5\u770bmysql\u4ece\u5e93\u4fe1\u606f\nshow slave status;                 \u67e5\u770b\u4ece\u5e93\u72b6\u6001\nSHOW TABLE STATUS LIKE 'table_name'; \u83b7\u53d6\u8868\u4fe1\u606f\uff0c\u7528Data_length\/1024\/1024\u5373\u53ef\u5f97\u5230\u8868\u5927\u5c0f\nSHOW INDEX FROM your_table_name\uff1b \u67e5\u8be2\u8868\u4e2d\u7d22\u5f15<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">intert into \u547d\u4ee4<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>insert into user values (null,'gong','\u5e7f\u4e1c',null); \u63d2\u5165\u6570\u636e\uff0c\u6ca1\u5185\u5bb9\u4f7f\u7528Null\u5145\u5f53(\u4e0d\u5b89\u5168\u63d2\u5165) \ninsert into user (username,address,email)values('gong','\u5e7f\u4e1c'\uff0c'abc@qq.com') \u63d2\u5165\u6570\u636e(\u5b89\u5168\u63d2\u5165)\ninsert into user (username) value('gong'),('guan');   \u63d2\u5165\u591a\u884c\u6570\u636e\ninsert into user (username) select username from user where id = 10;\u901a\u8fc7insert\u548cselect\u914d\u5408\u63d2\u5165\u6570\u636e\ninsert into test (username,password)values(\"admin\",select encode('test123456','123456789')); \u5411\u8868test\u4e2d\u6dfb\u52a0\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5bc6\u7801\u901a\u8fc7encode\u7684\u5f62\u5f0f\u52a0\u5bc6\uff0ctest123456\u4e3a\u8bbe\u7f6e\u7684\u5bc6\u7801\uff0c123456789\u4e3a\u52a0\u5bc6\u89e3\u5bc6\u5b57\u7b26\u4e32(\u6ce8\u610f\uff1apassword\u7684\u5b57\u6bb5\u7c7b\u578b\u9700\u8981\u4e3ablob,\u5b58\u50a8\u4e8c\u8fdb\u5236\u7c7b\u578b)\uff0c\u901a\u8fc7update\u66f4\u65b0\u4e5f\u53ef\u4ee5\n<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">update\u4e0edelete\u547d\u4ee4<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>update user set username=\"guan\" where id = 100;                 \u66f4\u65b0\u5355\u5217\u7684\u503c\nupdate user set username=\"guan\",address=\"\u6df1\u5733\" where id = 100;  \u66f4\u65b0\u591a\u5217\u7684\u503c\nupdate user set username= null where id = 100;                  \u5220\u9664\u67d0\u4e2a\u5217\uff0c\u8bbe\u4e3anull\nupdate user set password = password(\"passwd\") where user = \"root\"; \u4fee\u6539\u7528\u6237\u5bc6\u7801\ndelete from user where id = 100;                               \u5220\u9664id\u4e3a100\u7684\u6574\u884c\u7528\u6237\ndelete from user;                                              \u5220\u9664\u6574\u4e2a\u8868\u7684\u6570\u636e\uff0c\u7ed3\u6784\u4e0d\u53d8\ndrop  table user;                                              \u5220\u9664\u6574\u4e2a\u8868<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">create\u547d\u4ee4<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u521b\u5efa\u8868\uff0c\u4e3b\u952e\u5fc5\u987bnot null\uff0cid\u5217\u8bbe\u7f6e\u4e3a\u81ea\u589e,age\u9ed8\u8ba4\u503c\u4e3a10,\u6ca1\u6709\u9ed8\u8ba4\u53ef\u4e0d\u7528\u5199\ncreate table if not exists user (\n    id int not null auto_increment,\n    username varchar(10) not null,\n    age int              not null default 10,\n    address  varchar(100)    null,\n    email    varchar(100)    null,\n    primary key(id)\n)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n#\u6ce8\uff1a\u4e3b\u952e\u53ef\u4ee5\u6709\u591a\u4e2a\u5217\u7ec4\u6210\uff0c\u5982\u679c\u662f\u5355\u4e2a\u5217\uff0c\u90a3\u4e48\u6b64\u5217\u552f\u4e00\uff0c\u5982\u679c\u591a\u4e2a\u5217\uff0c\u7ec4\u5408\u503c\u5fc5\u987b\u552f\u4e00\n#\u672c\u4f8b\u5b50\u4e2d\u5f15\u64ce\u4e3ainnodb\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4f59\u5f15\u64ce\uff0c\u4f46\u8981\u6ce8\u610f\u4e00\u4e2a\u5f15\u64ce\u7684\u8868\u4e0d\u80fd\u5f15\u7528\u5177\u6709\u4f7f\u7528\u4e0d\u540c\u5f15\u64ce\u7684\u8868\u7684\u5916\u952e\ncreate user username identified by \"passwd\";     \u521b\u5efa\u7528\u6237username\uff0c\u8bbe\u7f6e\u5bc6\u7801\u4e3apasswd\ncreate table \u8868a like \u8868b\uff1b                      \u6839\u636e\u8868b\u7684\u7ed3\u6784\u521b\u5efa\u8868a\uff0c\u4e0d\u5305\u542b\u6570\u636e\ncreate table newTable as select * from oldTable; \u6839\u636e\u65e7\u8868\u521b\u5efa\u65b0\u8868\uff0c\u5305\u62ec\u6570\u636e<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">alter\u547d\u4ee4<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alter table user add age int;                            \u7ed9\u8868user\u6dfb\u52a0\u5217age\uff0c\u6307\u5b9a\u6570\u636e\u7c7b\u578b\nalter table user add age int default 10;                 \u7ed9\u8868user\u6dfb\u52a0\u5217age\uff0c\u9ed8\u8ba4\u503c\u4e3a10\nalter table user drop column age;                        \u5220\u9664\u8868user\u4e2d\u7684\u5217age\nalter user username identified by \"passwd\";              \u4fee\u6539\u7528\u6237\u5bc6\u7801\nalter table oldtable rename to newtable;                 \u4fee\u6539\u8868\u540d\nalter table user modify column age varchar(10) default null;\u4fee\u6539age\u7c7b\u578b\u4e3avarchar\uff0c\u9ed8\u8ba4\u7a7a\nalter table tablename change column age agef varchar(50);\u4fee\u6539\u5217\u540d\u4eceage\u5230agef\nalter table tablename auto_increment=4;                  \u91cd\u7f6e\u81ea\u589e\u952e\u4ece4\u5f00\u59cb\nalter table tablename add id int primary key not null auto_increment first;\u6dfb\u52a0id\u653e\u5728\u5f00\u59cb\u4f4d\u7f6e\nalter table tablename drop column id;                    \u5220\u9664id\u5b57\u6bb5\nalter table tablename modify id int auto_increment primary key;\u4fee\u6539id\u4e3a\u81ea\u589e\u5e76\u8bbe\u4e3a\u4e3b\u952e\nalter table tablename add unique(`id`);                  \u6dfb\u52a0\u552f\u4e00\u7ea6\u675f\uff0c\u62ec\u53f7\u5185\u4e3a\u53cd\u5f15\u53f7\nalter table user modify column created_on datetime default CURRENT_TIMESTAMP;\u4fee\u6539\u8868\u540duser\u4e2d\u7684\u5217created_on\u7684\u7c7b\u578b\u4e3adatetime\uff0c\u9ed8\u8ba4\u503c\u4e3aCURRENT_TIMESTAMP\nalter table tablename modify \u5b57\u6bb51 \u5b57\u6bb5\u7c7b\u578b after \u5b57\u6bb52; \u5c06\u5b57\u6bb51\u79fb\u52a8\u5230\u5b57\u6bb52\u540e\u9762\nalter table tablename modify \u5b57\u6bb51 \u5b57\u6bb5\u7c7b\u578b  first;      \u5c06\u5b57\u6bb51\u79fb\u52a8\u5230\u6700\u524d\u9762\n<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u6388\u6743\u547d\u4ee4<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grant select on test.* to 'username'@'ip';       \u7ed9\u7528\u6237username\u67e5\u8be2test\u5e93\u4e0b\u5168\u90e8\u8868\u7684\u6743\u9650\r\ngrant update on test.* to 'username'@'ip';       \u7ed9\u7528\u6237username\u66f4\u65b0test\u5e93\u4e0b\u5168\u90e8\u8868\u7684\u6743\u9650\r\ngrant delete on test.* to 'username'@'ip';       \u7ed9\u7528\u6237username\u5220\u9664test\u5e93\u4e0b\u5168\u90e8\u8868\u7684\u6743\u9650\r\ngrant insert on test.* to 'username'@'ip';       \u7ed9\u7528\u6237username\u5411test\u5e93\u4e0b\u5168\u90e8\u8868\u63d2\u5165\u6570\u636e\u7684\u6743\u9650\r\ngrant all privileges  on test.* to 'username'@'ip'; \u7ed9\u7528\u6237username\u64cd\u4f5ctest\u5e93\u4e0b\u6240\u6709\u8868\u7684\u6743\u9650\r\ngrant all privileges on `abc_%`.* to user@'%' identified by 'passwd'; #\u7ed9\u7528\u4e8e\u8d4b\u4e88abc_\u5f00\u5934\u7684\u6240\u6709\u5e93\u6743\u9650,\u6ce8\u610fabc\u90a3\u91cc\u662f\u53cd\u5f15\u53f7\nflush privileges;                            \u6388\u6743\u540e\u5237\u65b0\u6743\u9650\u751f\u6548\nSHOW GRANTS FOR '\u7528\u6237\u540d'@'\u4e3b\u673a';             \u67e5\u770b\u7528\u6237\u6743\u9650\n\nrevoke select on test.* from 'username'@'ip';       \u56de\u6536\u7528\u6237\u7684\u67e5\u8be2\u6743\u9650\r\nrevoke update,insert on test.* from 'username'@'ip';\u56de\u6536\u7528\u6237\u66f4\u65b0\u63d2\u5165\u6570\u636e\u6743\u9650\nrevoke select on `abc_%`.* from  'user'@'ip';       \u56de\u6536\u7528\u6237abc_\u5f00\u5934\u7684\u6240\u6709\u5e93\u7684select\u6743\u9650\nFLUSH PRIVILEGES;                                   \u64a4\u9500\u540e\u4e5f\u8981\u5237\u65b0\u6743\u9650<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">mysql\u5907\u4efd\u547d\u4ee4<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysqldump -u root -p --single-transaction --databases dbname > dbname.sql #\u5bfc\u51fa\u6570\u636e\u5e93\u548c\u91cc\u9762\u7684\u8868(\u5305\u62ec\u6570\u636e,\u52a0\u4e86--single-transaction\u540e\u4e0d\u4f1a\u9501\u8868\uff0c\u4fdd\u6301\u6570\u636e\u4e00\u81f4\u6027\uff0c\u964d\u4f4e\u6027\u80fd\u5f71\u54cd)\r\nmysqldump -u root -p --single-transaction  dbname tbname > tbname.sql \u5907\u4efd\u6307\u5b9a\u8868\u7684\u7ed3\u6784\u548c\u6570\u636e\r\nmysqldump -u root -p --single-transaction --all-databases > all.sql  \u5bfc\u51fa\u5168\u90e8\u6570\u636e\u5e93\r\nmysqldump -u root -p --single-transaction --databases dbname --tables table1 table2 > two_table.sql \u5bfc\u51fa\u591a\u5f20\u8868(\u5305\u542b\u8868\u7ed3\u6784\u4e0e\u6570\u636e\uff0c\u5982\u679c\u8868\u88ab\u5220\u9664\uff0c\u53ef\u4ee5\u901a\u8fc7\u6b64\u5907\u4efd\u6062\u590d)\r\nmysqldump -u root -p --single-transaction -d dbname tablename > table.sql \u52a0\u4e86-d\u53c2\u6570\uff0c\u53ea\u5bfc\u51fa\u8868\u7ed3\u6784\u4e0d\u5bfc\u8868\u6570\u636e(\u5982\u679c\u8868\u5220\u9664\u4e86\uff0c\u53ef\u901a\u8fc7\u6b64\u5bfc\u51fasql\u91cd\u65b0\u521b\u5efa\u8868\uff0c\u4f46\u662f\u8868\u4e2d\u6ca1\u6709\u6570\u636e)\r\nmysqldump -u root -p --single-transaction -t dbname tablename > table.sql \u52a0\u4e86-t\u53c2\u6570\uff0c\u53ea\u5bfc\u51fa\u8868\u6570\u636e\u4e0d\u5bfc\u8868\u7ed3\u6784(\u5982\u679c\u8868\u5220\u9664\u4e86\u90a3\u4e48\u6ca1\u6709\u7ed3\u6784\u65e0\u6cd5\u6062\u590d)\r\nmysqldump -u root -p --single-transaction --no-data dbname tablename > dbtable.sql \u5bfc\u51fa\u6570\u636e\u5e93\u4ee5\u53ca\u8868\u7ed3\u6784\uff0c\u6ca1\u6709\u6570\u636e(\u5982\u679c\u6570\u636e\u5e93\u88ab\u5220\u9664\uff0c\u901a\u8fc7\u6b64sql\u53ef\u6062\u590d\u6570\u636e\u5e93\u4ee5\u53ca\u91cc\u9762\u7684\u8868\uff0c\u4f46\u662f\u8868\u4e2d\u65e0\u6570\u636e)\nmysqldump -u root -p --single-transaction --no-data dbname > dbtable.sql #\u5bfc\u51fa\u5e93\u4e2d\u6240\u6709\u8868\uff0c\u4e0d\u8981\u6570\u636e<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5176\u4f59\u547d\u4ee4<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>help create table;                           \u67e5\u770b\u521b\u5efa\u8868\u8bed\u6cd5\nrename user oldusername to newusername;      \u91cd\u547d\u540d\u7528\u6237\u540d\ndrop user  username;                         \u5220\u9664\u7528\u6237\nset password for username = password(\"\u5bc6\u7801\");\u8bbe\u7f6e\u7528\u6237\u5bc6\u7801\nanalyze table tablename;                     \u68c0\u67e5\u8868\u952e\u662f\u5426\u6b63\u786e\uff0c\u5982\u6709\u5f02\u5e38\u5728Msg_text\u8f93\u51fa\nshow status;                                 \u67e5\u770bMySQL\u8fd0\u884c\u72b6\u6001\nshow variables;                              \u67e5\u770bMySQL\u670d\u52a1\u5668\u7684\u914d\u7f6e\u53c2\u6570\nshow variables like \"tran%\";                 \u7b5b\u9009\u4ee5tran\u4e3a\u5f00\u5934\u7684\u53c2\u6570\u4fe1\u606f\nshow processlist;                            \u67e5\u770b\u6240\u6709\u6d3b\u52a8\u8fdb\u7a0b\nshow slave status\\G;                         \u67e5\u770b\u4e3b\u4ece\u5e93\u7684\u72b6\u6001\nshow databases;                              \u67e5\u8be2\u6240\u6709\u6570\u636e\u5e93\u4fe1\u606f\nshow table status from dbname where name=\"tablename\";\u67e5\u770b\u67d0\u4e2a\u5e93\u4e0b\u6307\u5b9a\u8868\u7684\u5b58\u50a8\u5f15\u64ce\nshow table status from dbname\uff1b              \u67e5\u770b\u5e93\u4e0b\u6240\u6709\u8868\u7684\u5b58\u50a8\u5f15\u64ce\nshow create database dbname;                 \u67e5\u770b\u521b\u5efa\u5e93\u7684\u8bed\u53e5\nshow create database tablename;              \u67e5\u770b\u521b\u5efa\u8868\u7684\u8bed\u53e5\nkill id;                                     \u6740\u6389\u67d0\u4e2a\u8fdb\u7a0b\noptimize table table_name;                   \u5220\u9664\u8868\u540e\u8fdb\u884c\u788e\u7247\u6e05\u7406(\u4f1a\u9501\u8868\uff0c\u7a7a\u95f2\u65f6\u95f4\u6267\u884c)\nmysql -u root -p &lt; a.sql > b.txt             #\u6267\u884ca.sql\u5e76\u5c06\u7ed3\u679c\u5bfc\u5165\u5230b.txt\u4e2d\nmysqldump -u root -p --single-transaction --no-data dbname > dbtable.sql #\u5bfc\u51fa\u5e93\u4e2d\u6240\u6709\u8868\uff0c\u4e0d\u8981\u6570\u636e\n#\u5bfc\u51fa\u4ee5\u6307\u5b9a\u5b57\u6bcd\u5f00\u5934\u7684\u8868\n(1)\u3001mysql -u\u7528\u6237\u540d -p\u5bc6\u7801 -N -B -e \"SHOW TABLES FROM \u6570\u636e\u5e93\u540d LIKE 'user%'\" > tables.txt\n(2)\u3001mysqldump -u\u7528\u6237\u540d -p\u5bc6\u7801 \u6570\u636e\u5e93\u540d $(cat tables.txt) > backup.sql\n#\u521b\u5efa\u666e\u901a\u7528\u6237\u6765\u6267\u884cmysqldump\u5907\u4efd\u547d\u4ee4\n(1)\u3001CREATE USER 'mysql_backup'@'localhost' IDENTIFIED BY 'passwd';\n(2)\u3001grant select,show view,lock tables on asign_system.* to 'backup'@'192.168.0.30';#\u5e93\u7ea7\u522b\n(3)\u3001grant trigger,reload,process,event on *.* to 'backup'@'192.168.0.30';#\u5168\u5c40\u7ea7\u522b\uff0c\u5fc5\u987b\n(4)\u3001FLUSH PRIVILEGES;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001mysql\u6267\u884c\u89e6\u53d1\u5668\uff0c\u5982\u4f55\u5ffd\u7565\u8bed\u53e5\u4e2d\u95f4\u7684\u5206\u53f7\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728MySQL\u4e2d\uff0c\u89e6\u53d1\u5668\uff08Trigger\uff09\u662f\u7528\u6237\u5b9a\u4e49\u7684SQL\u8bed\u53e5\u7684\u96c6\u5408\uff0c\u5b83\u4eec\u5728\u6307\u5b9a\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u53d1\u751f\u65f6\u81ea\u52a8\u6267\u884c\u3002\u5982\u679c\u4f60\u5728\u6267\u884c\u89e6\u53d1\u5668\u65f6\u9047\u5230\u4e86\u5206\u53f7\uff08<code>;<\/code>\uff09\u7684\u95ee\u9898\uff0c\u901a\u5e38\u662f\u56e0\u4e3a\u89e6\u53d1\u5668\u7684\u5b9a\u4e49\u4e2d\u5305\u542b\u4e86\u591a\u4e2a\u8bed\u53e5\uff0c\u800c\u8fd9\u4e9b\u8bed\u53e5\u9700\u8981\u6b63\u786e\u7684\u5206\u9694\u7b26\uff0c\u5728MySQL\u4e2d\uff0c\u89e6\u53d1\u5668\u9ed8\u8ba4\u4f7f\u7528\u5206\u53f7\u4f5c\u4e3a\u8bed\u53e5\u7684\u7ed3\u675f\u7b26\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u7684\u89e6\u53d1\u5668\u5305\u542b\u591a\u4e2a\u8bed\u53e5\uff0c\u4f60\u9700\u8981\u6539\u53d8\u8bed\u53e5\u7684\u5206\u9694\u7b26\uff0c\u901a\u5e38\u4f7f\u7528<code>$$<\/code>\u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u7684\u5206\u9694\u7b26\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELIMITER $$\n\nCREATE TRIGGER your_trigger_name\nBEFORE INSERT ON your_table_name\nFOR EACH ROW\nBEGIN\n    -- \u4f60\u7684\u89e6\u53d1\u5668\u903b\u8f91\n    INSERT INTO another_table (column1, column2) VALUES (NEW.column1, NEW.column2);\nEND $$\n\nDELIMITER ;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>DELIMITER $$<\/code>\u6539\u53d8\u4e86\u8bed\u53e5\u7684\u5206\u9694\u7b26\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u89e6\u53d1\u5668\u5b9a\u4e49\u4e2d\u4f7f\u7528\u5206\u53f7\u800c\u4e0d\u4f1a\u5bfc\u81f4\u547d\u4ee4\u63d0\u524d\u7ed3\u675f\u3002\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u89e6\u53d1\u5668\uff0c\u5e76\u5728\u7ed3\u675f\u65f6\u4f7f\u7528<code>END $$<\/code>\u6765\u6807\u8bc6\u89e6\u53d1\u5668\u5b9a\u4e49\u7684\u7ed3\u675f\u3002\u6700\u540e\uff0c\u6211\u4eec\u7528<code>DELIMITER ;<\/code>\u5c06\u5206\u9694\u7b26\u6539\u56de\u9ed8\u8ba4\u7684\u5206\u53f7<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001mysql5.7\u5982\u4f55\u7981\u7528ssl\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fee\u6539my.cnf\u914d\u7f6e\u6587\u4ef6\uff0c\u6dfb\u52a0\u53c2\u6570ssl=0\u5373\u53ef<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001mysql\u4ece\u5e93\u8bbe\u7f6e\u53ea\u8bfb\uff1f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>read_only=1     #my.cnf\u4e2d\u914d\u7f6e\uff0c\u8868\u793a\u53ef\u8bfb\u5199\nread_only=0     #\u8868\u793a\u53ea\u8bfb<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8bbe\u7f6eread_only=1\uff0c\u90a3\u4e48\u666e\u901a\u7528\u6237\u53ea\u80fd\u6267\u884cselect\u67e5\u8be2\uff0c\u4e0d\u80fd\u6267\u884cinsert update delete create\u7b49\u64cd\u4f5c\uff0c\u4f46\u662froot\u7528\u6237\u4ecd\u7136\u53ef\u4ee5\u6267\u884c\u5199\u5165\u64cd\u4f5c\uff0c\u5982\u679c\u8981\u9650\u5236\u8d85\u7ea7\u7528\u6237\u7684\u6743\u9650\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>super_read_only=1   #\u8d85\u7ea7\u7528\u6237\u4e5f\u4e0d\u80fd\u5199\u5165<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u914d\u7f6e\u4e86super_read_only=1\u540e\u7684\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u4f1a\u5f71\u54cd\u590d\u5236\u7ebf\u7a0b\uff0c\u4e5f\u5c31\u662f\u4e3b\u4ece\u4e0d\u53d7\u5f71\u54cd<\/li>\n\n\n\n<li>\u4e34\u65f6\u8868\u4e0d\u53d7\u9650\u5236\uff1a\u7528\u6237\u4ecd\u7136\u53ef\u4ee5\u4fee\u6539\u548c\u521b\u5efa\u4e34\u65f6\u8868<\/li>\n\n\n\n<li>\u8bbe\u7f6e\u4e86super_read_only=1\u90a3\u4e48read_only\u81ea\u52a8\u4e3a\uff0c\u4f46\u662f\u8bbe\u7f6eread_only=1\u4e0d\u4f1a\u81ea\u52a8\u8bbe\u7f6esuper_read_only=1<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001\u5bfc\u51fa\u4ee5\u6307\u5b9a\u5b57\u6bcdabc_\u5f00\u5934\u7684\u6570\u636e\u5e93\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\ncd $(dirname $0)\nrm -rf *.sql\nsleep 1\nfor db in $(mysql -h 192.168.5.11 -u root -p'12345678' -e \"show databases like 'abc_'\" | grep -v Database)\ndo\n    mysqldump -h 192.168.5.11 --single-transaction -u root -p'12345678' --databases \"${db}\" &gt; ${db}.sql\ndone\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>select\u547d\u4ee4 intert into \u547d\u4ee4 update\u4e0edelete\u547d\u4ee4 create\u547d\u4ee4 alter\u547d [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-235","page","type-page","status-publish","hentry"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/pages\/235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":191,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/pages\/235\/revisions"}],"predecessor-version":[{"id":20974,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/pages\/235\/revisions\/20974"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}