{"id":2437,"date":"2018-09-25T23:00:15","date_gmt":"2018-09-25T14:00:15","guid":{"rendered":"http:\/\/okamurax.com\/?p=2437"},"modified":"2018-09-26T15:48:41","modified_gmt":"2018-09-26T06:48:41","slug":"mongodb%e5%9f%ba%e7%a4%8e","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2437","title":{"rendered":"MongoDB\u57fa\u790e"},"content":{"rendered":"<h2>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p><span style=\"color: #0000ff;\">\/etc\/yum.repos.d\/mongodb-org-4.0.repo<\/span><\/p>\n<p><span style=\"color: #0000ff;\">[mongodb-org-4.0]<\/span><br \/>\n<span style=\"color: #0000ff;\">name=MongoDB Repository<\/span><br \/>\n<span style=\"color: #0000ff;\">baseurl=https:\/\/repo.mongodb.org\/yum\/redhat\/$releasever\/mongodb-org\/4.0\/x86_64\/<\/span><br \/>\n<span style=\"color: #0000ff;\">gpgcheck=1<\/span><br \/>\n<span style=\"color: #0000ff;\">enabled=1<\/span><br \/>\n<span style=\"color: #0000ff;\">gpgkey=https:\/\/www.mongodb.org\/static\/pgp\/server-4.0.asc<\/span><\/p>\n<p>\u3092\u8a18\u8f09\u3057\u3001<\/p>\n<p><span style=\"color: #0000ff;\">yum install -y mongodb-org<\/span><\/p>\n<p>\u8d77\u52d5<br \/>\n<span style=\"color: #0000ff;\">systemctl start mongod<\/span><\/p>\n<p>\u505c\u6b62<br \/>\n<span style=\"color: #0000ff;\">systemctl stop mongod<\/span><\/p>\n<p>\u518d\u8d77\u52d5<br \/>\n<span style=\"color: #0000ff;\">systemctl restart mongod<\/span><\/p>\n<p>\u81ea\u52d5\u8d77\u52d5<br \/>\n<span style=\"color: #0000ff;\">systemctl enable mongod<\/span><\/p>\n<p>\u30b7\u30a7\u30eb\u8d77\u52d5<br \/>\n<span style=\"color: #0000ff;\">mongo<\/span><\/p>\n<p>\u30b7\u30a7\u30eb\u7d42\u4e86<br \/>\n<span style=\"color: #0000ff;\">exit<\/span><\/p>\n<h2>Mongo\u30b7\u30a7\u30eb\u30b3\u30de\u30f3\u30c9<\/h2>\n<p>db\u5207\u308a\u66ff\u3048(\u4f5c\u6210)<br \/>\n<span style=\"color: #0000ff;\">use DB<\/span><\/p>\n<p>db\u524a\u9664<br \/>\n<span style=\"color: #0000ff;\">db.dropDatabase()<\/span><\/p>\n<p>\u5229\u7528\u4e2ddb<br \/>\n<span style=\"color: #0000ff;\">db<\/span><\/p>\n<p>db\u4e00\u89a7<br \/>\n<span style=\"color: #0000ff;\">show dbs<\/span><\/p>\n<p>\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u4e00\u89a7<br \/>\n<span style=\"color: #0000ff;\">show collections<\/span><\/p>\n<p>\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u4f5c\u6210<br \/>\n<span style=\"color: #0000ff;\">db.createCollection(&#8220;\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d&#8221;)<br \/>\n\u203bC\u306f\u5927\u6587\u5b57<\/span><\/p>\n<p>\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u524a\u9664<br \/>\n<span style=\"color: #0000ff;\">db.\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d.drop()<\/span><\/p>\n<p>\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u4f5c\u6210<br \/>\n<span style=\"color: #0000ff;\">db.\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d.insert({&#8220;name&#8221;:&#8221;taro&#8221;,&#8221;age&#8221;:1})<br \/>\ndb.\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d.insert({&#8220;name&#8221;:&#8221;taro&#8221;,&#8221;age&#8221;:1, &#8220;addr&#8221;:&#8221;tokyo&#8221;})<br \/>\n<\/span><\/p>\n<p>\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u691c\u7d22<br \/>\n<span style=\"color: #0000ff;\">db.\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d.find()<br \/>\n\u203b\u3059\u3079\u3066<\/span><\/p>\n<p><span style=\"color: #0000ff;\">db.\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d.findOne()<br \/>\n\u203b\u4e00\u3064\u306e\u307f<\/span><\/p>\n<p><span style=\"color: #0000ff;\">db.\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d.find({&#8220;name&#8221;:&#8221;taro&#8221;})<br \/>\n\u203b\u6761\u4ef6<\/span><\/p>\n<p>\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u66f4\u65b0<br \/>\n<span style=\"color: #0000ff;\">db.\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d.update(\u691c\u7d22\u5185\u5bb9\u3001\u66f4\u65b0\u5185\u5bb9)<br \/>\ndb.\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d.update({&#8220;name&#8221;,&#8221;taro&#8221;},{&#8220;name&#8221;:&#8221;jiro&#8221;})<br \/>\n<\/span><\/p>\n<p><span style=\"color: #333333;\">\u901a\u5e38\u306f\u30d5\u30a3\u30fc\u30eb\u30c9\u304c\u5168\u3066\u4e0a\u66f8\u304d\u3055\u308c\u308b\u3002<br \/>\n\u66f4\u65b0\u3057\u305f\u3044\u5834\u5408$set\u3092\u4f7f\u3046\u3002<br \/>\n\u30d5\u30a3\u30fc\u30eb\u30c9\u304c\u5b58\u5728\u3059\u308c\u3070\u66f4\u65b0\u3001\u3057\u306a\u3044\u3068\u65b0\u3057\u3044\u30d5\u30a3\u30fc\u30eb\u30c9\u3068\u3057\u3066\u8ffd\u52a0\u3055\u308c\u308b\u3002<\/span><\/p>\n<p><span style=\"color: #0000ff;\">db.\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d.update({&#8220;name&#8221;,&#8221;taro&#8221;},{$set:{&#8220;name&#8221;:&#8221;jiro&#8221;}})<\/span><\/p>\n<p>\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u524a\u9664<br \/>\n<span style=\"color: #0000ff;\">db.\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d.remove()<\/span><br \/>\n<span style=\"color: #0000ff;\">\u203b\u3059\u3079\u3066<\/span><\/p>\n<p><span style=\"color: #0000ff;\">db.\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u540d.remove(\u691c\u7d22\u5185\u5bb9)<\/span><\/p>\n<h2>\u30e6\u30fc\u30b6\u30fc<\/h2>\n<p>\u30e6\u30fc\u30b6\u30fc\u306fDB\u3054\u3068\u306b\u4f5c\u6210admin\u306e\u30e6\u30fc\u30b6\u30fc\u306f\u7ba1\u7406\u8005\u3068\u306a\u308b\u3002<\/p>\n<p>\u73fe\u5728\u306eDB\u306b\u63a5\u7d9a\u4e2d\u306e\u30e6\u30fc\u30b6\u30fc<br \/>\n<span style=\"color: #0000ff;\">db.getUsers()<br \/>\ndb.system.users.find()<br \/>\n<\/span><\/p>\n<p>\u30e6\u30fc\u30b6\u4f5c\u6210<br \/>\n\u203b2.4\u307e\u3067\u306faddUser<\/p>\n<p><span style=\"color: #0000ff;\">use name_store<\/span><\/p>\n<p><span style=\"color: #0000ff;\">db.createUser({<\/span><br \/>\n<span style=\"color: #0000ff;\">user:&#8221;taro&#8221;,<\/span><br \/>\n<span style=\"color: #0000ff;\">pwd:&#8221;123&#8243;,<\/span><br \/>\n<span style=\"color: #0000ff;\">roles:[{<br \/>\n<\/span><span style=\"color: #0000ff;\">role: &#8220;readWrite&#8221;,<br \/>\ndb: &#8220;admin&#8221;<br \/>\n}]<\/span><br \/>\n<span style=\"color: #0000ff;\">})<\/span><\/p>\n<p>\u3042\u308b\u3044\u306f<\/p>\n<p><span style=\"color: #0000ff;\">db.createUser({<\/span><br \/>\n<span style=\"color: #0000ff;\">user:&#8221;jiro&#8221;,<\/span><br \/>\n<span style=\"color: #0000ff;\">pwd:&#8221;123&#8243;,<\/span><br \/>\n<span style=\"color: #0000ff;\">roles:[&#8220;dbAdminAnyDatabase&#8221;]\u00a0<\/span><br \/>\n<span style=\"color: #0000ff;\">})<\/span><\/p>\n<p><span style=\"color: #0000ff;\">db.createUser({<\/span><br \/>\n<span style=\"color: #0000ff;\">user:&#8221;shiro&#8221;,<\/span><br \/>\n<span style=\"color: #0000ff;\">pwd:&#8221;123&#8243;,<\/span><br \/>\n<span style=\"color: #0000ff;\">roles:[&#8220;root&#8221;]<\/span><br \/>\n<span style=\"color: #0000ff;\">})<\/span><\/p>\n<p>\u73fe\u5728\u306e\u63a5\u7d9a\u5148\u306eDB\u306b\u30e6\u30fc\u30b6\u30fc\u304c\u30fc\u4f5c\u6210\u3055\u308c\u308b\u3002<br \/>\ndb:\u306f\u5bfe\u8c61\u306eDB\u3068\u3044\u3046\u3088\u308a\u30ed\u30fc\u30eb\u304c\u5b58\u5728\u3059\u308bDB\u3068\u3044\u3046\u610f\u3002<\/p>\n<h2>\u4e3b\u8981\u306a\u30ed\u30fc\u30eb<\/h2>\n<p><span style=\"color: #0000ff;\">read<br \/>\n<\/span><span style=\"color: #0000ff;\">readWrite<br \/>\n<\/span><span style=\"color: #0000ff;\">dbAdmin<br \/>\n<\/span><span style=\"color: #0000ff;\">dbOwner<br \/>\n<\/span><span style=\"color: #0000ff;\">userAdmin<br \/>\nreadAnyDatabase<br \/>\nreadWriteAnyDatabase<br \/>\nuserAdminAnyDatabase<br \/>\ndbAdminAnyDatabase(\u4ed6\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306efind\u7b49\u3067\u304d\u306a\u3044)<br \/>\nroot<\/span><\/p>\n<p>\u30e6\u30fc\u30b6\u30fc\u524a\u9664<br \/>\n<span style=\"color: #0000ff;\">use db<\/span><br \/>\n<span style=\"color: #0000ff;\">db.dropUser(\u30e6\u30fc\u30b6\u30fc\u540d)<\/span><\/p>\n<h2>\u8a8d\u8a3c<\/h2>\n<p>\u8a8d\u8a3c\u6709\u52b9\u5316<\/p>\n<p><span style=\"color: #0000ff;\">\/etc\/mongod.conf<br \/>\n\u203bwin\u306a\u3089Program Files\u306e\u4e2d<\/span><\/p>\n<p><span style=\"color: #0000ff;\">#security:<\/span><br \/>\n<span style=\"color: #0000ff;\"><span style=\"font-size: 1rem;\">\u2193<br \/>\nsecurity:<br \/>\n<\/span>__authorization: enabled<br \/>\n\u203b\u30a4\u30f3\u30c7\u30f3\u30c8\u5fc5\u8981<br \/>\n<\/span><\/p>\n<p>\u30ed\u30b0\u30a4\u30f3\u65b9\u6cd5<\/p>\n<p><span style=\"color: #0000ff;\">mongo<\/span><br \/>\n<span style=\"color: #0000ff;\">use admin<\/span><br \/>\n<span style=\"color: #0000ff;\">db.auth(&#8220;taro&#8221;, &#8220;123&#8221;)<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb \/etc\/yum.repos.d\/mongodb-org-4.0.repo [mongodb-org-4.0] name=MongoDB Repository baseurl=https:\/\/repo.mo &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2437\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;MongoDB\u57fa\u790e&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2437","post","type-post","status-publish","format-standard","hentry","category-1"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2437"}],"version-history":[{"count":8,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2437\/revisions"}],"predecessor-version":[{"id":2445,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2437\/revisions\/2445"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}