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. Install mongoDB in Linux. The steps are as follows: 1.

Just getting started with mongoDB, everything starts from scratch. After an afternoon, I completed the installation and sorted it out. I. Install mongoDB in Linux. The steps are as follows: 1.

Just getting started with mongoDB, everything starts from scratch. After an afternoon, I completed the installation and sorted it out.

I. mongoDB Installation

I installed it in Linux. The steps are as follows:

1. 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.

Download page, you can select the appropriate version based on your machine.

I downloaded static-2.5.0, a 32-bit version.

2. Installation

In fact, the installation process is very simple. Just solve the pressure.
Tar xvzf mongodb-linux-i686-static-2.5.0.tgz

3. Start mongoDB

Assume that the decompressed directory is/home/qmhball/mongo, And the executable files related to mangoDB are located in the/home/qmhball/mongo/bin directory.

Write a simple configuration file

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 the startup is successful, you will see information similar to the following 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. Test

Run the following command 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 database

Ii. php extension Installation

1. Download and decompress the source code

Source package address

2. Installation

Assume that the extracted directory is a mongo-1.4.0

Go to the mongo-1.4.0 directory and execute:

Phpize
./Configure
Make
Make install

After the installation is successful, you will receive a message similar to the following:

Installing shared extensions:/usr/local/lib/php/extensions/no-debug-non-zts-20090626/

In this directory, you will find mongo. so


3. Modify php configuration

Add in 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

$ 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.