MongoDB and php extension Installation

Source: Internet
Author: User

Just getting started with mongoDB, everything starts from scratch. After an afternoon, I completed the installation and sorted it out. I. I installed mongoDB in Linux. The steps are as follows: 1. to download the installation package, be sure to check whether your machine is 32-bit or 64-bit. Otherwise, the package cannot be used. At first, I got the wrong package. After the installation, a Floating point exception was prompted. The download page is http://www.mongodb.org/downloads, which can be used to locate different types of machines. I downloaded static-2.5.0, a 32-bit version. 2. The installation process is very simple. Just relieve the pressure.
Tar xvzf mongodb-linux-i686-static-2.5.0.tgz 3. Start mongoDB assuming that the directory after decompression is/home/qmhball/mongo, mangoDB-related executable files are located under the/home/qmhball/mongo/bin directory. Write a simple configuration file named mongod. conf.

# Data directory
Dbpath =/home/qmhball/mongo/db
Port = 9304
Bind_ip = 10.1.146.163
# Log directory
Logpath =/home/qmhball/mongo/log/mongo. log
Logappend = true
# Running services in the form of Daemon
Fork = true

Run

Cd/home/qmhball/mongo/bin
./Mongod -- config mongod. conf

After successful startup, you will see the following information in the log Wed May 22 15:53:59. 825 [initandlisten] MongoDB starting: pid = 10527 port = 9304 dbpath =/home/qmhball/mongo/db 32-bit host = web

4. Run the test in the bin directory.

./Mongo -- host 10.1.146.163 -- port 9304

Enter Interaction Mode

Type db. test. save ({a: 1}) db. test. find () will get {"_ id": ObjectId ("519cebd23c053d9709065602"), "a": 1} indicates that the data has been stored in the db
2. php extension installation 1. Download the source code and unzip the source package address http://pecl.php.net/get/mongo-1.4.0.tgz 2. Installation assuming decompression directory for the mongo-1.4.0 into the mongo-1.4.0 directory, in turn execute: phpize
./Configure
Make
After the installation is successful, make install will get the following prompt: Installing shared extensions:/usr/local/lib/php/extensions/no-debug-non-zts-20090626/under this directory you will find mongo. so
3. Modify the php configuration and add it to php. ini.

Extension =/usr/local/lib/php/extensions/no-debug-non-zts-20090626/mongo. so

Success! Tips: if it is running under apache or other web Services, remember to restart the service. The new extension will take effect. 4. Test demo. php

<? Php
$ User = array (
'First _ name' => 'mongodb ',
'Last _ name' => 'fan ',
'Tags' => array ('developer ', 'user ')
);
// Configuration
$ Dbhost = '10. 1.146.163: 100 ';
$ Dbname = 'test ';
// Connect to test database
$ M = new Mongo ("mongodb: // $ dbhost ");
$ Db = $ m-> $ dbname;
// Get the users collection
$ Users = $ db-> users;
// Insert this new document into the users collection
$ Res = $ users-> save ($ user );
Var_dump ($ res );
$ Data = $ users-> findOne ();
Var_dump ($ data );

Run and get

Bool (true)
 
Array (4 ){
 
["_ Id"] =>
 
Object (upload ID) #7 (1 ){
 
["$ Id"] =>
 
String (24) "519cf324876d75714cb4e973"
 
}
 
["First_name"] =>
 
String (7) "MongoDB"
 
["Last_name"] =>
 
String (3) "Fan"
 
["Tags"] =>
 
Array (2 ){
 
[0] =>
 
String (9) "developer"
 
[1] =>
 
String (4) "user"
 
}
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.