You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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, and PDO.

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:

  1. Find your PHP specs:
    • Create a phpinfo.php file in your WAMP www folder with this content: <?php phpinfo(); ?>
    • Visit localhost/phpinfo.php and note down:
      • PHP Version (e.g., 8.2.10)
      • Architecture (x64 or x86)
      • Thread Safety (TS or NTS)
  2. Download the matching extension:
    • Grab the correct php_mongodb.dll from the PECL MongoDB page (make sure it matches all the specs you just noted—wrong version = no worky).
  3. Install the extension:
    • Drop the php_mongodb.dll into your WAMP PHP extensions folder, like C:\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!

Step 2: Create & Configure Your Laravel Project

Now let's get Laravel set up and connected to MongoDB:

  1. 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.
  2. Generate a Laravel project:
    • Open Command Prompt, navigate to your WAMP www folder: cd C:\wamp64\www
    • Run: composer create-project laravel/laravel laravel-mongo-demo (you can rename laravel-mongo-demo to whatever you want).
  3. 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.
  4. Set up database connections:
    • Open your project's .env file 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.php and add the MongoDB connection to the connections array:
      '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 default value in config/database.php to mongodb: 'default' => env('DB_CONNECTION', 'mongodb'),

Step 3: Test the Setup

Let's make sure everything works with a quick test:

  1. Create a test model:
    • Run: php artisan make:model Post
    • Open app/Models/Post.php and 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
      }
      
  2. Add a test route:
    • Open routes/web.php and 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();
      });
      
  3. 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!

Troubleshooting Common Issues

  • Extension not loading? Double-check that you edited the correct php.ini files (WAMP has two: one in PHP folder, one in Apache folder—sync them!). Also confirm the php_mongodb.dll is 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 .env credentials match your MongoDB setup.

内容的提问来源于stack exchange,提问作者Sheikh Ershad Munsuri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:23:48