Install MongoDB on Linux platform
MongoDB provides 32-bit and 64-bit installation packages on the Linux platform. You can download the installation package from the official website.
Download address: http://www.mongodb.org/downloads
Download the installation package and unzip ittgz( The following demonstrates the installation on 64-bit Linux).
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz # 下载 tar -zxvf mongodb-linux-x86_64-3.0.6.tgz # 解压 mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb # 将解压包拷贝到指定目录
The executable file of MongoDB is located in the bin directory, so you can add it to the PATH path:
export PATH=<mongodb-install-directory>/bin:$PATH
<mongodb-install-directory> ; is your MongoDB installation path. Such as /usr/local/mongodb in this article.
Create database directory
MongoDB data is stored in the db directory of the data directory, but this directory will not be automatically created during the installation process, so you need to manually create data directory and create the db directory in the data directory.
In the following example, we create the data directory under the root directory (/).
Note: /data/db is MongoDB’s default startup database path (--dbpath).
mkdir -p /data/db
Run the MongoDB service on the command line
You can execute the mongod command in the bin directory of the mongo installation directory on the command line to start the mongdb service.
Note: If your database directory is not /data/db, you can specify it through --dbpath.
$ ./mongod 2015-09-25T16:39:50.549+0800 I JOURNAL [initandlisten] journal dir=/data/db/journal 2015-09-25T16:39:50.550+0800 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed 2015-09-25T16:39:50.869+0800 I JOURNAL [initandlisten] preallocateIsFaster=true 3.16 2015-09-25T16:39:51.206+0800 I JOURNAL [initandlisten] preallocateIsFaster=true 3.52 2015-09-25T16:39:52.775+0800 I JOURNAL [initandlisten] preallocateIsFaster=true 7.7
MongoDB background management Shell
If you need to enter the MongoDB background management, you need to first open the bin directory under the mongodb installation directory, and then execute mongo command file.
MongoDB Shell is the interactive Javascript shell that comes with MongoDB, an interactive environment used to operate and manage MongoDB.
When you enter the mongoDB backend, it will link to the test document (database) by default:
$ cd /usr/local/mongodb/bin $ ./mongo MongoDB shell version: 3.0.6 connecting to: test Welcome to the MongoDB shell. ……
Since it is a JavaScript shell, you can run some simple arithmetic operations:
> 2+2 4 > 3+6 9
Now let's insert some simple data and retrieve the inserted data:
> db.php.insert({x:10}) WriteResult({ "nInserted" : 1 }) > db.php.find() { "_id" : ObjectId("5604ff74a274a611b0c990aa"), "x" : 10 } >
The first command inserts the number 10 into the x field of the php collection.
MongoDb web user interface
MongoDB provides a simple HTTP user interface. If you want to enable this feature, you need to specify the parameter --rest when starting.
$ ./mongod --dbpath=/data/db --rest
MongoDB's web interface access port is 1000 more than the service port.
If your MongoDB running port uses the default 27017, you can access the web user interface at port number 28017, that is, the address is: http://localhost:28017.