[root@localhost etc]# cd /data/
[root@localhost data]# ls
mongodb_data mongodb_log
Start MongoDB with MongoDB under the bin in the installation directory of 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 270 17. Of course, other unused ports can also be specified at startup.
First, check the port number to see if MongoDB is started.
[root@localhost data]# netstat -lanp | grep "270 17"
tcp 0 0 0.0.0.0:270 17 0.0.0.0:* LISTEN 1573/mongod
unix 2 [ ACC ] STREAM LISTENING 5874 1573/mongod /tmp/mongodb-270 17.sock
As you can see, it has been started successfully. Now use mongo client to access the database.
[root@localhost bin]# cd /usr/local/mongodb/bin/
[root@localhost bin]# ./mongo
MongoDB shell version: 1.8. 1
connecting to: test
>
At this point, the installation has been successful.
5. Extra work.
Note that we start MongoDB manually, so that when the computer is turned off, it won't start again the next time we come in, so we have to start it manually. Therefore, in order to avoid this tedious work, we can put MongoDB in the service self-startup item, so that the computer will start the MongoDB service as soon as it starts.
Edit /etc/rc.local, add the following code and then 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&
Let's restart the computer and see if mongoDB is started. After restarting, we can log in directly with the Mongo command, and finally we find that it can be successful.
In addition, we use the mongo command to log in to mongoDB, and we have to go to the directory where the Mongo command is located before executing. /mongo. Is this a bit troublesome? Therefore, we can simplify this and copy the command file to /usr/bin, so that we can use the mongo command in any directory.
[root@localhost bin]# ls
bsondump dbbak mongo mongod mongodump mongoexport mongofiles mongoimport mongorestore mongos mongosniff mongostat
[root@localhost bin]# cp mongo /usr/bin/
Go to any directory and try the mongo command:
[root@localhost bin]# cd /
[root@localhost /]# mongo
MongoDB shell version: 1.8. 1
connecting to: test
>
You can see that the login is successful, which means that we can use the mongo command just like the ls command.