linux安装mongodb_Linux平台下安装部署MongoDB数据库及基本使用(1)

下载安装MongoDB数据库

1.下载MongoDB数据库

MongoDB下载地址:https://www.mongodb.com/download-center#community9543b0477a8033a6229b55f17cdabdb9.png

[root@node1 ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.8.tgz     //下载[root@node1 ~]# ll mongodb-linux-x86_64-rhel70-4.2.8.tgz-rw-r--r-- 1 root root 132768194 7月  30 10:32 mongodb-linux-x86_64-rhel70-4.2.8.tgz

2.解压并设置环境变量(很重要)

[root@node1 ~]# tar -xvzf mongodb-linux-x86_64-rhel70-4.2.8.tgz   //解压mongodb-linux-x86_64-rhel70-4.2.8/THIRD-PARTY-NOTICES.gotoolsmongodb-linux-x86_64-rhel70-4.2.8/READMEmongodb-linux-x86_64-rhel70-4.2.8/THIRD-PARTY-NOTICESmongodb-linux-x86_64-rhel70-4.2.8/MPL-2mongodb-linux-x86_64-rhel70-4.2.8/LICENSE-Community.txtmongodb-linux-x86_64-rhel70-4.2.8/bin/mongodumpmongodb-linux-x86_64-rhel70-4.2.8/bin/mongorestoremongodb-linux-x86_64-rhel70-4.2.8/bin/mongoexportmongodb-linux-x86_64-rhel70-4.2.8/bin/mongoimportmongodb-linux-x86_64-rhel70-4.2.8/bin/mongostatmongodb-linux-x86_64-rhel70-4.2.8/bin/mongotopmongodb-linux-x86_64-rhel70-4.2.8/bin/bsondumpmongodb-linux-x86_64-rhel70-4.2.8/bin/mongofilesmongodb-linux-x86_64-rhel70-4.2.8/bin/mongoreplaymongodb-linux-x86_64-rhel70-4.2.8/bin/mongodmongodb-linux-x86_64-rhel70-4.2.8/bin/mongosmongodb-linux-x86_64-rhel70-4.2.8/bin/mongomongodb-linux-x86_64-rhel70-4.2.8/bin/install_compass[root@node1 ~]# mv mongodb-linux-x86_64-rhel70-4.2.8 /usr/local/mongodb  //拷贝至指定的目录并改名[root@node1 ~]# ll /usr/local/mongodb/总用量 312drwxr-xr-x 2 root root    231 7月  30 10:48 bin-rw-r--r-- 1 root root  30608 6月  12 00:31 LICENSE-Community.txt-rw-r--r-- 1 root root  16726 6月  12 00:31 MPL-2-rw-r--r-- 1 root root   2617 6月  12 00:31 README-rw-r--r-- 1 root root  75405 6月  12 00:31 THIRD-PARTY-NOTICES-rw-r--r-- 1 root root 183512 6月  12 00:32 THIRD-PARTY-NOTICES.gotools[root@node1 ~]# export PATH=/usr/local/mongodb/bin:$PATH    //添加环境变量(这里的安装路径/usr/local/mongod根据自己安装的路径修改)[root@node1 ~]# source /etc/profile

创建数据库相关目录启动MongoDB服务

1.创建数据库相关目录

 默认情况下 MongoDB 启动后会初始化以下两个目录:

  数据存储目录:/var/lib/mongodb

  日志文件目录:/var/log/mongodb

[root@node1 ~]# mkdir -p /var/lib/mongodb[root@node1 ~]# mkdir -p /var/log/mongodb

2.启动MongoDB服务

mongod --dbpath /var/lib/mongodb/ --logpath /var/log/mongodb/mongodb.log --fork

[root@node1 ~]# mongod --dbpath /var/lib/mongodb/ --logpath /var/log/mongodb/mongodb.log --forkabout to fork child process, waiting until server is ready for connections.forked process: 10603child process started successfully, parent exiting

3.停止MongoDB服务

mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --shutdown

进入MongoDB管理后台

 如果需要进入 MongoDB 管理后台,需要进入 MongoDB安装目录下的 bin 目录中,然后再执行 mongo 命令文件。

 MongoDB Shell 是 MongoDB 自带的交互式 Javascript shell,用来对 MongoDB 进行操作和管理的交互式环境。

当你进入 mongoDB 后台后,它默认会链接到 test 文档(数据库):

[root@node1 ~]# cd /usr/local/mongodb/bin/[root@node1 bin]# ./mongoMongoDB shell version v4.2.8connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodbImplicit session: session { "id" : UUID("7a696386-0993-4107-843e-59e3589825da") }MongoDB server version: 4.2.8Welcome to the MongoDB shell.....> db              //显示当前数据库对象或集合test  --》默认是在test数据库中-----》由于它是一个JavaScript shell,您可以运行一些简单的算术运算:> 15+59+85159> 5*525> 10-55> 10/25> show dbs        //显示所有数据库列表admin   0.000GBconfig  0.000GBlocal   0.000GB> use admin      //连接admin数据库switched to db admin> db            //显示当前数据库对象或集合admin

创建Mysql数据库并插入数据

> show dbsadmin   0.000GBconfig  0.000GBlocal   0.000GB> use mysql           //创建数据库mysqlswitched to db mysql> dbmysql> show dbs      //查看所有数据库,看不到刚才我们创建的mysql数据库,因为数据库中没有数据,需要插入一些数据进去admin   0.000GBconfig  0.000GBlocal   0.000GB> db.mysql.insert({"name":"feizhumingyunwei"})   //向mysql数据库插入数据WriteResult({ "nInserted" : 1 })> show dbsadmin   0.000GBconfig  0.000GBlocal   0.000GBmysql   0.000GB      //现在可以看到mysql数据库有数据之后,成功显示出来了> db.mysql.find()       //查询刚才插入到Mysql数据库中的数据{ "_id" : ObjectId("5f44b3763570709f8c043236"), "name" : "feizhumingyunwei" }
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值