Hướng dẫn mongodb laragon

MongoDB là hệ quản trị CSDL NoSQL phổ biến nhất trên thế giới. Tuy nhiên mặc định Laravel PHP Framework lại không hỗ trợ MongoDB. Vì vậy trong tutorial này, VinaSupport.com sẽ hướng dẫn các bạn cài đặt và tích hợp MongoDB vào project Laravel.

Cài đặt MongoDB

Các bạn vui lòng tham khảo bài viết: Hướng dẫn cài đặt MongoDB 

Cài đặt MongoDB PHP Extension

Để cài đặt mongodb Extension cho PHP, chúng ta sử dụng command sau:

sudo /opt/lampp/bin/pecl install mongodb

Hướng dẫn mongodb laragon

Sau đó thêm dòng sau vào file php.ini và restart lại httpd

extension=mongodb.so

Cài đặt package jenssegers/mongodb cho Laravel

Đâu là thư viện giúp cho Laravel có thể thao tác với MongoDB. Cài đặt thông qua Composer

composer require jenssegers/mongodb

Hướng dẫn mongodb laragon

Sửa file config/app.php thêm MongoServiceProvider

Jenssegers\Mongodb\MongodbServiceProvider::class,

Thêm kết nối vào file config/database.php

'mongodb' => [
    'driver' => 'mongodb',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', 27017),
    'database' => env('DB_DATABASE', 'homestead'),
    'username' => env('DB_USERNAME', 'homestead'),
    'password' => env('DB_PASSWORD', 'secret'),
    'options' => [
        // here you can pass more settings to the Mongo Driver Manager
        // see https://www.php.net/manual/en/mongodb-driver-manager.construct.php under "Uri Options" for a list of complete parameters that you can use


        'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // required with Mongo 3+
    ],
],

Chú ý: Trên Ubuntu 16.04 fix lỗi

Hướng dẫn mongodb laragon

Sử dụng các command sau đây để fix lỗi này

strings /opt/lampp/lib/libstdc++.so.6 | grep CXXABI
locate libstdc++.so.6
strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep CXXABI
sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /opt/lampp/lib/libstdc++.so.6

Nguồn: vinasupport.com

Hướng dẫn mongodb laragon

Installing MongoDB Server

Download The Latest Zip

Go to https://dl.mongodb.org/dl/win32/x86_64, find the latest zip available and download it. Here is what I got at the time:

Hướng dẫn mongodb laragon

Extract and Move To Laragon MongoDB Directory

Extract the zip file, it will result a single folder with the name of the version downloaded. Now just move the folder to C:\laragon\bin\mongodb or create the mongodb there first if it's not availble.

Hướng dẫn mongodb laragon

Run the MongoDB Server

Open the Laragon Dashboard, now the mongodb menu will available. You can run the server and stop it later from here.

Hướng dẫn mongodb laragon

GUI Client: Robo 3T (Robomongo)

The lightest client GUI available for MongoDB is robomongo and somehow it renamed to Robo 3T now. You can download it from https://robomongo.org and install it like a normal software installation. Once you done installed it, you can test and create connection the server like this:

Hướng dẫn mongodb laragon

CLI Client: MongoDB Shell (Mongosh)

If you prefer to work with terminal or shell, you can use MongoDB Shell and the windows installer is availble at https://www.mongodb.com/try/download/shell. Installing it will add the PATH for the command automatically. Here is when typed mongosh and ENTER in command prompt:

Hướng dẫn mongodb laragon

If the PATH isn't added automatically, then add it manually as instructed here https://docs.mongodb.com/mongodb-shell/install/#add-the-mongosh-binary-to-your-path-environment-variable