I. Introduction
As the saying goes, a journey of a thousand miles begins with a single step, begin to learn MongoDB technology, the bloggers did not start from the principle to explain MongoDB, but starting from the actual code, bloggers will explain MongoDB knowledge of the principles in the code, the problem will be encountered there are records, later will expand it for the scene of MongoDB. Let everyone know what kind of scene you can use MongoDB, come on open dry.
Two .MongoDB remote service connection
This module is designed to explore the use of MongoDB, the use of micro-services architecture. Come, come, line and up.
First, to ensure connectivity MongoDB services. I'm being given here, using the client tools to connect when an error, because I installed Linux MongoDB service when there is no password set up what what, we can not access it step by step investigation to see where is the problem?
- First, check your installed Linux system MongoDB service is the default port 27017, if not adjust, or the default is better, because some of the built-in commands to the 27017, restart the service.
- View port occupancy
[root@instance-n4r06itt bin]# netstat -anp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 28535/nginx: master
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 30189/./mongod
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 492/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1129/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1091/master
tcp 0 0 192.168.16.4:47468 100.64.253.42:80 ESTABLISHED 1182/./bcm-agent
tcp 0 0 192.168.16.4:41104 100.64.253.36:1813 ESTABLISHED 1174/hosteye
tcp 0 0 192.168.16.4:60742 100.64.253.36:1814 ESTABLISHED 1174/hosteye
tcp 0 0 127.0.0.1:34328 127.0.0.1:781 ESTABLISHED 1243/./bcm-si
Found that tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 30189/./mongod
only local connections, external can not access, we need to open the external access.
So we gave like MongoDB external access, modify configuration is as follows:
storage:
dbPath: "/usr/src/mongodb-4.0.14/data"
systemLog:
destination: file
path: "/usr/src/mongodb-4.0.14/logs/mongodb.log"
net:
port: 27017
bindIp: 0.0.0.0
processManagement:
fork: false
Modify over, restart MongoDB. success! ! !
III. Expand
- Create admin account, test library
# 切换到管理员
> use admin
switched to db admin
# 创建超级用户
> db.createUser({user:'root',pwd:'mongodb',roles:['root']});
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
> db.auth("root","mongodb");
1
# 查询用户
> db.system.users.find();
{ "_id" : "admin.root", "userId" : UUID("73384c6a-b525-4f1a-af1c-6e85cd097d08"), "user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "xYCfzhj+A+ICD90qoe7LZQ==", "storedKey" : "PKXd1bkC87E7gW7PO89nZOpl93k=", "serverKey" : "hMkc4P1e4lGjGL6m7vcCVAF4irA=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "6CTM9unCb3yhzg6NHvd92DwUzajKH3jhCVmPyQ==", "storedKey" : "lBYIj+EFdf8PL1RwSiftMkX/bDhWTe+OfVGPAHqHb/g=", "serverKey" : "QAmf4nf0l+OZG6Sr/gcWjiDg4KB2xXo62tJHHFlYs9s=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
# 切换到测试账户
> use test
switched to db test
# 创建test库
> db.createUser({user:'test',pwd:'mongodb',roles:[{role:'dbOwner', db:'test'}]});
Successfully added user: {
"user" : "test",
"roles" : [
{
"role" : "dbOwner",
"db" : "test"
}
]
}
- roles available options are:
- Database User roles: read, readWrite; database administration roles: dbAdmin, dbOwner, userAdmin;
- Cluster management roles: clusterAdmin, clusterManager, clusterMonitor, hostManager;
- The role of backup and recovery: backup, restore;
- All database roles: readAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabase, dbAdminAnyDatabase
- Super User role: root
So far, remote connectivity service MongoDB will introduce here! ! !