The application and practice of MongoDB in web development

Source: Internet
Author: User
Tags fpm install php install mongodb

First, what is MongoDB?

A set of high-performance, easy-to-develop document database. He holds data in the form of key-value pairs that can hold most data documents, including strings, arrays, data sequences, pictures, videos, and so on. MongoDB's well-designed, funny programmability makes it one of the hottest in today's NoSQL products.

Ii. Overview of NoSQL

1, the full name is not only SQL, refers to the non-relational database. Traditional relational database is operated in SQL language, although it is powerful enough in function, traditional relational database appears inefficient because it only needs simple data operation in Local. With the advent of Web2.0, the rise of SNS application, the requirements of database performance, the relational database is also exposed to the problem of difficult to solve. NoSQL has developed rapidly due to its own characteristics

2, the most important difference between the fly relational database and the relational database is that the processing logic is different, but the purpose is the same (it is used for storing data). The Fly relational database uses data sets to store data, there is no unified operating language in the data operation, if used memcache, then the non-relational database is not unfamiliar, because Memcache is a non-relational database of the embryonic.

3, in the use of the way, non-relational database using key-value to store the way. After all, a single database must have the concept of a library to become a database, such as data type curd operations, user permissions, data indexing, data backup, data synchronization, and so on, these are the concepts common to the database. NoSQL also has these concepts, even richer in functionality.

4, the characteristics of NoSQL:

Advantages:

(1) Processing massive amounts of data

(2) High availability and high scalability

(3) Flexible and efficient data processing

Disadvantages:

(1) There is no specific query language, nor is it compatible with SQL structured standard query language.

(2) NoSQL provides good support for the local data operation, but does not imply that NoSQL is suitable for all applications of the site, nor does it mean that NoSQL can replace relational databases. For example, high-transactional, complex queries that need to be outright and traditional business-only sites are not suitable for nosql.

(3) NoSQL is to solve big data storage, compared to relational database, gas is not mature enough. In addition, NoSQL databases do not have a relatively sophisticated commercial support.

5, MOGONDB Features:

Mogondb is one of the most famous NoSQL databases, and its greatest feature is its feature rich, powerful and simple to use. MOGONDB officially has been touted as a non-relational database that most closely resembles relational databases, in fact.

MOGONDB uses the Bson data format as the data storage structure, the Bson data format is the JSON data format binary Word, for the developer, in the use and the manifestation both are consistent. This means that developers can use the familiar JSON to mogondb all data structures, such as data set (table) additions and deletions and so on.

MOGONDB loose data structure, enrich the data characteristics and later extension, and can support the relational database in the primary key ID, the data is heterosexual, data index, data validation and so on. The specific features are as follows.

? Mode of freedom. Supports dynamic querying, full indexing, and easy retrieval of objects and arrays embedded in the document.

? Document-oriented storage. The developer can import an existing sequence document directly into the MOGONDB data set.

? An efficient storage mechanism. MOGONDB no special restrictions on the data, any data (including large images, video, etc.) are saved to the MOGONDB database, will eventually be converted to binary data, to a certain extent, to ensure that read and write funny, and ensure that the data integrity, security and so on.

? Supports replication and recovery. Provides master-slave, master-master-mode data replication, and data replication between servers.

? Automatic sharding and support for cloud-level scalability. Support the level of the database cluster, you can dynamically add additional servers.

? Supports Python, PHP, Ruby, Java, C, C #, JavaScript, Perl, and C + + language drivers.

? Full support for distributed architectures.

? Sphinx1.x started to support MongoDB drivers.

6. Installing MongoDB

1. Install MongoDB under Windows: slightly.

2. Install MongoDB under Linux:

(1) Download MongoDB, open the Http://www.mongodb.org/downloads page, select the appropriate platform for the Linux installation source code package; Here you choose 2.0.8 version of MongoDB, or you can download it directly at the command interrupt.

[Email protected] ~]# cd/data1/

[Email protected] data1]# wget https://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.8.tgz

[Email protected] data1]# tar zxvf mongodb-linux-i686-2.0.8.tgz

[Email protected] data1]# CD mongodb-linux-i686-2.0.8

[Email protected] mongodb-linux-i686-2.0.8]# cd/data1

[Email protected] data1]# mkdir-p/usr/local/mongodb

[Email protected] data1]# cp-r mongodb-linux-i686-2.0.8/*/usr/local/mongodb/

[Email protected] data1]# cd/usr/local/mongodb/

[Email protected] mongodb]# mkdir logs

[Email protected] mongodb]# mkdir data

Complete the previous steps, and then you are ready to start.

(2) Start-up service

[Email protected] bin]#/mongod--dbpath=/usr/local/mongodb/data/--logpath=/usr/local/mongodb/logs/mongodb.log-- Fork

