Linux环境下mongoDB的安装搭建及简单的增删改查操作

==----------------------------------------------------------------------------------------------------------------------
-----== mongoDB的安装及简单的增删改查操作

1、下载安装包资源
    官网:https://www.mongodb.com/download-center/community
    版本:mongodb-linux-x86_64-4.0.11.tgz

2、资源放指定目录,解压文件
    cd /mine/software/mongodb-4.0.11
    tar -zxvf mongodb-linux-x86_64-4.0.11.tgz

3、将解压后的文件下的bin目录,复制到你服务目录(自己定义的目录)
    服务目录:/mine/serve/
    创建新服务目录:mkdir mongodb
    复制文件:rsync -a bin /mine/serve/mongodb

4、进入到MongoDB服务目录下,创建数据目录和日志文件
    cd /mine/serve/mongodb
    mkdir data
    touch dblogs 【注意:这里一定是个文件,不是目录】

5、进入到mongodb服务下的bin目录,进行启动
    cd /bin
    ./mongod --dbpath=/mine/serve/mongodb/data --logpath=/mine/serve/mongodb/dblogs

    执行,有时候会报错,如:
    [root@iZm5eizpokikoertia0x31Z bin]# ./mongod --dbpath=/mine/serve/mongodb/data --logpath=/mine/serve/mongodb/dblogs
    2019-08-05T15:06:56.792+0800 I CONTROL  [main] log file "/mine/serve/mongodb/dblogs" exists; moved to "/mine/serve/mongodb/dblogs.2019-08-05T07-06-56".
    ^C

    加个--fork,放在后台执行,试试:./mongod --dbpath=/mine/serve/mongodb/data --logpath=/mine/serve/mongodb/dblogs --fork

    [root@iZm5eizpokikoertia0x31Z bin]# ./mongod --dbpath=/mine/serve/mongodb/data --logpath=/mine/serve/mongodb/dblogs --fork
    about to fork child process, waiting until server is ready for connections.
    forked process: 11794
    child process started successfully, parent exiting

    至此mongodb启动成功!

    注:通常情况,加--fork和不加--fork的区别在于,不加,需要开启一个MongoDB启动窗口。

