Intro
MongoDB itself is already a more mature and widely used NoSQL product. The best way to know the product is of course the official website, considering the network and language habits, some with Chinese stations will give us greater convenience. This article mainly records some of the steps of tossing and mongodb3.x, all of which are understood by reading official documents.
First, MongoDB installation
Since most of the time MongoDB Serever are running in Linux, the default operating system here is Linux CENTOS6. The MongoDB version is current, v3.4.2. tgz Package Download
wget ' https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.2.tgz '
Decompression
Tar zxvf mongodb-linux-x86_64-amazon-3.4.2.tgz
cd Mongodb-linux-x86_64-amazon-3.4.2/bin
Here you will see all the MongoDB related executables that are currently focused only on
executable |
desc |
Mongod |
Server Startup program |
Mongo |
Client startup program |
Second,MongoDB start 1. Start Server
Knowing that the startup file for MongoDB server is Mongod, guess./mongod can start the server, but naturally we will first focus on such a few issues.
1 The default listening port is how much, if set my own want to use the port number
2 run log where to see
3 where is the persisted data file
4 whether there is a configuration file to support more details of the configuration
Start command
./mongod--port 27017--dbpath/mongodb/data--logpath/logs/mongodb.log-f/mongodb/server.conf
The above command answers the 4 questions mentioned above.
--PORT Specifies the client connection port number, the default 27017
--dbpath the path where the persisted data resides, and the path must exist
--logpath log file-
f to specify a configuration file, a simple properties file, You can create it yourself
If the personalization parameters are written in the configuration file, the startup command will be refreshed.
./mongod-f/mongodb/server.conf
Configuration file cat/mongodb/server.conf
port=27017
logpath=/logs/mongodb.log
logappend=true #日志生产方式 (Append/overwrite)
Dbpath=/mongodb/data
2. Client Connection
MONGODB client's startup file is MONGO, and we know the ip:port of Mongodb service, guess all the clients connection commands
./mongo 127.0.0.1:27017
If the connection is successful, the MongoDB installation is over.
Third, the authority configuration
Now the MongoDB has been installed and started, but it's strange that you've never had permission problems, and it's not a problem if anyone else can get my data tampered with at random. The next problem to be solved is to add permission control to my database.
In general is the following steps: 1. Turn on permission validation
Add a line to the server.conf
Auth=true
2. Add admin account
The admin account is not like the omnipotent root in Linux, but an account that can be assigned an account. On the system where the server is located, connect the server with the client, and execute the following code
Use admin
db.createuser {
User: ' admin ',
pwd: ' 123456 ',
roles:[{role: ' Useradminanydatabase ', DB: ' Admin '}]
})
This creates an account with user admin, with a password of 123456, who can manage any library's users in the admin library. 3. Add Data Operation account
Once the rights management has been opened, all operations must be done with the appropriate account, just we can successfully add users, because this is our first account (first has the privilege, uh). We created the Admin account can only manage users, and we really need to be able to use the account to read and write data, it is necessary to create a read-write account.
Next, we will add a read-write user rwuser for a library named "Testdatabase".
Use the admin
db.auth ("admin", "123456") use
testdatabase
db.createuser ({
User: ' Rwuser ',
pwd: ' 123456 ',
roles:[{role: ' ReadWrite ', db: ' Testdatabase '}]
}
After using exit to exit the connection, we can use that account to connect to the server
./mongo 127.0.0.1:27017/testdatabase-urwuser-p123456
four. MongoDB Related Concepts
MongoDB also has a "library" concept similar to MySQL, with a broad concept similar to the default test library. The collection concept corresponds to the tables in MySQL.
Related commands:
Command |
desc |
Use ${databasename} |
Switch to DatabaseName |
Db |
Current library Name |
Show collections |
Lists all collection names in the current library |
Show DBS |
List all libraries |
Under the concept of "library" here, we look back at some of the actions of adding an account.
Use testdatabase => use
admin
db.createuser ({
User: ' Rwuser ',
pwd: ' 123456 ',
roles:[{role: ' ReadWrite ', db: ' Testdatabase '}]
}
Admin account login, for the Testdatabase library to create an account, do a library switch use testdatabase, can be clearly in roles:[{role: ' ReadWrite ', db: ' Testdatabase '}] This line of code has been indicated to the account is the Testdatabase library read and write permissions, but also need to be superfluous. In MongoDB, it does make sense. Simply put, is the account created under a library, can only be used to log into this library, if you want to operate other libraries, you need to do a library switch. For example, if you change the use Testdatabase in the previous section to "Take" admin, the login command needs to be changed to:
./mongo 127.0.0.1:27017/admin-urwuser-p123456
When the connection succeeds, you log in to the Admin library, and then you need to use Testdatabase to do the subsequent reads and writes.