简体   繁体   中英

MongoDB not working … (XAMPP on UBUNTU)

Okay, I have installed everything according to the official MongoDB guidelines. It doesnt work. When I try to set

$connect = new Mongo();

in a php file, it returns me:

Fatal error: Class 'Mongo' not found in /opt/lampp/htdocs/choqlet/contact/index.php on line 3

The mongodb server and the apache are both on... I tried this solution here (by Neil:

mongo.so: > undefined symbol: php_json_encode in Unknown on line 0. After installation mongo driver for php

Also watched over (and applied) this thread:

Mongodb connection in PHP not working

When i turn phpinfo(); there is no mongo in there.... And when i try php's

  echo extension_loaded("mongo") ? "loaded\n" : "not loaded\n"; ?>

It returns me: not loaded .

I have been looking for a solution all day long and nothing seems to work....

Here is the output of php --ini :

Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File:         /etc/php5/cli/php.ini
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed:      /etc/php5/cli/conf.d/05-opcache.ini,
/etc/php5/cli/conf.d/10-pdo.ini,
/etc/php5/cli/conf.d/20-json.ini,
/etc/php5/cli/conf.d/20-readline.ini,
/etc/php5/cli/conf.d/30-mongo.ini

And php -m:

[PHP Modules]
bcmath
bz2
calendar
Core
ctype
date
dba
dom
ereg
exif
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
mbstring
mhash
mongo
openssl
pcntl
pcre
PDO
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

So, mongo SHOULD work... I also ran

php -i | grep extension_dir

And it gave me some weird output:

PHP Warning:  Module 'mongo' already loaded in Unknown on line 0
extension_dir => /usr/lib/php5/20121212 => /usr/lib/php5/20121212

Okay, so basically, it is the problem that many people encounter (the ones that try to run it on XAMPP), and the final, and only solution for that is to:

  1. Check phpinfo(); on localhost after starting XAMPP; it should indicate where is your main php.ini file, the one responsible for XAMPP localhost.

  2. If you found one, add extension=mongo.so to the very end of the ini file. If you're on linux you obviously want to do that through sudo nano php.ini in your terminal.

  3. Now the second important part. There is a extensions_dir that is missing mongo.so file. It is localized in your linux's path: /opt/lampp/lib/php/extensions/no-debug-non-zts-20121212 (the number in the end may vary). If there is no mongo.so in that file, you have got your answer. To confirm that, check if after calling phpinfo(); there is mongo extension listed somewhere in the bottom (under the letter 'm'). If not, that is the solution.

  4. If you encountered problems that I've listed under the point number 3, go to your default php extension directory, which would be something like /usr/lib/php5/20121212/ , copy mongo.so file, and paste it to the lampp folder I showed you in the point 3.

5. Restart XAMPP, and apache in general. That should do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM