When PHP encounters MongoDB

Source: Internet
Author: User
Tags auth findone install php install mongodb mongoclient

From:http://www.cstor.cn/textdetail_7995.html

Before the author of an article is to teach you to install MongoDB under Linux, and through the Mongo Client operation database (Console Interface), but the actual application of the most often with the WEB or WebService implementation. Next we'll show you how to manipulate MongoDB in PHP. Here's how to install the CentOS installation, first install PHP Mongo Driver via EPEL, and install the following command:

sudo yum install Php-pecl-mongo

 Start Mongo DB Server:

sudo service Mongod start

Write a PHP through the Mongoclient category to access the database (the official teaching file used by the Mongo category has been abolished), the code is as follows:


  

//Configuration    $dbhost= ' localhost '; $dbname = ' My_mongodb‘; //Connect to MONGO database    $mongoClient=New\mongoclient (' MongoDB://'. $dbhost);     $db=$mongoClient-$dbname; //Get the Users collection    $cUsers=$db-users; //Insert Object    $user=Array(' first_name '= ' SJ ',' last_name '= ' Mongo ',' Roles '=Array(' Developer ',' Bugmaker ')); //Insert This new document into the Users collection    $cUsers->save ($user); //Query    $user=Array(' first_name '= ' SJ ',' last_name '=' Mongo '); $user=$cUsers->findone ($user); //Output    Print_r($user);
View Code



  The results of the implementation are as follows:

 

1    Array 2 3 (4 5[_id] = MongoIdObject 6 7 (8 9[$id] =53de543d58b420881b998c8bTen  One ) A  -[First_Name] =SJ -  the[Last_Name] =Mongo -  -[Roles] =Array  -  + ( -  +[0] = =Developer A  at[1] = =Bugmaker -  - ) -  -)
View Code


The above action will directly manipulate the MY_MONGODB database, create a Collection called Users and add an object, the whole process does not need to establish a database and Schema, is not very simple and fast!? After the completion of the implementation we can also see in the/var/lib/mongodb/directory My_mongodb.0~1 and other files, indicating that our information has been established.

But the process we found one thing, the whole MongoDB connection action unexpectedly did not verify! Yes, the default MongoDB enablement is really invincible, and then we'll show you how to enable account-line verification.

   turn on your MongoDB connection verification

First step through the MONGO command, in the Admin database to establish the password to connect to the account = MONGO

The MongoDB manager needs to create the user in the admin repository, so we execute the following Query:

Use admin;

Db.adduser (' sj ', ' My-password ');

Exit

Second step Modify/etc/mongodb.conf profile, turn on "auth = true" Enable Validator

sudo vim/etc/mongodb.conf

Re-enable MongoDB service

sudo service mongod restart

Then we can set the MY_MONGODB connection user through the MONGO command, as follows:

Use admin;

Db.auth (' sj ', ' My-password ');

Use My_mongodb;

Db.adduser (' sj ', ' My-password ');

Exit

We have logged in via Db.auth, since authentication has been enabled and must be logged in to operate the database.

Then we will change PHP to the following, add the MongoDB Connection account and password:


 

1    //Configuration2 3     $dbhost= ' localhost '; 4 5$dbname = ' My_mongodb '; 6 7     //Connect to MONGO database8 9     $mongoClient=New\mongoclient (Ten  One' MongoDB://'. $dbhost, A  -     Array(  -  the' db ' = =$dbname, -  -' Username ' = ' sj ', -  +' Password ' = ' my-Password ' -  + ) A  at );  -  -     $db=$mongoClient-$dbname;  -  -     //Get the Users collection -  in     $cUsers=$db-users; -  to     //Query +  -     $user=Array(  the  *' first_name ' = ' SJ ', $ Panax Notoginseng' Last_Name ' =' Mongo ' -  the );  +  A     $user=$cUsers->findone ($user);  the  +     //Output -  $     Print_r($user);
View Code



So you can connect with MongoDB correctly through the account, introduce here, next time goodbye.

When PHP encounters MongoDB

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.