MongoDB combined with PHP

Source: Internet
Author: User
Tags curl install php how to connect to mongodb zts zend


A previous article describes MongoDB installation using: http://msiyuetian.blog.51cto.com/8637744/1720559

The following article is mainly about how to connect to MongoDB in PHP, before the experiment to install PHP, I am here based on the lamp environment test.


1. Download and install PHP MONGO extension

[Email protected] ~]# cd/usr/local/src/

[Email protected] src]# wget Http://pecl.php.net/get/mongo-1.4.5.tgz-O mong_php.tar.gz

[Email protected] src]# TAR-ZXVF mong_php.tar.gz

[Email protected] src]# CD mongo-1.4.5/

[Email protected] mongo-1.4.5]#/usr/local/php/bin/phpize

Configuring for:

PHP Api version:20090626

Zend Module Api no:20090626

Zend Extension Api no:220090626

Note : If this error is missing some files, it indicates that there is a problem with the expansion pack, you need to download an available expansion pack online.

[Email protected] mongo-1.4.5]#/configure--with-php-config=/usr/local/php/bin/php-config

[[email protected] mongo-1.4.5]# make

[[email protected] mongo-1.4.5]# make install

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

The resulting mongo.so file can be seen on the

[Email protected] mongo-1.4.5]# ls/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

Mongo.so


2. Edit the configuration file

[[email protected] mongo-1.4.5]# Vim/usr/local/php/etc/php.ini//Last line add

Extension = mongo.so

[Email protected] mongo-1.4.5]#/usr/local/php/bin/php-m |grep MONGO

Mongo

The MONGO extension has been successfully loaded by the

Restart the Apache service (if LNMP, restart the PHP-FPM service)

[Email protected] mongo-1.4.5]#/usr/local/apache2/bin/apachectl restart


3. Testing

New test file

[Email protected] mongo-1.4.5]# vim/usr/local/apache2/htdocs/mongotest.php

<?php

Connect to MongoDB

$m = new Mongoclient ();

echo "Connection to database successfully";

Select a database

$db = $m->mydb;

echo "Database mydb selected";

?>

The test results are as follows

[Email protected] mongo-1.4.5]# Curl localhost/mongotest.php

Connection to database Successfullydatabase mydb selected

The configuration succeeded by the above.


Note: If we comment out the "extension = mongo.so" line of the/usr/local/php/etc/php.ini configuration file, the test result is 500 after restarting the Apache service.

[Email protected] mongo-1.4.5]# Curl Localhost/mongotest.php-i

http/1.0 Internal Server Error

Date:sat, 16:18:20 GMT

server:apache/2.2.31 (Unix) php/5.3.27

x-powered-by:php/5.3.27

Connection:close

Content-type:text/html



This article is from the "M April Days" blog, please be sure to keep this source http://msiyuetian.blog.51cto.com/8637744/1733258

MongoDB combined with PHP

Related Article

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.