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

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

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 //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

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

Installing MongoDB Server

Download The Latest Zip

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

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.

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.

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 //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:

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 //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:

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

Chủ Đề