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/
2. Edit the configuration file
[[email protected] mongo-1.4.5]# Vim/usr/local/php/etc/php.ini//Last line add
[Email protected] mongo-1.4.5]#/usr/local/php/bin/php-m |grep 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