统一配置命令输入:
mongod --dbpath "F:\mongoDB\data" --logpath "F:\mongoDB\log\MongoDB.log" --install --serviceName "MongoDB" --journal --storageEngine=mmapv1
mongod --dbpath=E:\MongoDB\data\db --logpath=E:\MongoDB\log\MongoDB.log --install --serviceName "MongoDB"
重新装一个MongoDB服务,注册为Windows服务, 采用连接密码认证模式
mongod --dbpath "H:\MongoDB\data" --logpath "H:\MongoDB\log\mongodb.log" --logappend --serviceName "MongoDB" --auth --install
# 启用认证
mongod --auth
CMD,将MongDB服务从windowsw服务中删除掉
sc delete MongoDB
移除 MongoDB 服务
mongod.exe --remove
mongod --remove --serviceName "MongoDB"
启动服务
net start MongoDB
关闭MongoDB服务
net stop MongoDB
查看Windows服务
services.msc
连接MongoDB
mongo 或者mongo.exe
mongostat -u msl -p msl123 --authenticationDatabase=admin
mongostat --username=msl --password=msl123 --authenticationDatabase=admin
切换到admin数据中
use admin
切换到root管理员账户
db.auth('root','123456')
直接使用root用户登录到admin数据库
mongo admin -u root -p 123456
授予角色:db.grantRolesToUser( "userName" , [ { role: "<role>", db: "<database>" } ])
取消角色:db.grantRolesToUser( "userName" , [ { role: "<role>", db: "<database>" } ])
db.system.users.find().pretty()
db.system.users.remove({user:"userName"})
Java开发MongoDB操作
导入mongo-java-driver-3.6.4.jar驱动包
package cn.mongodb.test;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
public class MongoDBJDBC2 {
public static void main(String args[]) {
testInsert();
testInsertbyAuth();
testQuery();
testUpdate();
tesDeletet();
}
public static void testInsert() {
MongoClient client = new MongoClient("localhost", 27017);
MongoDatabase database = client.getDatabase("runoob");
MongoCollection<Document> collection = database.getCollection("col");
System.out.println(collection.getNamespace());
Document doc = new Document("a new doc", "mogondb");
doc.append("title", "mige jioachao");
doc.append("desc", "mige is not a cainiao")
.append("by", "suhzou");
List<Document> list = new ArrayList<Document>();
list.add(doc);
collection.insertMany(list);
client.close();
}
public static void testInsertbyAuth(){
try {
//连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
//ServerAddress()两个参数分别为 服务器地址 和 端口
ServerAddress serverAddress = new ServerAddress("localhost",27017);
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
MongoCredential credential = MongoCredential.createScramSha1Credential("flowpp", "runoob", "flopww".toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
//通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(addrs,credentials);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("runoob");
MongoCollection<Document> collection = mongoDatabase.getCollection("col");
Document doc = new Document("a new doc", "mogondb");
doc.append("title", "mige jioachao");
doc.append("desc", "mige is not a cainiao")
.append("by", "suhzou");
List<Document> list = new ArrayList<Document>();
list.add(doc);
collection.insertMany(list);
mongoClient.close();
} catch (Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
public static void testQuery(){
try {
//连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
//ServerAddress()两个参数分别为 服务器地址 和 端口
ServerAddress serverAddress = new ServerAddress("localhost",27017);
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
MongoCredential credential = MongoCredential.createScramSha1Credential("flowpp", "runoob", "flopww".toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
//通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(addrs,credentials);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("runoob");
MongoCollection<Document> collection = mongoDatabase.getCollection("col");
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> docs = findIterable.iterator();
while(docs.hasNext()) {
System.out.println(docs.next());
}
mongoClient.close();
} catch (Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
public static void testUpdate(){
try {
//连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
//ServerAddress()两个参数分别为 服务器地址 和 端口
ServerAddress serverAddress = new ServerAddress("localhost",27017);
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
MongoCredential credential = MongoCredential.createScramSha1Credential("flowpp", "runoob", "flopww".toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
//通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(addrs,credentials);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("runoob");
MongoCollection<Document> collection = mongoDatabase.getCollection("col");
collection.updateMany(Filters.eq("likes", 100), new Document("$set", new Document("likes",1000001)));
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> docs = findIterable.iterator();
while(docs.hasNext()) {
System.out.println(docs.next());
}
mongoClient.close();
} catch (Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
public static void tesDeletet(){
try {
//连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
//ServerAddress()两个参数分别为 服务器地址 和 端口
ServerAddress serverAddress = new ServerAddress("localhost",27017);
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
MongoCredential credential = MongoCredential.createScramSha1Credential("flowpp", "runoob", "flopww".toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
//通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(addrs,credentials);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("runoob");
MongoCollection<Document> collection = mongoDatabase.getCollection("col");
collection.deleteOne(Filters.eq("likes", 200));
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> docs = findIterable.iterator();
while(docs.hasNext()) {
System.out.println(docs.next());
}
mongoClient.close();
} catch (Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
参考其他:
MongoDB下载安装以及指令操作
https://www.runoob.com/mongodb/mongodb-window-install.html
常见的NoSQL数据库及相关介绍
https://www.cnblogs.com/newlore/p/9318607.html
NoSql是什么?
https://blog.csdn.net/wyz0516071128/article/details/80877831
常见的nosql数据库有哪些?以及他们的特点与区别?
https://blog.csdn.net/wyz0516071128/article/details/80877984
【MongoDB】MongoDB的下载 安装 配置及使用
https://www.cnblogs.com/smilexumu/p/9090041.html
MongoDB 设置账号和密码
https://www.cnblogs.com/mengyu/p/9071371.html
mongodb用户权限管理最全攻略
https://segmentfault.com/a/1190000015603831