1. Download MongoDB, here the download version is: mongodb-linux-i686-1.8.1.tgz.tar.
Http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.8.1.tgz
Related reading:
Install and start MongoDB in Linux
Advanced query example in MongoDB
MongoDB Java API for insert and single collection basic query examples
Query operations under MongoDB (corresponding to Java API query operations)
2. Extract the file to a directory and rename it:
- [Root@ LocalhostSrc] # tar-xzvf mongodb-linux-i686-1.8.1.Tgz.tar
- [Root@ LocalhostSrc] # mv mongodb-linux-i686-1.8.1/Usr/local/mongodb/
3. view the installed files:
- [Root@ LocalhostSrc] # cd/usr/local/mongodb/
- [Root@ LocalhostMongodb] # ls
- Bin GNU-AGPL-3.0Readme third-PARTY-NOTICES
- [Root@ LocalhostMongodb] # cd bin/
- [Root@ LocalhostBin] # ls
- Bsondump dbbak mongo mongod mongodump unzip Export Program program import mongorestore mongos mongosniff unzip stat
Mongod in bin is the MongoDB server process, mongo is its client, and other commands are used for other purposes of MongoDB, such as MongoDB File Export.
4. Start MongoDB.
You must first create a directory for MongoDB to store data files and log files. Here, the directory is created under/data:
- [Root@ LocalhostEtc] # cd/data/
- [Root@ LocalhostData] # ls
- Mongodb_data mongodb_log
Use mongod in the bin directory of the MongoDB installation directory to start MongoDB,
- ./Mongod -- dbpath =/data/mongodb_data/-- logpath =/data/mongodb_log/mongodb. log -- logappend &
After the startup is successful, you can check whether the startup is successful. The default port number is 27017. Of course, you can also specify unused ports at startup.
Check the port number to see if MongoDB is started.
- [Root@ LocalhostData] # netstat-lanp | grep"27017"
- Tcp00 0.0.0.0:270170.0.0.0: * LISTEN1573/Mongod
- Unix2[ACC] STREAM LISTENING58741573/Mongod/tmp/mongodb-27017. Sock
You can see that the database has been started successfully. Now you can use the mongo client to access the database.
- [Root@ LocalhostBin] # cd/usr/local/mongodb/bin/
- [Root@ LocalhostBin] #./mongo
- MongoDB shell version:1.8.1
- Connecting to: test
- >
This step indicates that the installation is successful.
5. Additional work.
Note: Previously we started MongoDB manually using mongod to start it. In this way, after the computer is shut down, it will not start again when we come in again, so we have to start it manually. Therefore, to avoid this tedious work, you can place the idea d in the Service self-startup Item, so that the computer will start as soon as the idea D service is enabled.
Edit/etc/rc. local, add the following code and save it.
- # Add mongonDB service
- Rm-rf/data/mongodb_data/* &/usr/local/mongodb/bin/mongod -- dbpath =/data/mongodb_data/-- logpath =/data/mongodb_log/mongodb. log -- logappend &
Restart the computer and check whether MongoDB is started. After the restart, you can directly use the mongo command to log on. The final result is successful.
In addition, if we use the mongo command to log on to MongoDB, we need to go to the directory where the mongo command is located and then execute./mongo. Is this a bit of trouble? Therefore, we can simplify this by copying the command file to/usr/bin, so that you can use the mongo command in any directory.
- [Root@ LocalhostBin] # ls
- Bsondump dbbak mongo mongod mongodump unzip Export Program program import mongorestore mongos mongosniff unzip stat
- [Root@ LocalhostBin] # cp mongo/usr/bin/
Go to any directory and try the mongo command:
- [Root@ LocalhostBin] # cd/
- [Root@ Localhost/] # Mongo
- MongoDB shell version:1.8.1
- Connecting to: test
- >
We can see that the logon is successful, which means we can use the mongo command like using the ls command.