站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > MySQL 5.1参考手册中文版

Appendix聽A.聽Problems and Common Errors - MySQL 5.1参考手册中文版

闄勫綍A锛氶棶棰樺拰甯歌閿欒

鐩綍

A.1. 濡備綍纭畾瀵艰嚧闂鐨勫師鍥
A.2. 浣跨敤MySQL绋嬪簭鏃剁殑甯歌閿欒
A.2.1. 鎷掔粷璁块棶
A.2.2. 鏃犳硶杩炴帴鍒癧local] MySQL鏈嶅姟鍣
A.2.3. 瀹㈡埛绔笉鏀寔閴村畾鍗忚
A.2.4. 杈撳叆瀵嗙爜鏃跺嚭鐜板瘑鐮侀敊璇
A.2.5. 涓绘満鐨刪ost_name琚睆钄
A.2.6. 杩炴帴鏁拌繃澶
A.2.7. 鍐呭瓨婧㈠嚭
A.2.8. MySQL鏈嶅姟鍣ㄤ笉鍙敤
A.2.9. 淇℃伅鍖呰繃澶
A.2.10. 閫氫俊閿欒鍜屽け鏁堣繛鎺
A.2.11. 琛ㄥ凡婊
A.2.12. 鏃犳硶鍒涘缓鏂囦欢锛忓啓鍏ユ枃浠
A.2.13. 鍛戒护涓嶅悓姝
A.2.14. 蹇界暐鐢ㄦ埛
A.2.15. 琛╰bl_name涓嶅瓨鍦
A.2.16. 鏃犳硶鍒濆鍖栧瓧绗﹂泦
A.2.17. 鏂囦欢鏈壘鍒
A.3. 涓庡畨瑁呮湁鍏崇殑浜嬪疁
A.3.1. 涓嶮ySQL瀹㈡埛绔簱鐨勯摼鎺ラ棶棰
A.3.2. 濡備綍浠ユ櫘閫氱敤鎴疯韩浠借繍琛孧ySQL
A.3.3. 涓庢枃浠惰鍙湁鍏崇殑闂
A.4. 涓庣鐞嗘湁鍏崇殑浜嬪疁
A.4.1. 濡備綍澶嶄綅鏍圭敤鎴峰瘑鐮
A.4.2. 濡傛灉MySQL渚濈劧宕╂簝锛屽簲浣滀簺浠涔
A.4.3. MySQL澶勭悊纾佺洏婊$殑鏂瑰紡
A.4.4. MySQL灏嗕复鏃舵枃浠跺偍瀛樺湪鍝噷
A.4.5. 濡備綍淇濇姢鎴栨洿鏀筂ySQL濂楁帴瀛楁枃浠/tmp/mysql.sock
A.4.6. 鏃跺尯闂
A.5. 涓庢煡璇㈡湁鍏崇殑浜嬪疁
A.5.1. 鎼滅储涓殑澶у皬鍐欐晱鎰熸
A.5.2. 浣跨敤DATE鍒楁柟闈㈢殑闂
A.5.3. 涓嶯ULL鍊兼湁鍏崇殑闂
A.5.4. 涓庡垪鍒悕鏈夊叧鐨勯棶棰
A.5.5. 闈炰簨鍔¤〃鍥炴粴澶辫触
A.5.6. 浠庣浉鍏宠〃鍒犻櫎琛
A.5.7. 瑙e喅涓庝笉鍖归厤琛屾湁鍏崇殑闂
A.5.8. 涓庢诞鐐规瘮杈冩湁鍏崇殑闂
A.6. 涓庝紭鍖栧櫒鏈夊叧鐨勪簨瀹
A.7. 涓庤〃瀹氫箟鏈夊叧鐨勪簨瀹
A.7.1. 涓嶢LTER TABLE鏈夊叧鐨勯棶棰
A.7.2. 濡備綍鏇存敼琛ㄤ腑鐨勫垪椤哄簭
A.7.3. TEMPORARY TABLE闂
A.8. MySQL涓殑宸茬煡浜嬪疁
A.8.1. MySQL涓殑鎵撳紑浜嬪疁
鍦ㄦ湰闄勫綍涓紝鍒楀嚭浜嗕竴浜涗綘鍙兘浼氶亣鍒扮殑甯歌闂鍜岄敊璇秷鎭傚苟浠嬬粛浜嗙‘瀹氭晠闅滃師鍥犵殑鏂规硶锛屼互鍙婁负瑙e喅闂鎵搴旈噰鍙栫殑鎺柦銆

A.1. 濡備綍纭畾瀵艰嚧闂鐨勫師鍥

褰撲綘閬囧埌闂鏃讹紝棣栧厛瑕佸仛鐨勬槸鎵惧嚭瀵艰嚧闂鐨勭▼搴忓拰璁惧閮ㄤ欢锛

         濡傛灉閬囧埌涓嬭堪寰佸厗涔嬩竴锛屾垨璁告槸鍥犱负纭欢闂锛堝鍐呭瓨銆佷富鏉裤CPU鎴栫‖鐩橈級鎴栧唴鏍搁棶棰橈細

1.    閿洏涓嶅伐浣溿傛甯告儏鍐典笅鍙氳繃鎸Caps Lock寤鸿繘琛屾鏌ャ傚鏋Caps Lock鐨勭偣浜姸鎬佹湭鏀瑰彉锛屽氨闇瑕佹洿鎹㈤敭鐩橈紙鍦ㄦ涔嬪墠锛屽簲灏濊瘯閲嶅惎璁$畻鏈猴紝骞舵鏌ヤ笌閿洏鐩歌繛鐨勬墍鏈夌數缂嗭級銆

2.    榧犳爣鎸囬拡涓嶇Щ鍔ㄣ

3.    鏈哄櫒鏈杩滅▼鏈哄櫒鐨Ping鍛戒护鍋氬嚭搴旂瓟銆

4.    MySQL鏃犲叧鐨勫叾浠栫▼搴忓伐浣滀笉姝e父銆

5.    绯荤粺鎰忓閲嶅惎锛堟湁闂鐨勭敤鎴风骇绋嬪簭搴斾笉鑳戒娇绯荤粺鍑虹幇涓ラ噸闂锛夈

鍦ㄨ鎯呭喌涓嬶紝搴旀鏌ユ墍鏈夌殑鐢电紗骞惰繍琛屾煇浜涜瘖鏂伐鍏凤紝瀵逛綘鐨勭‖浠惰繘琛屾鏌ワ紒姝ゅ锛岃繕搴旀鏌ユ槸鍚︽湁鑳藉瑙e喅闂鐨勩侀傜敤浜庝綘鐨勬搷浣滅郴缁熺殑琛ヤ竵銆佹洿鏂版垨鏈嶅姟鍖呫傝妫鏌ユ墍鏈夌殑搴擄紙濡glibc锛夋槸鍚︽槸鏈鏂扮殑銆

浣跨敤閰嶅ECC鍐呭瓨鐨勬満鍣ㄤ互渚垮敖鏃╁彂鐜伴棶棰樻绘槸涓ソ涓绘剰銆

         濡傛灉閿洏宸查攣瀹氾紝鍙粠鍙︿竴鍙版満鍣ㄧ櫥褰曞埌浣犵殑鏈哄櫒锛屽苟鎵цkbd_mode 鈥揳锛屾垨璁歌兘鎭㈠銆

         璇锋鏌ョ郴缁熺殑鏃ュ織鏂囦欢锛/var/log/messages鎴栫被浼肩墿锛変互鎵惧嚭闂鐨勫師鍥犮傚鏋滀綘璁や负闂鍑哄湪MySQL涓紝搴旀鏌MySQL鐨勬棩蹇楁枃浠躲傝鍙傝5.11鑺傦紝鈥淢ySQL鏃ュ織鏂囦欢鈥

         濡傛灉浣犱笉璁や负瀛樺湪纭欢闂锛屽簲灏濊瘯鎵惧嚭瀵艰嚧闂鐨勫師鍥犮傝浣跨敤topps銆佷换鍔$鐞嗗櫒鎴栫被浼肩▼搴忥紝浠ユ鏌ュ摢涓▼搴忓崰鐢ㄤ簡鎵鏈CPU鏃堕棿鎴栭攣瀹氫簡鏈哄櫒銆

         浣跨敤topdf鎴栫被浼肩▼搴忔鏌ユ槸鍚﹀唴瀛樹笉澶熴佺鐩樼┖闂翠笉瓒炽佹枃浠舵弿杩扮缂轰箯銆佹垨鍏朵粬鍏抽敭璧勬簮缂哄皯銆

         濡傛灉闂鏄け鍘绘帶鍒剁殑杩涚▼锛屽簲灏濊瘯鏉姝诲畠銆傚鏋滄潃涓嶆杩涚▼锛屾垨璁告槸鍥犱负鎿嶄綔绯荤粺涓瓨鍦ㄧ己闄枫

濡傛灉鍦ㄦ鏌ヤ簡鎵鏈夊叾浠栧彲鑳芥т箣鍚庯紝骞跺緱鍑虹粨璁洪棶棰樻槸鐢MySQL鏈嶅姟鍣ㄦ垨MySQL瀹㈡埛绔鑷寸殑锛屽簲鍒涘缓鎻愪緵缁欐垜鏂圭殑閭欢鍒楄〃鎴栨敮鎸佸洟闃熺殑缂洪櫡鎶ュ憡銆傚湪缂洪櫡鎶ュ憡涓紝璇疯缁嗘弿杩扮郴缁熺殑琛屼负锛屼互鍙婁綘璁や负鍙戠敓浜嗕粈涔堟儏鍐点+3.

杩樺簲闃愭槑涓轰粈涔堜綘璁や负鏄MySQL瀵艰嚧浜嗛棶棰樸傝鑰冭檻鏈珷浠嬬粛鐨勬墍鏈夋儏鍐点傚噯纭槓鏄庡綋浣犳鏌ョ郴缁熸椂闂鏄浣曞嚭鐜扮殑銆傚浜庣▼搴忓拰鏃ュ織鏂囦欢鐨勪换浣曡緭鍑哄拰閿欒娑堟伅锛岃浣跨敤鈥滃鍒跺拰绮樿创鈥濇柟娉曘

灏介噺璇︾粏鎻忚堪涓嶅伐浣滅殑绋嬪簭锛屼互鍙婁綘鎵瑙佸埌鐨勬墍鏈夊緛鍏嗐傛垜浠繃鍘绘敹鍒拌繃寰堝浠呰鏄庘滅郴缁熶笉宸ヤ綔鈥濈殑缂洪櫡鎶ュ憡銆傝繖涓嶄細涓烘垜浠彁渚涙湁鍔╀簬瑙e喅闂鐨勪俊鎭

濡傛灉绋嬪簭澶辫触锛屼簡瑙d笅杩颁俊鎭绘槸鏈夌敤鐨勶細

         鏈夊珜鐤戠殑绋嬪簭鏄惁鍑虹幇浜嗗垎娈垫晠闅滐紙鏄惁杞偍鍐呮牳锛夛紵

         绋嬪簭鏄惁鍗犵敤浜嗘墍鏈夊彲鐢ㄧ殑CPU鏃堕棿锛熺敤top.杩涜妫鏌ャ傝绋嬪簭杩愯涓娈垫椂闂达紝鎴栬鑳界畝鍗曞湴璇勪及鏌愪簺浜嬫槸鍚︽槸璁$畻瀵嗛泦鎬х殑銆

         濡傛灉闂鏄洜mysqld鏈嶅姟鍣ㄥ鑷寸殑锛屼娇鐢mysqladmin -u root pingmysqladmin -u root processlist鏄惁鑳借幏寰楁湇鍔″櫒鐨勫搷搴旓紵

         褰撲綘灏濊瘯杩炴帴鍒MySQL鏈嶅姟鍣紙渚嬪锛mysql锛夋椂锛屽鎴风绋嬪簭缁欏嚭鐨勪俊鎭槸浠涔堬紵瀹㈡埛绔槸鍚﹀牭濉烇紵鏄惁鑾峰緱浜嗘潵鑷▼搴忕殑浠讳綍杈撳嚭锛

鍙戦佺己闄锋姤鍛婃椂锛岃閬靛惊1.7.1.2鑺傦紝鈥滆鏁欓棶棰樻垨閫氭姤缂洪櫡鈥涓粰鍑虹殑璇存槑銆

A.2. 浣跨敤MySQL绋嬪簭鏃剁殑甯歌閿欒

鏈妭鍒楀嚭浜嗙敤鎴疯繍琛MySQL鏈嶅姟鍣ㄦ椂甯镐細閬囧埌鐨勪竴浜涢敊璇傚敖绠¢棶棰樻槸鍦ㄤ綘灏濊瘯杩愯瀹㈡埛绔椂鍑虹幇鐨勶紝浣嗗寰堝闂鐨勮В鍐虫柟妗堟潵璇达紝闇瑕佹洿鏀MySQL鏈嶅姟鍣ㄧ殑閰嶇疆銆

A.2.1.聽鎷掔粷璁块棶

瀵艰嚧鎷掔粷璁块棶閿欒鐨勫師鍥犲緢澶氥傝閿欒甯镐笌杩炴帴鏃舵湇鍔″櫒鍏佽瀹㈡埛绔娇鐢ㄧ殑MySQL璐︽埛鏈夊叧銆傝鍙傝5.7.8鑺傦紝鈥滄嫆缁濊闂敊璇殑鍘熷洜銆傝鍙傝5.7.2鑺傦紝鈥滄潈闄愮郴缁熷伐浣滃師鐞嗏

A.2.2.聽鏃犳硶杩炴帴鍒癧local] MySQL鏈嶅姟鍣

Unix骞冲彴涓婄殑MySQL瀹㈡埛绔兘澶熶互涓ょ涓嶅悓鐨勬柟寮忚繛鎺ュ埌mysqld鏈嶅姟鍣細閫氳繃鏂囦欢绯荤粺涓殑鏂囦欢锛堥粯璁や负/tmp/mysql.sock锛変娇鐢Unix濂楁帴瀛楄繘琛岃繛鎺ワ紝鎴栭氳繃绔彛鍙蜂娇鐢TCP/IP杩涜杩炴帴銆Unix濂楁帴瀛楁枃浠剁殑杩炴帴閫熷害姣TCP/IP蹇紝浣嗕粎鑳藉湪涓庣浉鍚岃绠楁満涓婄殑鏈嶅姟鍣ㄧ浉杩炴椂浣跨敤銆傚鏋滄湭鎸囧畾鎸囧畾涓绘満鍚嶆垨鎸囧畾浜嗙壒娈婄殑涓绘満鍚localhost锛屽皢浣跨敤Unix濂楁帴瀛椼

濡傛灉MySQL鏈嶅姟鍣ㄨ繍琛屽湪Windows 9xMe涓婏紝浠呰兘閫氳繃TCP/IP杩涜杩炴帴銆傚鏈嶅姟鍣ㄨ繍琛屽湪Windows NT2000XP2003涓婏紝鑰屼笖浣跨敤--enable-named-pipe閫夐」鍚姩锛屽鏋滃湪杩愯鏈嶅姟鍣ㄧ殑鏈哄櫒涓婅繍琛屽鎴风锛屼篃鑳戒娇鐢ㄥ懡鍚嶇閬撹繘琛岃繛鎺ャ榛樿鎯呭喌涓嬶紝鍛藉悕绠¢亾鐨勫悕绉颁负MySQL銆傚鏋滃湪杩炴帴鍒mysqld鏃舵湭缁欏畾涓绘満鍚嶏紝MySQL瀹㈡埛绔鍏堜細灏濊瘯杩炴帴鍒板懡鍚嶇閬撱傚鏋滀笉鑳藉伐浣滐紝灏嗚繛鎺ュ埌TCP/IP绔彛銆備娇鐢ㄢ.鈥濅綔涓轰富鏈哄悕锛屽彲鍦Windows骞冲彴涓婂己鍒朵娇鐢ㄥ懡鍚嶇閬撱

閿欒(2002)鈥滄棤娉曡繛鎺ュ埌鈥︹閫氬父鎰忓懗鐫鍦ㄧ郴缁熸病鏈夎繍琛岀殑MySQL鏈嶅姟鍣紝鎴栧湪杩炴帴鍒版湇鍔″櫒鏃朵娇鐢ㄤ簡涓嶆纭殑Unix濂楁帴瀛楁枃浠跺悕鎴TCP/IP绔彛鍙枫

棣栧厛妫鏌ユ湇鍔″櫒涓绘満涓婃槸鍚︽湁鍚嶄负mysqld鐨勮繘绋嬶紙鍦Unix骞冲彴涓婁娇鐢ps xa | grep mysqld锛屾垨鍦Windows骞冲彴涓婁娇鐢ㄤ换鍔$鐞嗗櫒锛夈傚鏋滄病鏈夎繖绫昏繘绋嬶紝搴斿惎鍔ㄦ湇鍔″櫒銆傝鍙傝2.9.2.3鑺傦紝鈥滃惎鍔∕ySQL鏈嶅姟鍣ㄤ互鍙婂叾鏁呴殰璇婃柇鍜屾帓闄も

濡傛灉mysqld杩涚▼姝e湪杩愯锛屽彲浣跨敤涓嬭堪鍛戒护妫鏌ャ傚湪浣犵殑鍏蜂綋璁剧疆涓紝绔彛鍙锋垨Unix濂楁帴瀛楁枃浠跺悕鍙兘浼氭湁鎵涓嶅悓銆host_ip浠h〃杩愯鏈嶅姟鍣ㄧ殑鏈哄櫒鐨IP缂栧彿銆

shell> mysqladmin version
shell> mysqladmin variables
shell> mysqladmin -h `hostname` version variables
shell> mysqladmin -h `hostname` --port=3306 version
shell> mysqladmin -h host_ip version
shell> mysqladmin --protocol=socket --socket=/tmp/mysql.sock version

娉ㄦ剰锛屽簲涓庝富鏈哄悕鍛戒护涓璧蜂娇鐢ㄢ`鈥濊屼笉鏄溾欌濓紝杩欎細浣夸富鏈哄悕杈撳嚭锛堝綋鍓嶄富鏈哄悕锛夎浠e叆mysqladmin鍛戒护銆傚鏋滄病鏈変富鏈哄悕鍛戒护鎴栨杩愯鍦Windows骞冲彴涓婏紝搴斾互鎵嬪姩鏂瑰紡杈撳叆鏈哄櫒鐨勪富鏈哄悕锛堟棤鈥`鈥濈鍙凤級锛屽悗璺-h閫夐」銆涔熷彲浠ヤ娇鐢TCP/IP鍗忚鐢-h 127.0.0.1杩炴帴鍒版湰鍦颁富鏈恒

涓嬮潰缁欏嚭浜嗕竴浜涒滄棤娉曡繛鎺ュ埌鏈湴MySQL鏈嶅姟鍣ㄢ濋敊璇殑鍙兘鍘熷洜锛

1.    Mysqld鏈繍琛屻傝妫鏌ユ搷浣滅郴缁熺殑杩涚▼鍒楄〃浠ョ‘淇mysqld杩涚▼姝e湪杩愯銆

2.    浣犳鍦ㄥ叿鏈夊緢澶TCP/IP杩炴帴鐨Windows骞冲彴涓婅繍琛MySQL鏈嶅姟鍣ㄣ傚鏋滀綘鐨勫鎴风缁忓父鍑虹幇閿欒锛岃鍙傝A.2.2.1鑺傦紝鈥滃湪Windows涓婁笌MySQL鏈嶅姟鍣ㄧ殑杩炴帴澶辫触鈥锛屼互鎵惧嚭瑙勯伩鏂规硶銆

3.    浣犳鍦ㄤ娇鐢MIT-pthreads鐨勭郴缁熶笂杩愯銆傚鏋滀綘姝e湪杩愯涓嶅叿鏈夊浐鏈夌嚎绋嬬殑鎿嶄綔绯荤粺锛mysqld灏嗕娇鐢MIT-pthreads杞欢鍖呫傝鍙傝2.1.1鑺傦紝鈥淢ySQL鏀寔鐨勬搷浣滅郴缁熲銆備絾鏄紝骞堕潪鎵鏈夌殑MIT-pthreads鐗堟湰鍧囨敮鎸Unix濂楁帴瀛楁枃浠躲傚湪涓嶆敮鎸佸鎺ュ瓧鏂囦欢鐨勭郴缁熶笂锛岃繛鎺ュ埌鏈嶅姟鍣ㄦ椂锛屽繀椤绘槑纭寚瀹氫富鏈哄悕銆傝浣跨敤涓嬭堪鍛戒护鏉ユ鏌ユ槸鍚﹁繛鎺ュ埌浜嗘湇鍔″櫒锛

4.           shell> mysqladmin -h `hostname` version

5.    鏌愪汉绉诲姩浜mysqld浣跨敤鐨Unix濂楁帴瀛楁枃浠讹紙榛樿涓/tmp/mysql.sock锛夈備緥濡傦紝浣犲彲鑳芥墽琛屼簡灏嗘棫鏂囦欢浠/tmp鐩綍鍒犻櫎鐨cron浠诲姟銆備綘鎬昏兘鎵цmysqladmin version鏉ユ鏌mysqladmin璇曞浘浣跨敤鐨Unix濂楁帴瀛楁枃浠舵槸鍚︾殑纭瓨鍦ㄣ傚湪璇ユ儏鍐典笅锛屾洿姝f柟寮忔槸鏇存敼cron浠诲姟锛屼笉鍒犻櫎mysql.sock鏂囦欢锛屾垨灏嗗鎺ュ瓧鏂囦欢缃簬鍏朵粬鍦版柟銆璇峰弬瑙A.4.5鑺傦紝鈥滃浣曚繚鎶ゆ垨鏇存敼MySQL濂楁帴瀛楁枃浠/tmp/mysql.sock

6.    浣犱娇鐢--socket=/path/to/socket閫夐」鍚姩浜mysqld鏈嶅姟鍣紝褰撳繕璁板皢濂楁帴瀛楁枃浠剁殑鏂板悕绉伴氱煡瀹㈡埛绔▼搴銆傚鏋滄洿鏀逛簡鍏充簬鏈嶅姟鍣ㄧ殑濂楁帴瀛楄矾寰勶紝涔熷繀椤婚氱煡MySQL瀹㈡埛绔傚彲鍦ㄨ繍琛屽鎴风绋嬪簭鏃朵娇鐢ㄧ浉鍚岀殑鈥攕ocket閫夐」鏉ュ畬鎴愯浠诲姟銆傛澶栵紝浣犺繕搴旂‘淇濆鎴风鍏锋湁璁块棶鏂囦欢mysql.sock鐨勬潈闄愩傝鎯虫壘鍑哄鎺ュ瓧鏂囦欢鐨勪綅缃紝鍙娇鐢細

7.           shell> netstat -ln | grep mysql

璇峰弬瑙A.4.5鑺傦紝鈥滃浣曚繚鎶ゆ垨鏇存敼MySQL濂楁帴瀛楁枃浠/tmp/mysql.sock

8.    浣犳鍦ㄤ娇鐢Linux鑰屼笖1涓湇鍔″櫒绾跨▼宸叉浜★紙鍐呮牳宸叉竻闄わ級銆傚湪姝ゆ儏鍐典笅锛屽湪閲嶅惎MySQL鏈嶅姟鍣ㄤ箣鍓嶏紝蹇呴』鏉姝诲叾浠mysqld绾跨▼锛堜緥濡傦紝浣跨敤killmysql_zap鑴氭湰锛夈璇峰弬瑙A.4.2鑺傦紝鈥滃鏋淢ySQL渚濈劧宕╂簝锛屽簲浣滀簺浠涔堚

9.    鏈嶅姟鍣ㄦ垨瀹㈡埛绔▼搴忎笉鍏锋湁璁块棶鍖呭惈Unix濂楁帴瀛楁枃浠剁殑鐩綍鎴栧鎺ュ瓧鏂囦欢鏈韩鐨勬伆褰撴潈闄愩傚湪璇ユ儏鍐典笅锛屽繀椤绘洿鏀圭洰褰曟垨濂楁帴瀛楁枃浠剁殑璁块棶鏉冮檺锛屼互渚挎湇鍔″櫒鎴栧鎴风绋嬪簭鑳藉璁块棶瀹冧滑锛屾垨鐢鈥搒ocket閫夐」閲嶅惎mysqld锛屽湪璇ラ夐」涓寚瀹氭湇鍔″櫒鑳藉垱寤恒佽屼笖瀹㈡埛绔彲璁块棶鐨勭洰褰曚笅鐨勫鎺ュ瓧鏂囦欢鍚嶃

