Home  >  Article  >  Backend Development  >  Tutorial on how to use mongodb database in laravel

Tutorial on how to use mongodb database in laravel

小云云
小云云Original
2017-11-23 09:29:184014browse

Mongodb, a distributed document storage database, written in C++ language, aims to provide scalable, high-performance data storage solutions for WEB applications. MongoDB is a high-performance, open source, schema-less document database, which is currently a popular NoSQL database. It can be used to replace traditional relational databases or key/value storage in many scenarios. So in this article we will talk about how to use mongodb database in laravel.

1. Install mongodb

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

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

Enter mongodb Directory
cd /usr/local/mongodb

Create a custom data directory
mkdir -p ./data/db/

Create a new log directory
mkdir logs

Start mongodb in background mode
/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/db --logpath=/usr/local/mongodb/logs/ mongodb.log --fork

Set auto-start at boot:
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 specifies the master machine
--slave specifies the slave machine
--source specifies the IP address of the master machine
--pologSize specifies the log file size not to exceed 64M. Because resync is a very heavy operation and Time-consuming, it is best to avoid resync by setting a large enough oplogSize (the default oplog size is 5% of the free disk size).
--logappend Add
--port to the end of the log file to enable the port number
--fork runs in the background
--only specifies which database to copy only
--slavedelay refers to slave replication detection Time interval
--auth Whether you need to verify the permission login (user name and password)

2. Install php mongodb extension

Install openssl
apt-get install openssl

Install php-mongodb driver
pecl install mongodb

php --ini View the php.ini file civilian
Add
extension=/{your own service provider in the php.ini file Corresponding road Jin}/mongodb.so

3. Install laravel extension

Download the expansion package
composer require jenssegers/mongodb

In config/app.php Register service
JenssegersMongodbMongodbServiceProvider::class,
Alias ​​'Mongo' => JenssegersMongodbMongodbServiceProvider::class,

Modify the database configuration file config/database.php

'mongodb' => [
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 27017,
'database' => 'mydb', // 自己数据库名字
'username' => '',
'password' => '',
],

USECase

use DB; //引用数据库
class MongoController extends Controller{
pubulic function index(){
DB::connection('mongodb') //选择使用mongodb
->collection('users') //选择使用users集合
->insert([ //插入数据
'name' => 'tom',
'age' => 18
]);
}
$res = DB::connection('mongodb')->collection('users')->all();
dd($res);
}

The above is a tutorial on how to use mongodb database in laravel. I hope it can help you understand and master it.

Related recommendations:

Introduction to the installation and basic operation of Mongodb in Node

Use python to monitor Linux memory and write it to mongodb

How to use Nodejs to connect to mongodb database tutorial detailed explanation

The above is the detailed content of Tutorial on how to use mongodb database in laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn