Laravel How to use MongoDB database tutorial

Source: Internet
Author: User
Tags install mongodb install openssl php mongodb
Mongodb, a distributed document storage database, written in the C + + language, is designed to provide scalable, high-performance data storage solutions for Web applications. MongoDB is a high-performance, open-source, modeless document-based database that is a popular one in the current NoSQL database. It can be used in many scenarios to replace the traditional relational database or key/value storage methods. So in this article we'll talk about laravel how to use the MongoDB database.

1. Installing MongoDB

Download package: wget Http://fastdl.mongodb.org/lin ...

Extract:
Tar zxvf mongodb-linux-x86_64-2.2.3.tgz

Move directory to/usr/local/mongodb
MV Mongodb-linux-x86_64-2.2.3/usr/local/mongodb

Go to the MongoDB directory
Cd/usr/local/mongodb

New Custom Data Catalog
Mkdir-p./data/db/

New log Directory
mkdir logs

Next Run mode start MongoDB
/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data/db--logpath=/usr/local/mongodb/logs/mongodb.log- -fork

To set up boot from:
echo "/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data/db--logpath=/usr/local/mongodb/logs/ Mongodb.log--fork ">>/etc/rc.local

View MongoDB Logs
Tail-f/usr/local/mongodb/logs/mongodb.log

View process
PS aux |grep MongoDB

Parameter explanation:--dbpath database path (data file)
--logpath log file path
--master designated as the main machine
--slave specified as Slave machine
--source Specify the IP address of the host machine
--POLOGSIZE specifies that the log file size does not exceed 64M. Because Resync is very large and time-consuming, it is best to avoid resync by setting a sufficiently large oplogsize (the default oplog size is 5% of the size of the free disk).
--logappend log file End add
--port Enable port numbers
--fork running in the background
--ONLY specifies which database to replicate only
--slavedelay refers to the time interval from which replication is detected
--auth If you need to verify permissions login (username and password)

2. Installing the PHP MongoDB extension

Installing OpenSSL
Apt-get Install OpenSSL

Installing the Php-mongodb Drive
PECL Install MongoDB

PHP--ini view php.ini file civilian
Add in the php.ini file
extension=/{own service provider corresponding road strength}/mongodb.so

3. Installing the Laravel Extension

Download Expansion Packs
Composer require Jenssegers/mongodb

Registering a service in config/app.php
Jenssegersmongodbmongodbserviceprovider::class,
Alias ' Mongo ' = Jenssegersmongodbmongodbserviceprovider::class,

Modify the database configuration file in config/database.php

' MongoDB ' = [' Driver ' = ' mongodb ', ' host ' = ' localhost ', ' port ' = ' 27017 ', ' database ' = ' mydb ',//own database name ' Username ' = ' and ', ' password ' = ', ',],

Use case

Use DB; Reference Database class Mongocontroller extends Controller{pubulic function index () {db::connection (' MongoDB ')//select Use mongodb- >collection (' users ')//Choose to use the Users collection->insert ([//Insert data ' name ' = ' Tom ', ' age ' = 18]);} $res = db::connection (' MongoDB ')->collection (' users ')->all ();dd($res);}

The above is a tutorial on how to use the MongoDB database in Laravel, hoping to give you some understanding and mastery.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.