Mongodb installation steps sharing under Windows


NoSQL is very popular. Mongodb, as an excellent distributed file storage database, has also received considerable attention, There are about 20 messages in Sina Weibo every day.

Official website:
PHP extension:

I chose the installation package of windows and downloaded version 1.8.2 of windows 32-bit. The following is the installation record:

Unzip to D: \ www \ mongodb

cd d:\www
bin\mongod.exe –dbpath=d:/www/mongodb/data

Sat Jul 09 09:03:28 [initandlisten] db version v1.8.2, pdfile version 4.5

Copy codeThe code is as follows:
Sat Jul 09 09:03:28 [initandlisten] git version: 433bbaa14aaba6860da15bd4de8edf6
Sat Jul 09 09:03:28 [initandlisten] build sys info: windows (5, 1, 2600, 2, ‘Ser
vice Pack 3′) BOOST_LIB_VERSION=1_35
Sat Jul 09 09:03:28 [initandlisten] waiting for connections on port 27017
Sat Jul 09 09:03:28 [websvr] web admin interface listening on port 28017

In this way, mongodb is running, but DOS cannot be shut down. Once it is shut down, it stops. Therefore, it needs to be set as a system service:
D:\www\mongodb>bin\mongod.exe –dbpath=d:/www/mongodb/data –logpath=d:/www/mongodb/mongodb.log –install

Copy codeThe code is as follows:
all output going to: d:/www/mongodb/mongodb.log
Creating service MongoDB.
Service creation successful.
Service can be started from the command line via ‘net start “MongoDB”‘.

In this way, it can serve in the system( services.msc )There are more mongodb services in it, which can be set to automatically start mongodb every time windows starts.
Look at the client that comes with it. It is still in its bin directory. The file name is mongo.exe

Copy codeThe code is as follows:
MongoDB shell version: 1.8.2
connecting to: test

stay Download the PHP extension of mongodb, PHP 5.2 VC6 thread safe Mongo extension.
Version 5.2 has only 5.2.13 mongodb.dll 。 The local PHP is reinstalled with 5.2.13, ready to use.
Main code:

Copy codeThe code is as follows:
$m = new mongo(); / / connect to port 27017 of the machine by default
$MDB = $m – > HX; / / select the HX database. If not, it will be created automatically. You can also use $m – > selectdb (“HX”);
$collection = $mdb->hx_ Site; / / select HX in HX_ Site set, equivalent to HX in HX Library_ For the site table, you can also use $MDB – > select collection (“HX_ site”);
Bulk insert:
$start = $i * 10000;
$end = ($i+1) * 10000;
$sql = “SELECT * FROM hx_site WHERE id > $start AND id <= $end”;
$tmp = $db->query($sql);
$arrs = array();
while ($arr = $db->fetch_array($tmp)) {
$arrs[] = $arr;

Use PHP to convert MySQL with 83.6w local data into mongodb, and insert 1W local data each time, which takes 109-125ms.

Copy codeThe code is as follows:
Mongodb syntax corresponding to MySQL statement://
Take PageRank = 5, in reverse order of ID, take 30
be similar toSELECT * FROM hx_site WHERE pagerank = 5 ORDER BY id DESC LIMIT 30
$cursor = $collection->find(array(‘pagerank’=>’5’))->sort(array(‘id’=>-1))->limit(30);
Foreach ($cursor as $obj) {/ / traverses all documents in the collection
echo $obj[“id”].’ ‘.$obj[“domain”] . “<br>”;

When there is no index, it takes 0.7s to get a record, 656ms, mysql, basically no difference.
Count (*) some condition, more than 800 ms, more than 100 ms after indexing