下载安装MongoDB数据库
1.下载MongoDB数据库
MongoDB下载地址:https://www.mongodb.com/download-center#community
[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" }