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