濡傛灉閬囧埌閿欒娑堟伅鈥滄棤娉曡繛鎺ュ埌some_host涓婄殑MySQL鏈嶅姟鍣鈥濓紝鍙皾璇曢噰鍙栦笅杩版楠や互鎵惧嚭闂鎵鍦細

         鎵ц鈥telnet some_host 3306骞舵寜涓ゆ鍥炶溅閿紝妫鏌ユ湇鍔″櫒鏄惁杩愯鍦ㄨ涓绘満涓婏紙3306鏄粯璁ょ殑MySQL绔彛鍙枫傚鏋滀綘鐨勬湇鍔″櫒姝e湪鐩戝惉涓嶅悓鐨勭鍙o紝璇锋洿鏀硅鍊硷級銆傚鏋滄湁1MySQL鏈嶅姟鍣ㄦ鍦ㄨ繍琛屽苟鐩戝惉璇ョ鍙o紝浣犲簲鏀跺埌鍖呭惈鏈嶅姟鍣ㄧ増鏈彿鐨勫洖搴斻傚鏋滈亣鍒伴敊璇紝濡傗telnet:鏃犳硶杩炴帴鍒拌繙绋嬩富鏈:鎷掔粷杩炴帴锛岃〃绀哄湪璇ュ畾绔彛涓婃病鏈夎繍琛岀殑鏈嶅姟鍣ㄣ

         濡傛灉鏈嶅姟鍣ㄦ杩愯鍦ㄦ湰鍦颁富鏈轰笂锛岃浣跨敤Unix濂楁帴瀛楁枃浠讹紝骞朵娇鐢mysqladmin -h localhost variables杩涜杩炴帴銆傞獙璇佹湇鍔″櫒鐩戝惉鐨TCP/IP绔彛鍙凤紙瀹冩槸port鍙橀噺鐨勫硷級銆

         纭繚浣犵殑mysqld鏈嶅姟鍣ㄦ湭鐢--skip-networking閫夐」鍚姩銆濡傛灉浣跨敤浜嗚閫夐」锛屽皢鏃犳硶浣跨敤TCP/IP杩炴帴鍒板畠銆

         妫鏌ュ苟纭涓嶅瓨鍦ㄥ睆钄戒簡瀵MySQL璁块棶鐨勯槻鐏銆傞渶瑕侀厤缃濡ZoneAlarmWindows XP涓汉闃茬伀澧欑瓑搴旂敤绋嬪簭锛屼互鍏佽瀵MySQL鏈嶅姟鍣ㄧ殑澶栭儴璁块棶銆

A.2.2.1.聽鍦╓indows涓婁笌MySQL鏈嶅姟鍣ㄧ殑杩炴帴澶辫触

褰撲綘鍦ㄥ叿鏈夊緢澶TCP/IP杩炴帴鐨Windows涓婅繍琛MySQL鏈嶅姟鍣紝骞剁粡甯稿湪瀹㈡埛绔笂閬囧埌鈥滄棤娉曡繛鎺ュ埌MySQL鏈嶅姟鍣ㄢ濋敊璇椂锛屽彲鑳芥槸鍥犱负Windows涓嶅厑璁歌冻澶熺殑涓存椂锛堢煭鍛斤級绔彛鐢ㄤ簬杩欑被杩炴帴銆

榛樿鎯呭喌涓嬶紝Windows鍏佽鐢ㄤ簬浣跨敤5000涓复鏃讹紙鐭懡锛TCP绔彛銆備换浣曠鍙e叧闂悗锛屽畠灏嗗湪TIME_WAIT鐘舵佷繚鎸120绉掋備笌閲嶆柊鍒濆鍖栧叏鏂扮殑杩炴帴鐩告瘮锛岃鐘舵佸厑璁镐互鏇翠綆鐨勫紑閿閲嶆柊浣跨敤杩炴帴銆浣嗘槸锛屽湪璇ユ椂闂撮濆幓鍓嶏紝鏃犳硶鍐嶆浣跨敤璇ョ鍙c

瀵逛簬灏忕殑鍙敤TCP绔彛鍫嗘爤锛5000锛夛紝浠ュ強鍏锋湁TIME_WAIT鐘舵佺殑澶ч噺鍦ㄧ煭鏃堕棿鍐呮墦寮鍜屽叧闂殑TCP绔彛锛屼綘寰堝彲鑳介亣鍒扮鍙h楀敖闂銆傚鐞嗚闂鐨勬柟娉曟湁涓ょ锛

         閫氳繃璋冩煡杩炴帴姹犱互鍙婂彲鑳界殑鎸佷箙杩炴帴锛屽噺灏戝揩閫熸秷鑰楃殑TCP绔彛鏁般

         璋冩暣Windows娉ㄥ唽琛ㄤ腑鐨勬煇浜涜缃紙璇峰弬瑙佷笅闈級銆

瑕佺偣锛氫笅杩版楠ゆ秹鍙婃洿鏀Windows 娉ㄥ唽琛ㄣ傛洿鏀规敞鍐岃〃涔嬪墠锛岃澶囦唤娉ㄥ唽琛紝骞剁‘璁や綘宸叉帉鎻″湪鍑虹幇闂鏃舵仮澶嶆敞鍐岃〃鐨勬柟娉曘傚叧浜庡浠藉勾銆佹仮澶嶅拰缂栬緫娉ㄥ唽琛ㄧ殑鏇村淇℃伅锛岃璇峰弬瑙Microsoft鐭ヨ瘑搴撲腑鐨勪笅杩版枃鐚細http://support.microsoft.com/kb/256986/EN-US/

         鍚姩娉ㄥ唽琛ㄧ紪杈戝櫒锛Regedt32.exe锛夈

         鍦ㄦ敞鍐岃〃涓‘瀹氫笅杩伴敭鍊肩殑浣嶇疆锛

                HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters

         鍦ㄢ滅紪杈戔濊彍鍗曚笂鐐瑰嚮鈥滄坊鍔犲尖濓紝鐒跺悗澧炲姞涓嬭堪娉ㄥ唽鍊硷細

                Value Name: MaxUserPort
                Data Type: REG_DWORD
                Value: 65534

瀹冪敤浜庤缃负浠讳綍鐢ㄦ埛鎻愪緵鐨勪复鏃剁鍙f暟銆傛湁鏁堣寖鍥翠粙浜500065534涔嬮棿锛堝崄杩涘埗锛夈傞粯璁ゅ间负0x13885000锛屽崄杩涘埗锛夈

         鍦ㄢ滅紪杈戔濊彍鍗曚笂鐐瑰嚮鈥滄坊鍔犲尖濓紝鐒跺悗澧炲姞涓嬭堪娉ㄥ唽鍊硷細

                Value Name: TcpTimedWaitDelay
                Data Type: REG_DWORD
                Value: 30

瀹冪敤浜庤缃叧闂箣鍓嶅皢TCP绔彛杩炴帴淇濇寔鍦TIME_WAIT鐘舵佺殑绉掓暟銆鏈夋晥鑼冨洿浠嬩簬0绉掑拰300绉掍箣闂淬傞粯璁ゅ间负0x78120绉掞級銆

         閫鍑烘敞鍐岃〃缂栬緫鍣ㄣ

         閲嶆柊寮曞鏈哄櫒銆

娉ㄩ噴锛氭挙閿涓婅堪璁剧疆鍗佸垎绠鍗曪紝灏卞儚鍒犻櫎浣犲垱寤虹殑娉ㄥ唽琛ㄤ竴鏍枫

A.2.3.聽瀹㈡埛绔笉鏀寔閴村畾鍗忚

MySQL 5.1閲囩敤浜嗗熀浜庡瘑鐮佹贩缂栫畻娉曠殑閴村畾鍗忚锛屽畠涓庢棭鏈熷鎴风锛4.1涔嬪墠锛変娇鐢ㄧ殑鍗忚涓嶅吋瀹广傚鏋滀綘灏嗘湇鍔″櫒鍗囩骇鍒4.1涔嬩笂锛岀敤鏃╂湡鐨勫鎴风杩涜杩炴帴鍙兘澶辫触锛屽苟缁欏嚭涓嬭堪娑堟伅锛

shell> mysql
瀹㈡埛绔笉鏀寔鏈嶅姟鍣ㄨ姹傜殑閴村畾鍗忚锛氳鑰冭檻鍗囩骇MySQL瀹㈡埛绔

瑕佹兂瑙e喅璇ラ棶棰橈紝搴斾娇鐢ㄤ笅杩版柟娉曚箣涓锛

         鍗囩骇鎵鏈夌殑瀹㈡埛绔▼搴忥紝浠ヤ娇鐢4.1.1鎴栨洿鏂扮殑瀹㈡埛绔簱銆

         4.1鐗堜箣鍓嶇殑瀹㈡埛绔繛鎺ュ埌鏈嶅姟鍣ㄦ椂锛岃浣跨敤浠嶅叿鏈4.1鐗堜箣鍓嶉鏍煎瘑鐮佺殑璐︽埛銆

         瀵逛簬闇瑕佷娇鐢4.1鐗堜箣鍓嶇殑瀹㈡埛绔殑姣忎綅鐢ㄦ埛锛屽皢瀵嗙爜鎭㈠涓4.1鐗堜箣鍓嶇殑椋庢牸銆傚彲浠ヤ娇鐢SET PASSWORD璇彞鍜OLD_PASSWORD()鍑芥暟瀹屾垚璇ヤ换鍔★細

                mysql> SET PASSWORD FOR
                    -> 'some_user'@'some_host' = OLD_PASSWORD('newpwd');

涔熷彲浠ヤ娇鐢UPDATEFLUSH PRIVILEGES

mysql> UPDATE mysql.user SET Password = OLD_PASSWORD('newpwd')
    -> WHERE Host = 'some_host' AND User = 'some_user';
mysql> FLUSH PRIVILEGES;

鐢ㄤ綘鎵撶畻浣跨敤鐨勫瘑鐮佹浛鎹㈠墠渚嬩腑鐨勨newpwd鈥濄MySQL涓嶈兘鍛婅瘔浣犲師鏉ョ殑瀵嗙爜鏄粈涔堬紝鍥犳锛屼綘闇瑕侀夋嫨鏂扮殑瀵嗙爜銆

         閫氱煡鏈嶅姟鍣ㄤ娇鐢ㄦ棫鐨勫瘑鐮佹贩缂栫畻娉曪細

1.    浣跨敤鈥--old-passwords鈥濋夐」鍚姩mysqld

2.    瀵逛簬宸插皢瀵嗙爜鏇存柊涓鸿緝闀4.1鏍煎紡鐨勬瘡涓处鎴凤紝涓哄叾鎸囧畾鍏锋湁鏃ф牸寮忕殑瀵嗙爜銆傚彲浠ヤ娇鐢ㄤ笅杩版煡璇㈢‘瀹氳繖浜涜处鎴凤細

3.                 mysql> SELECT Host, User, Password FROM mysql.user
4.                     -> WHERE LENGTH(Password) > 16;

瀵逛簬鏌ヨ鏄剧ず鐨勬瘡涓处鎴疯褰曪紝璇蜂娇鐢HostUser鍊硷紝骞朵娇鐢OLD_PASSWORD()鍑芥暟浠ュ強SET PASSWORDUPDATE涔嬩竴鎸囧畾瀵嗙爜锛屽鍓嶉潰鎵浠嬬粛鐨勯偅鏍枫

娉ㄩ噴锛鍦ㄦ棭鏈熺殑PHP鐗堟湰涓紝mysql鎵╁睍涓嶆敮鎸MySQL 4.1.1鍜屾洿楂樼増涓殑閴村畾鍗忚銆傛棤璁轰娇鐢ㄧ殑PHP鐗堟湰鏄粈涔堬紝瀹冨潎鏄纭殑銆傚鏋滀綘鎵撶畻涓MySQL 4.1鎴栨洿楂樼増鏈竴璧蜂娇鐢mysql鎵╁睍锛岄渶瑕佷娇鐢ㄥ墠闈粙缁嶇殑閫夐」涔嬩竴锛岄厤缃MySQL锛屼互渚夸笌杈冩棭鐨勫鎴风涓璧蜂娇鐢ㄣmysqli鎵╁睍锛堟敮鎸鏀硅繘鐨MySQL鈥锛屽湪PHP 5涓鍔狅級涓MySQL 4.1鍜屾洿楂樼増鏈腑浣跨敤鐨勬敼杩涚殑瀵嗙爜娣风紪绠楁硶鍏煎锛屼笉闇瑕佸MySQL杩涜鐗规畩閰嶇疆灏辫兘浣跨敤璇MySQL瀹㈡埛绔簱銆傚叧浜mysqli鎵╁睍鐨勬洿澶氫俊鎭紝璇峰弬瑙http://php.net/mysqli

鍏充簬瀵嗙爜娣风紪鍜岄壌瀹氬姛鑳界殑棰濆鑳屾櫙鐭ヨ瘑锛岃鍙傝5.7.9鑺傦紝鈥淢ySQL 4.1涓殑瀵嗙爜鍝堝笇澶勭悊鈥

A.2.4. 杈撳叆瀵嗙爜鏃跺嚭鐜板瘑鐮侀敊璇

浣跨敤鏃犱笅杩板瘑鐮佸肩殑鈥鈥攑assword鈥-p鈥閫夐」璋冪敤鏃讹紝MySQL瀹㈡埛绔▼搴忓皢鎻愮ず杈撳叆瀵嗙爜锛
shell> mysql -u user_name -p
Enter password:

鍦ㄦ煇浜涚郴缁熶笂锛屽綋浣犲湪閫夐」鏂囦欢鎴栧懡浠よ涓婃寚瀹氭椂锛屼綘鍙兘浼氬彂鐜板瘑鐮佽兘澶熷伐浣滐紝浣嗘槸褰撲綘鍦ㄢEnter password:鈥濇彁绀轰笅浠ヤ氦浜掓柟寮忚緭鍏ュ瘑鐮佹椂锛屼綘鍙兘浼氬彂鐜拌緭鍏ョ殑瀵嗙爜涓嶅伐浣溿傚綋绯荤粺鎵鎻愪緵鐨勭敤浜庤鍙栧瘑鐮佺殑搴撳皢瀵嗙爜鍊奸檺瀹氬湪灏戞暟瀛楃鏃讹紙鍏稿瀷鎯呭喌涓嬩负8涓級锛屽氨浼氬嚭鐜拌闂銆傝繖鏄笌绯荤粺搴撴湁鍏崇殑闂锛屼笌MySQL鏃犲叧銆傝鎯冲鐞嗚闂锛屽彲灏MySQL瀵嗙爜鏇存敼涓虹敱8涓瓧绗︽垨鏇村皯瀛楃鏋勬垚鐨勫硷紝鎴栧皢瀵嗙爜缃簬閫夐」鏂囦欢涓

A.2.5.聽涓绘満鐨刪ost_name琚睆钄

濡傛灉閬囧埌涓嬭堪閿欒锛岃〃绀mysqld宸叉敹鍒版潵鏉ヨ嚜涓绘満鈥host_name鈥濈殑寰堝杩炴帴璇锋眰锛屼絾璇ヤ富鏈哄嵈鍦ㄤ腑閫斾腑鏂

鐢变簬鍑虹幇寰堝杩炴帴閿欒锛屼富鏈'host_name'琚睆钄姐
鍙娇鐢'mysqladmin flush-hosts'瑙i櫎灞忚斀銆

鍏佽鐨勪腑鏂繛鎺ヨ姹傜殑鏁扮洰鐢max_connect_errors绯荤粺鍙橀噺鐨勫煎喅瀹氥褰撹秴鍑max_connect_errors瑙勫畾鐨勮繛鎺ヨ姹傛椂锛mysqld灏嗚涓烘煇澶勫嚭閿欙紙渚嬪锛屾煇浜烘璇曞浘鎻掑叆锛夛紝骞跺睆钄戒富鏈虹殑杩涗竴姝ヨ繛鎺ヨ姹傦紝鐩磋嚦鎵ц浜mysqladmin flush-hosts鍛戒护锛屾垨鍙戝嚭浜FLUSH HOSTS璇彞涓烘銆傝鍙傝5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥

鍦ㄩ粯璁ゆ儏鍐典笅锛mysqld浼氬湪10娆¤繛鎺ラ敊璇悗灞忚斀涓绘満銆備綘鍙互閫氳繃涓嬭堪鏂瑰紡鍚姩鏈嶅姟鍣ㄦ潵璋冩暣璇ュ硷細

shell> mysqld_safe --max_connect_errors=10000 &

濡傛灉鍦ㄧ粰瀹氫富鏈轰笂閬囧埌璇ラ敊璇紝棣栧厛搴旀牳瀹炶涓绘満鐨TCP/IP杩炴帴鏄惁姝g‘銆傚鏋滃瓨鍦ㄧ綉缁滈棶棰橈紝澧炲姞max_connect_errors鍙橀噺鐨勫间笉浼氭湁浠讳綍濂藉銆

A.2.6.聽杩炴帴鏁拌繃澶

褰撲綘璇曞浘杩炴帴鍒mysqld鏈嶅姟鍣ㄦ椂閬囧埌鈥滆繃澶氳繛鎺モ濋敊璇紝杩欒〃绀烘墍鏈夊彲鐢ㄧ殑杩炴帴鍧囧凡琚叾浠栧鎴风浣跨敤銆

鍏佽鐨勮繛鎺ユ暟鐢max_connections绯荤粺鍙橀噺鎺у埗銆榛樿鍊间负100銆傚鏋滈渶瑕佹敮鎸佹洿澶氱殑杩炴帴锛屽簲浣跨敤璇ュ彉閲忕殑杈冨ぇ鍊奸噸鍚mysqld

mysqld瀹為檯涓婂厑璁max_connections+1涓鎴风杩涜杩炴帴銆棰濆鐨勮繛鎺ヤ繚鐣欑粰鍏锋湁SUPER鏉冮檺鐨勮处鎴枫傞氳繃涓虹郴缁熺鐞嗗憳鑰屼笉鏄櫘閫氱敤鎴锋巿浜SUPER鏉冮檺锛堟櫘閫氱敤鎴蜂笉搴斿叿鏈夎鏉冮檺锛夛紝绯荤粺绠$悊鍛樿兘澶熻繛鎺ュ埌鏈嶅姟鍣紝骞朵娇鐢SHOW PROCESSLIST鏉ヨ瘖鏂棶棰橈紝鍗充娇宸茶繛鎺ョ殑鏃犵壒鏉冨鎴风鏁板凡杈惧埌鏈澶у间篃鍚屾牱銆璇峰弬瑙13.5.4.16鑺傦紝鈥淪HOW PROCESSLIST璇硶鈥

MySQL鑳芥敮鎸佺殑鏈澶ц繛鎺ユ暟鍙栧喅浜庣粰瀹氬钩鍙颁笂绾跨▼搴撶殑璐ㄩ噺銆LinuxSolaris搴旇兘鏀寔500-1000涓苟鍙戣繛鎺ワ紝鍏蜂綋鎯呭喌鍙栧喅浜RAM瀹归噺锛屼互鍙婂鎴风姝e湪浣滀粈涔堛MySQL AB鎻愪緵鐨勯潤鎬Linux搴撹兘鏀寔楂樿揪4000涓繛鎺ャ

A.2.7.聽鍐呭瓨婧㈠嚭

濡傛灉浣跨敤mysql瀹㈡埛绔▼搴忓彂鍑轰簡鏌ヨ锛屽苟鏀跺埌涓嬭堪閿欒涔嬩竴锛屽垯琛ㄧずmysql娌℃湁瓒冲鍐呭瓨鏉ヤ繚瀛樺叏閮ㄦ煡璇㈢粨鏋滐細

mysql: Out of memory at line 42, 'malloc.c'
mysql: needed 8136 byte (8k), memory in use: 12481367 bytes (12189k)
閿欒2008: MySQL client ran out of memory

瑕佹兂鏇存璇ラ棶棰橈紝棣栧厛搴旀鏌ユ煡璇㈡槸鍚︽纭傝繑鍥炶繖涔堝琛屾槸鍚﹀悎鐞嗭紵濡傛灉涓嶅悎鐞嗭紝鏇存鏌ヨ骞跺啀娆″皾璇曘傚惁鍒欙紝搴斾娇鐢ㄢ--quick鈥濋夐」璋冪敤mysql銆傝繖鏍凤紝灏嗕娇鐢mysql_use_result() C API鍑芥暟鏉ユ绱㈢粨鏋滈泦锛岃繖绫诲嚱鏁拌兘澶熼檷浣庡鎴风涓婄殑璐熻浇锛堜絾浼氬姞閲嶆湇鍔″櫒涓婄殑璐熻浇锛夈

A.2.8.聽MySQL鏈嶅姟鍣ㄤ笉鍙敤

鍦ㄦ湰鑺備腑锛岃繕浠嬬粛浜嗗嚭鐜版煡璇㈤敊璇湡闂达紝涓庝涪澶变簡鏈嶅姟鍣ㄨ繛鎺ユ湁鍏崇殑浜嬪疁銆

MySQL鏈嶅姟鍣ㄤ笉鍙敤閿欒鐨勬渶甯歌鍘熷洜鏄湇鍔″櫒瓒呮椂浠ュ強杩炴帴宸插叧闂傚湪璇ユ儏鍐典笅锛岄氬父鑳借鍒颁笅杩伴敊璇唬鐮佷箣涓锛堝叿浣撶殑閿欒浠g爜涓庢搷浣滅郴缁熸湁鍏筹級锛

閿欒浠g爜

鎻忚堪

CR_SERVER_GONE_ERROR

瀹㈡埛绔棤娉曞皢闂鍙戦佽嚦鏈嶅姟鍣ㄣ

CR_SERVER_LOST

鍐欏叆鏈嶅姟鍣ㄦ椂瀹㈡埛绔湭鏀跺埌閿欒锛屼絾涔熸湭鑾峰緱闂鐨勫畬鏁寸瓟妗堬紙鎴栦换浣曠瓟妗堬級銆

鍦ㄩ粯璁ゆ儏鍐典笅锛屽鏋滄湭鍙戠敓浠讳綍浜嬶紝8灏忔椂鍚庢湇鍔″櫒灏嗗叧闂繛鎺ャ備篃鍙互鍦ㄥ惎鍔mysqld鏃讹紝閫氳繃璁剧疆wait_timeout鍙橀噺鏇存敼鏃堕棿闄愬埗銆傝鍙傝5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥.

濡傛灉鏈1涓剼鏈紝浣犱粎闇瑕佸啀娆″彂鍑烘煡璇紝璁╁鎴风鍐嶆杩涜鑷姩杩炴帴鍗冲彲銆傚叾涓紝鍋囧畾鍦ㄥ鎴风涓惎鐢ㄤ簡鑷姩鍐嶈繛鎺ュ姛鑳斤紙瀵逛簬mysql鍛戒护琛屽鎴风锛岃繖鏄粯璁よ缃級銆

MySQL鏈嶅姟鍣ㄤ笉鍙敤閿欒鐨勪竴浜涘叾浠栧父瑙佸師鍥犲涓嬶細

         浣狅紙鎴db绯荤粺绠$悊鍛橈級浣跨敤KILL璇彞鎴mysqladmin kill鍛戒护鏉姝讳簡姝e湪杩愯鐨勭嚎绋嬨

         浣犺瘯鍥惧湪鍏抽棴浜嗕笌鏈嶅姟鍣ㄧ殑杩炴帴鍚庤繍琛屾煡璇€傝繖琛ㄦ槑搴旀洿姝e簲鐢ㄧ▼搴忎腑鐨勯昏緫閿欒銆

         浣犲湪瀹㈡埛绔竴渚ч亣鍒TCP/IP杩炴帴瓒呮椂閿欒銆傚鏋滀綘浣跨敤浜嗗懡浠わ細mysql_options(..., MYSQL_OPT_READ_TIMEOUT,...)mysql_options(..., MYSQL_OPT_WRITE_TIMEOUT,...)锛屽氨鍙兘鍑虹幇璇ラ棶棰樸鍦ㄨ鎯呭喌涓嬶紝澧炲姞瓒呮椂鍊煎彲鑳芥湁鍔╀簬闂鐨勮В鍐炽

         浣犲湪鏈嶅姟鍣ㄧ閬囧埌瓒呮椂閿欒锛岃屼笖绂佹浜嗗鎴风涓殑鑷姩鍐嶈繛鎺ュ姛鑳斤紙MYSQL缁撴瀯涓殑鍐嶈繛鎺ユ爣蹇楃瓑浜0锛夈

         浣犳鍦ㄤ娇鐢Windows瀹㈡埛绔紝鑰屼笖鍦ㄥ彂鍑哄懡浠や箣鍓嶆湇鍔″櫒鎾ら攢浜嗚繛鎺ワ紙鎴栬鏄洜涓哄凡瓒呰繃wait_timeout

