如何在Windows系统及WAMP服务器上安装并配置Laravel与MongoDB
Setting Up Laravel with MongoDB on WAMP (Windows)
Hey folks, let's break down exactly how to get Laravel and MongoDB playing nicely on your WAMP server. I've tackled this setup dozens of times, so I'll walk you through each step clearly—no confusing detours.
First: Confirm Your Base Environment
Before we jump into the fun stuff, make sure your WAMP setup is ready:
- Your PHP version should be at least 8.0 (adjust if you need to use an older Laravel version).
- Enable essential PHP extensions via WAMP's tray menu: Right-click the WAMP icon → PHP → PHP Extensions, then check
mbstring,openssl, andPDO.
Step 1: Install the MongoDB PHP Extension
This is the critical bridge between PHP (and Laravel) and MongoDB. Here's how to do it right:
- Find your PHP specs:
- Create a
phpinfo.phpfile in your WAMPwwwfolder with this content:<?php phpinfo(); ?> - Visit
localhost/phpinfo.phpand note down:- PHP Version (e.g., 8.2.10)
- Architecture (x64 or x86)
- Thread Safety (TS or NTS)
- Create a
- Download the matching extension:
- Grab the correct
php_mongodb.dllfrom the PECL MongoDB page (make sure it matches all the specs you just noted—wrong version = no worky).
- Grab the correct
- Install the extension:
- Drop the
php_mongodb.dllinto your WAMP PHP extensions folder, likeC:\wamp64\bin\php\php8.2.10\ext(replace with your PHP version). - Open your PHP config file: Right-click WAMP tray → PHP → php.ini. Add this line somewhere in the extensions section:
extension=mongodb - Restart WAMP, then refresh
phpinfo.php. Search for "mongodb"—if you see a section about it, you're good to go!
- Drop the
Step 2: Create & Configure Your Laravel Project
Now let's get Laravel set up and connected to MongoDB:
- Install Composer (if you haven't):
- Grab the Windows installer from the Composer site and follow the prompts—make sure it picks up your WAMP PHP installation during setup.
- Generate a Laravel project:
- Open Command Prompt, navigate to your WAMP
wwwfolder:cd C:\wamp64\www - Run:
composer create-project laravel/laravel laravel-mongo-demo(you can renamelaravel-mongo-demoto whatever you want).
- Open Command Prompt, navigate to your WAMP
- Add the Laravel MongoDB Eloquent package:
- Move into your project folder:
cd laravel-mongo-demo - Run:
composer require jenssegers/mongodb—this is the go-to package for integrating MongoDB with Laravel's Eloquent ORM.
- Move into your project folder:
- Set up database connections:
- Open your project's
.envfile and update the DB section:DB_CONNECTION=mongodb DB_HOST=127.0.0.1 DB_PORT=27017 DB_DATABASE=laravel_mongo_db DB_USERNAME= # Leave blank if you don't have MongoDB auth set up DB_PASSWORD= # Same as above - Open
config/database.phpand add the MongoDB connection to theconnectionsarray:'mongodb' => [ 'driver' => 'mongodb', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', 27017), 'database' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'password' => env('DB_PASSWORD'), 'options' => [ 'database' => 'admin' // Default auth database, adjust if needed ] ], - Optional: If you want MongoDB to be your default database, change the
defaultvalue inconfig/database.phptomongodb:'default' => env('DB_CONNECTION', 'mongodb'),
- Open your project's
Step 3: Test the Setup
Let's make sure everything works with a quick test:
- Create a test model:
- Run:
php artisan make:model Post - Open
app/Models/Post.phpand modify it to use the MongoDB model:namespace App\Models; use Jenssegers\Mongodb\Eloquent\Model; class Post extends Model { protected $connection = 'mongodb'; protected $collection = 'posts'; // Name of your MongoDB collection protected $fillable = ['title', 'content']; // Fields you want to mass-assign }
- Run:
- Add a test route:
- Open
routes/web.phpand add this:use App\Models\Post; Route::get('/test-mongo', function () { // Create a test post Post::create([ 'title' => 'First MongoDB Post', 'content' => 'This is proof Laravel and MongoDB are talking!' ]); // Fetch all posts and return them as JSON return Post::all(); });
- Open
- Run the test:
- Start WAMP, then visit
localhost/laravel-mongo-demo/public/test-mongo - If you see a JSON response with your test post, congratulations—you're all set!
- Start WAMP, then visit
Troubleshooting Common Issues
- Extension not loading? Double-check that you edited the correct
php.inifiles (WAMP has two: one in PHP folder, one in Apache folder—sync them!). Also confirm thephp_mongodb.dllis in the right path. - Composer package install fails? Check your internet connection, or switch to a Composer mirror if you're in a region with slow access to Packagist.
- MongoDB connection errors? Make sure the MongoDB service is running, port 27017 isn't blocked, and your
.envcredentials match your MongoDB setup.
内容的提问来源于stack exchange,提问作者Sheikh Ershad Munsuri
相关产品推荐
相关产品推荐

