mongodb鐨勮繛鎺ユ睜绠$悊
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb鐨勮繛鎺ユ睜绠$悊相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%95%b0%e6%8d%ae' title='鏁版嵁'>鏁版嵁
鍗曚緥妯″紡 god red lin ntop 绾跨▼ 鍒涘缓 閫夋嫨https://blog.csdn.net/lin_tuer/article/details/78480203
鐢ㄥ崟渚嬫ā寮忓缓绔媘ongoDB鐨勮繛鎺ワ紝闇€瑕佹瘡娆℃搷浣滃畬鍏抽棴杩炴帴鍚楋紵
鍏充簬寤虹珛鍗曚緥妯″紡鐨凪ongoDB杩炴帴锛屽弬鑰冭繖閲孧ongoClient杩炴帴姹犵敤娉?/p>
寤虹珛浜嗕箣鍚庯紝姣忔鎿嶄綔璋冪敤鍚屼竴涓疄渚嬶紝MongoClient鍙堟槸绾跨▼瀹夊叏鐨勶紝璨屼技灏辨病鏈変粈涔堥棶棰樹簡銆備絾鏄?绐佺劧鎯冲埌锛岃繖浜涜繛鎺ヨ幏鍙栧悗锛屾搷浣滃畬涓嶄富鍔ㄩ噴鏀撅紝浼氫笉浼氫竴鐩磋鍗犳湁锛?/p>
– 浜庢槸寮€濮嬫壘鍚勭璧勬枡锛岃繖绡囨枃绔犵粰鍑轰簡寰堝舰璞$殑璇存槑锛孧ongoDB鐨勮繛鎺ユ睜鍜岀鐞?
– 鍘熸潵锛岀瓟妗堟槸娌℃湁蹇呰锛?
鍦ㄥ叧绯诲瀷鏁版嵁搴撲腑锛屾垜浠€绘槸闇€瑕佸叧闂娇鐢ㄧ殑鏁版嵁搴撹繛鎺ワ紝涓嶇劧澶ч噺鐨勫垱寤鸿繛鎺ヤ細瀵艰嚧璧勬簮鐨勬氮璐圭敋鑷充簬鏁版嵁搴撳畷鏈恒€?/p>
閫氬父鎴戜滑涔犳儻浜巒ew 涓€涓猚onnection骞朵笖閫氬父鍦╢inally璇彞涓皟鐢╟onnection鐨刢lose()鏂规硶灏嗗叾鍏抽棴銆傛宸э紝mongoDB涓綋鎴戜滑new涓€涓狹ongo鐨勬椂鍊欙紝浼氬彂鐜板畠涔熸湁涓€涓猚lose()鏂规硶銆傛墍浠ヤ細鍑虹幇杩欐牱鐨勬儏鍐碉細鎴戜滑鍦ㄩ渶瑕丏B鎿嶄綔鐨勬柟娉曚腑new涓€涓猰ongo瀹炰緥锛岀劧鍚庤皟鐢╩ongo.getDB()鏂规硶鎷垮埌瀵瑰簲鐨勮繛鎺ワ紝鎿嶄綔瀹屾暟鎹箣鍚庡啀璋冪敤mongo.close()鏂规硶鏉ュ叧闂繛鎺ャ€?鐪嬭捣鏉ヨ矊浼兼槸娌℃湁浠€涔堥棶棰橈紝浣嗘槸濡傛灉浣犲啀鐮旂┒涓€涓媘ongo鐨凙PI锛屼綘浼氬彂鐜拌繖鏍疯€虫湹鎿嶄綔灏辩浉褰撲簬鍥竵鍦ㄦ祰鑺辩殑鏃跺€欏幓鎵撲簡涓€妗舵按锛岀劧鍚庤垁浜嗕竴鍕烘按娴囦竴鏈佃姳锛岀劧鍚庝粬鎶婁竴妗舵按鍏ㄥ€掍簡鍥炲幓锛岄噸鏂版墦涓€妗舵按锛屽啀鑸€浜嗕竴鍕烘按娴囧彟澶栦竴鏈佃姳銆傘€傘€?/p>
璇村埌杩欓噷澶у搴旇閮藉凡缁忔槑鐧戒簡锛屽叾瀹炲綋浣爊ew Mongo()鐨勬椂鍊欙紝灏卞垱寤轰簡涓€涓繛鎺ユ睜锛実etDB()鍙槸浠庤繖涓繛鎺ユ睜涓嬁涓€涓彲鐢ㄧ殑杩炴帴銆傝€岃繛鎺ユ睜鏄笉闇€瑕佹垜浠強鏃跺叧闂殑锛屾垜浠彲浠ュ湪绋嬪簭鐨勭敓鍛藉懆鏈熶腑缁存姢涓€涓繖鏍风殑鍗曚緥锛岃嚦浜庝粠杩炴帴姹犱腑鎷垮嚭鐨勮繛鎺ワ紝鎴戜滑闇€瑕佸叧闂悧锛熺瓟妗堟槸NO銆備綘浼氬彂鐜癉B鏍规湰娌℃湁close()涔嬬被鐨勬柟娉曘€傚湪mongoDB涓紝涓€涓繛鎺ユ睜浼氱淮鎸佷竴瀹氭暟鐩殑杩炴帴锛屽綋浣犻渶瑕佺殑鏃跺€欒皟鐢╣etDB()鍘昏繛鎺ユ睜涓嬁鍒拌繛鎺ワ紝鑰宮ongo浼氬湪杩欎釜DB鎵ц瀹屾暟鎹搷浣滄椂鍊欒嚜鍔ㄦ敹鍥炶繛鎺ュ埌杩炴帴姹犱腑寰呯敤銆傛墍浠ュ湪mongoDB涓ぇ瀹朵笉蹇呮媴蹇冭繛鎺ユ病鏈夊叧闂殑闂锛屽湪浣犻渶瑕佸湪鎵€鏈夋搷浣滅粨鏉熸垨鑰呮暣涓▼搴弒hutdown鐨勬椂鍊欒皟鐢╩ongo鐨刢lose()鏂规硶鍗冲彲銆?/p>
MongoDB甯﹀畨鍏ㄩ獙璇佺殑杩炴帴濡備綍璁剧疆MongoClientOptions
绠$悊濡備綍鍒涘缓MongoClient 杩欑瘒鏂囩珷缁欎簡璇︾粏鐨勪粙缁?MongoDB 杩炴帴 for java
鐢变簬鎴戠殑闇€瑕佸畨鍏ㄩ獙璇侊紝鎴戝氨閫夋嫨浜嗚繖绉?/p>
MongoClientOptions clientOptions =
new MongoClientOptions.Builder()
.connectionsPerHost(50)
.maxWaitTime(2000).build();
List<MongoCredential> lstCredentials =
Arrays.asList(MongoCredential.createMongoCRCredential(
"admin", "myinfo", "123456".toCharArray()));
client = new MongoClient(new ServerAddress("127.0.0.1"),lstCredentials, clientOptions);
client.close();
1
2
3
4
5
6
7
8
9
鍙槸 杩愯椤圭洰锛屽嵈鎶ュ嚭浜嗚璇侀敊璇紝鏌ヤ簡涔嬪悗鍙戠幇銆傘€傛柟娉曚笉瀵癸紝銆?/p>
Arrays.asList(MongoCredential.createMongoCRCredential(
"admin", "myinfo", "123456".toCharArray()));
1
2
鏀逛负
Arrays.asList(MongoCredential.createCredential(
"admin", "myinfo", "123456".toCharArray()));
1
2
濂戒簡灏便€傘€傘€?/p>
————————————————
鐗堟潈澹版槑锛氭湰鏂囦负CSDN鍗氫富銆宭in_tuer銆嶇殑鍘熷垱鏂囩珷锛岄伒寰?CC 4.0 BY-SA 鐗堟潈鍗忚锛岃浆杞借闄勪笂鍘熸枃鍑哄閾炬帴鍙婃湰澹版槑銆?br />鍘熸枃閾炬帴锛歨ttps://blog.csdn.net/lin_tuer/article/details/78480203
以上是关于mongodb鐨勮繛鎺ユ睜绠$悊的主要内容,如果未能解决你的问题,请参考以下文章
涓插彛鏈嶅姟鍣ㄧ瓑搴旂敤浜庢薄姘村鐞嗗巶鐨勮嚜鍔ㄧ洃鎺у拰鎺у埗绠$悊