The above command joins the fork option, which indicates that the Mongod service process is pushed to the background to run. The Mongod launcher also supports additional startup options, as follows:

? --dbpath: Specifies the database directory.

? --port: Specify communication port, default is 27017

? --BIND_IP: Binding Management IP

? --DIRECTORYPERDB: Use separate folders to store library files.

? --logpath: Sticky log storage directory.

? --logappend: How to generate a sticky log (append, overwrite).

? --pidfilepath: Specifies the process file path, and Null does not produce a process file

? --keyfile: Key identification for cluster mode.

? --CPU: Periodically displays CPU and IO utilization.

? --journal: Start the log.

? --ipv6: Enable IPv6 support.

? --nssize: Specifies the size of the. ns file, in megabytes, default 16MB, and 2GB max.

? --maxconns: Maximum number of concurrent connections.

? --notablescan: Table scan is not allowed.

? --quota: Limit the number of files per database by default to 8.

? --quotafiles: The number of files per database needs to be used in conjunction with-quota.

? --noprealloc: Closes the data file with the Assign function.

? --auth: Started in user authorization mode.

After MongoDB successfully starts, 27017 ports are used as the communication port by default, and the corresponding service status can be detected by the command.

[Email protected] data1]# NETSTAT-ANTLP | grep 27017

TCP 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1895/mongod

(4) Add random start

After a successful startup, simply add the Mongod main program to the Rc.local configuration file, and start the MongoDB database service at the time of system restart, opening the rc.local configuration file first.

[Email protected] data1]# vim/etc/rc.local

Then add the Mongod startup entry to the last facet of the configuration file, as follows:

#!/bin/sh

#

# This script is executed *after* all and the other init scripts.

# can put your own initialization stuff in here if you don ' t

# want to does the full Sys V style init stuff.

Touch/var/lock/subsys/local

/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data/--logpath=/usr/local/mongodb/logs/mongodb.log– Fork

Save the Rc.local configuration file, the MongoDB database is installed, and now iukeyi use MONGO Terminal management tool to log into the MongoDB database.

Third, the use of MongoDB

1, understanding MONGODB data structure, MONGODB data structure is composed of the database (databases), set (collection), Documents (document) three parts. Among them, the developers are mainly faced with collection and document. Where the data document is a data carrier, its data format is defined as: {document name: Document contents}

2, MongoDB becomes the most like relational database non-relational database, is because MongoDB also follows the relational database in the operation of the syntax, the former use Bson syntax, the latter using SQL syntax. The structure comparison of MongoDB in relational database MySQL, as shown in table 1:

Table 1 Structure comparison of MongoDB in relational database MySQL

Mongodb

Mysql

Databases (Database)

Databases (Database)

Collection (Collection)

Tables (Data Sheet)

Document (documentation)

Row (column)

Document name

Empty

Iv. using MongoDB in MVC

1. Install PHP extension (installation under Linux)

You can find the appropriate PHP extension package in Http://pecl.php.net/package/mongo, which is currently using the latest version of mongo-1.6.14.

[Email protected] data1]# wget http://pecl.php.net/package/mongo/get/mongo-1.6.14.tgz

[Email protected] data1]# tar zxvf mongo-1.6.14.tgz

[Email protected] data1]# CD mongo-1.6.14

[Email protected] mongo-1.6.14]#/usr/bin/phpize

[Email protected] mongo-1.6.14]#/configure--with-php-config=/usr/bin/php-config--enable-mongo

[[email protected] mongo-1.6.14]# make && make install

[Email protected] mongo-1.6.14]# ls/usr/lib/php/modules/

The above informational hint indicates that the MONGO for PHP extension was successfully installed, then open the php.ini file and add the MONGO extension reference.

[Email protected] ~]# Vim/etc/php.ini

Extension=mongo.so

Finally restart the PHP-FPM to

[Email protected] ~]# Pkill PHP-FPM

[[Email protected] ~]# service PHP-FPM start

2, thinkphp Operation MongoDB

Thinkphp3.x provides full support for MongoDB, encapsulating commonly used curd operations and making them more suitable for MVC development.

(1) Configure the database connection in a way similar to the MySQL database configuration, as follows:

Return Array (

' Db_type ' = ' Mongo ',

' Db_host ' = ' 127.0.0.1 ',

' db_name ' = ' test ',

' Db_user ' = ',

' Db_pwd ' = ',

' Db_prefix ' = ',

);

(2) Test database is connected to normal, the most direct way is to output data set information in the Controller method, the code is as follows:

Public Function test ()

{

$m = new Mongomodel (' C2 ');

Dump ($m);

}

If the output dataset information indicates that the link is normal, you can perform a curd operation.

The application and practice of MongoDB in web development

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.