Windows骞冲彴涓婂嚭鐜伴棶棰樼殑鍘熷洜锛屽湪鏌愪簺鎯呭喌涓嬶紝灏TCP/IP杩炴帴鍐欏叆鏈嶅姟鍣ㄦ椂锛MySQL鏈敹鍒版潵鑷搷浣滅郴缁熺殑閿欒锛屼絾褰撹瘯鍥句粠杩炴帴璇诲彇绛旀鏃跺嚭鐜伴敊璇

鍦ㄨ鎯呭喌涓嬶紝鍗充娇MYSQL缁撴瀯涓殑鍐嶈繛鎺ユ爣蹇楃瓑浜1MySQL涔熶笉浼氭墽琛岃嚜鍔ㄥ啀杩炴帴骞跺啀娆″彂鍑烘煡璇紝杩欐槸鍥犱负瀹冧笉鐭ラ亾鏈嶅姟鍣ㄦ槸鍚︽敹鍒板師濮嬫煡璇€

瀵规鐨勮В鍐虫柟寮忔槸锛氬鏋滆嚜涓婁竴娆℃煡璇互鏉ョ粡杩囦簡杈冮暱鏃堕棿锛屽湪杩炴帴涓婃墽琛mysql_ping锛堟鏄MyODBC鎵浣滅殑锛夛紱鎴栧湪mysqld鏈嶅姟鍣ㄤ笂灏wait_timeout璁剧疆寰楀緢楂橈紝浣夸箣瀹為檯涓婁笉瀛樺湪瓒呮椂銆

         濡傛灉浣犲悜鏈嶅姟鍣ㄥ彂鍑轰簡涓嶆纭垨杩囧ぇ鐨勬煡璇紝涔熶細閬囧埌杩欑被闂銆傚鏋mysqld鏀跺埌杩囧ぇ鎴栨棤搴忕殑淇℃伅鍖咃紝瀹冧細璁や负瀹㈡埛绔嚭閿欙紝骞跺叧闂繛鎺ャ傚鏋滈渶瑕佹墽琛岃緝澶х殑鏌ヨ锛堜緥濡傦紝姝e湪澶勭悊澶х殑BLOB鍒楋級锛鍙氳繃璁剧疆鏈嶅姟鍣ㄧ殑max_allowed_packet鍙橀噺锛屽鍔犳煡璇㈤檺鍒跺硷紝璇ュ彉閲忕殑榛樿鍊间负1MB銆傛垨璁革紝浣犺繕闇澧炲姞瀹㈡埛绔笂鐨勬渶澶т俊鎭寘澶у皬銆傚叧浜庤缃俊鎭寘澶у皬鐨勬洿澶氫俊鎭紝璇峰弬瑙A.2.9鑺傦紝鈥滀俊鎭寘杩囧ぇ鈥

         濡傛灉浣犵殑瀹㈡埛绔綆浜4.0.8鑰屼笖浣犵殑鏈嶅姟鍣ㄩ珮浜4.0.8锛屽綋浣犳帴鏀16MB鎴栨洿澶х殑淇℃伅鍖呮椂锛屽彲鑳戒細涓㈠け杩炴帴銆

         濡傛灉MySQL鏄敤鈥--skip-networking鈥濋夐」鍚姩鐨勶紝涔熶細瑙佸埌MySQL鏈嶅姟鍣ㄤ笉鍙敤閿欒銆

         浣犻亣鍒颁簡鎵ц鏌ヨ鏃舵湇鍔″櫒瀹曟満鐨勭己闄枫

閫氳繃鎵цmysqladmin version骞舵鏌ユ湇鍔″櫒鐨勬甯稿伐浣滄椂闂达紝鍙鏌ユ湇鍔″櫒鏄惁瀹曟満骞堕噸鍚傚鏋滃鎴风杩炴帴鏄洜mysqld宕╂簝鍜岄噸鍚屾柇寮鐨勶紝搴斿皢閲嶇偣鏀惧湪鏌ユ壘宕╂簝浣犳柟闈€傞鍏堝簲鍐嶆妫鏌ュ彂鍑虹殑鏌ヨ鏄惁鍐嶆鏉姝讳簡鏈嶅姟鍣ㄣ傝鍙傝A.4.2鑺傦紝鈥滃鏋淢ySQL渚濈劧宕╂簝锛屽簲浣滀簺浠涔堚

鐢ㄢ--log-warnings=2閫夐」鍚姩mysqld锛屽彲鑾峰緱鍏充簬杩炴帴鐨勬洿澶氫俊鎭傝繖鏍凤紝灏辫兘灏嗘煇浜涙柇寮杩炴帴閿欒璁板綍鍒hostname.err鏂囦欢涓銆傝鍙傝5.11.1鑺傦紝鈥滈敊璇棩蹇椻

濡傛灉浣犳墦绠楀垱寤轰笌璇ラ棶棰樻湁鍏崇殑缂洪櫡鎶ュ憡锛屽姟蹇呭寘鍚笅杩颁俊鎭細

1.    鎸囨槑MySQL鏈嶅姟鍣ㄦ槸鍚﹀畷鏈恒傞氳繃鏈嶅姟鍣ㄩ敊璇棩蹇楀彲鍙戠幇杩欐柟闈㈢殑淇℃伅銆傝鍙傝A.4.2鑺傦紝鈥滃鏋淢ySQL渚濈劧宕╂簝锛屽簲浣滀簺浠涔堚

2.    濡傛灉鐗瑰畾鏌ヨ鏉姝讳簡mysqld锛岃屼笖鍦ㄨ繍琛屾煡璇㈠墠鐢CHECK TABLE妫鏌ヤ簡娑夊強鐨勮〃锛屼綘鏄惁鑳芥彁渚涘彲閲嶅鐨勬祴璇曡寖渚嬶紵璇峰弬瑙E.1.6鑺傦紝鈥滃鏋滃嚭鐜拌〃宕╂簝锛岃鐢熸垚娴嬭瘯妗堜緥鈥

3.    MySQL鏈嶅姟鍣ㄤ腑锛岀郴缁熷彉閲wait_timeout鐨勫兼槸浠涔堬紵mysqladmin variables缁欏嚭浜嗚鍙橀噺鐨勫硷級銆

4.    浣犳槸鍚﹀皾璇曚娇鐢ㄢ--log鈥濋夐」鏉ヨ繍琛mysqld锛屼互纭畾鏄惁鍦ㄦ棩蹇椾腑鍑虹幇闂锛

鍙﹁鍙傝A.2.10鑺傦紝鈥滈氫俊閿欒鍜屽け鏁堣繛鎺モ

璇峰弬瑙1.7.1.2鑺傦紝鈥滆鏁欓棶棰樻垨閫氭姤缂洪櫡鈥

A.2.9.聽淇℃伅鍖呰繃澶

閫氫俊淇℃伅鍖呮槸鍙戦佽嚦MySQL鏈嶅姟鍣ㄧ殑鍗曚釜SQL璇彞锛屾垨鍙戦佽嚦瀹㈡埛绔殑鍗曚竴琛屻

MySQL 5.1鏈嶅姟鍣ㄥ拰瀹㈡埛绔箣闂存渶澶ц兘鍙戦佺殑鍙兘淇℃伅鍖呬负1GB

MySQL瀹㈡埛绔垨mysqld鏈嶅姟鍣ㄦ敹鍒板ぇ浜max_allowed_packet瀛楄妭鐨勪俊鎭寘鏃讹紝灏嗗彂鍑淇℃伅鍖呰繃澶閿欒锛屽苟鍏抽棴杩炴帴銆傚浜庢煇浜涘鎴风锛屽鏋滈氫俊淇℃伅鍖呰繃澶э紝鍦ㄦ墽琛屾煡璇㈡湡闂达紝浜嗚兘鍥為亣鍒涓㈠け涓MySQL鏈嶅姟鍣ㄧ殑杩炴帴閿欒銆

瀹㈡埛绔拰鏈嶅姟鍣ㄥ潎鏈夎嚜宸辩殑max_allowed_packet鍙橀噺锛鍥犳锛屽浣犳墦绠楀鐞嗗ぇ鐨勪俊鎭寘锛屽繀椤诲鍔犲鎴风鍜屾湇鍔″櫒涓婄殑璇ュ彉閲忋

濡傛灉浣犳鍦ㄤ娇鐢mysql瀹㈡埛绔▼搴忥紝鍏max_allowed_packet鍙橀噺鐨勯粯璁ゅ间负16MB銆傝鎯宠缃緝澶х殑鍊硷紝鍙敤涓嬭堪鏂瑰紡鍚姩mysql

mysql> mysql --max_allowed_packet=32M

瀹冨皢淇℃伅鍖呯殑澶у皬璁剧疆涓32MB

鏈嶅姟鍣ㄧ殑榛樿max_allowed_packet鍊间负1MB銆傚鏋滄湇鍔″櫒闇瑕佸鐞嗗ぇ鐨勬煡璇紝鍙鍔犺鍊硷紙渚嬪锛屽鏋滃噯澶囧鐞嗗ぇ鐨BLOB鍒楋級銆備緥濡傦紝瑕佹兂灏嗚璁剧疆涓16MB锛屽彲閲囩敤涓嬭堪鏂瑰紡鍚姩鏈嶅姟鍣細

mysql> mysqld --max_allowed_packet=16M

涔熻兘浣跨敤閫夐」鏂囦欢鏉ヨ缃max_allowed_packet瑕佹兂灏嗘湇鍔″櫒鐨勮鍙橀噺璁剧疆涓16MB锛屽彲鍦ㄩ夐」鏂囦欢涓鍔犱笅琛屽唴瀹癸細

[mysqld]
max_allowed_packet=16M

澧炲姞璇ュ彉閲忕殑鍊煎崄鍒嗗畨鍏紝杩欐槸鍥犱负浠呭綋闇瑕佹椂鎵嶄細鍒嗛厤棰濆鍐呭瓨銆備緥濡傦紝浠呭綋浣犲彂鍑洪暱鏌ヨ鎴mysqld蹇呴』杩斿洖澶х殑缁撴灉琛屾椂mysqld鎵嶄細鍒嗛厤鏇村鍐呭瓨銆傝鍙橀噺涔嬫墍浠ュ彇杈冨皬榛樿鍊兼槸涓绉嶉闃叉帾鏂斤紝浠ユ崟鑾峰鎴风鍜屾湇鍔″櫒涔嬮棿鐨勯敊璇俊鎭寘锛屽苟纭繚涓嶄細鍥犲伓鐒朵娇鐢ㄥぇ鐨勪俊鎭寘鑰屽鑷村唴瀛樻孩鍑恒

濡傛灉浣犳鏄敤澶х殑BLOB鍊硷紝鑰屼笖鏈负mysqld鎺堜簣涓哄鐞嗘煡璇㈣岃闂冻澶熷唴瀛樼殑鏉冮檺锛屼篃浼氶亣鍒颁笌澶т俊鎭寘鏈夊叧鐨勫鎬棶棰樸傚鏋滄鐤戝嚭鐜颁簡璇ユ儏鍐碉紝璇峰皾璇曞湪mysqld_safe鑴氭湰寮濮嬪鍔ulimit -d 256000锛屽苟閲嶅惎mysqld

A.2.10. 閫氫俊閿欒鍜屽け鏁堣繛鎺

瀵逛簬杩炴帴闂锛屾湇鍔″櫒閿欒鏃ュ織鏄湁鐢ㄧ殑淇℃伅婧愩傝鍙傝5.11.1鑺傦紝鈥滈敊璇棩蹇椻銆傚鏋滄湇鍔″櫒鏄敤鈥--log-warnings鈥濋夐」鍚姩鐨勶紝鍦ㄩ敊璇棩蹇椾腑鍙兘浼氬彂鐜颁笅杩版秷鎭細

010301 14:38:23  Aborted connection 854 to db: 'users' user: 'josh'

濡傛灉鈥Aborted connections鈥锛堟斁寮冭繛鎺ワ級娑堟伅鍑虹幇鍦ㄩ敊璇棩蹇椾腑锛屽彲鑳界殑鍘熷洜鏄細

1.    瀹㈡埛绔▼搴忓湪閫鍑轰箣鍓嶆湭璋冪敤mysql_close()

2.    瀹㈡埛绔殑绌洪棽鏃堕棿瓒呰繃wait_timeoutinteractive_timeout绉掞紝鏈悜鏈嶅姟鍣ㄥ彂鍑轰换浣曡姹傘璇峰弬瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥

3.    瀹㈡埛绔湪鏁版嵁浼犺緭涓旂獊鐒剁粨鏉熴

鍑虹幇杩欑被鎯呭喌鏃讹紝鏈嶅姟鍣ㄥ皢澧炲姞鈥Aborted_clients鈥锛堟斁寮冨鎴风锛夌姸鎬佸彉閲忋

鍑虹幇涓嬭堪鎯呭喌鏃讹紝鏈嶅姟鍣ㄥ皢澧炲姞鈥Aborted_clients鈥锛堟斁寮冨鎴风锛夌姸鎬佸彉閲忋

         瀹㈡埛绔笉鍏锋湁杩炴帴鑷虫暟鎹簱鐨勬潈闄愩

         瀹㈡埛绔噰鐢ㄤ簡涓嶆纭殑瀵嗙爜銆

         杩炴帴淇℃伅鍖呬笉鍚纭俊鎭

         鑾峰彇杩炴帴淇℃伅鍖呯殑鏃堕棿瓒呰繃connect_timeout绉掋璇峰弬瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥

濡傛灉鍑虹幇杩欑被鎯呭喌锛屽彲鑳借〃鏄庢煇浜烘璇曞浘渚靛叆浣犵殑鏈嶅姟鍣紒

瀵逛簬鏀惧純瀹㈡埛绔垨鏀惧純杩炴帴闂锛屽叾浠栧彲鑳界殑婧愬簲鍖呮嫭锛

         Linux涓璧蜂娇鐢ㄤ互澶綉鍗忚锛屽崐鍙屽伐鎴栧叏鍙屽伐銆傚緢澶Linux浠ュお缃戦┍鍔ㄥ潎瀛樺湪璇ョ己闄枫傚簲閫氳繃FTP鍦ㄥ鎴风鍜屾湇鍔″櫒鏈哄櫒涔嬮棿浼犺緭澶ф枃浠舵潵娴嬭瘯璇ョ己闄枫傚鏋滀紶杈撳浜burst-pause-burst-pause锛堢垎鍙-鏆傚仠-鐖嗗彂-鏆傚仠锛夋ā寮忥紝琛ㄦ槑浣犻亣鍒颁簡Linux鍙屽伐鏁呴殰銆傚敮涓鐨勮В鍐虫柟娉曟槸锛屽皢缃戝崱鍜Hub/浜ゆ崲鍣ㄧ殑鍙屽伐妯″紡鍒囨崲涓哄叏鍙屽伐鎴栧崐鍙屽伐锛屽苟瀵圭粨鏋滆繘琛屾祴璇曚互纭畾鏈浣宠缃

         涓庣嚎绋嬪簱鏈夊叧鐨勬煇浜涢棶棰樺鑷磋鍙栦腑鏂

         閰嶇疆涓嶈壇鐨TCP/IP

         鏈夐棶棰樼殑浠ュお缃戙Hub銆佷氦鎹㈠櫒銆佺數缂嗙瓑銆備粎鑳介氳繃鏇存崲纭欢鎵嶈兘鎭板綋璇婃柇銆

         鍙橀噺max_allowed_packet杩囧皬鎴鏌ヨ瑕佹眰鐨勫唴瀛樿秴杩囦负mysqld鍒嗛厤鐨勫唴瀛樸傝鍙傝A.2.9鑺傦紝鈥滀俊鎭寘杩囧ぇ鈥

鍙﹁鍙傝A.2.8鑺傦紝鈥淢ySQL鏈嶅姟鍣ㄤ笉鍙敤鈥

A.2.11.聽琛ㄥ凡婊

琛ㄥ凡婊¢敊璇嚭鐜扮殑鏂瑰紡鏈夋暟绉嶏細

         浣犳鍦ㄤ娇鐢ㄤ綆浜3.23鐗堢殑MySQL鏈嶅姟鍣紝鑰屼笖鈥滃唴瀛樹腑鈥濅复鏃惰〃瓒呰繃浜tmp_table_size瀛楄妭銆瑕佹兂閬垮厤璇ラ棶棰橈紝鍙娇鐢ㄢ-O tmp_table_size=val鈥濋夐」浠ヤ究mysqld澧炲姞涓存椂琛ㄧ殑澶у皬锛屾垨鍦ㄥ彂鍑烘湁闂鐨勬煡璇箣鍓嶏紝浣跨敤SQL閫夐」SQL_BIG_TABLES璇峰弬瑙13.5.3鑺傦紝鈥淪ET璇硶鈥

涔熷彲浠ヤ娇鐢ㄢ--big-tables鈥濋夐」鍚姩mysqld銆傚畠涓庝娇鐢ㄩ拡瀵规墍鏈夋煡璇㈢殑SQL_BIG_TABLES瀹屽叏鐩稿悓銆

MySQL 3.23璧凤紝璇ラ棶棰樺簲涓嶅啀鍑虹幇銆傚鏋溾滃唴瀛樹腑鈥濅复鏃惰〃瓒呰繃tmp_table_size锛屾湇鍔″櫒浼氳嚜鍔ㄥ皢鍏惰浆鎹负鍩轰簬纾佺洏鐨MyISAM琛ㄣ

         浣犳鍦ㄤ娇鐢InnoDB琛紝骞惰秴鍑轰簡InnoDB琛ㄧ┖闂淬鍦ㄨ鎯呭喌涓嬶紝瑙e喅鏂规硶鏄鍔InnoDB琛ㄧ┖闂淬傝鍙傝15.2.7鑺傦紝鈥滄坊鍔犲拰鍒犻櫎InnoDB鏁版嵁鍜屾棩蹇楁枃浠

         浣犳鍦ㄤ粎鏀寔2GB鏂囦欢鐨勬搷浣滅郴缁熶笂浣跨敤ISAMMyISAM琛紝鏁版嵁鏂囦欢鎴栫储寮曟枃浠惰揪鍒颁簡璇ラ檺鍒跺笺

         浣犳鍦ㄤ娇鐢MyISAM琛紝鑰屼笖琛ㄦ墍闇鐨勭┖闂磋秴杩囧唴閮ㄦ寚閽堝厑璁哥殑澶у皬銆傚鏋滃湪鍒涘缓琛ㄦ椂鏈寚瀹MAX_ROWS琛紝MySQL灏嗕娇鐢myisam_data_pointer_size绯荤粺鍙橀噺銆榛樿鍊间负6瀛楄妭锛屽畠瓒充互瀹圭撼65536TB鏁版嵁銆傝鍙傝5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥

浣跨敤璇ヨ鍙ワ紝鍙鏌ユ渶澶ф暟鎹/绱㈠紩澶у皬锛

SHOW TABLE STATUS FROM database LIKE 'tbl_name';

涔熷彲浠ヤ娇鐢myisamchk -dv /path/to/table-index-file

濡傛灉鎸囬拡澶у皬杩囧皬锛屽彲浣跨敤ALTER TABLE鏇存璇ラ棶棰橈細

ALTER TABLE tbl_name MAX_ROWS=1000000000 AVG_ROW_LENGTH=nnn;

浠呭簲涓哄叿鏈BLOBTEXT鍒楃殑琛ㄦ寚瀹AVG_ROW_LENGTH鍦ㄨ鎯呭喌涓嬶紝MySQL涓嶈兘浠呮牴鎹鏁颁紭鍖栨墍闇鐨勭┖闂淬

A.2.12.聽鏃犳硶鍒涘缓鏂囦欢锛忓啓鍏ユ枃浠

濡傛灉瀵规煇浜涙煡璇㈤亣鍒颁笅杩扮被鍨嬬殑閿欒锛屽畠鎰忓懗鐫MySQL涓嶈兘涓轰复鏃剁洰褰曚笅鐨勭粨鏋滈泦鍒涘缓涓存椂鏂囦欢锛

鏃犳硶鍒涘缓/鍐欏叆鏂囦欢'\\sqla3fe_0.ism'

鍓嶈堪閿欒鏄Windows骞冲彴涓婄殑鍏稿瀷娑堟伅锛Unix骞冲彴涓婄殑娑堟伅涓庝箣绫讳技銆

涓绉嶆洿姝f柟寮忔槸浣跨敤鈥--tmpdir鈥濋夐」鍚姩mysqld锛屾垨鍦ㄩ夐」鏂囦欢鐨[mysqld]閮ㄥ垎澧炲姞璇ラ夐」銆渚嬪锛岃鎯虫寚瀹氱洰褰C:\temp锛屽彲浣跨敤锛

[mysqld]
tmpdir=C:/temp

鐩綍C:\temp蹇呴』瀛樺湪锛骞舵湁瓒冲鐨勭┖闂村厑璁MySQL鍐欏叆瀹冦傝鍙傝4.3.2鑺傦紝鈥滀娇鐢ㄩ夐」鏂囦欢鈥

璇ラ敊璇殑鍙︿竴涓師鍥犲彲鑳芥槸璁稿彲浜嬪疁銆傝纭MySQL鏈嶅姟鍣ㄨ兘澶熷啓鍏tmpdir鐩綍銆

姝ゅ锛岃繕鐢ㄤ娇鐢perror妫鏌ラ敊璇唬鐮併傛湇鍔″櫒鏃犳硶鍐欏叆琛ㄧ殑涓涓師鍥犳槸鏂囦欢绯荤粺宸叉弧銆

shell> perror 28
閿欒浠g爜28锛氱鐩樹笂鏃犲墿浣欑┖闂淬

A.2.13.聽鍛戒护涓嶅悓姝

濡傛灉閬囧埌鈥滃懡浠や笉鍚屾鈥濋敊璇紝灏嗘棤娉曞湪浣犵殑瀹㈡埛绔唬鐮佷腑杩愯璇ュ懡浠わ紝浣犳鍦ㄤ互閿欒椤哄簭璋冪敤瀹㈡埛绔嚱鏁般

渚嬪锛屽鏋滀綘姝d娇鐢mysql_use_result()锛屽苟鎵撶畻鍦ㄨ皟鐢mysql_free_result()涔嬪墠鎵ц鏂版煡璇紝灏变細鍑虹幇璇ラ棶棰樸傚鏋滀綘璇曞浘鎵ц涓ゆ鏌ヨ锛屼絾骞舵湭鍦ㄤ袱娆℃煡璇箣闂磋皟鐢mysql_use_result()mysql_store_result()锛屼篃浼氬嚭鐜拌闂銆

A.2.14.聽蹇界暐鐢ㄦ埛

濡傛灉閬囧埌涓嬭堪閿欒锛岃〃绀哄綋鍚姩mysqld鏃舵垨閲嶆柊鍔犺浇鎺堟潈琛ㄦ椂锛屽湪鐢ㄦ埛琛ㄤ腑鍙戠幇鍏锋湁闈炴硶瀵嗙爜鐨勮处鎴枫

鍙戠幇鐢ㄦ埛'some_user'@'some_host'瀵嗙爜閿欒锛氬拷鐣ョ敤鎴枫

浣滀负鍏剁粨鏋滐紝璁稿彲绯荤粺灏嗙畝鍗曞拷鐣ヨ处鎴枫

鍦ㄤ笅闈㈢殑浠嬬粛涓紝鎸囨槑浜嗗彲鑳界殑鍘熷洜鍜岄棶棰樼殑鏇存鎺柦锛

