MacOS环境下MAMP(PHP7.1.12)通过Brew安装MongoDB可行性咨询
Hey there, great question—yes, this setup is feasible, but you’ll need to work around the fact that PHP 7.1 is end-of-life (EOL) and official package support has dropped off. Let’s walk through why your initial Brew attempts failed and how to fix it:
Why Brew Isn’t Working for You
Brew’s official PHP taps and extension repos prioritize actively supported PHP versions (7.4+ at the time of writing). The mongodb extension for PHP 7.1 was last updated in version 1.9.x (releases after 1.10.x require PHP 7.2+), and these older builds are no longer hosted in the default Brew taps.
Step-by-Step Fixes to Get MongoDB Working
Option 1: Install via PECL (Most Reliable Method)
PECL still hosts archived versions of the mongodb extension compatible with PHP 7.1. Here’s how to use it with your MAMP setup:
- First, point your terminal to MAMP’s PHP 7.1.12 binary to avoid conflicts with system PHP:
export PATH=/Applications/MAMP/bin/php/php7.1.12/bin:$PATH - Verify you’re using the correct PHP version:
You should seephp -vPHP 7.1.12in the output. - Install the last compatible
mongodbextension version (1.9.2):pecl install mongodb-1.9.2 - Open your MAMP PHP 7.1.12 config file (usually at
/Applications/MAMP/bin/php/php7.1.12/conf/php.ini) and add this line at the end to enable the extension:extension=mongodb.so - Restart MAMP’s Apache or PHP-FPM service, then verify the extension is loaded with:
If you seephp -m | grep mongodbmongodbin the list, you’re good to go.
Option 2: Use a Third-Party Brew Tap (Alternative)
Some community-maintained Brew taps still host older PHP extensions. While less reliable than PECL, you can try searching for taps that support PHP 7.1 extensions. Just make sure to vet the source for security before adding it to your system.
Important Note
PHP 7.1 reached end-of-life in December 2019, meaning it no longer receives security updates. If this is a production environment, strongly consider upgrading to a supported PHP version (7.4 or newer) to avoid vulnerabilities. But if you’re stuck on PHP 7.1.12 for legacy reasons, the PECL method above should get MongoDB up and running for you.
内容的提问来源于stack exchange,提问作者Lukáš Pícha