6、查看相关进程 pstree -p | grep mongod
    [root@iZm5eizpokikoertia0x31Z bin]# pstree -p | grep mongod
               |-mongod(11794)-+-{mongod}(11795)
               |               |-{mongod}(11796)
               |               |-{mongod}(11802)
               |               |-{mongod}(11803)
               |               |-{mongod}(11804)
               |               |-{mongod}(11805)
               |               |-{mongod}(11806)
               |               |-{mongod}(11807)
               |               |-{mongod}(11808)
               |               |-{mongod}(11809)
               |               |-{mongod}(11810)
               |               |-{mongod}(11811)
               |               |-{mongod}(11812)
               |               |-{mongod}(11813)
               |               |-{mongod}(11814)
               |               |-{mongod}(11815)
               |               |-{mongod}(11816)
               |               |-{mongod}(11817)
               |               |-{mongod}(11818)
               |               |-{mongod}(11819)
               |               |-{mongod}(11820)
               |               |-{mongod}(11821)
               |               `-{mongod}(11822)

    注意:通过以上我们可以看到主进程11794。

7、将mongodb启动放到开机启动,方便日后操作
    开机启动文件:/etc/rc.local
    重新拼接mongo启动命令:/mine/serve/mongodb/bin/mongod --dbpath=/mine/serve/mongodb/data --logpath=/mine/serve/mongodb/dblogs --fork

    加入文件:
    #!/bin/bash
    # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
    #
    # It is highly advisable to create own systemd services or udev rules
    # to run scripts during boot instead of using this file.
    #
    # In contrast to previous versions due to parallel execution during boot
    # this script will NOT be run after all other services.
    #
    # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
    # that this script will be executed during boot.

    touch /var/lock/subsys/local
    /mine/serve/mongodb/bin/mongod --dbpath=/mine/serve/mongodb/data --logpath=/mine/serve/mongodb/dblogs --fork


8、若想停止mongodb,如何进行操作
    killall mongod
    pkill mongod
    kill -9 mongod (这种方式,是不推荐的,因为如果使用这种方式,有可能,停止后,就起不来了。解决方法:删除data目录下的mongod.lock,再重启!)

    killall mongod关闭,启动

    [root@iZm5eizpokikoertia0x31Z bin]# killall mongod
    [root@iZm5eizpokikoertia0x31Z bin]# pstree -p | grep mongod
    [root@iZm5eizpokikoertia0x31Z bin]# ls
    bsondump  install_compass  mongo  mongod  mongodump  mongoexport  mongofiles  mongoimport  mongoreplay  mongorestore  mongos  mongostat  mongotop
    [root@iZm5eizpokikoertia0x31Z bin]# ./mongod --dbpath=/mine/serve/mongodb/data/ --logpath=/mine/serve/mongodb/dblogs --fork
    about to fork child process, waiting until server is ready for connections.
    forked process: 13848
    child process started successfully, parent exiting
    [root@iZm5eizpokikoertia0x31Z bin]# pstree -p | grep mongod
               |-mongod(13848)-+-{mongod}(13849)
               |               |-{mongod}(13850)
               |               |-{mongod}(13857)
               |               |-{mongod}(13858)
               |               |-{mongod}(13859)
               |               |-{mongod}(13860)
               |               |-{mongod}(13861)
               |               |-{mongod}(13862)
               |               |-{mongod}(13863)
               |               |-{mongod}(13864)
               |               |-{mongod}(13865)
               |               |-{mongod}(13866)
               |               |-{mongod}(13867)
               |               |-{mongod}(13868)
               |               |-{mongod}(13870)
               |               |-{mongod}(13871)
               |               |-{mongod}(13872)
               |               |-{mongod}(13873)
               |               |-{mongod}(13874)
               |               |-{mongod}(13875)
               |               |-{mongod}(13876)
               |               |-{mongod}(13877)
               |               `-{mongod}(13878)
    [root@iZm5eizpokikoertia0x31Z bin]#

    kill -9 关闭,启动

    [root@iZm5eizpokikoertia0x31Z data]# kill -9 13848
    [root@iZm5eizpokikoertia0x31Z data]# pstree -p | grep mongod
    [root@iZm5eizpokikoertia0x31Z data]# cd ../bin/
    [root@iZm5eizpokikoertia0x31Z bin]# ./mongod --dbpath=/mine/serve/mongodb/data/ --logpath=/mine/serve/mongodb/dblogs --fork
    about to fork child process, waiting until server is ready for connections.
    forked process: 14065
    child process started successfully, parent exiting
    [root@iZm5eizpokikoertia0x31Z bin]# pstree -p | grep mongod
               |-mongod(14065)-+-{mongod}(14066)
               |               |-{mongod}(14067)
               |               |-{mongod}(14072)
               |               |-{mongod}(14073)
               |               |-{mongod}(14074)
               |               |-{mongod}(14075)
               |               |-{mongod}(14076)
               |               |-{mongod}(14077)
               |               |-{mongod}(14078)
               |               |-{mongod}(14079)
               |               |-{mongod}(14080)
               |               |-{mongod}(14081)
               |               |-{mongod}(14082)
               |               |-{mongod}(14084)
               |               |-{mongod}(14085)
               |               |-{mongod}(14086)
               |               |-{mongod}(14087)
               |               |-{mongod}(14088)
               |               |-{mongod}(14089)
               |               |-{mongod}(14090)
               |               |-{mongod}(14091)
               |               |-{mongod}(14092)
               |               `-{mongod}(14093)
    [root@iZm5eizpokikoertia0x31Z bin]# cd ../data/
    [root@iZm5eizpokikoertia0x31Z data]# ls
    collection-0--2740207084622415070.wt  index-1--2740207084622415070.wt  journal          storage.bson      WiredTiger.turtle
    collection-2--2740207084622415070.wt  index-3--2740207084622415070.wt  _mdb_catalog.wt  WiredTiger        WiredTiger.wt
    collection-4--2740207084622415070.wt  index-5--2740207084622415070.wt  mongod.lock      WiredTigerLAS.wt
    diagnostic.data                       index-6--2740207084622415070.wt  sizeStorer.wt    WiredTiger.lock

9、登录mongodb数据库
    cd /mine/serve/mongodb/bin
    ./mongo

    这时,应该就进入到mongodb数据库了。
    [root@iZm5eizpokikoertia0x31Z bin]# ./mongo
    MongoDB shell version v4.0.11
    connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("7b5e2f0b-5b6e-4460-9255-71382a7b60c7") }
    MongoDB server version: 4.0.11
    Welcome to the MongoDB shell.
    For interactive help, type "help".
    For more comprehensive documentation, see
       http://docs.mongodb.org/
    Questions? Try the support group
       http://groups.google.com/group/mongodb-user
    Server has startup warnings:
    2019-08-05T15:54:24.793+0800 I STORAGE  [initandlisten]
    2019-08-05T15:54:24.793+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
    2019-08-05T15:54:24.793+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten]
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten]
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten]
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten]
    2019-08-05T15:54:25.890+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1834 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
    >

    查看数据库;show dbs
    > show dbs
    admin   0.000GB
    config  0.000GB
    local   0.000GB

    查看当前数据库:db
    > db
    test

    查看当前库的表或集合:show tables   |    show collections
    > show collections; (无数据)
    > show tables; (无数据)

    插入一条数据:db.c1.insert({name:"zhangsan",age:"29"});
    > db.c1.insert({name:"LDR",age:"29"});
    WriteResult({ "nInserted" : 1 })

    这个时候,我们查看一下数据库:
    > show dbs;
    admin   0.000GB
    config  0.000GB
    local   0.000GB
    test    0.000GB
    发现多了一个test库,这是为什么呢?原来mongodb的数据库是隐式的,默认是看不到test库的。

    查看数据:
    > db.c1.find();
    { "_id" : ObjectId("5d47e64862a767ca65d70324"), "name" : "LDR", "age" : "29" }

    再添加一条数据:
    db.c1.insert({name:"SXL",age:"28",gender:"female",home:"TianShui"});

    > db.c1.insert({name:"SXL",age:"28",gender:"female",home:"TianShui"});
    WriteResult({ "nInserted" : 1 })
    > db.c1.find();
    { "_id" : ObjectId("5d47e64862a767ca65d70324"), "name" : "LDR", "age" : "29" }
    { "_id" : ObjectId("5d47e7de62a767ca65d70325"), "name" : "SXL", "age" : "28", "gender" : "female", "home" : "TianShui" }

    删除数据:db.c1.remove({name:"SB"});
    > db.c1.insert({name:"SB",age:"0"});
    WriteResult({ "nInserted" : 1 })
    > db.c1.find();
    { "_id" : ObjectId("5d47e64862a767ca65d70324"), "name" : "LDR", "age" : "29" }
    { "_id" : ObjectId("5d47e7de62a767ca65d70325"), "name" : "SXL", "age" : "28", "gender" : "female", "home" : "TianShui" }
    { "_id" : ObjectId("5d47e9759db9b635a6868272"), "name" : "SB", "age" : "0" }
    > db.c1.remove({name:"SB"});
    WriteResult({ "nRemoved" : 1 })
    > db.c1.find();
    { "_id" : ObjectId("5d47e64862a767ca65d70324"), "name" : "LDR", "age" : "29" }
    { "_id" : ObjectId("5d47e7de62a767ca65d70325"), "name" : "SXL", "age" : "28", "gender" : "female", "home" : "TianShui" }

    更改数据:db.c1.update({name:"GF"},{gender:"male"});
    > db.c1.insert({name:"GF",age:"30",gender:"female"});
    WriteResult({ "nInserted" : 1 })
    > db.c1.find();
    { "_id" : ObjectId("5d47e64862a767ca65d70324"), "name" : "LDR", "age" : "29" }
    { "_id" : ObjectId("5d47e7de62a767ca65d70325"), "name" : "SXL", "age" : "28", "gender" : "female", "home" : "TianShui" }
    { "_id" : ObjectId("5d47ea029db9b635a6868273"), "name" : "GF", "age" : "30", "gender" : "female" }
    > db.c1.update({name:"GF"},{gender:"male"});
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.c1.find();
    { "_id" : ObjectId("5d47e64862a767ca65d70324"), "name" : "LDR", "age" : "29" }
    { "_id" : ObjectId("5d47e7de62a767ca65d70325"), "name" : "SXL", "age" : "28", "gender" : "female", "home" : "TianShui" }
    { "_id" : ObjectId("5d47ea029db9b635a6868273"), "gender" : "male" }


----------------------------------------------------------------------------------------------------------------------==
发布了59 篇原创文章 · 获赞 2 · 访问量 5597

猜你喜欢

转载自blog.csdn.net/LDR1109/article/details/98497225