1.    鎴栬锛屼綘姝f墦绠楃敤鏃х殑鐢ㄦ埛琛ㄨ繍琛屾柊鐗堟湰鐨mysqld銆傛墽琛mysqlshow mysql user妫鏌Password锛堝瘑鐮侊級鍒楁槸鍚︾煭浜16涓瓧绗︼紝閫氳繃璇ユ柟寮忓彲妫鏌ヨ闂銆傚鏋滅粨鏋滄槸鑲畾鐨勶紝鍙繍琛岃剼鏈/add_long_password鑴氭湰鏇存璇ラ棶棰樸

2.    璐︽埛鍏锋湁鏃х殑瀵嗙爜锛8瀛楃闀匡級锛岃屼笖鏈娇鐢ㄢ--old-protocol鈥濋夐」鍚姩mysqld銆傛洿鏂扮敤鎴疯〃涓殑璐︽埛锛屼娇涔嬪叿鏈夋柊鐨勫瘑鐮侊紝鎴栦娇鐢ㄢ--old-protocol鈥濋夐」閲嶅惎mysqld

3.    鍦ㄧ敤鎴疯〃涓湭浣跨敤PASSWORD()鍑芥暟鎸囧畾浜嗗瘑鐮併浣跨敤mysql鐢ㄦ柊瀵嗙爜鏇存柊鐢ㄦ埛琛ㄤ腑鐨勮处鎴凤紝鍔″繀浣跨敤PASSWORD()鍑芥暟锛

4.           mysql> UPDATE user SET Password=PASSWORD('newpwd')
5.               -> WHERE User='some_user' AND Host='some_host';

A.2.15.聽琛╰bl_name涓嶅瓨鍦

濡傛灉閬囧埌涓嬭堪閿欒涔嬩竴锛岄氬父鎰忓懗鐫褰撳墠鏁版嵁搴撲腑涓嶅瓨鍦ㄥ叿鏈夌粰瀹氬悕绉扮殑琛細

'tbl_name'涓嶅瓨鍦
鏃犳硶鎵惧埌鏂囦欢锛'tbl_name' (errno: 2)

鍦ㄦ煇浜涙儏鍐典笅锛岃〃鎴栬瀛樺湪锛屼絾鏈纭紩鐢ㄥ畠锛

         鐢变簬MySQL浣跨敤鐩綍鍜屾枃浠舵潵淇濆瓨鏁版嵁搴撳拰琛紝濡傛灉瀹冧滑浣嶄簬鍖哄垎鏂囦欢鍚嶅ぇ灏忓啓鐨勬枃浠剁郴缁熶笂锛屾暟鎹簱鍜岃〃鍚嶄篃鍖哄垎鏂囦欢澶у皬鍐欍

         鍗充娇瀵逛簬涓嶅尯鍒嗗ぇ灏忓啓鐨勬枃浠剁郴缁燂紝濡Windows锛屽湪鏌ヨ鍐呭缁欏畾琛ㄧ殑鎵鏈夊紩鐢ㄥ繀椤讳娇鐢ㄧ浉鍚岀殑澶у皬鍐欍

鍙互浣跨敤SHOW TABLES妫鏌ヤ綅浜庡綋鍓嶆暟鎹簱涓殑琛銆傝鍙傝13.5.4鑺傦紝鈥淪HOW璇硶鈥

A.2.16.聽鏃犳硶鍒濆鍖栧瓧绗﹂泦

濡傛灉瀛樺湪瀛楃闆嗛棶棰橈紝鍙兘浼氶亣鍒颁笅杩伴敊璇細

MySQL杩炴帴澶辫触锛氭棤娉曞垵濮嬪寲瀛楃闆charset_name

瀵艰嚧璇ラ敊璇殑鍘熷洜锛

         瀛楃闆嗕负澶氬瓧鑺傚瓧绗﹂泦锛屼絾瀹㈡埛绔笉鏀寔璇ュ瓧绗﹂泦銆傚湪璇ユ儏鍐典笅锛岄渶瑕佷娇鐢ㄢ--with-charset=charset_name鈥濇垨鈥--with-extra-charsets=charset_name鈥濋夐」杩愯configure浠ラ噸鏂扮紪璇戝鎴风銆傝鍙傝2.8.2鑺傦紝鈥滃吀鍨嬮厤缃夐」

鎵鏈夌殑鏍囧噯MySQL浜岃繘鍒舵枃浠跺潎鏄噰鐢ㄢ--with-extra-character-sets=complex鈥濈紪璇戠殑锛岃兘澶熸敮鎸佹墍鏈夌殑澶氬瓧鑺傚瓧绗﹂泦銆傝鍙傝5.10.1鑺傦紝鈥滄暟鎹拰鎺掑簭鐢ㄥ瓧绗﹂泦鈥

         瀛楃闆嗘槸鏈紪璇戝埌mysqld涓殑绠鍗曞瓧绗﹂泦锛岃屼笖瀛楃闆嗗畾涔夋枃浠朵笉鍦ㄥ鎴风棰勬湡鐨勪綅缃

鍦ㄨ鎯呭喌涓嬶紝闇瑕侀噰鍙栦笅杩版柟娉曚箣涓瑙e喅闂锛

1.    閲嶆柊缂栬瘧瀹㈡埛绔紝浣夸箣鏀寔瀛楃闆嗐傝鍙傝2.8.2鑺傦紝鈥滃吀鍨嬮厤缃夐」

2.    涓哄鎴风鎸囧畾瀛楃闆嗗畾涔夋枃浠舵墍鍦ㄧ殑鐩綍銆傚浜庡緢澶氬鎴风锛屽彲浣跨敤鈥--character-sets-dir鈥濋夐」瀹屾垚璇ヤ换鍔°

3.    灏嗗瓧绗﹂泦瀹氫箟鏂囦欢澶嶅埗鍒板鎴风棰勬湡鐨勪綅缃

A.2.17. 鏂囦欢鏈壘鍒

濡傛灉閬囧埌鈥ERROR '...'鏈彂鐜(errno: 23)鈥鏃犳硶鎵撳紑鏂囦欢锛... (errno: 24)鈥锛屾垨鏉ヨ嚜MySQL鐨勫叿鏈errno 23errno 24鐨勫叾瀹冮敊璇紝瀹冭〃绀烘湭涓MySQL鏈嶅姟鍣ㄥ垎閰嶈冻澶熺殑鏂囦欢鎻忚堪绗銆備綘鍙互浣跨敤perror瀹炵敤宸ュ叿鏉ヤ簡瑙i敊璇紪鍙风殑鍚箟锛

shell> perror 23
閿欒浠g爜23锛氭枃浠惰〃婧㈠嚭
shell> perror 24
閿欒浠g爜24锛氭墦寮鏂囦欢杩囧
shell> perror 11
閿欒浠g爜11锛氳祫婧愭殏鏃朵笉鍙敤

杩欓噷鐨勯棶棰樻槸锛mysqld姝h瘯鍥惧悓鏃舵墦寮杩囧鐨勬枃浠躲備綘鍙互閫氱煡mysqld涓嶈涓娆℃墦寮杩囧鏂囦欢锛屾垨澧炲姞mysqld鍙敤鏂囦欢鎻忚堪绗︾殑鏁扮洰銆

瑕佹兂閫氱煡mysqld灏嗕竴娆℃墦寮鐨勬枃浠舵帶鍒跺湪杈冨皬鐨勬暟鐩笂锛屽彲闄嶄綆table_cache绯荤粺鍙橀噺鐨勫硷紙锛夛紝浠庤屽噺灏戣〃楂橀熺紦鍐诧紙榛樿鍊间负64锛夈傞檷浣max_connections鐨勫间篃鑳介檷浣鎵撳紑鏂囦欢鐨勬暟鐩紙榛樿鍊间负100锛夈

瑕佹兂鏇存敼mysqld鍙敤鐨勬枃浠舵弿杩扮鐨勬暟鐩紝鍙湪mysqld_safe涓婁娇鐢ㄢ--open-files-limit鈥閫夐」鎴栬缃锛堣嚜MySQL 3.23.30寮濮嬶級open_files_limit绯荤粺鍙橀噺銆傝鍙傝5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥銆傝缃繖浜涘肩殑鏈绠鍗曟柟寮忔槸鍦ㄩ夐」鏂囦欢涓鍔1涓夐」銆傝鍙傝4.3.2鑺傦紝鈥滀娇鐢ㄩ夐」鏂囦欢鈥銆傚鏋mysqld鐨勭増鏈緝浣庯紝涓嶆敮鎸佽缃墦寮鏂囦欢鐨勬暟鐩紝鍙紪杈mysqld_safe鑴氭湰銆傚湪鑴氭湰涓湁1涓敞閲婃帀鐨勮ulimit -n 256銆備綘鍙互鍒犻櫎#鈥欏瓧绗﹀彇娑堝璇ヨ鐨勬敞閲婏紝鏇存敼鏁板256锛屼互璁剧疆mysqld鍙敤鐨勬枃浠舵弿杩扮鏁扮洰銆

鈥--open-files-limit鈥ulimit鑳藉澧炲姞鏂囦欢鎻忚堪绗︾殑鏁扮洰锛屼絾鏈楂樹笉鑳借秴杩囨搷浣滅郴缁熼檺鍒剁殑鏁扮洰銆傛澶栬繕鏈1涓滅‖鈥濋檺鍒讹紝浠呭綋浠ユ牴鐢ㄦ埛韬唤鍚姩mysqld_safemysqld鏃舵墠鑳借鐩栧畠锛堣璁颁綇锛屽湪璇ユ儏鍐典笅锛岃繕闇浣跨敤鈥--user鈥濋夐」鍚姩鏈嶅姟鍣紝浠ヤ究鍦ㄥ惎鍔ㄥ悗涓嶅啀浠ユ牴鐢ㄦ埛韬唤缁х画杩愯锛夈傚鏋滈渶瑕佸鍔犳搷浣滅郴缁熼檺鍒剁殑瀵瑰悇杩涚▼鍙敤鏂囦欢鎻忚堪绗︾殑鏁扮洰锛岃鍙傞槄绯荤粺鏂囨。銆

娉ㄩ噴锛濡傛灉杩愯tcsh shellulimit涓嶅伐浣滐紒璇锋眰褰撳墠闄愬埗鍊兼椂锛tcsh杩樿兘閫氭姤涓嶆纭殑鍊笺傚湪璇ユ儏鍐典笅锛屽簲浣跨敤sh鍚姩mysqld_safe

A.3.聽涓庡畨瑁呮湁鍏崇殑浜嬪疁

A.3.1. 涓嶮ySQL瀹㈡埛绔簱鐨勯摼鎺ラ棶棰

褰撲綘閾炬帴鍒板簲鐢ㄧ▼搴忎互浣跨敤MySQL瀹㈡埛绔簱鏃讹紝鍙兘浼氶亣鍒颁互mysql_寮濮嬬殑鏈畾涔夊紩鐢ㄩ敊璇紝濡備笅鎵绀猴細

/tmp/ccFKsdPa.o: 鍦ㄥ嚱鏁`main':
/tmp/ccFKsdPa.o(.text+0xb): `mysql_init'鐨勬湭瀹氫箟寮曠敤銆
/tmp/ccFKsdPa.o(.text+0x31): `mysql_real_connect'鐨勬湭瀹氫箟寮曠敤銆
/tmp/ccFKsdPa.o(.text+0x57): `mysql_real_connect'鐨勬湭瀹氫箟寮曠敤銆
/tmp/ccFKsdPa.o(.text+0x69): `mysql_error'鐨勬湭瀹氫箟寮曠敤銆
/tmp/ccFKsdPa.o(.text+0x9a): `mysql_close'鐨勬湭瀹氫箟寮曠敤銆

閫氳繃鍦ㄩ摼鎺ュ懡浠ゅ悗澧炲姞鈥-Ldir_path -lmysqlclient鈥濋夐」锛屽簲鑳借В鍐宠闂锛屽叾涓紝dir_path浠h〃瀹㈡埛绔簱鎵鍦ㄧ洰褰曠殑璺緞鍚嶃傝鎯崇‘瀹氭纭殑鐩綍锛屽彲灏濊瘯涓嬭堪鍛戒护锛

shell> mysql_config --libs

鏉ヨ嚜mysql_config鐨勮緭鍑哄彲鑳戒細鎸囨槑搴斿湪閾炬帴鍛戒护涓婃寚瀹氱殑鍏朵粬搴撱

瀵逛簬闈炲帇缂╂垨鍘嬬缉鍑芥暟锛屽鏋滈亣鍒版湭瀹氫箟寮曠敤閿欒锛屽彲鍦ㄩ摼鎺ュ懡浠ゅ悗娣诲姞-lz锛屽苟鍐嶆灏濊瘯銆

瀵逛簬搴斿湪绯荤粺涓婂瓨鍦ㄧ殑鍑芥暟锛堝connect锛夛紝濡傛灉閬囧埌鏈畾涔夊紩鐢ㄩ敊璇紝璇锋鏌ョ浉鍏冲嚱鏁扮殑鎵嬪唽椤碉紝浠ヤ究纭畾搴斿湪閾炬帴鍛戒护涓婂鍔犲摢浜涘簱銆

瀵逛簬绯荤粺涓婁笉瀛樺湪鐨勫嚱鏁帮紝鍙兘浼氶亣鍒版湭瀹氫箟寮曠敤閿欒锛屽涓嬫墍绀猴細

mf_format.o(.text+0x201): `__lxstat'鐨勬湭瀹氫箟寮曠敤銆

瀹冮氬父鎰忓懗鐫浣犵殑MySQL瀹㈡埛绔簱鏄湪涓庝綘鐨勭郴缁熶笉100锛呭吋瀹圭殑绯荤粺涓婄紪璇戠殑銆傚湪璇ユ儏鍐典笅锛屽簲涓嬭浇鏈鏂扮殑MySQL婧愮爜鍒嗗彂鐗堬紝骞惰嚜宸辩紪璇MySQL銆傝鍙傝2.8鑺傦紝鈥滀娇鐢ㄦ簮鐮佸垎鍙戠増瀹夎MySQL鈥

褰撲綘璇曞浘鎵цMySQL绋嬪簭鏃讹紝鍙兘浼氶亣鍒拌繍琛屾椂鏈畾涔夊紩鐢ㄩ敊璇傚鏋滆繖绫婚敊璇寚鏄庝簡浠mysql_寮濮嬬殑绗﹀彿锛鎴栨寚鏄庢湭鍙戠幇mysqlclient锛岃繖鎰忓懗鐫浣犵殑绯荤粺鏃犳硶鎵惧埌鍏变韩鐨libmysqlclient.so搴撱瀵瑰叾鐨勬洿姝f柟寮忔槸锛岄氱煡绯荤粺鍦ㄥ簱鎵鍦ㄤ綅缃悳绱㈠叡浜簱銆傝浣跨敤涓庣郴缁熺浉閫傚簲鐨勪笅杩版柟娉曪細

         libmysqlclient.so鎵鍦ㄧ洰褰曠殑璺緞娣诲姞鍒LD_LIBRARY_PATH鐜鍙橀噺涓

         libmysqlclient.so鎵鍦ㄧ洰褰曠殑璺緞娣诲姞鍒LD_LIBRARY鐜鍙橀噺涓

         libmysqlclient.so鎷疯礉鍒板彲琚郴缁熸悳绱㈢殑鐩綍涓嬶紝濡/lib锛岀劧鍚庨氳繃鎵цldconfig鏇存柊鍏变韩搴撲俊鎭

瑙e喅璇ラ棶棰樼殑鍙︿竴绉嶆柟娉曟槸锛屼互闈欐佹柟寮忓皢浣犵殑绋嬪簭涓庘-static鈥濋夐」閾炬帴鍦ㄤ竴璧凤紝鎴栧湪閾炬帴浠g爜涔嬪墠鍒犻櫎鍔ㄦMySQL搴撱備娇鐢ㄧ2绉嶆柟娉曚箣鍓嶏紝搴旂‘淇濇病鏈変娇鐢ㄥ姩鎬佸簱鐨勫叾瀹冪▼搴忋

A.3.2. 濡備綍浠ユ櫘閫氱敤鎴疯韩浠借繍琛孧ySQL

Windows骞冲彴涓婏紝鑳藉浣跨敤鏅氱敤鎴疯处鎴蜂互Windows鏈嶅姟鏂瑰紡杩愯鏈嶅姟鍣ㄣ

Unix骞冲彴涓婏紝涓嶆槸浠讳綍鐢ㄦ埛閮借兘鍚姩骞惰繍琛MySQL鏈嶅姟鍣mysqld銆備絾鏄紝澶勪簬瀹夊叏鏂归潰鐨勫師鍥狅紝搴旈伩鍏嶄互Unix鏍圭敤鎴疯韩浠借繍琛屾湇鍔″櫒銆傝鎯虫洿鏀mysqld锛屼娇涔嬭兘浠ユ甯哥殑鏃犵壒鏉Unix鐢ㄦ埛user_name韬唤杩愯锛屽繀椤婚噰鍙栦笅杩版楠わ細

濡傛灉鏈嶅姟鍣ㄦ鍦ㄨ繍琛岋紝鍋滄瀹冿紙浣跨敤mysqladmin shutdown锛夈

鏇存敼鏁版嵁搴撶洰褰曞拰鏂囦欢锛屽厑璁user_name璇诲啓鍏朵腑鐨勬枃浠讹紙鍙兘闇瑕佷互Unix鏍圭敤鎴疯韩浠藉畬鎴愯繖绫昏缃級锛

shell> chown -R user_name /path/to/mysql/datadir

濡傛灉鏈繖鏍峰仛锛屽綋浠user_name韬唤杩愯鏃讹紝鏈嶅姟鍣ㄦ棤娉曡闂暟鎹簱鎴栬〃銆

濡傛灉MySQL鏁版嵁鐩綍涓嬬殑鐩綍鎴栨枃浠堕噰鐢ㄧ殑鏄鍙烽摼鎺ワ紝杩橀渶璺熻釜杩欎簺閾炬帴锛屽苟鏇存敼瀹冧滑鎸囧悜鐨勭洰褰曞拰鏂囦欢銆chown 鈥揜鍙府鍔╀綘璺熻釜绗﹀彿閾炬帴銆

user_name韬唤鍚姩鏈嶅姟鍣ㄣ傚鏋滀綘姝e湪浣跨敤MySQL 3.22鎴栨洿楂樼増鏈紝鍙︿竴绉嶅彲閫夋柟寮忔槸锛屼互Unix鏍圭敤鎴疯韩浠藉惎鍔mysqld锛屽苟浣跨敤--user=user_name閫夐」銆mysqld鍚姩锛岀劧鍚庡湪鎺ュ彈浠讳綍杩炴帴鍓嶏紝鍒囨崲鑷Unix鐢ㄦ埛user_name骞朵互璇ョ敤鎴疯韩浠借繍琛屻

瑕佹兂鍦ㄧ郴缁熷惎鍔ㄦ椂鑷姩浠ョ粰瀹氱殑鐢ㄦ埛韬唤鍚姩鏈嶅姟鍣紝鍙湪鏈嶅姟鍣ㄦ暟鎹洰褰曚笅鐨/etc/my.cnf閫夐」鏂囦欢鎴my.cnf閫夐」鏂囦欢鐨[mysqld]缁勪腑锛岄氳繃澧炲姞鐢ㄦ埛閫夐」鏉ユ寚瀹氱敤鎴峰悕銆銆備緥濡傦細

[mysqld]
user=user_name

濡傛灉浣犵殑Unix鏈哄櫒鏈韩骞朵笉瀹夊叏锛屽簲鍦ㄦ巿鏉冭〃涓负MySQL鏍硅处鎴锋寚瀹氬瘑鐮併傚涓嶇劧锛屼换浣曞湪璇ユ満鍣ㄤ笂鍏锋湁鐧诲綍璐︽埛鐨勭敤鎴烽兘鑳戒娇鐢ㄢ--user=root鈥濋夐」杩愯mysql瀹㈡埛绔紝骞舵墽琛屼换浣曟搷浣溿傚湪浠讳綍鎯呭喌涓嬪潎搴斾负MySQL璐︽埛鎸囧畾瀵嗙爜锛岃繖鏄釜濂戒富鎰忥紝灏ゅ叾鏄湪鏈嶅姟鍣ㄤ富鏈轰笂瀛樺湪鍏朵粬鐧诲綍璐︽埛鏃讹紝鏇存槸濡傛銆傝鍙傝2.9鑺傦紝鈥滃畨瑁呭悗鐨勮缃拰娴嬭瘯鈥

A.3.3. 涓庢枃浠惰鍙湁鍏崇殑闂

濡傛灉閬囧埌涓庢枃浠惰鍙湁鍏崇殑闂锛屽彲鑳芥暟鍚姩mysqldUMASK鐜鍙橀噺璁剧疆寰椾笉姝g‘銆備緥濡傦紝褰撲綘鍒涘缓琛ㄦ椂锛MySQL鍙兘浼氬彂鍑轰笅杩伴敊璇秷鎭細

ERROR: 鏃犳硶鎵惧埌鏂囦欢锛'path/with/filename.frm' (Errcode: 13)

UMASK鐨勯粯璁ゅ兼槸0660銆傞氳繃涓嬭堪鏂瑰紡鍚姩mysqld_safe锛屽彲鏀瑰彉璇ユ儏鍐碉細

shell> UMASK=384  # = 600 in octal
shell> export UMASK
shell> mysqld_safe &

鍦ㄩ粯璁ゆ儏鍐典笅锛MySQL0700鐨勮鍙垱寤烘暟鎹簱鍜RAID鐩綍銆備綘鍙互閫氳繃璁剧疆UMASK_DIR鍙橀噺鏇存敼璇ヨ涓恒傚鏋滀綘璁剧疆浜嗗畠鐨勫硷紝灏嗕娇鐢ㄧ粍鍚堢殑UMASKUMASK_DIR鍊煎垱寤烘柊鐩綍銆備緥濡锛屽鏋滀綘鎵撶畻涓烘墍鏈夋柊鐨勭洰褰曟巿浜堢粍璁块棶鏉冮檺锛屽彲锛

shell> UMASK_DIR=504  # = 770 in octal
shell> export UMASK_DIR
shell> mysqld_safe &

MySQL 3.23.25鍜屾洿楂樼増鏈腑锛屽鏋滄槸浠0寮濮嬬殑锛MySQL灏嗚涓UMASKUMASK_DIR鐨勫煎潎閲囩敤鍏繘鍒跺舰寮忋

璇峰弬瑙闄勫綍F锛鐜鍙橀噺

A.4. 涓庣鐞嗘湁鍏崇殑浜嬪疁

A.4.1. 濡備綍澶嶄綅鏍圭敤鎴峰瘑鐮

濡傛灉浣犱粠鏈负MySQL璁剧疆鏍圭敤鎴峰瘑鐮侊紝鏈嶅姟鍣ㄥ湪浠ユ牴鐢ㄦ埛韬唤杩涜杩炴帴鏃朵笉闇瑕佸瘑鐮併備絾鏄紝寤鸿浣犱负姣忎釜璐︽埛璁剧疆瀵嗙爜銆傝鍙傝5.6.1鑺傦紝鈥滈氱敤瀹夊叏鎸囧崡鈥

濡傛灉浣犱互鍓嶈缃簡鏍圭敤鎴峰瘑鐮侊紝浣嗗嵈蹇樿浜嗚瀵嗙爜锛屽彲璁剧疆鏂扮殑瀵嗙爜銆備笅杩版楠ゆ槸閽堝Windows骞冲彴鐨勩傚湪鏈妭鍚庨潰鐨勫唴瀹逛腑锛屼粙缁嶄簡閽堝Unix骞冲彴鐨勬楠ゃ

Windows骞冲彴涓嬶紝璇ユ楠ゆ槸锛

浠ョ郴缁熺鐞嗗憳韬唤鐧诲綍鍒扮郴缁熴

濡傛灉MySQL鏈嶅姟鍣ㄦ鍦ㄨ繍琛岋紝鍋滄瀹冦傚浜庝綔涓Windows鏈嶅姟杩愯鐨勬湇鍔″櫒锛岃繘鍏ユ湇鍔$鐞嗗櫒锛

寮濮嬭彍鍗->鎺у埗闈㈡澘->绠$悊宸ュ叿->鏈嶅姟

鐒跺悗鍦ㄥ垪琛ㄤ腑鎵惧嚭MySQL鏈嶅姟鍣紝骞跺仠姝㈠畠銆

濡傛灉鏈嶅姟鍣ㄤ笉鏄綔涓烘湇鍔¤岃繍琛岀殑锛屽彲鑳介渶瑕佷娇鐢ㄤ换鍔$鐞嗗櫒鏉ュ己鍒跺仠姝㈠畠銆

鍒涘缓1涓枃鏈枃浠讹紝骞跺皢涓嬭堪鍛戒护缃簬鍗曚竴琛屼腑锛

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPassword');

鐢ㄤ换鎰忓悕绉颁繚瀛樿鏂囦欢銆傚湪鏈緥涓紝璇ユ枃浠朵负C:\mysql-init.txt

鎵撳紑鎺у埗鍙扮獥鍙o紝杩涘叆DOS鍛戒护鎻愮ず锛

寮濮嬭彍鍗->杩愯-> cmd

鍋囧畾浣犲凡灏MySQL瀹夎鍒C:\mysql銆傚鏋滀綘灏MySQL瀹夎鍒颁簡鍙︿竴浣嶇疆锛岃瀵逛笅杩板懡浠よ繘琛岀浉搴旂殑璋冩暣銆

DOS鍛戒护鎻愮ず绗︿笅锛屾墽琛屽懡浠わ細

C:\> C:\mysql\bin\mysqld-nt --init-file=C:\mysql-init.txt

鍦ㄦ湇鍔″櫒鍚姩鏃讹紝鎵ц鐢扁--init-file鈥濋夐」鍛藉悕鐨勬枃浠剁殑鍐呭锛屾洿鏀规牴鐢ㄦ埛瀵嗙爜銆傚綋鏈嶅姟鍣ㄦ垚鍔熷惎鍔ㄥ悗锛屽簲鍒犻櫎C:\mysql-init.txt

濡傛灉浣犱娇鐢MySQL瀹夎鍚戝瀹夎浜MySQL锛屾垨璁搁渶瑕佹寚瀹氣--defaults-file鈥閫夐」锛

C:\> C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld-nt.exe
         --defaults-file="C:\Program Files\MySQL\MySQL Server 5.1\my.ini"
         --init-file=C:\mysql-init.txt

浣跨敤鏈嶅姟绠$悊鍣紝鍙壘鍒版伆褰撶殑鈥--defaults-file鈥璁剧疆锛

寮濮嬭彍鍗->鎺у埗闈㈡澘->绠$悊宸ュ叿->鏈嶅姟

鍦ㄥ垪琛ㄤ腑鎵惧嚭MySQL鏈嶅姟锛屽彸鍑伙紝骞堕夋嫨鈥滃睘鎬р濋夐」銆傚湪鍙墽琛屽瓧娈电殑Path锛堣矾寰勶級涓寘鍚鈥--defaults-file鈥璁剧疆銆

鍋滄MySQL鏈嶅姟鍣紝鐒跺悗鍦ㄦ甯告ā寮忎笅閲嶅惎瀹冦傚鏋滀互鏈嶅姟鏂瑰紡杩愯鏈嶅姟鍣紝搴斾粠Windows鏈嶅姟绐楀彛鍚姩瀹冦傚鏋滀互鎵嬪姩鏂瑰紡鍚姩浜嗘湇鍔″櫒锛岃兘澶熷儚姝e父鎯呭舰涓嬩竴鏍蜂娇鐢ㄥ懡浠ゃ

搴旇兘浣跨敤鏂板瘑鐮佽繘琛岃繛鎺ャ

Unix鐜涓嬶紝閲嶇疆鏍圭敤鎴峰瘑鐮佺殑姝ラ濡備笅锛

Unix鏍圭敤鎴疯韩浠姐佹垨浠ヨ繍琛mysqld鏈嶅姟鍣ㄧ殑鐩稿悓韬唤鐧诲綍鍒扮郴缁熴

鎵惧埌鍖呭惈鏈嶅姟鍣ㄨ繘绋ID.pid鏂囦欢銆傝鏂囦欢鐨勫噯纭綅缃拰鍚嶇О鍙栧喅浜庝綘鐨勫垎鍙戠増銆佷富鏈哄悕鍜岄厤缃傚父瑙佷綅缃槸/var/lib/mysql//var/run/mysqld//usr/local/mysql/data/涓鑸儏鍐典笅锛屾枃浠跺悕鐨勬墿灞曞悕涓.pid锛屽苟浠mysqld鎴栫郴缁熺殑涓绘満鍚嶅紑濮嬨

鍦ㄤ笅杩板懡浠や腑浣跨敤.pid鏂囦欢鐨勮矾寰勫悕锛屽悜mysqld杩涚▼鍙戝嚭姝e父鐨kill锛堣屼笉鏄kill -9锛夛紝鍙仠姝MySQL鏈嶅姟鍣細

shell> kill `cat /mysql-data-directory/host_name.pid`

娉ㄦ剰锛cat鍛戒护浣跨敤绗﹀彿鈥`鈥濊屼笉鏄溾欌濓細杩欎細浣cat鐨勮緭鍑轰唬鍏ュ埌kill鍛戒护涓

