MongoDB set access rights, set user
MongoDB has been used for a long time, and MongoDB-based data storage has not been used to access permissions (MongoDB defaults to unrestricted access restrictions), today took a little time to study, the research results are as follows:
Note: The research results are based on the Windows platform
MongoDB after the native installation is well-deployed
1. Enter the command: Show DBS, you will find it built with two databases, one named admin, one named Local. Local does not seem to be useful, if the use of the process found the use of the local table, hoping to be able to leave a message to remind, then we focus on the admin table
2. Input command: Use admin, you will find that the DB contains a system.user table, hehe, yes, this table is equivalent to the user table in MSSQL, used to store the Super administrator, then we add a super administrator to it to try
3. Input command: db.adduser (' sa ', ' sa '), here I add a super Admin user, username for Sa,password also SA, that is, we added a super administrator, then we come to test, Let's see if we can connect MongoDB again need not prompt to enter the user name, password, we first exit (CTRL + C)
4. Input command: Use admin
5. Enter the command: Show collections, see all the tables under the library, you will find that MongoDB does not prompt you to enter the user name, password, it is strange, this is how it happened. Mentioned at the beginning of the article,
MongoDB default set to No access restrictions, that is, then we set it to require access restrictions, let's see the effect, how to set it.
6. In the registry, locate the MongoDB node, in its imgpath, we modify, add -authas follows:
"D:\Program files\mongodb\bin\mongod"-dbpath e:\work\data\mongodb\db-logpath e:\work\data\mongodb\log -auth -service
7. Input command: Use admin
8. Enter the command: Show collections, hehe, we found unable to view the table under the library, prompted: "$err": "Unauthorized db:admin lock Type:-1 client:127.0.0.1", it is clear that Prompt without permission, it seems that the key is here, we start MongoDB, we need to add the -auth parameter, so that we set the permissions to take effect, OK, next we use the user name, password that we just set up to access
9. Input command: Db.auth (' sa ', ' sa '), output a result value of 1, indicating that the user matches, if the user name, password is not correct, will enter 0
10. Enter the command: Show collections, hehe, the results came out, to here, the permissions set also only talk about more than half, and then down, we first exit (CTRL + C)
11. Enter the command: MONGO TestDB, we try to connect to a new library (regardless of whether the library exists, if it does not exist, add data to the library by default, the library is created), and then we want to see the table in that library
12. Enter command: Show collections, guys, no permissions, we enter the username and password created above
13. Enter the command: Db.auth (' sa ', ' sa '), enter the result 0, the user does not exist, this someone may not understand, just before the creation, how can not exist. The reason: When we visit the MongoDB database separately, the user name password is not the Super administrator, but the user in the library's System.user table, note that I am talking about the situation of individual access , what is no separate access to the situation. Then again, now what, no authority, then we try to add users to the library's System.user table
14. Input command: db.adduser (' Test ', ' 111111 '), wow, still prompt no permissions, this can do, the new database using Super Administrator also can not access, create users do not have permissions, oh, don't worry, then set the Super Administrator user, Then it must have access to all the libraries
15. Input command: Use admin
16. Input command: Db.auth (' sa ', ' sa ')
17. Input command: Use TestDB
18. Input command: Show collections, Haha, all the way, we found that can use Super Administrator user access to other libraries, hehe, this is not alone access to the situation, it is not difficult to find that we are first into the admin library, and then transferred to other libraries, Admin is the equivalent of a top-level official area, if you are a real estate, want to get a big project in the area to do, you want to do without the senior officials, it is not feasible, you need to go to their there, send some courtesy, then down to the place, engineering you can get hands, this speech is only a personal point of view, does not represent the blog park, that is, the project to get hands, will start to build, then we do not have to add a brick, add a tile have to go and that senior officials greet it, so we have to make this project legalized, we have to get the relevant procedures and documents to complete, not to be against the construction
19. Input command: db.adduser (' Test ', ' 111111 '), we add a user to the TestDB library, each time I access the library, I use the user just created, we first exit (CTRL + C)
20. Input command: MONGO TestDB
21. Enter command: Show collections, prompt without permission
22. Input command: Db.auth (' Test ', ' 111111 '), output 1, user present, validation successful
23. Enter command: Show collections, no longer prompt me no permission, congratulations, success
So tired. Write your breath and finish, huh?
Note: When you need permission to access MongoDB, if you need to see all the libraries in MongoDB, we can only use Super admin privileges, enter the command show DBS to view.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
1. Introduction
Tossing MongoDB for a few hours finally had a result. Uh. Now let's summarize it briefly.
In fact, my requirement is very simple, is to call the library function of MongoDB in C + + code, that is to get mongoclient.lib. Originally want to download directly, can not find on the internet, feel that recompile is not troublesome, so the source code compiled. Can be under the wrong source code, made up half a day can not be made out, dizzy.
In fact, refer to the articles of netizens, here download the source Mongodb-src-r2.6.3.zip. Compile the command is also very simple, everyone said direct input: SCons mongoclient.lib can, can keep popping such errors:
SCons: * * * does not know what to make File target ' mongoclient.lib ' (C:\mongo-maste
R\mongoclient.lib). Stop.
The results do not know how to find Mongoclient.lib in the new version is no longer the target, has been moved to another source Mongo-cxx-driver-legacy.zip, the address here. However, in mongo-src, like Mongo,mongod or directly can be compiled successfully, seemingly do not need the boost library
2. Preparatory work
2.1 Download and install Mongo-win32.msi
In order to save time, I downloaded the relevant EXE file directly like Mogo.exe,mogod.exe
2.2 Installing Python,scons,boost in turn
For more information, refer to the link address I gave you. I'll highlight some of the things I've found:
2.2.1 Some people say that mongoclient only link boost Static library, I find that is not the case, when you specify--dynamic-windows,--sharedclient parameters, you need to give the boost dynamic Lib library directory, not *-s.lib
2.2.2 When the specified--dynamic-windows,--sharedclient parameter is not specified, it seems that I don't have to give the Boost library directory, I'm not sure, anyway, I didn't give a libboost_date_time-vc110-mt-s-1_ like that. Path to files such as 53.lib
3. Compiling mongoclient
From the vs2012 command line into the Mongo-cxx-driver-legacy directory, execute:
SCons install-mongoclient--cpppath=boost Header file path
Results direct generation of libmongoclient-s.lib,128m in Build\install\lib
Of course, you can also add parameters such as--dynamic-windows,--sharedclient,--dbg to generate the corresponding dynamic libraries, including debug and release versions. Detailed description can be seen here. As a result, the following files are in my directory:
To tell the truth, I really did not understand the differences between these documents, I have been messy ...
4. Testing
I have to say, this step is really hanging out, there are two important steps I guessed.
Originally the code is very simple, connected on the Mongod.exe process to open the server on the line, do not know how the matter, the Netizen did not I say that two steps, is not the new version of the issue of updating, or I overlooked something.
The code is as follows:
[CPP] View Plain copy print? #include <iostream> #include <cstdlib> #include < winsock2.h> #include "mongo\client\dbclient.h" using namespace std; void run () { mongo::D bclientconnection con; con.connect ("127.0.0.1:27017"); } int main () { wsadata wsaData; if (WSAStartup (Makeword (), &wsadata) != 0) { return - 1; } try { run (&NBSP);; cout << "Conn ok" << endl; } catch (const MONGO::D bexception &e) { cout << "caught " << e.what () << endl; } getchar (); return EXIT_SUCCESS; } before running, needless to say, be sure to add boost and Mongoclient header files and library files. Note that I add the Dynamic library lib path, should be dynamic link, so also need to add boost DLL path and Mongoclient-gd.dll path to the path, and also add Ws2_32.lib library, As for the boost library and the Mongoclient library, you can write the directory directly without writing the library name because there is an automatic link mechanism.
Here are my two major changes, just compared to the files of netizens:
1, add winsock2.h before dbclient.h header file, because in vs2012 if no error: "You must include the Windows and Windows Sockets headers before Dbclient.h "
2, at the beginning of the main function, I added the WSAStartup 5 lines of code, because if not added, the GETADDR call in a file will be an exception: "WSAStartup () failed"
Can not say that the regular code is wrong, can only say for a variety of reasons, in this case need to do so in order to compile and run normally.
Run is simple, first start Mogod-dbpath d:\data, the default port at 27017, and then start the above client, output conn OK. Done!
5. Reference website