{"id":49,"date":"2023-07-20T10:30:40","date_gmt":"2023-07-20T02:30:40","guid":{"rendered":"https:\/\/guoyingxu.cn\/?p=49"},"modified":"2023-07-20T10:30:40","modified_gmt":"2023-07-20T02:30:40","slug":"install_mongodb","status":"publish","type":"post","link":"https:\/\/guoyingxu.cn\/?p=49","title":{"rendered":"install_MongoDB"},"content":{"rendered":"\n<!doctype html>\n<html>\n<head>\n<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'>\n<title><\/title>\n<\/head>\n<body><h2 id='installmongodb'>install_MongoDB<\/h2>\n<pre><code class='language-shell' lang='shell'>#!\/usr\/bin\/bash\nrm -rf \/root\/mongodb-linux-x86_64-3.0.0 mongodb-linux-x86_64-3.0.0.tgz \nrm -rf \/usr\/local\/mongodb\ncd $home;\necho &quot;\u5f00\u59cb\u4e0b\u8f7d\u5b89\u88c5\u5305&quot;\nwget https:\/\/fastdl.mongodb.org\/linux\/mongodb-linux-x86_64-3.0.0.tgz\ntar tf mongodb-linux-x86_64-3.0.0.tgz\nif [ $? -eq 0  ];then\n\ttar -zxvf mongodb-linux-x86_64-3.0.0.tgz;\n\tif [ ! -d &quot;\/usr\/local\/mongodb\/&quot; ];then\n\t\techo &quot;\u79fb\u52a8\u6587\u4ef6&quot;;\n\t\tmv \/root\/mongodb-linux-x86_64-3.0.0 \/usr\/local\/mongodb;\n\t\tmkdir -p \/usr\/local\/mongodb\/data \/usr\/local\/mongodb\/log;\n\t\t\tif [ $? -eq 0 ];then\n\t\t\t\t\/usr\/local\/mongodb\/bin\/mongod --dbpath=\/usr\/local\/mongodb\/data --logpath=\/usr\/local\/mongodb\/log\/mongodb.log --logappend --port=27017 --fork\n\t\t\t\techo &quot;\u670d\u52a1\u5df2\u521b\u5efa&quot;\n\t\t\t\techo &quot;\u8bf7\u901a\u8fc7\/usr\/local\/mongodb\/bin\/mongo \u8fdb\u884c\u8bbf\u95ee&quot;\n\t\t\tfi\n\telse\n\t\t\techo &quot;\u6587\u4ef6\u5df2\u5b58\u5728&quot;\n\t\t\texit\n\tfi\nelse\n\techo &quot;\u6587\u4ef6\u635f\u574f&quot;\n\texit\nfi\n<\/code><\/pre>\n<pre><code class='language-shell' lang='shell'>#!\/usr\/bin\/bash\n#\u4e3b\u8981\u5b9e\u73b0\u4ee5\u4e0b\u51e0\u4e2a\u529f\u80fd\u3002\n#1\u3001\u8fdc\u7a0b\u4e3b\u673a\u63a8\u9001\u516c\u94a5\u3002\n#2\u3001\u62f7\u8d1d\u6587\u4ef6\u914d\u7f6emongo.conf\uff0c\u5b8c\u6210\u526f\u672c\u96c6\u7fa4\u7684\u521b\u5efa\n#3\u3001\u914d\u7f6e\u8fdc\u7a0b\u4e3b\u673a\u521d\u59cb\u5316\u526f\u672c\u96c6\u7fa4\u529f\u80fd\u3002\n#\u6ce8\u610f\uff0c\u9700\u8981\u914d\u7f6emongo\u4ece\u8282\u70b9\u53ef\u5199\u72b6\u6001\u9700\u8981\u5728\u4ece\u8282\u70b9\u6267\u884crs.slaveOk()\u547d\u4ee4\u3002\n#\u914d\u7f6e\u7684mongo.conf\u6587\u4ef6\u4e2d\u7f29\u8fdb\u5e94\u4ee5\u56db\u4e2a\u7a7a\u683c\u4e3a\u524d\u63d0\uff0c\u4e8c\u7ea7\u7f29\u8fdb\u516b\u4e2a\u7a7a\u683c\uff0c\u4e0d\u7136\u4f1a\u51fa\u73b0\u914d\u7f6e\u65e0\u6cd5\u542f\u52a8\u670d\u52a1\u62a5yml\u9519\u8bef\u95ee\u9898\n#\u914d\u7f6e\u7684\u6bcf\u4e2a\u4e3b\u673a\u7684\u7aef\u53e3\u4e0d\u4e00\uff0c\u811a\u672c\u6682\u65f6\u4e3a\u8bbe\u7f6eport\u4e3a\u53d8\u91cf\u9700\u8981\u6539\u4e3a\u4e00\u76f4\u53ef\u4ee5\u5168\u5c40\u66ff\u6362\n#expect send \u4e2d\u7684&quot;\u53f7\u548c:\u53f7\u9700\u8981\u8fdb\u884c\u8f6c\u8bd1\u4e0d\u7136\u4f1a\u51fa\u73b0\u62a5\u9519\u3002\n#\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4ece\u8282\u70b9\u662f\u6ca1\u6709\u8bfb\u5199\u6743\u9650\u7684\uff0c\u53ef\u4ee5\u589e\u52a0\u8bfb\u7684\u6743\u9650\uff0c\u4f46\u9700\u8981\u8fdb\u884c\u8bbe\u7f6e\u3002\n#\u9700\u8981\u767b\u5f55\u4ece\u8282\u70b9\u914d\u7f6e\u4ee5\u4e0b\u53c2\u6570rs.slaveOk()\n#\u6267\u884c\u540e\u67e5\u770b\u6570\u636e\u5e93 show dbs;\u4e0d\u663e\u793a\u62a5\u9519\u8bf4\u660e\u6210\u529f\n#version v1.0 by gyx\nip=&#39;\n192.168.98.157\n192.168.98.158\n192.168.98.159\n&#39;\n#\u4e3b\u8282\u70b9ip\nip_1=192.168.98.157\n#\u526f\u672c\u8282\u70b9ip\nip_2=192.168.98.158\n#\u4ef2\u88c1\u8282\u70b9ip\nip_3=192.168.98.159\npassword=123456\n#\u901a\u7528passwd\nmongodb_file_name=&#39;mongodb-linux-x86_64-4.0.10.tgz&#39;\n\nfor a in $ip\ndo\n\tping c1 -w1 $a &amp;&gt;\/dev\/null\n\tif [ $? -ne 0 ];then\n\t\techo &quot;$a \u4e3b\u673a\u4e0d\u901a&quot;  &gt; ip.txt\n\tfi\ndone\n\n\nfor  i in $ip\ndo\n\/usr\/bin\/expect &lt;&lt;-EOF\nspawn ssh-copy-id $i\nexpect {\n\t&quot;yes\/no&quot; { send &quot;yes\\r&quot;; exp_continue }\n\t&quot;password&quot; { send &quot;$password\/r&quot;}\n}\nEOF\ndone\n\nfor j in $ip\ndo\n\techo &quot;copy fle $i &quot;\n\tssh root@$j &quot;\nrm -rf \/root\/$mongodb_file_name\nrm -rf \/usr\/local\/mongodb&quot;\n\tscp $mongodb_file_name root@$j:\/root\/\nssh root@$j &quot;tar -zxvf \/root\/$mongodb_file_name \nmv \/root\/mongodb-linux-x86_64-4.0.10 \/usr\/local\/mongodb\n&quot;\ndone\n#\u521b\u5efa\u4e3b\u8282\u70b9\nssh root@$ip_1 &quot;\nmkdir -p \/mongodb\/replica_sets\/myrs_27017\/log\nmkdir -p \/mongodb\/replica_sets\/myrs_27017\/data\/db\ncat &lt;&lt;EOF &gt;\/mongodb\/replica_sets\/myrs_27017\/mongod.conf\nsystemLog:\n    #MongoDB\u53d1\u9001\u6240\u6709\u65e5\u5fd7\u8f93\u51fa\u7684\u76ee\u6807\u6307\u5b9a\u4e3a\u6587\u4ef6\n    destination: file\n    #mongod\u6216mongos\u5e94\u5411\u5176\u53d1\u9001\u6240\u6709\u8bca\u65ad\u65e5\u5fd7\u8bb0\u5f55\u4fe1\u606f\u7684\u65e5\u5fd7\u6587\u4ef6\u7684\u8def\u5f84\n    path: \/mongodb\/replica_sets\/myrs_27017\/log\/mongod.log\n    #\u5f53mongos\u6216mongod\u5b9e\u4f8b\u91cd\u65b0\u542f\u52a8\u65f6\uff0cmongos\u6216mongod\u4f1a\u5c06\u65b0\u6761\u76ee\u9644\u52a0\u5230\u73b0\u6709\u65e5\u5fd7\u6587\u4ef6\u7684\u672b\u5c3e\u3002\n    logAppend: true\nstorage:\n    #mongod\u5b9e\u4f8b\u5b58\u50a8\u5176\u6570\u636e\u7684\u76ee\u5f55\u3002storage.dbPath\u8bbe\u7f6e\u4ec5\u9002\u7528\u4e8emongod\u3002\n    dbPath: \/mongodb\/replica_sets\/myrs_27017\/data\/db\n    journal:\n        #\u542f\u7528\u6216\u7981\u7528\u6301\u4e45\u6027\u65e5\u5fd7\u4ee5\u786e\u4fdd\u6570\u636e\u6587\u4ef6\u4fdd\u6301\u6709\u6548\u548c\u53ef\u6062\u590d\u3002\n        enabled: true\nprocessManagement:\n    #\u542f\u7528\u5728\u540e\u53f0\u8fd0\u884cmongos\u6216mongod\u8fdb\u7a0b\u7684\u5b88\u62a4\u8fdb\u7a0b\u6a21\u5f0f\u3002\n    fork: true\n    #\u6307\u5b9a\u7528\u4e8e\u4fdd\u5b58mongos\u6216mongod\u8fdb\u7a0b\u7684\u8fdb\u7a0bID\u7684\u6587\u4ef6\u4f4d\u7f6e\uff0c\u5176\u4e2dmongos\u6216mongod\u5c06\u5199\u5165\u5176PID\n    pidFilePath: \/mongodb\/replica_sets\/myrs_27017\/log\/mongod.pid\nnet:\n    #\u670d\u52a1\u5b9e\u4f8b\u7ed1\u5b9a\u6240\u6709IP\uff0c\u6709\u526f\u4f5c\u7528\uff0c\u526f\u672c\u96c6\u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u8282\u70b9\u540d\u5b57\u4f1a\u81ea\u52a8\u8bbe\u7f6e\u4e3a\u672c\u5730\u57df\u540d\uff0c\u800c\u4e0d\u662fip\n    #bindIpAll: true\n    #\u670d\u52a1\u5b9e\u4f8b\u7ed1\u5b9a\u7684IP\n    bindIp: localhost,$ip_1\n    #bindIp\n    #\u7ed1\u5b9a\u7684\u7aef\u53e3\n    port: 27017\nreplication:\n    #\u526f\u672c\u96c6\u7684\u540d\u79f0\n    replSetName: myrs\nEOF\n\/usr\/local\/mongodb\/bin\/mongod -f \/mongodb\/replica_sets\/myrs_27017\/mongod.conf &gt;\/root\/log.txt\ncat &lt;&lt;EOF &gt;\/root\/mongo_start.sh\n#!\/usr\/bin\/bash\n\/usr\/local\/mongodb\/bin\/mongod -f \/mongodb\/replica_sets\/myrs_27017\/mongod.conf \nEOF\n&quot;\n\n#\u521b\u5efa\u526f\u672c\u8282\u70b9\nssh root@$ip_2 &quot;\nmkdir -p \/mongodb\/replica_sets\/myrs_27018\/log \nmkdir -p \/mongodb\/replica_sets\/myrs_27018\/data\/db\ncat &lt;&lt;EOF &gt;\/mongodb\/replica_sets\/myrs_27018\/mongod.conf\nsystemLog:\n    #MongoDB\u53d1\u9001\u6240\u6709\u65e5\u5fd7\u8f93\u51fa\u7684\u76ee\u6807\u6307\u5b9a\u4e3a\u6587\u4ef6\n    destination: file\n    #mongod\u6216mongos\u5e94\u5411\u5176\u53d1\u9001\u6240\u6709\u8bca\u65ad\u65e5\u5fd7\u8bb0\u5f55\u4fe1\u606f\u7684\u65e5\u5fd7\u6587\u4ef6\u7684\u8def\u5f84\n    path: \/mongodb\/replica_sets\/myrs_27018\/log\/mongod.log\n    #\u5f53mongos\u6216mongod\u5b9e\u4f8b\u91cd\u65b0\u542f\u52a8\u65f6\uff0cmongos\u6216mongod\u4f1a\u5c06\u65b0\u6761\u76ee\u9644\u52a0\u5230\u73b0\u6709\u65e5\u5fd7\u6587\u4ef6\u7684\u672b\u5c3e\u3002\n    logAppend: true\nstorage:\n    #mongod\u5b9e\u4f8b\u5b58\u50a8\u5176\u6570\u636e\u7684\u76ee\u5f55\u3002storage.dbPath\u8bbe\u7f6e\u4ec5\u9002\u7528\u4e8emongod\u3002\n    dbPath: \/mongodb\/replica_sets\/myrs_27018\/data\/db\n    journal:\n        #\u542f\u7528\u6216\u7981\u7528\u6301\u4e45\u6027\u65e5\u5fd7\u4ee5\u786e\u4fdd\u6570\u636e\u6587\u4ef6\u4fdd\u6301\u6709\u6548\u548c\u53ef\u6062\u590d\u3002\n        enabled: true\nprocessManagement:\n    #\u542f\u7528\u5728\u540e\u53f0\u8fd0\u884cmongos\u6216mongod\u8fdb\u7a0b\u7684\u5b88\u62a4\u8fdb\u7a0b\u6a21\u5f0f\u3002\n    fork: true\n    #\u6307\u5b9a\u7528\u4e8e\u4fdd\u5b58mongos\u6216mongod\u8fdb\u7a0b\u7684\u8fdb\u7a0bID\u7684\u6587\u4ef6\u4f4d\u7f6e\uff0c\u5176\u4e2dmongos\u6216mongod\u5c06\u5199\u5165\u5176PID\n    pidFilePath: \/mongodb\/replica_sets\/myrs_27018\/log\/mongod.pid\nnet:\n    #\u670d\u52a1\u5b9e\u4f8b\u7ed1\u5b9a\u6240\u6709IP\uff0c\u6709\u526f\u4f5c\u7528\uff0c\u526f\u672c\u96c6\u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u8282\u70b9\u540d\u5b57\u4f1a\u81ea\u52a8\u8bbe\u7f6e\u4e3a\u672c\u5730\u57df\u540d\uff0c\u800c\u4e0d\u662fip\n    #bindIpAll: true\n    #\u670d\u52a1\u5b9e\u4f8b\u7ed1\u5b9a\u7684IP\n    bindIp: localhost,$ip_2\n    #bindIp\n    #\u7ed1\u5b9a\u7684\u7aef\u53e3\n    port: 27018\nreplication:\n    #\u526f\u672c\u96c6\u7684\u540d\u79f0\n    replSetName: myrs\nEOF\n\/usr\/local\/mongodb\/bin\/mongod -f \/mongodb\/replica_sets\/myrs_27018\/mongod.conf &gt;\/root\/log.txt\ncat &lt;&lt;EOF &gt;\/root\/mongo_start.sh\n#!\/usr\/bin\/bash\n\/usr\/local\/mongodb\/bin\/mongod -f \/mongodb\/replica_sets\/myrs_27018\/mongod.conf \nEOF\n&quot;\n\n\n#\u521b\u5efa\u4ef2\u88c1\u8282\u70b9\nssh root@$ip_3 &quot;\nmkdir -p \/mongodb\/replica_sets\/myrs_27019\/log\nmkdir -p \/mongodb\/replica_sets\/myrs_27019\/data\/db\ncat &lt;&lt;EOF &gt;\/mongodb\/replica_sets\/myrs_27019\/mongod.conf\nsystemLog:\n    #MongoDB\u53d1\u9001\u6240\u6709\u65e5\u5fd7\u8f93\u51fa\u7684\u76ee\u6807\u6307\u5b9a\u4e3a\u6587\u4ef6\n    destination: file\n    #mongod\u6216mongos\u5e94\u5411\u5176\u53d1\u9001\u6240\u6709\u8bca\u65ad\u65e5\u5fd7\u8bb0\u5f55\u4fe1\u606f\u7684\u65e5\u5fd7\u6587\u4ef6\u7684\u8def\u5f84\n    path: \/mongodb\/replica_sets\/myrs_27019\/log\/mongod.log\n    #\u5f53mongos\u6216mongod\u5b9e\u4f8b\u91cd\u65b0\u542f\u52a8\u65f6\uff0cmongos\u6216mongod\u4f1a\u5c06\u65b0\u6761\u76ee\u9644\u52a0\u5230\u73b0\u6709\u65e5\u5fd7\u6587\u4ef6\u7684\u672b\u5c3e\u3002\n    logAppend: true\nstorage:\n    #mongod\u5b9e\u4f8b\u5b58\u50a8\u5176\u6570\u636e\u7684\u76ee\u5f55\u3002storage.dbPath\u8bbe\u7f6e\u4ec5\u9002\u7528\u4e8emongod\u3002\n    dbPath: \/mongodb\/replica_sets\/myrs_27019\/data\/db\n    journal:\n    #\u542f\u7528\u6216\u7981\u7528\u6301\u4e45\u6027\u65e5\u5fd7\u4ee5\u786e\u4fdd\u6570\u636e\u6587\u4ef6\u4fdd\u6301\u6709\u6548\u548c\u53ef\u6062\u590d\u3002\n        enabled: true\nprocessManagement:\n    #\u542f\u7528\u5728\u540e\u53f0\u8fd0\u884cmongos\u6216mongod\u8fdb\u7a0b\u7684\u5b88\u62a4\u8fdb\u7a0b\u6a21\u5f0f\u3002\n    fork: true\n    #\u6307\u5b9a\u7528\u4e8e\u4fdd\u5b58mongos\u6216mongod\u8fdb\u7a0b\u7684\u8fdb\u7a0bID\u7684\u6587\u4ef6\u4f4d\u7f6e\uff0c\u5176\u4e2dmongos\u6216mongod\u5c06\u5199\u5165\u5176PID\n    pidFilePath: \/mongodb\/replica_sets\/myrs_27019\/log\/mongod.pid\nnet:\n    #\u670d\u52a1\u5b9e\u4f8b\u7ed1\u5b9a\u6240\u6709IP\uff0c\u6709\u526f\u4f5c\u7528\uff0c\u526f\u672c\u96c6\u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u8282\u70b9\u540d\u5b57\u4f1a\u81ea\u52a8\u8bbe\u7f6e\u4e3a\u672c\u5730\u57df\u540d\uff0c\u800c\u4e0d\u662fip\n    #bindIpAll: true\n    #\u670d\u52a1\u5b9e\u4f8b\u7ed1\u5b9a\u7684IP\n    bindIp: localhost,$ip_3\n    #bindIp\n    #\u7ed1\u5b9a\u7684\u7aef\u53e3\n    port: 27019\nreplication:\n    #\u526f\u672c\u96c6\u7684\u540d\u79f0\n    replSetName: myrs\nEOF\n\/usr\/local\/mongodb\/bin\/mongod -f \/mongodb\/replica_sets\/myrs_27019\/mongod.conf &gt;\/root\/log.txt\ncat &lt;&lt;EOF &gt;\/root\/mongo_start.sh\n#!\/usr\/bin\/bash\n\/usr\/local\/mongodb\/bin\/mongod -f \/mongodb\/replica_sets\/myrs_27019\/mongod.conf\nEOF\n&quot;\n#\u68c0\u67e5\u670d\u52a1\nfor  g in $ip\ndo\n\tssh root@$g ps -ef|grep mongo\n\tif [ $? -eq 0 ];then\n\t\techo &quot; $g install success&quot;\n\telse\n\t\techo &quot;$g install fail&quot;\n\t\techo &quot;\u670d\u52a1\u672a\u542f\u52a8\u6210\u529f\u811a\u672c\u5c06\u65e0\u6cd5\u914d\u7f6emongo\u526f\u672c\u6a21\u5f0f\u3002\u7a0b\u5e8f\u9000\u51fa\u3002\u3002\u3002&quot;\n\t\texit\n\tfi\ndone\n\n#\u81ea\u52a8\u914d\u7f6e\u526f\u672c\u96c6\u6a21\u5f0f\n#&quot;rs.initiate()&quot; \u8282\u70b9\u521d\u59cb\u5316\n#rs.add(\\&quot;192.168.98.158\\:27018\\&quot; \u589e\u52a0\u526f\u672c\u8282\u70b9\n#rs.addArb(\\&quot;192.168.98.159\\:27019\\&quot;) \u589e\u52a0\u4ef2\u88c1\u8282\u70b9\n\/usr\/bin\/expect &lt;&lt;-EOF  \n        spawn ssh root@$ip_1\n        expect &quot;#*&quot;\n        send &quot;\/usr\/local\/mongodb\/bin\/mongo --host=$ip_1 --port=27017 \\n&quot;\n        expect &quot;#*&quot;\n        send &quot;rs.initiate() \\n&quot;\n        expect &quot;#*&quot;\n        send &quot;rs.add(\\&quot;$ip_2\\:27018\\&quot;) \\n&quot;\n        expect &quot;#*&quot;\n        send &quot;rs.addArb(\\&quot;$ip_3\\:27019\\&quot;) \\n&quot;\n        expect &quot;#*&quot;\n        send &quot;exit \\n&quot;\n        expect eof\nEOF\n\necho &quot;\u8fdb\u5165mongo\u4e3b\u8282\u70b9\u8f93\u5165rs.status() \u5728\u914d\u7f6e\u6709\u521a\u624d\u914d\u7f6e\u7684\u4e09\u4e2aip\u53ca\u526f\u672c\u521b\u5efa\u6210\u529f&quot;\necho &quot;\u767b\u5f55\u547d\u4ee4\u53c2\u7167    \/usr\/local\/mongodb\/bin\/mongo --host=$ip_1 --port=27017&quot;\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<\/body>\n<\/html>\n","protected":false},"excerpt":{"rendered":"<p>install_MongoDB #!\/usr\/bin\/bash rm -rf \/root\/mongodb-li [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/guoyingxu.cn\/index.php?rest_route=\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guoyingxu.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guoyingxu.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guoyingxu.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guoyingxu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=49"}],"version-history":[{"count":0,"href":"https:\/\/guoyingxu.cn\/index.php?rest_route=\/wp\/v2\/posts\/49\/revisions"}],"wp:attachment":[{"href":"https:\/\/guoyingxu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guoyingxu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guoyingxu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}