鍒涘缓鏂囨湰鏂囦欢锛屽苟灏嗕笅杩板懡浠ゆ斁鍦ㄦ枃浠跺唴鐨1琛屼笂锛

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPassword');

鐢ㄤ换鎰忓悕绉颁繚瀛樻枃浠躲傚浜庢湰渚嬶紝鏂囦欢涓~/mysql-init

鐢ㄧ壒娈婄殑鈥--init-file=~/mysql-init鈥濋夐」閲嶅惎MySQL鏈嶅姟鍣細

shell> mysqld_safe --init-file=~/mysql-init &

鏂囦欢init-file鐨勫唴瀹瑰湪鏈嶅姟鍣ㄥ惎鍔ㄦ椂鎵ц锛屾洿鏀规牴鐢ㄦ埛瀵嗙爜銆傛湇鍔″櫒鎴愬姛鍚姩鍚庯紝搴斿垹闄~/mysql-init

搴旇兘浣跨敤鏂板瘑鐮佽繘琛岃繛鎺ャ

浣滀负鍙夋柟寮忥紝鍦ㄤ换浣曞钩鍙颁笂锛屽彲浣跨敤mysql瀹㈡埛绔缃柊瀵嗙爜锛堜絾璇ユ柟娉曚笉澶熷畨鍏級锛

鍋滄mysqld锛屽苟鐢ㄢ--skip-grant-tables --user=root鈥閫夐」閲嶅惎瀹Windows鐢ㄦ埛鍙渷鐣--user=root閮ㄥ垎锛夈

浣跨敤涓嬭堪鍛戒护杩炴帴鍒mysqld鏈嶅姟鍣細

shell> mysql -u root

mysql瀹㈡埛绔彂鍑轰笅杩拌鍙ワ細

mysql> UPDATE mysql.user SET Password=PASSWORD('newpwd')
    ->                   WHERE User='root';
mysql> FLUSH PRIVILEGES;

鐢ㄦ墦绠椾娇鐢ㄧ殑瀹為檯鏍圭敤鎴峰瘑鐮佹浛鎹⑩newpwd鈥濄

搴旇兘浣跨敤鏂板瘑鐮佽繘琛岃繛鎺ャ

A.4.2. 濡傛灉MySQL渚濈劧宕╂簝锛屽簲浣滀簺浠涔

姝e紡鍙戝竷涔嬪墠锛屾瘡涓MySQL鐗堟湰鍧囧湪寰堝骞冲彴涓婅繘琛屼簡娴嬭瘯銆傝繖涓嶈〃绀MySQL涓笉瀛樺湪缂洪櫡锛屼絾鏄紝濡傛灉瀛樺湪缂洪櫡锛屽畠浠簲寰堝皯锛岃屼笖寰堥毦鍙戠幇銆傚鏋滀綘閬囧埌闂锛屽鏋滀綘灏濊瘯鎵惧嚭瀵艰嚧绯荤粺宕╂簝鐨勫噯纭師鍥狅紝杩欏缁堝緢鏈夊府鍔╋紝杩欐槸鍥犱负锛屽鏋滆繖鏍风殑璇濓紝蹇熻В鍐抽棶棰樼殑鏈轰細寰堝ぇ銆

棣栧厛锛屽簲灏濊瘯鎵惧嚭闂鏄惁涓mysqld鏈嶅姟鍣ㄦ湁鍏筹紝鎴栨槸鍚︿笌瀹㈡埛绔湁鍏炽傞氳繃鎵цmysqladmin version锛屽彲妫鏌mysqld鏈嶅姟鍣ㄨ繍琛屼簡澶氶暱鏃堕棿銆傚鏋mysqld瀹曟満骞堕噸鍚紝搴旀煡鐪嬫湇鍔″櫒鐨勯敊璇棩蹇椾互鎵惧嚭鍘熷洜銆傝鍙傝5.11.1鑺傦紝鈥滈敊璇棩蹇椻

鍦ㄦ煇浜涚郴缁熶笂锛屽湪閿欒鏃ュ織涓紝鍙彂鐜mysqld瀹曟満鐨勫爢鏍堣窡韪紝鍙娇鐢resolve_stack_dump绋嬪簭瑙e喅瀹銆傝鍙傝E.1.4鑺傦紝鈥滀娇鐢ㄥ爢鏍堣窡韪銆傛敞鎰忥紝閿欒鏃ュ織涓殑鍙橀噺鍊煎苟闈炲缁堟槸100锛呮纭殑銆

寰堝鏈嶅姟鍣ㄥ穿婧冩槸鍥犳崯鍧忕殑鏁版嵁鏂囦欢鎴栫储寮曟枃浠惰屽鑷寸殑锛屾瘡娆℃墽琛屽畬SQL璇彞涔嬪悗骞跺湪鍚戝鎴风閫氬憡缁撴灉涔嬪墠锛MySQL灏嗕娇鐢write()绯荤粺璋冪敤鏇存柊纾佺洏涓婄殑鏂囦欢锛堝鏋滀綘浣跨敤浜嗏--delay-key-write鈥濋夐」锛屾儏鍐靛苟闈炲姝わ紝姝ゆ椂灏嗗啓鍏ユ暟鎹枃浠惰屼笉鏄储寮曟枃浠讹級銆傝繖鎰忓懗鐫锛屽嵆浣mysqld宕╂簝锛屾暟鎹枃浠剁殑鍐呭涔熸槸瀹夊叏鐨勶紝杩欐槸鍥犱负鎿嶄綔绯荤粺鑳戒繚璇佸皢鏈埛鏂扮殑鏁版嵁鍐欏叆纾佺洏銆備娇鐢ㄢ--flush鈥濋夐」鍚姩mysqld锛岃繖鏍凤紝姣忔鎵ц瀹SQL璇彞鍚庯紝鍙己鍒MySQL灏嗘墍鏈夊唴瀹瑰啓鍏ョ鐩樸

鍓嶈堪浠嬬粛琛ㄦ槑锛屽湪姝e父鎯呭喌涓嬩笉浼氬嚭鐜版崯鍧忕殑琛紝闄ら潪鍑虹幇浜嗕笅杩版儏鍐典箣涓锛

鍦ㄦ洿鏂拌繃绋嬩腑锛MySQL鏈嶅姟鍣ㄦ垨鏈嶅姟鍣ㄤ富鏈鸿鍋滄銆

浣犲彂鐜颁簡mysqld涓瓨鍦ㄧ殑1涓己闄凤紝璇ョ己闄峰鑷mysqld鍦ㄦ洿鏂颁腑閫斾腑姝€

mysqld鎿嶄綔鐨勫悓鏃讹紝鏌愪簺澶栭儴绋嬪簭姝e湪鎿嶆帶鏁版嵁鏂囦欢鎴栫储寮曟枃浠讹紝鏈伆褰撻攣瀹氳〃銆

浣犳浣跨敤绯荤粺涓婄殑鐩稿悓鏁版嵁鐩綍杩愯寰堝mysqld鏈嶅姟鍣紝璇ョ郴缁熶笉鏀寔鑹ソ鐨勬枃浠剁郴缁熼攣瀹氾紙閫氬父鏄敱lockd閿佸畾绠$悊鍣ㄨ礋璐g殑锛夛紝鎴栦娇鐢ㄢ--skip-external-locking鈥濋夐」杩愯浜嗗涓湇鍔″櫒銆

宕╂簝鐨勬暟鎹枃浠舵垨绱㈠紩鏂囦欢锛屽叾涓寘鍚鑷mysqld娣蜂贡鐨勬崯鍧忓緢涓ラ噸鐨勬暟鎹

鍦ㄦ暟鎹瓨鍌ㄨ妭鐐瑰彂鐜扮己闄枫傝繖绉嶅彲鑳芥т笉澶э紝浣嗚嚦灏戞槸鍙兘鐨勩傚湪璇ユ儏鍐典笅锛屽彲鍦ㄤ慨澶嶇殑琛ㄥ壇鏈笂锛岄氳繃浣跨敤ALTER TABLE灏濊瘯灏嗚〃绫诲瀷鏇存敼涓哄彟涓绉嶅瓨鍌ㄥ紩鎿庛

鐢变簬寰堥毦寰楃煡涓轰粈涔堟煇浜嬩細鍑虹幇宕╂簝锛岄鍏堣妫鏌ョ敤浜庡叾浠栨柟闈㈢殑浜嬮」鏄惁宕╂簝銆傝灏濊瘯閲囧彇涓嬭堪鎺柦锛

