1. Configuration of the MongoDB server
MongoDB default does not turn on the authentication function, the following is the configuration steps:
1) Create an Admin user before you turn on authentication
$mongo
$db. CreateUser ({User: "root", pwd: "Root", Roles:[{role: "Useradminanydatabase", DB: "Admin"}]})
Create an Admin user root, password root, admin user with useradminanydatabase role
2) Enable MongoDB authentication function
Enter the MongoDB configuration file for modification, the personal address is/etc/mongod.conf
$sudo vim/etc/mongod.conf
Remove "#auth = True" # number at the beginning of this line and exit after saving
3) Restart MongoDB service
$sudo Service Mongod Restart
4) Log in with the administrator user you created
$mongo-u root-p root--authenticationdatabase admin
5) Increase the user name, password, and permissions of the database that needs to be logged in with spring
$db. CreateUser ({User: "Auser", pwd: "Apassword", Roles:[{role: "Dbowner", DB: "Somedb"}]})
Telnet to the MongoDB somedb database user name Auser, password is Apassword, the assigned permission is Dbower
At this point MongoDB local authentication configuration is complete.
2. Configure the user name password in the spring configuration file
Because the mongodb3.0+ server changes the authentication policy, the spring XML file needs to be adjusted accordingly, with the following XML configuration code:
<mongo:mongo-client id= "mongoclient" host= "127.0.0.1" port= "27017" credentials= "Auser:apassword@somedb"/>
<mongo:db-factory id= "Mongodbfactory"
Dbname= "Somedb"
mongo-ref= "Mongoclient"/>
<mongo:template id= "Mongotemplate" db-factory-ref= "Mongodbfactory" write-concern= "NORMAL"/>
Credentials is the authentication information, the format is "User name: password @ database name"
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.