MongoDB Add user authentication

Source: Internet
Author: User
Tags mongodb add

Connect to MongoDB and create a new user

Use Malldb.createuser ({User: "haha", pwd: "123456", roles: [{role: "ReadWrite", DB: "Mall" },                ]   }

Add the--auth parameter when starting

Can be logged in with the-u-p parameter MONGO [library name]-u [user name]-p
./mongo Mall-u Haha-p

Where the mall is the library name, haha is the user name

If the default 27017 port is modified with the following connection

./mongo 127.0.0.1:27019/mall-u Haha-p

and enter the password to log in.

PHP validates MongoDB user-authenticated scripts:

<?php

Echo 1;

Echo ' <meta charset= ' utf-8 ' ></meta> ';//Place page header to Utf-8
$conn = new Mongo ("Mongodb://system:[email protected]");
The first way of connecting
$conn = new Mongo ("192.168.4.16:27017");//connect locally, do not fill in parameters and connect to local
$conn = new Mongo ("Mongodb://haha:[email protected]:27017");//with user authentication connection
The connection method in the second connection way

$conn = new Mongo (' mongodb://127.0.0.3:27019 ', Array (' db ' = ' mall ', ' timeout ' = ' + ', ' connect ' = ' 1 ', ' Username ' = ' mall ', ' password ' = ' whty_mall ');
Echo 2;exit;
Var_dump ($conn);//print to be able to connect MongoDB service
$db = $conn->selectdb ("Mall");
Var_dump ($DB);//can print to indicate can link MongoDB database
$collection = $db->user;
$db->selectcollection ("collection");
Querying all the records
$cursor = $collection->find ();
Var_dump ($cursor);
$list =array ();
Traverse a document in all collections
foreach ($cursor as $obj)
{
$list []= $obj;
}
Echo ' <pre> ';
Print_r ($list);
Disconnecting MongoDB connections
$conn->close ();
, ' username ' = ' root ', ' password ' = ' 123456 '


?>

In the attachment is the script to validate the MongoDB php, the suffix txt is modified to PHP in the environment can be executed.

MongoDB Add user authentication

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.