mysqladmin shutdown鍋滄mysqld鏈嶅姟鍣紝浠庢暟鎹洰褰曡繍琛myisamchk --silent --force */*.MYI锛屾鏌ユ墍鏈夌殑MyISAM琛紝骞堕噸鍚mysqld銆傝繖鏍凤紝灏辫兘纭繚浠庡共鍑鐨勭姸鎬佽繍琛屾湇鍔″櫒銆傝鍙傝绗5绔狅細鏁版嵁搴撶鐞

浣跨敤鈥--log鈥濋夐」鍚姩mysqld锛屽苟鏍规嵁鍐欏叆鏃ュ織鐨勪俊鎭‘瀹氭槸鍚︽煇浜涚壒娈婄殑鏌ヨ鏉姝讳簡鏈嶅姟鍣ㄣ傜害95%鐨勭己闄蜂笌鐗瑰畾鐨勬煡璇㈡湁鍏炽傛甯告儏鍐典笅锛岃繖鏄湇鍔″櫒閲嶅惎鍓嶆棩蹇楁枃浠朵腑鏈澶熸暟涓煡璇腑鐨1涓傝鍙傝5.11.2鑺傦紝鈥滈氱敤鏌ヨ鏃ュ織鈥銆傚鏋滆兘澶熺敤鐗规畩鏌ヨ閲嶅鏉姝MySQL锛屽嵆浣垮湪鍙戝嚭鏌ヨ鍓嶆鏌ヤ簡鎵鏈夎〃鐨勬儏鍐典笅涔熷悓鏍凤紝閭d箞浣犲氨搴旇兘纭畾缂洪櫡锛屽苟搴旀彁浜ゅ叧浜庤缂洪櫡鐨勭己闄锋姤鍛娿傝鍙傝1.7.1.3鑺傦紝鈥滃浣曢氭姤缂洪櫡鍜岄棶棰樷

灏濊瘯鎻愪緵涓涓祴璇曡寖渚嬶紝鎴戜滑搴旇兘鍒╃敤璇ヨ寖渚嬮噸澶嶉棶棰樸傝鍙傝E.1.6鑺傦紝鈥滃鏋滃嚭鐜拌〃宕╂簝锛岃鐢熸垚娴嬭瘯妗堜緥鈥

璇峰湪mysql-test鐩綍涓嬪苟鏍规嵁MySQL鍩哄噯杩涜娴嬭瘯銆璇峰弬瑙27.1.2鑺傦紝鈥淢ySQL娴嬭瘯濂椾欢鈥銆傚畠浠兘鐩稿綋鑹ソ鍦版祴璇MySQL銆備綘涔熷彲浠ヤ负鍩哄噯娴嬭瘯澧炲姞浠g爜锛屼互妯℃嫙浣犵殑搴旂敤绋嬪簭銆傚熀鍑嗘祴璇曞彲鍦ㄦ簮鐮佸垎鍙戠増鐨sql-bench鐩綍涓嬫壘鍒帮紝瀵逛簬浜岃繘鍒跺垎鍙戠増锛屽彲鍦MySQL瀹夎鐩綍涓嬬殑sql-bench鐩綍涓嬫壘鍒般

灏濊瘯浣跨敤fork_big.pl鑴氭湰锛堝畠浣嶄簬婧愮爜鍒嗗彂鐗堢殑娴嬭瘯鐩綍涓嬶級銆

濡傛灉浣犲皢MySQL閰嶇疆涓鸿皟璇曟ā寮忥紝濡傛灉鏌愪簨鍑洪敊锛屽彲鏇翠负瀹规槗鍦版悳闆嗗叧浜庡彲鑳介敊璇殑淇℃伅銆傚鏋滃皢MySQL閰嶇疆涓鸿皟璇曟ā寮忥紝鍙敓鎴1涓畨鍏ㄧ殑鍐呭瓨鍒嗛厤绋嬪簭锛屽彲浣跨敤瀹冨彂鐜版煇浜涢敊璇傛澶栵紝瀹冭繕鎻愪緵浜嗗緢澶氳緭鍑猴紝杩欑被杈撳嚭涓庡嚭鐜扮殑闂鐩稿叧銆傚湪configure涓婁娇鐢ㄢ--with-debug鈥濇垨鈥--with-debug=full鈥濋夐」閲嶆柊閰嶇疆MySQL锛岀劧鍚庡啀缂栬瘧瀹冦傝鍙傝E.1鑺傦紝鈥滆皟璇昅ySQL鏈嶅姟鍣ㄢ

纭繚涓轰綘鐨勬搷浣滅郴缁熷簲鐢ㄤ簡鏈鏂扮殑琛ヤ竵銆

mysqld浣跨敤鈥--skip-external-locking鈥濋夐」銆傚湪鏌愪簺绯荤粺涓婏紝lockd閿佸畾绠$悊鍣ㄤ笉鑳芥纭伐浣滐紝--skip-external-locking鈥閫夐」閫氱煡mysqld涓嶄娇鐢ㄥ閮ㄩ攣瀹氥傦紙杩欐剰鍛崇潃锛屼綘涓嶈兘鍦ㄧ浉鍚岀殑鏁版嵁鐩綍涓婅繍琛2mysqld鏈嶅姟鍣紝濡傛灉浣跨敤myisamchk锛屽繀椤昏皑鎱庛傜劧鑰岋紝灏濊瘯灏嗚閫夐」鐢ㄤ綔娴嬭瘯涔熸槸鏈夌泭鐨勶級銆

mysqld鐪嬩笂鍘绘鍦ㄨ繍琛屼絾骞舵湭鍝嶅簲鏃讹紝鏄惁杩愯浜mysqladmin -u root processlist锛熸煇浜涙椂鍊欙紝鍗充娇浣犺涓mysqld澶勪簬闂茬疆鐘舵佹椂锛屽疄闄呮儏鍐靛苟闈炲姝ゃ傞棶棰樺彲鑳芥槸鍥犱负鎵鏈夎繛鎺ュ潎宸蹭娇鐢紝鎴栧瓨鍦ㄦ煇浜涘唴閮ㄩ攣瀹氶棶棰樸傚嵆浣垮湪璇ユ儏鍐典笅锛mysqladmin -u root processlist閫氬父鑳藉杩涜杩炴帴锛屽苟鑳芥彁渚涘叧浜庡綋鍓嶈繛鎺ユ暟浠ュ強鍏剁姸鎬佺殑鏈夌敤淇℃伅銆

鍦ㄨ繍琛屽叾浠栨煡璇㈢殑鍚屾椂锛屽湪鍗曠嫭鐨勭獥鍙d腑杩愯鍛戒护mysqladmin -i 5 statusmysqladmin -i 5 -r status锛屼互鐢熸垚缁熻淇℃伅銆

灏濊瘯閲囩敤涓嬭堪鏂规硶锛

gdb锛堟垨鍙︿竴涓皟璇曞櫒锛夊惎鍔mysqld銆傝鍙傝E.1.3鑺傦紝鈥滃湪gdb鐜涓嬭皟璇昺ysqld

杩愯娴嬭瘯鑴氭湰銆

3涓緝浣庡眰闈笂杈撳嚭backtrace锛堝悜鍚庤窡韪級鍜屽眬閮ㄥ彉閲忋傚湪gdb涓紝褰mysqldgdb鍐呭穿婧冩椂锛屽彲浣跨敤涓嬭堪鍛戒护瀹屾垚璇ヤ换鍔★細

backtrace
info local
up
info local
up
info local

浣跨敤gdb锛屼綘杩樿兘妫鏌ヤ笌info绾跨▼鍏卞瓨鐨勭嚎绋嬶紝骞跺垏鎹㈣嚦鐗瑰畾鐨勭嚎绋N锛屽叾涓紝N鏄嚎绋ID

灏濊瘯鐢Perl鑴氭湰妯℃嫙浣犵殑搴旂敤绋嬪簭锛屽己鍒MySQL宕╂簝鎴栬涓哄紓甯搞

鍙戦佹甯哥殑缂洪櫡鎶ュ憡銆傝鍙傝1.7.1.3鑺傦紝鈥滃浣曢氭姤缂洪櫡鍜岄棶棰樷銆傚簲姣旈氬父鐨勬姤鍛婃洿璇︾粏銆傜敱浜MySQL鏄负寰堝浜烘彁渚涙湇鍔$殑锛屽畠鍙兘鍥犱粎瀛樺湪浜庝綘鐨勮绠楁満涓婄殑鏌愪簨宕╂簝锛堜緥濡傦紝涓庝綘鐨勭壒瀹氱郴缁熷簱鏈夊叧鐨勯敊璇級銆

濡傛灉浣犻亣鍒颁笌鍖呭惈鍔ㄦ侀暱搴﹁鐨勮〃鏈夊叧鐨勯棶棰橈紝鑰屼笖浣犱粎浣跨敤VARCHAR锛堣屼笉鏄BLOBTEXT鍒楋級锛屽彲灏濊瘯鐢ALTER TABLE灏嗘墍鏈VARCHAR鍒楁洿鏀逛负CHAR鍒椼傝繖鏍凤紝灏变細寮哄埗MySQL浣跨敤鍥哄畾澶у皬鐨勮銆傚浐瀹氬ぇ灏忕殑琛屽崰鐢ㄧ殑绌洪棿鐣ュ锛屼絾瀵规崯鍧忕殑瀹瑰繊搴︽洿楂樸

鐩墠鐨勫姩鎬佽浠g爜鍦MySQL AB宸蹭娇鐢ㄥ骞达紝寰堝皯閬囧埌闂锛屼絾浠庢湰璐ㄤ笂鐪嬶紝鍔ㄦ侀暱搴﹁鏇村惧悜浜庡嚭鐜伴敊璇紝鍥犳锛屼笉濡ㄥ皾璇曢噰鐢ㄨ绛栫暐浠ユ煡鐪嬪畠鏄惁鏈夊府鍔╋紝杩欎笉澶变负涓涓ソ涓绘剰銆

璇婃柇闂鏃朵笉瑕佸皢浣犵殑鏈嶅姟鍣ㄧ‖浠舵帓闄ゅ湪澶栥傛湁缂洪櫡鐨勭‖浠惰兘澶熷鑷存暟鎹崯鍧忋傚纭欢杩涜鏁呴殰璇婃柇涓庢帓闄ゆ搷浣滄椂锛屽挨鍏跺簲娉ㄦ剰RAM鍜岀‖鐩橀┍鍔ㄥ櫒銆

A.4.3. MySQL澶勭悊纾佺洏婊$殑鏂瑰紡

鍦ㄦ湰鑺備腑锛屼粙缁嶄簡MySQL鍝嶅簲纾佺洏婊¢敊璇殑鏂瑰紡锛堝鈥滆澶囦笂鏃犲墿浣欑┖闂粹濓級锛屼互鍙婂搷搴旇秴閰嶉閿欒鐨勬柟寮忥紙濡傗滃啓鍏ュけ璐モ濇垨鈥滆揪鍒颁簡鐢ㄦ埛灞忚斀闄愬埗鈥濓級銆

鏈妭浠嬬粛鐨勫唴瀹逛笌鍐欏叆MyISAM琛ㄦ湁鍏炽傚畠涔熼傜敤浜庡啓鍏ヤ簩杩涘埗鏃ュ織鏂囦欢鍜屼簩杩涘埗绱㈠紩鏂囦欢锛屼絾瀵row鈥濆拰鈥record鈥濈殑搴旂敤搴旇瑙嗕负鈥event鈥濄

鍑虹幇纾佺洏婊$姸鍐垫椂锛MySQL灏嗭細

姣忓垎閽熸鏌ヤ竴娆★紝鏌ョ湅鏄惁鏈夎冻澶熺┖闂村啓鍏ュ綋鍓嶈銆傚鏋滄湁瓒冲绌洪棿锛屽皢缁х画锛屽氨鍍忎粈涔堜篃鏈彂鐢熶竴鏍枫

10鍒嗛挓灏1涓潯鐩啓鍏ユ棩蹇楁枃浠讹紝鎻愰啋纾佺洏婊$姸鍐点

涓轰簡鍑忚交闂锛屽彲閲囧彇涓嬭堪鎺柦锛

瑕佹兂缁х画锛屼粎闇鏈夎冻澶熺殑纾佺洏绌洪棿浠ユ彃鍏ユ墍鏈夎褰曘

瑕佹兂鏀惧純绾跨▼锛屽繀椤讳娇鐢mysqladmin kill銆備笅娆℃鏌ョ鐩樻椂灏嗘斁寮冪嚎绋嬶紙1鍒嗛挓锛夈

鍏朵粬绾跨▼鍙兘浼氭鍦ㄧ瓑寰呭鑷寸鐩樻弧鐘跺喌鐨勮〃銆傚鏋滄湁鏁颁釜鈥滃凡閿佸畾鈥濈殑绾跨▼锛屾潃姝绘鍦ㄧ鐩樻弧鐘跺喌涓嬬瓑寰呯殑鏌愪竴绾跨▼锛屼互渚垮厑璁稿叾浠栫嚎绋嬬户缁

瀵瑰墠杩拌涓虹殑渚嬪鏄紝褰撲綘浣跨敤REPAIR TABLEOPTIMIZE TABLE鏃讹紝鎴栧綋绱㈠紩鏄湪LOAD DATA INFILEALTER TABLE璇彞鍚庛佸湪鎵规搷浣滀腑鍒涘缓鐨勩傛墍鏈夎繖浜涜鍙ヨ兘鍒涘缓澶х殑涓存椂鏂囦欢锛屽鏋滀繚鐣欒繖浜涙枃浠讹紝浼氬鑷寸郴缁熷叾浠栭儴鍒嗗嚭鐜板ぇ闂銆傚鏋滃湪MySQL鎵ц杩欑被鎿嶄綔鐨勫悓鏃剁鐩樺凡婊★紝瀹冨皢鍒犻櫎澶х殑涓存椂鏂囦欢锛屽苟灏嗚〃鏍囨敞涓哄穿婧冦備絾瀵逛簬ALTER TABLE渚嬪锛屾棫琛ㄤ繚鎸佷笉鍙樸

A.4.4. MySQL灏嗕复鏃舵枃浠跺偍瀛樺湪鍝噷

MySQL浣跨敤鐜鍙橀噺TMPDIR鐨勫间綔涓轰繚瀛樹复鏃舵枃浠剁殑鐩綍鐨勮矾寰勫悕銆傚鏋滄湭璁剧疆TMPDIRMySQL灏嗕娇鐢ㄧ郴缁熺殑榛樿鍊硷紝閫氬父涓/tmp/var/tmp/usr/tmp銆傚鏋滃寘鍚复鏃舵枃浠剁洰褰曠殑鏂囦欢绯荤粺杩囧皬锛屽彲瀵mysqld浣跨敤鈥鈥攖mpdir鈥閫夐」锛鍦ㄥ叿鏈夎冻澶熺┖闂寸殑鏂囦欢绯荤粺鍐呮寚瀹1涓洰褰曘

MySQL 5.1涓紝鈥鈥攖mpdir鈥閫夐」鍙璁剧疆涓烘暟涓矾寰勭殑鍒楄〃锛屼互寰幆鏂瑰紡浣跨敤銆傚湪Unix骞冲彴涓婏紝璺緞鐢ㄥ啋鍙峰瓧绗鈥:鈥闅斿紑锛屽湪WindowsNetWareOS/2骞冲彴涓婏紝璺緞鐢ㄥ垎鍙峰瓧绗鈥;鈥闅斿紑銆傛敞鎰忥紝涓轰簡鏈夋晥鍒嗗竷璐熻浇锛岃繖浜涜矾寰勫簲浣嶄簬涓嶅悓鐨勭墿鐞嗙鐩樹笂锛岃屼笉鏄綅浜庣浉鍚岀鐩樼殑涓嶅悓鍒嗗尯涓

濡傛灉MySQL鏈嶅姟鍣ㄦ浣滀负澶嶅埗浠庢湇鍔″櫒浣跨敤锛屼笉搴斿皢鈥--tmpdir鈥璁剧疆涓烘寚鍚戝熀浜庡唴瀛樼殑鏂囦欢绯荤粺鐨勭洰褰曪紝鎴栧綋鏈嶅姟鍣ㄤ富鏈洪噸鍚椂灏嗘竻绌虹殑鐩綍銆傚浜庡鍒朵粠鏈嶅姟鍣紝闇瑕佸湪鏈哄櫒閲嶅惎鏃朵粛淇濈暀涓浜涗复鏃舵枃浠讹紝浠ヤ究鑳藉澶嶅埗涓存椂琛ㄦ垨鎵цLOAD DATA INFILE鎿嶄綔銆傚鏋滃湪鏈嶅姟鍣ㄩ噸鍚椂涓㈠け浜嗕复鏃舵枃浠剁洰褰曚笅鐨勬枃浠讹紝澶嶅埗灏嗗け璐ャ

MySQL浼氫互闅愬惈鏂瑰紡鍒涘缓鎵鏈夌殑涓存椂鏂囦欢銆傝繖鏍凤紝灏辫兘纭繚涓mysqld鏃朵細鍒犻櫎鎵鏈変复鏃舵枃浠躲備娇鐢ㄩ殣鍚枃浠剁殑缂虹偣鍦ㄤ簬锛屽湪涓存椂鏂囦欢鐩綍鎵鍦ㄧ殑浣嶇疆涓紝鐪嬩笉鍒板崰鐢ㄤ簡鏂囦欢绯荤粺鐨勫ぇ涓存椂鏂囦欢銆

杩涜鎺掑簭鏃讹紙ORDER BYGROUP BY锛夛紝MySQL閫氬父浼氫娇鐢1涓垨澶氫釜涓存椂鏂囦欢銆傛墍闇鐨勬渶澶х鐩樼┖闂寸敱涓嬭堪琛ㄨ揪寮忓喅瀹氾細

(length of what is sorted + sizeof(row pointer))
* number of matched rows
* 2

row pointer鈥濓紙琛屾寚閽堬級鐨勫ぇ灏忛氬父鏄4瀛楄妭锛屼絾鍦ㄤ互鍚庯紝瀵逛簬澶х殑琛紝璇ュ煎彲鑳戒細澧炲姞銆

瀵逛簬鏌愪簺SELECT鏌ヨ锛MySQL杩樹細鍒涘缓涓存椂SQL琛ㄣ傚畠浠笉鏄殣鍚〃锛屽苟鍏锋湁SQL_*褰㈠紡鐨勫悕绉般

ALTER TABLE浼氬湪涓庡師濮嬭〃鐩綍鐩稿悓鐨勭洰褰曚笅鍒涘缓涓存椂琛ㄣ

A.4.5. 濡備綍淇濇姢鎴栨洿鏀筂ySQL濂楁帴瀛楁枃浠/tmp/mysql.sock

瀵逛簬鏈嶅姟鍣ㄧ敤鏉ヤ笌鏈湴瀹㈡埛绔繘琛岄氫俊鐨Unix濂楁帴瀛楁枃浠讹紝鍏堕粯璁や綅缃槸/tmp/mysql.sock銆傝繖鏈夊彲鑳藉鑷撮棶棰橈紝鍘熷洜鍦ㄤ簬锛屽湪鏌愪簺鐗堟湰鐨Unix涓婏紝浠讳綍浜洪兘鑳藉垹闄/tmp鐩綍涓嬬殑鏂囦欢銆

鍦ㄥぇ澶氭暟Unix鐗堟湰涓紝鍙/tmp鐩綍杩涜淇濇姢锛屼娇寰楁枃浠跺彧鑳借鍏舵墍鏈夎繖鎴栬秴绾х敤鎴凤紙鏍圭敤鎴凤級鍒犻櫎銆備负姝わ紝浠ユ牴鐢ㄦ埛韬唤鐧诲綍锛屽苟浣跨敤涓嬭堪鍛戒护鍦/tmp鐩綍涓婅缃矘鐫浣嶏細

shell> chmod +t /tmp

閫氳繃鎵цls -ld /tmp锛屽彲妫鏌ユ槸鍚﹁缃簡绮樼潃浣嶃傚鏋滄渶鍚庝竴涓鍙瓧绗︽槸鈥渢鈥锛岃〃鏄庤缃簡绮樼潃浣嶃

鍙︿竴绉嶆柟娉曟槸鏀瑰彉鏈嶅姟鍣ㄥ垱寤Unix濂楁帴瀛楁枃浠剁殑浣嶇疆銆傚鏋滆繘琛屼簡杩欑被鎿嶄綔锛岃繕搴旇瀹㈡埛绔▼搴忕煡閬撴枃浠剁殑浣嶇疆銆傝兘澶熶互澶氱涓嶅悓鏂瑰紡鎸囧畾鏂囦欢浣嶇疆锛

鍦ㄥ叏灞鎴栧眬閮ㄩ夐」鏂囦欢涓寚瀹氳矾寰勩備緥濡傦紝灏嗕笅杩拌缃簬鏂囦欢/etc/my.cnf涓細

[mysqld]
socket=/path/to/socket
 
[client]
socket=/path/to/socket

璇峰弬瑙4.3.2鑺傦紝鈥滀娇鐢ㄩ夐」鏂囦欢鈥

鍦ㄨ繍琛屽鎴风绋嬪簭鏃讹紝鍦ㄥ懡浠よ涓婁负mysqld_safe鎸囧畾--socket鈥閫夐」銆

MYSQL_UNIX_PORT鐜鍙橀噺璁剧疆涓Unix濂楁帴瀛楁枃浠剁殑璺緞銆

閲嶆柊浠庢簮鐮佺紪璇MySQL锛屼互浣跨敤涓嶅悓鐨勯粯璁Unix濂楁帴瀛楁枃浠朵綅缃傝繍琛configure鏃讹紝鐢ㄢ--with-unix-socket-path鈥濋夐」瀹氫箟鏂囦欢璺緞銆傝鍙傝2.8.2鑺傦紝鈥滃吀鍨嬮厤缃夐」

鐢ㄤ笅杩板懡浠よ繛鎺ユ湇鍔″櫒锛岃兘澶熸祴璇曟柊鐨勫鎺ュ瓧浣嶇疆鏄惁宸ヤ綔锛

shell> mysqladmin --socket=/path/to/socket version

A.4.6. 鏃跺尯闂

濡傛灉閬囧埌涓SELECT NOW()鏈夊叧鐨勯棶棰橈紝瀹冭繑鍥GMT鍊艰屼笉鏄綋鍦版椂闂达紝灏卞簲閫氱煡鏈嶅姟鍣ㄤ綘鐨勫綋鍓嶅け鍘汇傚鏋UNIX_TIMESTAMP()杩斿洖閿欒鍊硷紝涓婅堪鏂瑰紡鍚屾牱閫傜敤銆傚簲涓烘湇鍔″櫒鎵杩愯鐨勭幆澧冭繘琛岃繖绫昏缃紝渚嬪锛屽湪mysqld_safemysql.server涓傝鍙傝闄勫綍F锛鐜鍙橀噺

涔熷彲浠ュmysqld_safe浣跨敤鈥--timezone=timezone_name鈥濋夐」锛屼负鏈嶅姟鍣ㄨ缃け鍘汇備篃鍙互鍦ㄥ惎鍔mysqld涔嬪墠锛岄氳繃璁剧疆TZ鐜鍙橀噺瀹屾垚璇ヨ缃

--timezone鈥濇垨TZ鐨勫厑璁稿间笌绯荤粺鏈夊叧銆傚叧浜庡彲鎺ュ彈鐨勫硷紝璇峰弬瑙佹搷浣滅郴缁熸枃妗c

A.5. 涓庢煡璇㈡湁鍏崇殑浜嬪疁

A.5.1. 鎼滅储涓殑澶у皬鍐欐晱鎰熸

鍦ㄩ粯璁ゆ儏鍐典笅锛MySQL鎼滅储涓嶅尯鍒嗗ぇ灏忓啓锛堜絾鏌愪簺瀛楃闆嗗缁堝尯鍒嗗ぇ灏忓啓锛屽czech锛夈傝繖鎰忓懗鐫锛屽鏋滀綘浣跨敤col_name LIKE 'a%'杩涜鎼滅储锛屼綘灏嗚幏寰椾互Aa寮濮嬬殑鎵鏈夊垪銆傚鏋滄墦绠椾娇鎼滅储鍖哄垎澶у皬鍐欙紝璇风‘淇濇搷浣滄暟涔嬩竴鍏锋湁鍖哄垎澶у皬鍐欑殑鎴栦簩杩涘埗鏍″銆備緥濡傦紝濡傛灉浣犳鍦ㄦ瘮杈冨潎閫傜敤latin1瀛楃闆嗙殑鍒楀拰瀛楃涓诧紝鍙娇鐢COLLATE鎿嶄綔绗锛屼娇1涓搷浣滄暟鍏锋湁latin1_general_cslatin1_bin鏍″鐗规с備緥濡傦細

col_name COLLATE latin1_general_cs LIKE 'a%'
col_name LIKE 'a%' COLLATE latin1_general_cs
col_name COLLATE latin1_bin LIKE 'a%'
col_name LIKE 'a%' COLLATE latin1_bin

濡傛灉甯屾湜鎬绘槸浠ュ尯鍒嗗ぇ灏忓啓鐨勬柟寮忓鐞嗗垪锛屽彲浣跨敤鍖哄垎澶у皬鍐欑殑鎴栦簩杩涘埗鏍″澹版槑瀹冦傝鍙傝13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥

绠鍗曠殑姣旇緝鎿嶄綔锛>=, >, =, <, <=, 鎺掑簭鍜屽垎缁勶級鍩轰簬姣忎釜瀛楃鐨勨滄帓搴忓尖濄傚叿鏈夌浉鍚屾帓搴忓肩殑瀛楃锛堝鈥E, e,鍜屸脙漏鈥欙級灏嗚褰撲綔鐩稿悓鐨勫啓瀛楃銆

A.5.2. 浣跨敤DATE鍒楁柟闈㈢殑闂

DATE鍊肩殑鏍煎紡鏄'YYYY-MM-DD'鎸夌収鏍囧噯鐨SQL锛屼笉鍏佽鍏朵粬鏍煎紡銆傚湪UPDATE琛ㄨ揪寮忎互鍙SELECT璇彞鐨WHERE瀛愬彞涓簲浣跨敤璇ユ牸寮忋備緥濡傦細

mysql> SELECT * FROM tbl_name WHERE date >= '2003-05-05';

涓轰簡鏂逛究锛屽鏋滄棩鏈熸槸鍦ㄦ暟鍊肩幆澧冧笅浣跨敤鐨勶紝MySQL浼氳嚜鍔ㄥ皢鏃ユ湡杞崲涓烘暟鍊硷紙鍙嶄箣浜︾劧锛夈傚畠杩樺叿鏈夌浉褰撶殑鏅鸿兘锛屽湪鏇存柊鏃舵垨鍦ㄤ笌TIMESTAMPDATEDATETIME鍒楁瘮杈冩棩鏈熺殑WHERE瀛愬彞涓紝鍏佽瀹芥澗鐨瀛楃涓插舰寮忥紙瀹芥澗褰㈠紡琛ㄧず锛屼换浣曟爣鐐瑰瓧绗﹀潎鑳界敤浣滃悇閮ㄥ垎涔嬮棿鐨勫垎闅旂銆備緥濡傦紝'2004-08-15''2004#08#15'鏄瓑鍚岀殑锛夈MySQL杩樿兘杞崲涓嶅惈浠讳綍鍒嗛殧绗︾殑瀛楃涓诧紙濡'20040815'锛夛紝鍓嶄綋鏄畠蹇呴』鏄湁鎰忎箟鐨勬棩鏈熴

浣跨敤<<==>=>銆佹垨BETWEEN鎿嶄綔绗﹀皢DATETIMEDATETIMETIMESTAMP涓庡父閲忓瓧绗︿覆杩涜姣旇緝鏃讹紝MySQL閫氬父浼氬皢瀛楃涓茶浆鎹负鍐呴儴闀挎暣鏁帮紝浠ヤ究杩涜蹇熸瘮杈冿紙浠ュ強鐣ヤ负鈥滃鏉锯濈殑瀛楃涓叉鏌ワ級銆備絾鏄紝璇ヨ浆鎹㈠叿鏈変笅杩颁緥澶栵細

姣旇緝涓ゅ垪鏃

DATETIMEDATETIMETIMESTAMP鍒椾笌琛ㄨ揪寮忚繘琛屾瘮杈冩椂

浣跨敤鍏朵粬姣旇緝鏂规硶鏃讹紝濡INSTRCMP()

瀵逛簬杩欎簺渚嬪鎯呭舰锛屼細灏嗗璞¤浆鎹负瀛楃涓插苟鎵ц瀛楃涓叉瘮杈冿紝閲囩敤璇ユ柟寮忚繘琛屾瘮杈冦

涓轰簡淇濇寔瀹夊叏锛屽亣瀹氭寜瀛楃涓叉瘮杈冨瓧绗︿覆锛屽鏋滀綘鎵撶畻姣旇緝涓存椂鍊煎拰瀛楃涓诧紝灏嗕娇鐢ㄦ伆褰撶殑瀛楃涓插嚱鏁般

瀵逛簬鐗规畩鏃ユ湡'0000-00-00'锛岃兘澶熶互'0000-00-00'褰㈠紡淇濆瓨鍜屾绱€傚湪MyODBC涓娇鐢'0000-00-00'鏃ユ湡鏃讹紝瀵逛簬MyODBC 2.50.12鎴栨洿楂樼増鏈紝璇ユ棩鏈熷皢琚嚜鍔ㄨ浆鎹负NULL锛岃繖鏄洜涓ODBC涓嶈兘澶勭悊杩欑被鏃ユ湡銆

鐢变簬MySQL鑳藉鎵ц鍓嶉潰鎵浠嬬粛鐨勮浆鎹紝涓嬭堪璇彞鍧囪兘姝e父宸ヤ綔锛

mysql> INSERT INTO tbl_name (idate) VALUES (19970505);
mysql> INSERT INTO tbl_name (idate) VALUES ('19970505');
mysql> INSERT INTO tbl_name (idate) VALUES ('97-05-05');
mysql> INSERT INTO tbl_name (idate) VALUES ('1997.05.05');
mysql> INSERT INTO tbl_name (idate) VALUES ('1997 05 05');
mysql> INSERT INTO tbl_name (idate) VALUES ('0000-00-00');
 
mysql> SELECT idate FROM tbl_name WHERE idate >= '1997-05-05';
mysql> SELECT idate FROM tbl_name WHERE idate >= 19970505;
mysql> SELECT MOD(idate,100) FROM tbl_name WHERE idate >= 19970505;
mysql> SELECT idate FROM tbl_name WHERE idate >= '19970505';

浣嗘槸锛屼笅杩拌鍙ヤ笉鑳芥甯稿伐浣滐細

mysql> SELECT idate FROM tbl_name WHERE STRCMP(idate,'20030505')=0;

STRCMP()鏄竴绉嶅瓧绗︿覆鍑芥暟锛屽畠鑳藉皢idate杞崲涓'YYYY-MM-DD'鏍煎紡鐨勫瓧绗︿覆锛骞舵墽琛屽瓧绗︿覆姣旇緝銆傚畠涓嶈兘灏'20030505'杞崲涓鏃ユ湡'2003-05-05'骞惰繘琛屾棩鏈熸瘮杈冦

濡傛灉浣犳鍦ㄤ娇鐢ALLOW_INVALID_DATES SQL妯″紡锛MySQL鍏佽浠ヤ粎鎵ц缁欏畾鐨勬湁闄愭鏌ユ柟寮忎繚瀛樻棩鏈燂細MySQL浠呬繚璇佸ぉ浣嶄簬131鐨勮寖鍥村唴锛屾湀浣嶄簬112鐨勮寖鍥村唴銆

杩欐牱灏变娇寰MySQL寰堥傚悎浜Web搴旂敤绋嬪簭锛屽叾涓紝浣犺兘鑾峰緱涓変釜涓嶅悓瀛楁涓殑骞淬佹湀銆佹棩鍊硷紝涔熻兘鍑嗙‘淇濆瓨鐢ㄦ埛鎻掑叆鐨勫硷紙鏃犳棩鏈熼獙璇侊級銆

濡傛灉鏈娇鐢NO_ZERO_IN_DATE SQL妯″紡锛屸滃ぉ鈥濆拰鈥滄湀鈥濋儴鍒嗗彲鑳戒负0銆傚鏋滀綘鎵撶畻灏嗙敓鏃ヤ繚瀛樺湪DATE鍒楄屼笖浠呯煡閬撻儴鍒嗘棩鏈燂紝瀹冨崄鍒嗘柟渚裤

濡傛灉鏈娇鐢NO_ZERO_DATE SQL妯″紡锛MySQL涔熷厑璁镐綘灏'0000-00-00'淇濆瓨涓衡滀吉鏃ユ湡鈥濄傚湪鏌愪簺鎯呭喌涓嬶紝瀹冩瘮浣跨敤NULL鍊兼洿鏂逛究銆

濡傛灉鏃犳硶灏嗘棩鏈熻浆鎹负浠讳綍鍚堢悊鍊硷紝鈥0鈥濆皢淇濆瓨鍦DATE鍒椾腑锛屽苟琚绱负'0000-00-00'銆傝繖鏄吋椤鹃熷害鍜屼究鍒╂х殑浜嬪疁銆傛垜浠涓猴紝鏁版嵁搴撴湇鍔″櫒鐨勮亴璐f槸妫绱笌浣犱繚瀛樼殑鏃ユ湡鐩稿悓鐨勬棩鏈锛堝嵆浣垮湪浠讳綍鎯呭喌涓嬶紝鏁版嵁鍦ㄩ昏緫涓婁笉姝g‘涔熷悓鏍凤級銆傛垜浠涓猴紝瀵规棩鏈熺殑妫鏌ュ簲鐢卞簲鐢ㄧ▼搴忚屼笉鏄湇鍔″櫒璐熻矗銆

濡傛灉浣犲笇鏈MySQL妫鏌ユ墍鏈夋棩鏈熷苟浠呮帴鍙楀悎娉曟棩鏈燂紙闄ら潪鐢IGNORE瑕嗙洊锛夛紝搴斿皢sql_mode璁剧疆涓"NO_ZERO_IN_DATE,NO_ZERO_DATE"

A.5.3. 涓嶯ULL鍊兼湁鍏崇殑闂

瀵逛簬SQL鐨勬柊鎵嬶紝NULL鍊肩殑姒傚康甯稿父浼氶犳垚娣锋穯锛屼粬浠父璁や负NULL鏄笌绌哄瓧绗︿覆''鐩稿悓鐨勪簨銆鎯呭喌骞堕潪濡傛銆備緥濡傦紝涓嬭堪璇彞鏄畬鍏ㄤ笉鍚岀殑锛

mysql> INSERT INTO my_table (phone) VALUES (NULL);
mysql> INSERT INTO my_table (phone) VALUES ('');

杩欎袱鏉¤鍙ュ潎浼氬皢鍊兼彃鍏phone锛堢數璇濓級鍒楋紝浣嗙1鏉¤鍙ユ彃鍏ョ殑鏄NULL鍊硷紝绗2鏉¤鍙ユ彃鍏ョ殑鏄┖瀛楃涓层傜1绉嶆儏鍐电殑鍚箟鍙瑙i噴涓衡滅數璇濆彿鐮佹湭鐭モ濓紝鑰岀2绉嶆儏鍐电殑鍚箟鍙瑙i噴涓衡滆浜哄憳娌℃湁鐢佃瘽锛屽洜姝ゆ病鏈夌數璇濆彿鐮佲濄

涓轰簡杩涜NULL澶勭悊锛屽彲浣跨敤IS NULLIS NOT NULL鎿嶄綔绗︿互鍙IFNULL()鍑芥暟銆

SQL涓紝NULL涓庝换浣曞叾瀹冨肩殑姣旇緝锛堝嵆浣挎槸NULL锛夋案杩滀笉浼氫负鈥滅湡鈥濄傚寘鍚NULL鐨勮〃杈惧紡鎬绘槸浼氬鍑NULL鍊硷紝闄ら潪鍦ㄥ叧浜庢搷浣滅鐨勬枃妗d腑浠ュ強琛ㄨ揪寮忕殑鍑芥暟涓綔浜嗗叾浠栬瀹氥備笅杩扮ず渚嬩腑鐨勬墍鏈夊垪鍧囪繑鍥NULL

mysql> SELECT NULL, 1+NULL, CONCAT('Invisible',NULL);

濡傛灉鎵撶畻鎼滅储鍒楀间负NULL鐨勫垪锛屼笉鑳戒娇鐢expr = NULL娴嬭瘯銆備笅杩拌鍙ヤ笉杩斿洖浠讳綍琛岋紝杩欐槸鍥犱负锛屽浜庝换浣曡〃杈惧紡锛expr = NULL姘歌繙涓嶄负

mysql> SELECT * FROM my_table WHERE phone = NULL;

瑕佹兂鏌ユ壘NULL鍊硷紝蹇呴』浣跨敤IS NULL娴嬭瘯銆鍦ㄤ笅闈㈢殑璇彞涓紝浠嬬粛浜嗘煡鎵NULL鐢佃瘽鍙风爜鍜岀┖鐢佃瘽鍙风爜鐨勬柟寮忥細

mysql> SELECT * FROM my_table WHERE phone IS NULL;
mysql> SELECT * FROM my_table WHERE phone = '';

鏇村淇℃伅鍜岀ず渚嬶紝璇峰弬瑙3.3.4.6鑺傦紝鈥滀娇鐢∟ULL鍊尖

濡傛灉浣犳鍦ㄤ娇鐢MyISAMInnoDBBDB銆佹垨MEMORY瀛樺偍寮曟搸锛岃兘澶熷湪鍙兘鍏锋湁NULL鍊肩殑鍒椾笂澧炲姞1鏉$储寮曘傚涓嶇劧锛屽繀椤诲0鏄庣储寮曞垪涓NOT NULL锛岃屼笖涓嶈兘灏NULL鎻掑叆鍒板垪涓

LOAD DATA INFILE璇诲彇鏁版嵁鏃讹紝瀵逛簬绌虹殑鎴栦涪澶辩殑鍒楋紝灏嗙敤''鏇存柊瀹冧滑銆傚鏋滃笇鏈涘湪鍒椾腑鍏锋湁NULL鍊硷紝搴斿湪鏁版嵁鏂囦欢涓娇鐢\N銆傚湪鏌愪簺鎯呭喌涓嬶紝涔熷彲浠ヤ娇鐢ㄦ枃瀛楁у崟璇嶁NULL鈥濄傝鍙傝13.2.5 LOAD DATA INFILE璇硶鈥

浣跨敤DISTINCTGROUP BYORDER BY鏃讹紝鎵鏈NULL鍊煎皢琚涓虹瓑鍚岀殑銆

浣跨敤ORDER BY鏃讹紝棣栧厛灏嗘樉绀NULL鍊硷紝濡傛灉鎸囧畾浜DESC鎸夐檷搴忔帓鍒楋紝NULL鍊煎皢鏈鍚庢樉绀恒

瀵逛簬鑱氬悎锛堢疮璁★級鍑芥暟锛屽COUNT()MIN()SUM()锛屽皢蹇界暐NULL鍊笺瀵规鐨勪緥澶栨槸COUNT(*)锛屽畠灏嗚鏁拌鑰屼笉鏄崟鐙殑鍒楀笺備緥濡傦紝涓嬭堪璇彞浜х敓涓や釜璁℃暟銆傞鍏堣鏁拌〃涓殑琛屾暟锛屽叾娆¤鏁age鍒椾腑鐨勯潪NULL鍊兼暟鐩細

mysql> SELECT COUNT(*), COUNT(age) FROM person;

瀵逛簬鏌愪簺鍒楃被鍨嬶紝MySQL灏嗗NULL鍊艰繘琛岀壒娈婂鐞嗐傚鏋滃皢NULL鎻掑叆TIMESTAMP鍒楋紝灏嗘彃鍏ュ綋鍓嶆棩鏈熷拰鏃堕棿銆傚鏋滃皢NULL鎻掑叆鍏锋湁AUTO_INCREMENT灞炴х殑鏁存暟鍒楋紝灏嗘彃鍏ュ簭鍒椾腑鐨勪笅涓涓紪鍙枫

A.5.4. 涓庡垪鍒悕鏈夊叧鐨勯棶棰

鍙互浣跨敤鍒悕鏉ュ紩鐢GROUP BYORDER BYHAVING瀛愬彞涓殑鍒椼傚埆鍚嶄篃鑳界敤浜庝负鍒楁彁渚涙洿濂界殑鍚嶇О锛
SELECT SQRT(a*b) AS root FROM tbl_name GROUP BY root HAVING root > 0;
SELECT id, COUNT(*) AS cnt FROM tbl_name GROUP BY id HAVING cnt > 0;
SELECT id AS 'Customer identity' FROM tbl_name;

鏍囧噯SQL涓嶅厑璁稿湪WHERE瀛愬彞涓凡鐢ㄥ垪鍒悕銆傝繖鏄洜涓猴紝鎵цWHERE浠g爜鏃讹紝鍙兘灏氭湭纭畾鍒楀笺備緥濡傦紝涓嬭堪鏌ヨ鏄潪娉曠殑锛

SELECT id, COUNT(*) AS cnt FROM tbl_name WHERE cnt > 0 GROUP BY id;

鎵цWHERE璇彞浠ョ‘瀹鍝簺琛屽簲琚寘鍚湪GROUP BY閮ㄥ垎涓紝鑰HAVING鐢ㄤ簬纭畾搴斾娇鐢ㄧ粨鏋滈泦涓殑鍝簺琛屻

A.5.5. 闈炰簨鍔¤〃鍥炴粴澶辫触

鎵цROLLBACK锛堝洖婊氾級鏃讹紝濡傛灉鏀跺埌涓嬭堪娑堟伅锛岃〃绀轰簨鍔′腑浣跨敤鐨1涓垨澶氫釜琛ㄤ笉鏀寔浜嬪姟锛

璀﹀憡锛氭煇浜涙洿鏀圭殑闈炰簨鍔℃ц〃涓嶈兘琚洖婊氥

杩欎簺闈炰簨鍔℃ц〃涓嶅彈ROLLBACK璇彞鐨勫奖鍝嶃

濡傛灉鍦ㄤ簨鍔′腑鎰忓鍦版贩鍚堜簡浜嬪姟鎬ц〃鍜岄潪浜嬪姟鎬ц〃锛屽鑷磋娑堟伅鐨勬渶鍙兘鍘熷洜鏄紝浣犺涓烘湰搴旀槸浜嬪姟鎬х殑琛ㄥ疄闄呬笂涓嶆槸銆傚浣犺瘯鍥句娇鐢mysqld鏈嶅姟鍣ㄤ笉鏀寔鐨勪簨鍔℃у瓨鍌ㄥ紩鎿庯紙鎴栫敤鍚姩閫夐」绂佹浜嗗畠锛夊垱寤鸿〃锛屽氨鍙兘鍑虹幇璇ユ儏鍐点傚鏋mysqld涓嶆敮鎸佸瓨鍌ㄥ紩鎿庯紝瀹冨皢浠MyISAM琛ㄥ垱寤鸿〃锛岃繖鏄潪浜嬪姟鎬ц〃銆

鍙娇鐢ㄤ笅杩拌鍙ヤ箣涓妫鏌ヨ〃鐨勬爣绫诲瀷锛

SHOW TABLE STATUS LIKE 'tbl_name';
SHOW CREATE TABLE tbl_name;

璇峰弬瑙13.5.4.18鑺傦紝鈥淪HOW TABLE STATUS璇硶浠ュ強13.5.4.5鑺傦紝鈥淪HOW CREATE TABLE璇硶鈥

浣跨敤涓嬭堪璇彞锛屽彲妫鏌mysqld鏈嶅姟鍣ㄦ敮鎸佺殑瀛樺偍寮曟搸锛

SHOW ENGINES;

涔熷彲浠ヤ娇鐢ㄤ笅杩拌鍙ワ紝妫鏌ヤ笌浣犳劅鍏磋叮鐨勫瓨鍌ㄥ紩鎿庢湁鍏崇殑鍙橀噺鍊硷細

SHOW VARIABLES LIKE 'have_%';

渚嬪锛岃鎯崇‘瀹InnoDB瀛樺偍寮曟搸鏄惁鍙敤锛屽彲妫鏌have_innodb鍙橀噺鐨勫笺

璇峰弬瑙13.5.4.8鑺傦紝鈥淪HOW ENGINES璇硶鈥13.5.4.21鑺傦紝鈥淪HOW VARIABLES璇硶鈥

A.5.6. 浠庣浉鍏宠〃鍒犻櫎琛

濡傛灉閽堝related_tableDELETE璇彞鐨勬婚暱搴﹁秴杩1MB锛堢郴缁熷彉閲max_allowed_packet鐨勯粯璁ゅ硷級锛屽簲灏嗗叾鍒嗕负杈冨皬鐨勯儴鍒嗭紝骞舵墽琛屽涓DELETE璇彞銆傚鏋related_column鏄储寮曞垪锛屼负姣忔潯璇彞鎸囧畾1001000related_column鍊硷紝鎴栬鑳借幏寰楁洿蹇殑DELETE閫熷害銆傚鏋related_column涓嶆槸绱㈠紩鍒楋紝閫熷害涓IN瀛愬彞涓殑鍙傞噺鏁扮洰鏃犲叧銆

A.5.7. 瑙e喅涓庝笉鍖归厤琛屾湁鍏崇殑闂

濡傛灉鏈変娇鐢ㄤ簡寰堝琛ㄧ殑澶嶆潅鏌ヨ锛屼絾鏈繑鍥炰换浣曡锛屽簲閲囩敤涓嬭堪姝ラ鎵惧嚭浠涔堝嚭閿欙細

EXPLAIN娴嬭瘯鏌ヨ锛屼互妫鏌鏄惁鍙戠幇鏌愪簨鏄剧劧鍑洪敊銆傝鍙傝7.2.1鑺傦紝鈥淓XPLAIN璇硶锛堣幏鍙栧叧浜嶴ELECT鐨勪俊鎭級

浠呴夋嫨鍦WHERE瀛愬彞涓娇鐢ㄧ殑鍒椼

浠庢煡璇腑1娆″垹闄1涓〃锛岀洿鑷宠繑鍥炰簡鏌愪簺琛屼负姝€傚鏋滆〃寰堝ぇ锛岃緝濂界殑涓绘剰鏄湪鏌ヨ涓娇鐢LIMIT 10

瀵逛簬鍏锋湁涓庝笂娆′粠鏌ヨ涓垹闄ょ殑琛ㄥ尮閰嶇殑琛岀殑鍒楋紝鍙戝嚭SELECT鏌ヨ銆

濡傛灉灏FLOATDOUBLE鍒椾笌鍏锋湁鏁板肩被鍨嬬殑鏁板艰繘琛屾瘮杈冿紝涓嶈兘浣跨敤绛夊紡(=)姣旇緝銆傚湪澶у鏁拌绠楁満璇█涓紝璇ラ棶棰樺緢甯歌锛岃繖鏄洜涓猴紝骞堕潪鎵鏈夌殑娴偣鍊煎潎鑳戒互鍑嗙‘鐨勭簿搴︿繚瀛樸傚湪鏌愪簺鎯呭喌涓嬶紝灏FLOAT鏇存敼涓DOUBLE鍙洿姝h闂銆璇峰弬瑙A.5.8鑺傦紝鈥滀笌娴偣姣旇緝鏈夊叧鐨勯棶棰樷

濡傛灉浠嶄笉鑳芥壘鍑洪棶棰樹箣鎵鍦紝璇峰垱寤鸿兘涓庢樉绀洪棶棰樼殑鈥mysql test < query.sql鈥濅竴璧疯繍琛岀殑鏈灏忔祴璇曘傞氳繃浣跨敤mysqldump --quick db_name tbl_name_1 ... tbl_name_n > query.sql杞偍琛紝鍙垱寤烘祴璇曟枃浠躲傚湪缂栬緫鍣ㄤ腑鎵撳紑鏂囦欢锛屽垹闄ゆ煇浜涙彃鍏ョ殑琛岋紙濡傛灉鏈夎秴鍑烘紨绀洪棶棰樻墍闇鐨勮锛夛紝骞跺湪鏂囦欢鏈熬娣诲姞SELECT璇彞銆

閫氳繃鎵ц涓嬭堪鍛戒护锛岄獙璇佹祴璇曟枃浠惰兘婕旂ず闂锛

shell> mysqladmin create test2
shell> mysql test2 < query.sql

浣跨敤mysqlbug灏嗘祴璇曟枃浠跺紶璐村埌鍝熷閫氱敤MySQL閭欢鍒楄〃銆傝鍙傝1.7.1.1鑺傦紝鈥淭he MySQL閭欢鍒楄〃鈥

A.5.8. 涓庢诞鐐规瘮杈冩湁鍏崇殑闂

娉ㄦ剰锛屼笅杩伴儴鍒嗕富瑕佷笌DOUBLEFLOAT鍒楃浉鍏筹紝鍘熷洜鍦ㄤ簬娴偣鏁扮殑涓嶅噯纭湰璐ㄣMySQL浣跨敤64浣嶅崄杩涘埗鏁板肩殑绮惧害鎵цDECIMAL鎿嶄綔锛屽綋澶勭悊DECIMAL鍒楁椂锛屽簲鑳借В鍐冲ぇ澶氭暟甯歌鐨勪笉鍑嗙‘闂銆

娴偣鏁版湁鏃朵細瀵艰嚧娣锋穯锛岃繖鏄洜涓哄畠浠棤娉曚互鍑嗙‘鍊间繚瀛樺湪璁$畻鏈轰綋绯荤粨鏋勪腑銆備綘鍦ㄥ睆骞曚笂鎵鐪嬪埌鐨勫奸氬父涓嶆槸鏁板肩殑鍑嗙‘鍊笺傚浜FLOATDOUBLE鍒楃被鍨嬶紝鎯呭喌灏辨槸濡傛銆DECIMAL鍒楄兘淇濆瓨鍏锋湁鍑嗙‘绮惧害鐨勫硷紝杩欐槸鍥犱负瀹冧滑鏄敱瀛楃涓茶〃绀虹殑銆

鍦ㄤ笅闈㈢殑绀轰緥涓紝浠嬬粛浜嗕娇鐢DOUBLE鏃剁殑闂锛

mysql> CREATE TABLE t1 (i INT, d1 DOUBLE, d2 DOUBLE);
mysql> INSERT INTO t1 VALUES (1, 101.40, 21.40), (1, -80.00, 0.00),
    -> (2, 0.00, 0.00), (2, -13.20, 0.00), (2, 59.60, 46.40),
    -> (2, 30.40, 30.40), (3, 37.00, 7.40), (3, -29.60, 0.00),
    -> (4, 60.00, 15.40), (4, -10.60, 0.00), (4, -34.00, 0.00),
    -> (5, 33.00, 0.00), (5, -25.80, 0.00), (5, 0.00, 7.20),
    -> (6, 0.00, 0.00), (6, -51.40, 0.00);
 
mysql> SELECT i, SUM(d1) AS a, SUM(d2) AS b
    -> FROM t1 GROUP BY i HAVING a <> b;
 
+------+-------+------+
| i    | a     | b    |
+------+-------+------+
|    1 |  21.4 | 21.4 |
|    2 |  76.8 | 76.8 |
|    3 |   7.4 |  7.4 |
|    4 |  15.4 | 15.4 |
|    5 |   7.2 |  7.2 |
|    6 | -51.4 |    0 |
+------+-------+------+

缁撴灉鏄纭殑銆傚敖绠″墠5涓褰曠湅涓婂幓涓嶅簲鑳借繘琛屾瘮杈冩祴璇曪紙ab鐨勫肩湅涓婂幓娌℃湁浠涔堜笉鍚岋級锛屼絾瀹冧滑鑳借繘琛屾瘮杈冿紝杩欐槸鍥犱负鏄剧ず鐨勬暟鍊奸棿鐨勫樊寮傚湪鍗佸垎浣嶅乏鍙筹紝鍏蜂綋鎯呭喌鍙栧喅浜庤绠楁満鐨勪綋绯荤粨鏋勩

濡傛灉鍒d1d2瀹氫箟涓DECIMAL鑰屼笉鏄DOUBLESELECT鏌ヨ鐨勭粨鏋滀粎鍖呭惈1琛岋紝鍗充笂闈㈡樉绀虹殑鏈鍚1琛屻

A.6. 涓庝紭鍖栧櫒鏈夊叧鐨勪簨瀹

MySQL閲囩敤浜嗗熀浜庡紑閿鐨勪紭鍖栧櫒锛屼互纭畾澶勭悊鏌ヨ鐨勬渶瑙f柟寮忋傚湪寰堝鎯呭喌涓嬶紝MySQL鑳藉璁$畻鏈浣崇殑鍙兘鏌ヨ璁″垝锛屼絾鍦ㄦ煇浜涙儏鍐典笅锛MySQL娌℃湁鍏充簬鏁版嵁鐨勮冻澶熶俊鎭紝涓嶅緱涓嶅氨鏁版嵁杩涜鈥滄湁鏁欏吇鈥濈殑浼版祴銆

MySQL鏈兘鍋氣滄纭殑鈥濅簨鏃讹紝鍙娇鐢ㄤ笅杩板伐鍏锋潵甯姪MySQL

浣跨敤EXPLAIN璇彞鑾峰彇鍏充簬MySQL濡備綍澶勭悊鏌ヨ鐨勪俊鎭傝鎯充娇鐢ㄥ畠锛屽彲鍦SELECT璇彞鍓嶆坊鍔犲叧閿瓧EXPLAIN

mysql> EXPLAIN SELECT * FROM t1, t2 WHERE t1.i = t2.i;

鍏充簬EXPLAIN鐨勮缁嗚璁猴紝璇峰弬瑙7.2.1鑺傦紝鈥淓XPLAIN璇硶锛堣幏鍙栧叧浜嶴ELECT鐨勪俊鎭級

浣跨敤ANALYZE TABLE tbl_name锛屼负宸叉壂鎻忕殑琛ㄦ洿鏂伴敭鍒嗛厤銆傝鍙傝13.5.2.1鑺傦紝鈥淎NALYZE TABLE璇硶鈥

涓哄凡鎵弿鐨勮〃浣跨敤FORCE INDEX锛岄氱煡MySQL锛氫笌浣跨敤缁欏畾鐨勭储寮曠浉姣旓紝琛ㄦ壂鎻忓紑閿鏄傝吹銆傝鍙傝13.2.7鑺傦紝鈥淪ELECT璇硶鈥

SELECT * FROM t1, t2 FORCE INDEX (index_for_column)
WHERE t1.col_name=t2.col_name;

USE INDEXIGNORE INDEX涔熸湁涓瀹氱殑甯姪銆

鍏充簬鍏ㄥ眬鍜岃〃绾у埆鐨STRAIGHT_JOIN銆傝鍙傝13.2.7鑺傦紝鈥淪ELECT璇硶鈥

浣犲彲浠ヨ皟鑺傚叏灞鎴栫嚎绋嬬被绯荤粺鍙橀噺銆備緥濡傦紝鐢ㄢ--max-seeks-for-key=1000閫夐」鍚姩mysqld锛屾垨浣跨敤鈥淪ET max_seeks_for_key=1000鈥鏉ラ氱煡浼樺寲鍣細鍋囧畾浠讳綍琛ㄦ壂鎻忓潎涓嶄細瀵艰嚧1000涓互涓婄殑閿悳绱€璇峰弬瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥

A.7. 涓庤〃瀹氫箟鏈夊叧鐨勪簨瀹

A.7.1. 涓嶢LTER TABLE鏈夊叧鐨勯棶棰

ALTER TABLE灏嗚〃鏇存敼涓哄綋鍓嶅瓧绗﹂泦銆傚鏋滃湪鎵цALTER TABLE鎿嶄綔鏈熼棿閬囧埌閲嶅閿敊璇紝鍘熷洜鍦ㄤ簬鏂扮殑瀛楃闆嗗皢2涓敭鏄犲皠鍒颁簡鐩稿悓鍊硷紝鎴栨槸琛ㄥ凡鎹熷潖銆傚湪鍚庝竴绉嶆儏鍐典笅锛屽簲鍦ㄨ〃涓婅繍琛REPAIR TABLE

濡傛灉ALTER TABLE澶辫触骞剁粰鍑轰笅杩伴敊璇紝闂鍙兘鏄洜涓哄湪ALTER TABLE鎿嶄綔鐨勬棭鏈熼樁娈靛嚭鐜MySQL宕╂簝锛屾病鏈夊悕涓A-xxxB-xxx鐨勬棫琛細

Error on rename of './database/name.frm'
to './database/B-xxx.frm' (Errcode: 17)

鍦ㄨ鎯呭喌涓嬶紝杩涘叆MySQL鏁版嵁鐩綍锛屽苟鍒犻櫎鍏跺悕绉颁负浠A-B-寮濮嬬殑鎵鏈夋枃浠锛堟垨璁镐綘甯屾湜灏嗗畠浠Щ鍔ㄥ埌鍏朵粬鍦版柟鑰屼笉鏄垹闄ゅ畠浠級銆

ALTER TABLE鐨勫伐浣滄柟寮忓涓嬶細

  • 鐢ㄨ姹傜殑缁撴瀯鍙樺寲鍒涘缓鍚嶄负A-xxx鐨勬柊琛ㄣ
  • 灏嗘墍鏈夎浠庡師濮嬭〃鎷疯礉鍒A-xxx
  • 灏嗗師濮嬭〃閲嶅懡鍚嶄负B-xxx
  • A-xxx閲嶅懡鍚嶄负鍘熷琛ㄧ殑鍚嶇О銆
  • 鍒犻櫎B-xxx

濡傛灉鍦ㄩ噸鍛藉悕鎿嶄綔涓嚭閿欙紝MySQL灏嗗皾璇曟挙閿鏇存敼銆傚鏋滈敊璇緢涓ラ噸锛堝敖绠¤繖涓嶅簲鍑虹幇锛夛紝MySQL浼氬皢鏃ц〃淇濈暀涓B-xxx銆傜畝鍗曞湴鍦ㄧ郴缁熺骇鍒笂閲嶅懡鍚嶈〃鏂囦欢锛屽簲鑳戒娇鏁版嵁澶嶅師銆

濡傛灉鍦ㄤ簨鍔℃ц〃涓婁娇鐢ALTER TABLE锛屾垨姝e湪浣跨敤WindowsOS/2鎿嶄綔绯荤粺锛屽鏋滃凡鍦ㄨ〃涓婃墽琛屼簡LOCK TABLE鎿嶄綔锛ALTER TABLE灏嗗琛ㄦ墽琛岃В閿佹搷浣溿傝繖鏄洜涓InnoDB鍜岃繖绫绘搷浣滅郴缁熶笉鑳芥挙閿姝e湪浣跨敤鐨勮〃銆

A.7.2. 濡備綍鏇存敼琛ㄤ腑鐨勫垪椤哄簭

棣栧厛锛岃鑰冭檻鏄惁鐨勭‘闇瑕佹洿鏀硅〃涓殑鍒楅『搴忋SQL鐨勬牳蹇冭鐐规槸浠庢暟鎹瓨鍌ㄦ牸寮忚幏鍙栧簲鐢ㄣ傛诲簲鎸囧畾妫绱㈡暟鎹殑椤哄簭銆傚湪涓嬮潰鐨勭1鏉¤鍙ヤ腑锛屼互col_name1col_name2col_name3椤哄簭杩斿洖鍒楋紱鍦ㄧ2鏉¤鍙ヤ腑锛屼互col_name1col_name3col_name2椤哄簭杩斿洖鍒楋細

mysql> SELECT col_name1, col_name2, col_name3 FROM tbl_name;
mysql> SELECT col_name1, col_name3, col_name2 FROM tbl_name;

濡傛灉鍐冲畾鏇存敼琛ㄥ垪鐨勯『搴忥紝鍙墽琛屼笅杩版搷浣滐細

  1. 鐢ㄥ叿鏈夋柊椤哄簭鐨勫垪鍒涘缓鏂拌〃銆
  2. 鎵ц璇ヨ鍙ワ細
mysql> INSERT INTO new_table
    -> SELECT columns-in-new-order FROM old_table;
  1. 鎾ら攢鎴栭噸鍛藉悕old_table
  2. 灏嗘柊琛ㄩ噸鍛藉悕涓哄師濮嬪悕绉帮細
mysql> ALTER TABLE new_table RENAME old_table;

SELECT *鍗佸垎閫傚悎浜庢祴璇曟煡璇€備絾鏄紝鍦ㄥ簲鐢ㄧ▼搴忎腑锛屾案杩滀笉瑕佷緷璧SELECT *鐨勪娇鐢紝涓嶈渚濊禆鏍规嵁鍏朵綅缃绱㈠垪銆傚鏋滄坊鍔犮佺Щ鍔ㄦ垨鍒犻櫎浜嗗垪锛屾墍杩斿洖鐨勫垪鐨勯『搴忓拰浣嶇疆涓嶄細淇濇寔鐩稿悓銆傚琛ㄧ粨鏋勭殑绠鍗曟洿鏀逛篃浼氬鑷村簲鐢ㄧ▼搴忓け璐ャ

A.7.3.聽TEMPORARY TABLE闂

涓嬮潰浠嬬粛浜嗗浣跨敤TEMPORARY琛ㄧ殑闄愬埗锛

  • TEMPORARY琛ㄥ彧鑳芥槸HEAPISAMMyISAMMERGE銆佹垨InnoDB绫诲瀷銆
  • 鍦ㄧ浉鍚岀殑鏌ヨ涓紝涓嶈兘寮曠敤TEMPORARY1娆′互涓娿備緥濡傦紝涓嬩緥涓嶈兘姝e父宸ヤ綔锛
mysql> SELECT * FROM temp_table, temp_table AS t2;
閿欒1137锛氫笉鑳藉啀娆℃墦寮琛細'temp_table'
  • SHOW TABLES璇彞涓嶄細鍒楀嚭TEMPORARY琛ㄣ
  • 涓嶈兘浣跨敤RENAME閲嶅懡鍚TEMPORARY琛ㄣ浣嗚兘浣跨敤ALTER TABLE鍙栬屼唬涔嬶細
mysql> ALTER TABLE orig_name RENAME new_name;

A.8. MySQL涓殑宸茬煡浜嬪疁

鍦ㄦ湰鑺備腑锛屽垪鍑轰簡褰撳墠MySQL鐗堟湰涓殑宸茬煡浜嬪疁銆

鍏充簬骞冲彴鐩稿叧浜嬪疁鐨勬洿澶氫俊鎭紝璇峰弬瑙2.12鑺傦紝鈥滃叿浣撴搷浣滅郴缁熺浉鍏崇殑娉ㄦ剰浜嬮」鈥闄勫綍E锛绉绘鍒板叾浠栫郴缁涓殑瀹夎鍜岀Щ妞嶈鏄庛

A.8.1. MySQL涓殑鎵撳紑浜嬪疁

涓嬮潰鍒楀嚭浜嗗凡鐭ラ棶棰橈紝鏇存瀹冧滑鍏锋湁杈冮珮鐨勪紭鍏堢骇锛

  • 濡傛灉灏NULL鍊间笌浣跨敤ALL/ANY/SOME鐨勫瓙鏌ヨ杩涜姣旇緝锛岃屼笖瀛愭煡璇㈣繑鍥绌虹殑缁撴灉锛屾瘮杈冩搷浣滀細璇勪及NULL鐨勯潪鏍囧噯缁撴灉鑰屼笉鏄TRUEFALSE銆傚湪MySQL 5.1涓皢鏇存璇ラ棶棰樸
  • 瀵逛簬IN鐨勭嚎瀛愭煡璇紭鍖栦笉鍍忊=鈥濋偅鏍锋湁鏁堛
  • 鍗充娇浣跨敤浜lower_case_table_names=2锛堝厑璁MySQL璁颁綇鏁版嵁搴撳悕鍜岃〃鍚嶄娇鐢ㄧ殑澶у皬鍐欙級锛屽浜庡嚱鏁DATABASE()鎴栧湪鍚勭鏃ュ織鍐咃紙鍦ㄤ笉鍖哄垎澶у皬鍐欑殑绯荤粺涓婏級锛MySQL涔熶笉浼氳浣忔暟鎹簱鍚嶄娇鐢ㄧ殑澶у皬鍐欐儏鍐点
  • 鍦ㄥ鍒舵搷浣滀腑锛屾挙閿FOREIGN KEY绾︽潫涓嶅伐浣滐紝杩欐槸鍥犱负绾︽潫鍙兘鍦ㄤ粠鏈嶅姟鍣ㄤ笂鏈夊彟涓涓悕绉般
  • REPLACE锛堜互鍙婂叿鏈REPLACE閫夐」鐨LOAD DATA锛変笉浼氳Е鍙ON DELETE CASCADE
  • 濡傛灉鏈娇鐢ㄦ墍鏈夊垪鑰屼笖浠呬娇鐢DISTINCT鍒楄〃涓殑鍒楋紝鍦GROUP_CONCAT()涓紝DISTINCT涓嶈兘涓ORDER BY涓璧峰伐浣溿
  • 濡傛灉1浣嶇敤鎴锋嫢鏈夐暱鏃堕棿杩愯鐨勪簨鍔★紝鑰屼笖鍙1浣嶇敤鎴锋挙閿浜嗗湪浜嬪姟涓洿鏂扮殑鏌1琛紝閭d箞鍦ㄨ〃鐢ㄤ簬浜嬪姟鏈韩涔嬪墠锛屽瓨鍦ㄨ緝灏忕殑鏈轰細锛屼細鍦ㄤ簩杩涘埗鏃ュ織涓寘鍚DROP TABLE鍛戒护銆傛垜浠鍒掓洿姝h闂锛屾柟娉曟槸璁DROP TABLE鍛戒护绛夊緟锛岀洿鑷宠〃鏈湪浠讳綍浜嬪姟涓娇鐢ㄤ负姝€
  • 灏嗗ぇ鐨勬暣鏁板硷紙浠嬩簬2632641涔嬮棿锛夋彃鍏ユ暟鍊兼垨瀛楃涓插垪鏃讹紝瀹冨皢浣滀负璐熷兼彃鍏ワ紝杩欐槸鍥犱负璇ユ暟鍊兼槸鍦ㄦ湁绗﹀彿鏁存暟鐜涓嬭瘎浼扮殑銆
  • 濡傛灉鏈嶅姟鍣ㄨ繍琛屽湪涓嶅叿澶囦簩杩涘埗鏃ュ織鍔熻兘鐨勬潯浠朵笅锛FLUSH TABLES WITH READ LOCK涓嶈兘灞忚斀COMMIT锛屾墽琛屽畬鏁村浠芥椂杩欏彲鑳戒細瀵艰嚧闂锛堣〃闂寸殑涓鑷存ч棶棰橈級銆
  • 鍦ㄦ煇浜涙儏鍐典笅锛屼綔鐢ㄥ湪BDB琛ㄤ笂鐨ANALYZE TABLE浼氬鑷磋〃涓嶅彲鐢紝鐩磋嚦閲嶅惎mysqld涓烘銆傚鏋滃嚭鐜拌鎯呭喌锛岃鍦MySQL閿欒鏂囦欢涓煡鎵句笅杩板舰寮忕殑閿欒锛
001207 22:07:56  bdb:  log_flush: LSN past current end-of-log
  • 鍦ㄦ墍鏈変簨鍔″畬鎴愪箣鍓嶏紝涓嶈鍦BDB琛紙姝e湪鍏朵笂杩愯澶氳鍙ヤ簨鍔★級涓婃墽琛ALTER TABLE锛堝彲鑳戒細蹇界暐浜嬪姟锛夈
  • 瀵逛簬姝e湪浣跨敤INSERT DELAYED鐨勮〃锛屽湪鍏朵笂鎵цANALYZE TABLEOPTIMIZE TABLEREPAIR TABLE鏃讹紝鍙兘浼氬鑷撮棶棰樸
  • 鍦ㄨ〃涓婃墽琛LOCK TABLE ...FLUSH TABLES ...鏃讹紝涓嶄繚璇佹病鏈夊畬鎴愪竴鍗婄殑浜嬪姟銆
  • BDB鎵撳紑鐨勯熷害鐩稿杈冩參銆傚鏋滀綘鍦ㄦ暟鎹簱涓婃湁寰堝BDB琛紝濡傛灉鏈娇鐢鈥-A鈥閫夐」鎴栨浣跨敤鍐嶆贩缂栧姛鑳斤紝瑕佹兂鍦ㄦ暟鎹簱涓婁娇鐢mysql瀹㈡埛绔紝闇瑕佽姳璐硅緝闀跨殑鏃堕棿銆傚綋浣犳湁澶х殑琛ㄩ珮閫熺紦鍐叉椂锛岃繖鐐瑰挨鍏舵槑鏄俱
  • 澶嶅埗鍔熻兘閲囩敤浜嗘煡璇㈢骇鏃ュ織鍔熻兘锛氫富鏈嶅姟鍣ㄥ皢宸叉墽琛岀殑鏌ヨ鍐欏叆浜岃繘鍒舵棩蹇椼傝繖鏄竴绉嶉熷害寰堝揩銆佺畝娲佸拰鏈夋晥鐨勮褰曟柟娉曪紝鍦ㄥぇ澶氭暟鎯呭喌涓嬪伐浣滆壇濂姐

濡傛灉浠ョ壒瀹氱殑鏂瑰紡璁捐鏌ヨ锛屼娇寰楁暟鎹洿鏀规槸闈炲喅瀹氭э紙閫氬父涓嶆帹鑽愶紝鍗充娇鍦ㄥ鍒朵箣澶栦篃鍚屾牱锛夛紝涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄤ笂鐨勬暟鎹皢鍙樺緱涓嶅悓銆

渚嬪锛

  • 灏0鎴朜ULL鍊兼彃鍏UTO_INCREMENT鍒椾腑鐨凜REATE ... SELECT鎴朓NSERT ... SELECT璇彞銆
  • DELETE锛屽鏋滀粠鍏锋湁ON DELETE CASCADE灞炴х殑澶栭敭鐨勮〃涓垹闄よ銆
  • REPLACE ... SELECT銆両NSERT IGNORE ... SELECT锛屽鏋滃湪鎻掑叆鐨勬暟鎹腑鍏锋湁閲嶅閿

褰撲笖浠呭綋鍓嶈堪鏌ヨ娌℃湁淇濊瘉鍐冲畾琛岄『搴忕殑ORDER BY瀛愬彞鏃躲

渚嬪锛屽浜庝笉鍏锋湁ORDER BYINSERT ... SELECTSELECT鍙兘浼氫互涓嶅悓鐨勯『搴忚繑鍥炶锛堝畠浼氬鑷村叿鏈変笉鍚岀瓑绾х殑琛岋紝浠庤屽鑷AUTO_INCREMENT鍒椾腑鐨勪笉鍚屾暟鍊硷級锛屽叿浣撴儏鍐靛彇鍐充簬浼樺寲鍣ㄥ湪涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄤ笂鎵浣滅殑閫夋嫨銆

鍦ㄤ富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒涓婏紝鏌ヨ灏嗚繘琛屼笉鍚岀殑浼樺寲锛屼粎褰擄細

  • 浣跨敤涓嶅悓鐨勫瓨鍌ㄥ紩鎿庡湪涓绘湇鍔″櫒涓婅屼笉鏄粠鏈嶅姟鍣ㄤ笂淇濆瓨琛ㄣ傦紙鑳藉鍦ㄤ富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒涓婁娇鐢ㄤ笉鍚岀殑瀛樺偍寮曟搸銆備緥濡傦紝濡傛灉浠庢湇鍔″櫒鍏锋湁杈冨皯鐨勫彲鐢ㄧ鐩樼┖闂达紝鍙互鍦ㄤ富鏈嶅姟鍣ㄤ笂浣跨敤InnoDB锛屼絾鍦 浠庢湇鍔″櫒妗戜娇鐢MyISAM锛夈
  • 鍦ㄤ富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒涓婏紝MySQL缂撳啿鍖哄ぇ灏忔槸涓嶅悓鐨勶紙key_buffer_size绛夛級銆
  • 鍦ㄤ富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒涓婅繍琛屼笉鍚岀殑MySQL鐗堟湰锛岀増鏈棿鐨勪紭鍖栧櫒浠g爜涔熶笉鍚屻

璇ラ棶棰樹篃浼氬奖鍝嶄娇鐢mysqlbinlog|mysql鐨勬暟鎹簱鎭㈠銆

閬垮厤璇ラ棶棰樼殑鏈绠鍗曟柟娉曟槸锛屼负鍓嶈堪鐨勯潪鍐冲畾鎬ф煡璇㈠鍔ORDER BY瀛愬彞锛屼互纭繚鎬绘槸浠ョ浉鍚岀殑椤哄簭淇濆瓨鎴栨洿鏀硅銆

鍦ㄥ皢鏉ョ殑MySQL鐗堟湰涓紝闇瑕佹椂锛屾垜浠皢鑷姩澧炲姞ORDER BY瀛愬彞銆

涓嬮潰鍒楀嚭浜嗗凡鐭ョ殑浜嬪疁锛岃繖浜涗簨瀹滃皢鍦ㄦ伆褰撶殑鏃跺欐洿姝o細

  • 鏃ュ織鏂囦欢鍚嶅熀浜庢湇鍔″櫒涓绘満鍚嶏紙濡傛灉鏈娇鐢ㄥ惎鍔ㄩ夐」鎸囧畾鏂囦欢鍚嶇殑璇濓級銆傚鏋滄洿鏀逛簡涓绘満鍚嶏紝浣犲皢涓嶅緱涓嶄娇鐢ㄨ濡傗--log-bin=old_host_name-bin鈥濈瓑閫変笅缇庡浗銆傚彟涓绉嶉夋嫨鏄噸鍛藉悕鏃ф枃浠讹紝浠ュ弽鏄犱富鏈哄悕鍙樻洿鎯呭喌锛堝鏋滄槸浜岃繘鍒舵棩蹇楋紝闇瑕佺紪杈戜簩杩涘埗鏃ュ織绱㈠紩鏂囦欢锛屽苟鏇存binlog鍚嶇О锛夈傝鍙傝5.3.1鑺傦紝鈥mysqld鍛戒护琛岄夐」鈥
  • Mysqlbinlog涓嶅垹闄ゆ墽琛LOAD DATA INFILE鍛戒护鍚庨仐鐣欑殑涓存椂鏂囦欢銆傝鍙傝8.6鑺傦紝鈥渕ysqlbinlog锛氱敤浜庡鐞嗕簩杩涘埗鏃ュ織鏂囦欢鐨勫疄鐢ㄥ伐鍏封
  • RENAME涓嶈兘涓TEMPORARY琛ㄤ竴璧峰伐浣滐紝涔熶笉鑳戒笌MERGE琛ㄤ腑浣跨敤鐨勮〃涓璧峰伐浣溿
  • 鐢变簬琛ㄥ畾涔夋枃浠剁殑淇濆瓨鏂瑰紡锛屼笉鑳藉湪琛ㄥ悕銆佸垪鍚嶆垨鏋氫妇涓娇鐢ㄥ瓧绗255CHAR(255)锛夈傛寜鐓у畨鎺掞紝褰撴垜浠疄鏂戒簡鏂扮殑琛ㄥ畾涔夋牸寮忔枃浠舵椂锛屽皢鍦5.1鐗堜腑鏇存璇ラ棶棰樸
  • 浣跨敤SET CHARACTER SET鏃讹紝涓嶈兘鍦ㄦ暟鎹簱銆佽〃鍜屽垪鍚嶄腑浣跨敤杞崲鐨勫瓧绗︺
  • 涓嶈兘鍦LIKE ... ESCAPE涓笌ESCAPE涓璧蜂娇鐢_鈥欐垨鈥%鈥欍
  • 濡傛灉浣犳湁1DECIMAL鍒楋紝鍏朵腑锛岀浉鍚岀殑鏁板间互涓嶅悓鐨勬牸寮忎繚瀛橈紙渚嬪锛+01.001.0001.00锛夛紝GROUP BY鍙兘浼氬皢姣忎釜鍊煎綋浣滀笉鍚岀殑鍊笺
  • 浣跨敤MIT-pthreads鏃讹紝涓嶈兘鍦ㄥ彟涓涓洰褰曚笅鍒涘缓鏈嶅姟鍣ㄣ傝繖鏄洜涓哄畠闇瑕佹洿鏀MIT-pthreads锛屾垜浠笉澶細鏇存璇ラ棶棰樸傝鍙傝2.8.5 MIT-pthreads娉ㄦ剰浜嬮」鈥
  • GROUP BYORDER BYDISTINCT涓紝涓嶈兘鍙潬鍦颁娇鐢BLOBTEXT鍊笺傚湪杩欑被鎯呭喌涓嬶紝涓BLOB鍊艰繘琛屾瘮杈冩椂锛屼粎浣跨敤鏈鍓嶇殑max_sort_length瀛楄妭銆max_sort_length鐨勯粯璁ゅ兼槸1024锛屽彲鍦ㄦ湇鍔″櫒鍚姩鏃舵垨杩愯鏃舵洿鏀瑰畠銆
  • 鏁板艰绠楁槸浣跨敤BIGINTDOUBLE锛堟甯告儏鍐典笅鍧囦负64浣嶉暱锛夎繘琛岀殑銆浣犳墍鑳借幏寰楃殑绮惧害鍙栧喅浜庡嚱鏁般傞氱敤瑙勫垯鏄綅鍑芥暟鏄寜BIGINT绮惧害鎵ц鐨勶紝IFELT()鏄寜BIGINTDOUBLE绮惧害鎵ц鐨勶紝鍏朵綑鐨勫嚱鏁版槸鎸DOUBLE绮惧害鎵ц鐨勩傚浜庨櫎浣嶅瓧娈靛鐨勫叾浠栨暟锛屽鏋滃ぇ浜63浣嶏紙9223372036854775807锛夛紝搴旈伩鍏嶄娇鐢鏃犵鍙烽暱long鍊笺
  • 1涓〃涓紝鏈澶氳兘鏈255ENUMSET鍒椼
  • MIN()MAX()浠ュ強鍏朵粬鑱氬悎鍑芥暟涓紝MySQL鐩墠浼氭牴鎹叾瀛楃涓插兼瘮杈ENUMSET鍒楋紝鑰屼笉鏄牴鎹瓧绗︿覆鍦ㄩ泦鍚堜腑鐨勭浉瀵逛綅缃
  • mysqld_safe浼氬皢鏉ヨ嚜mysqld鐨勬墍鏈夋秷鎭啀瀹氬悜鍒mysqld鏃ュ織銆備笌涔嬬浉鍏崇殑1涓棶棰樻槸锛屽鏋滀綘鎵цmysqladmin refresh鍏抽棴骞跺啀娆℃墦寮鏃ュ織锛stdoutstderr浠嶄細琚噸瀹氬悜鍒版棫鐨勬棩蹇椼濡傛灉浣犱互骞夸箟鏂瑰紡浣跨敤鈥--log鈥搴旂紪杈mysqld_safe浠ヨ褰曞埌host_name.err鑰屼笉鏄host_name.log锛屼互渚块氳繃鍒犻櫎瀹冨苟鎵цmysqladmin refresh锛屾柟渚垮湴鏀跺洖涓烘棫鏃ュ織鍒嗛厤鐨勭┖闂淬
  • UPDATE璇彞涓紝鍒椾粠宸﹀悜鍙虫洿鏂般傚鏋滃紩鐢ㄤ簡宸叉洿鏂扮殑鍒楋紝浣犲皢寰楀埌鏇存柊鍊艰屼笉鏄師濮嬪笺備緥濡傦紝涓嬭堪璇彞浼氬皢KEY澧炲姞2锛岃屼笉鏄1
mysql> UPDATE tbl_name SET KEY=KEY+1,KEY=KEY+1;
  • 浣犲彲浠ュ湪鐩稿悓鏌ヨ涓紩鐢ㄥ涓复鏃惰〃锛屼絾涓嶈兘寮曠敤浠讳綍缁欏畾鐨勪复鏃惰〃1娆′互涓娿備緥濡傦紝涓嬭堪璇彞涓嶈兘姝e父宸ヤ綔锛
mysql> SELECT * FROM temp_table, temp_table AS t2;
閿欒1137锛氫笉鑳藉啀娆℃墦寮琛細'temp_table'
  • 褰撲綘鍦ㄨ仈鍚堟搷浣滀腑浣跨敤鈥滈殣鍚濆垪鏃讹紝涓庢湭浣跨敤闅愬惈鍒楃浉姣旓紝浼樺寲鍣ㄥ皢浠ヤ笉鍚岀殑鏂瑰紡澶勭悊DISTINCT銆傚湪鑱斿悎鎿嶄綔涓紝闅愬惈鍒楀皢浣滀负缁撴灉鐨勭粍鎴愰儴浠借鏁帮紙鍗充娇鏈樉绀猴級锛屼絾鍦ㄦ甯告煡璇腑锛岄殣鍚垪涓嶅弬涓DISTINCT姣旇緝銆傚湪浠ュ悗锛屾垜浠彲鑳戒細鏇存敼璇ユ儏鍐碉紝鍦ㄦ墽琛DISTINCT鏃朵笉姣旇緝闅愬惈鍒椼

渚嬪锛

SELECT DISTINCT mp3id FROM band_downloads
       WHERE userid = 9 ORDER BY id DESC;

浠ュ強

SELECT DISTINCT band_downloads.mp3id
       FROM band_downloads,band_mp3
       WHERE band_downloads.userid = 9
       AND band_mp3.id = band_downloads.mp3id
       ORDER BY band_downloads.id DESC;

鍦ㄧ2绉嶆儏鍐典笅锛屼娇鐢MySQL鏈嶅姟鍣3.23.x锛屽彲鍦ㄧ粨鏋滈泦涓幏寰2涓瓑鍚岃锛堣繖鏄洜涓猴紝闅愯棌ID鍒椾腑鐨勫煎彲鑳戒笉鍚岋級銆

娉ㄦ剰锛屽湪缁撴灉闆嗕腑锛屼粎瀵逛笉鍚ORDER BY鍒楃殑鏌ヨ鎵嶄細鍑虹幇璇ユ儏鍐点

  • 濡傛灉鍦ㄨ繑鍥炵┖闆嗙殑鏌ヨ涓婃墽琛PROCEDURE锛屽湪鏌愪簺鎯呭喌涓嬶紝PROCEDURE涓嶈浆鎹㈠垪銆
  • 鍒涘缓鍏锋湁MERGE绫诲瀷鐨勮〃鏃讹紝涓嶆鏌ュ熀鏈〃鏄惁鍏锋湁鍏煎鐨勭被鍨嬨
  • 濡傛灉浣跨敤ALTER TABLEMERGE琛ㄤ腑浣跨敤鐨勮〃澧炲姞浜UNIQUE绱㈠紩锛岀劧鍚庡湪MERGE琛ㄤ笂澧炲姞浜嗘甯哥储寮曪紝濡傛灉鍦ㄨ〃涓瓨鍦ㄦ棫鐨勩侀潪UNIQUE閿紝瀵逛簬杩欎簺琛紝閿『搴忔槸涓嶅悓鐨勩傝繖鏄洜涓猴紝ALTER TABLE浼氬皢UNIQUE绱㈠紩鏀惧湪姝e父绱㈠紩涔嬪墠锛屼互渚胯兘灏芥棭妫娴嬪埌閲嶅鐨勯敭銆
 

杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆傚師濮嬪弬鑰冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