Setup Mongodb in Laravel Sail

Rafa Rafael
4 min readMay 9, 2023
Eureka!

I wanted to update my knowledge in using mongodb with my very favorite framework which is Laravel. And I don’t want to mess up my local development environment that everytime I tried something new I will install it my local machine.

Luckily Laravel has new kid in its ecosystem where I can easily setup a virtual dev environment (containers as they say) and use it as I learned new things.

At first I thought setting it up is easy peasy but did encountered some bottlenecks which almost discouraged me to continue and use different provider where I can achieve the same thing.

But for the love of Laravel, I tried to find a way and decided I need to make it work. And yes I am glad I was able to set it up and fixed the errors that tried to make change my mind.

Anyhow, enough of that little story and let’s get our hands dirty.

Steps

1. Create a new project using composer create-project or laravel new command. But as of date of this writing only laravel v9 and below are supported with jenssegers/mongodb so I don’t have a choice but it’s no beggie as they say.

2. Then open your project in the editor and make sure to open the terminal.

3. Then we need first to install laravel sail before we can use it.

php artisan sail:install

4. Since mongodb is still not included in the extension, for now select any that you think you will going to need in your project. And the we will add the mongodb later.

After successfully building Sail, try if everything is fine by running the commands below:

./vendor/bin/sail up -d
./vendor/bin/sail composer --version

5. After checking all is well then you we can now install jenssegers/mongodb that we are going to use to connect to mongodb and use it as our database.

Run ./vendor/bin/sail composer require jenssegers/mongodb

Unfortunately you will see an error messages like below because mongodb binary is not yet installed in our container and enabled in php.ini

 Problem 1
- jenssegers/mongodb[v3.9.0, ..., v3.9.5] require mongodb/mongodb ^1.11 -> satisfiable by mongodb/mongodb[1.11.0, ..., 1.15.0].
- jenssegers/mongodb[v1.0.0, ..., v1.0.8] require illuminate/support 4.0.x -> found illuminate/support[v4.0.0, .......

6. So first install mongodb binary in our container but before we can do that we need to start a bash session first where we can run arbitrary shell commands within our container.

./vendor/bin/sail root-shell

Then in root run the following commands:

#Install binary
pecl install mongodb

#Add extension to php.ini
echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

Then type exit to proceed in installing mongodb php extension. But before you decided to install it manually, how about we add it in our setup so we can always be sure that it is always included and automatically installed when we build the sail.

7. So to achieve it, run sail artisan sail:publish and you will see a new folder will be created named docker. Open it and you will see a folders named with version numbers that corresponds to your PHP version that you are using in your container.

Since I am using PHP v8.2, I will only change the files on this folder. But this is pretty simple if you are working on different PHP version as you will only change the files that are the same with your version number.

Okay, then open the Dockerfile and add our mongodb php extension inside, after swoole like what shown below so it will be included and installed automatically during build. And don’t forget to change it to your PHP version.

php8.2-swoole php8.2-mongodb \

After that we will build our container again so run the following commands:

#Stop sail
./vendor/bin/sail stop

#Rebuild container
/vendor/bin/sail build

#Start sail
/vendor/bin/sail sail up -d

#Install the mongodb package
./vendor/bin/sail composer require jenssegers/mongodb

We can now install the jenssegers/mongodb package and expect that it will be installed successfully.🤞

Errors and Fixes

After running ./vendor/bin/sail artisan migrate I encounter this error:

Authentication failed

After tinkering around, I found the solution, by just checking the database.php drivers config, docker-compose.yml and your .env file values that they use the same variable names.

>

I wanted to see the records that has been stored in my mongdb so I decided to used MongoDB Compass but encountered this error:

Unable to connect to mongodb

As you can see I connected to localhost with port 27017 but I found out that this wrong, by using google I found a way that you can check your mongodb containers properties, just run the code below.

docker inspect mongo-container-name

#Example
docker inspect sail-mongo-1

Then it will show you all the container properties like this

docker inspect mongo-container-name

Just copy the IPAddress and add to your connection url like this.

mongodb://username:password@172.27.0.3:27017/

That’s it! You can now manage your database using compass.

Tip

In case you are still new in using laravel sail, you can make a shorter command name. Check making a sail shell alias on how to achieve it.

Enjoy!

--

--

Rafa Rafael

Dad / Husband / Full Stack Developer / Lifelong Learner