MongoDB Community Edition
1
2
3
|
mkdir -p ~/dbs/master ~/dbs/slave
./mongod --master --port 10000 --dbpath ~/dbs/master
./mongod --slave --port 10001 --dbpath ~/dbs/slave --source localhost:10000
|
- 传递数据库存储路径,默认是"/data/db"
- 端口号 默认是 “27017”
https://www.mongodb.com/docs/manual/administration/install-community/
1
2
3
4
5
6
7
8
9
10
11
|
brew tap mongodb/brew
brew update
brew install mongodb-community@5.0
brew --prefix
# To run MongoDB as a macOS service
brew services start mongodb-community@5.0
brew services list
brew services stop mongodb-community@5.0
# To run MongoDB manually as a background process
mongod --config /opt/homebrew/etc/mongod.conf --fork
ps aux | grep -v grep | grep mongod
|
- configuration file
/opt/homebrew/etc/mongod.conf
- log directory
/opt/homebrew/var/log/mongodb
- data directory
/opt/homebrew/var/mongodb
MongoDB Shell
mongosh
1
2
3
4
5
6
7
8
|
# mongo 新版本 支持语法高亮 命令历史 改进日志
mongosh
mongosh "mongodb://localhost:27017"
mongosh "mongodb://mongodb0.example.com:28015" --username alice --authenticationDatabase admin
show dbs
show collections
db_adminCommand("connPoolStats")
|
MongoDB Java Drivers
1
2
3
|
dependencies {
implementation 'org.mongodb:mongodb-driver-sync:4.6.0'
}
|
1
2
3
4
5
6
7
8
9
10
|
public static void main( String[] args ) {
// Replace the uri string with your MongoDB deployment's connection string
String uri = "<connection string uri>";
try (MongoClient mongoClient = MongoClients.create(uri)) {
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> collection = database.getCollection("movies");
Document doc = collection.find(eq("title", "Back to the Future")).first();
System.out.println(doc.toJson());
}
}
|
1
2
3
|
dependencies {
compile 'org.mongodb:mongodb-driver-reactivestreams:4.5.0'
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
MongoClient mongoClient = MongoClients.create(
MongoClientSettings.builder()
.applyToClusterSettings(builder ->
builder.hosts(Arrays.asList(new ServerAddress("hostOne", 27018))))
.build());
MongoDatabase database = mongoClient.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
Document doc = new Document("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
.append("info", new Document("x", 203).append("y", 102));
collection.insertOne(doc).subscribe(new OperationSubscriber<InsertOneResult>());
|
Database Profiler
Database Profiler 收集有关针对的 mongod 实例运行操作命令的详细信息。这包括 CRUD 操作以及配置和管理命令。分析器将它收集的所有数据写入一个 system.profile
集合,每个分析数据库中的一个上限集合(capped collection)。
Profiler 是默认是 off 的。您可以在多个分析级别之一上基于每个数据库或每个实例启用分析器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# 为所有数据库开启慢查询记录
db.setProfilingLevel(2)
# 指定数据库,并指定阈值慢查询 ,超过20毫秒的查询被记录
use test
db.setProfilingLevel(1, { slowms: 20 })
# 随机采集慢查询的百分比值,sampleRate 值默认为1,表示都采集,0.42 表示采集42%的内容。
db.setProfilingLevel(1, { sampleRate: 0.42 })
#记录所有操作日志,过滤查询操作超过2秒的
db.setProfilingLevel( 2, { filter: { op: "query", millis: { $gt: 2000 } } } )
# 查询慢查询级别和其它信息
db.getProfilingStatus()
# 仅返回慢查询级别
db.getProfilingLevel()
|
分析日志查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# 查询最近的10个慢查询日志
db.system.profile.find().limit(10).sort( { ts : -1 } ).pretty()
# 查询除命令类型为 ‘command’ 的日志
db.system.profile.find( { op: { $ne : 'command' } } ).pretty()
# 查询数据库为 mydb 集合为 test 的 日志
db.system.profile.find( { ns : 'mydb.test' } ).pretty()
# 查询 低于 5毫秒的日志
db.system.profile.find( { millis : { $gt : 5 } } ).pretty()
# 查询时间从 2012-12-09 3点整到 2012-12-09 3点40分之间的日志
db.system.profile.find({
ts : {
$gt: new ISODate("2012-12-09T03:00:00Z"),
$lt: new ISODate("2012-12-09T03:40:00Z")
}
}).pretty()
# 下面的示例查看时间范围,将用户字段从输出中删除以使其更容易阅读,并根据每个操作运行的时间对结果进行排序
db.system.profile.find({
ts : {
$gt: new ISODate("2011-07-12T03:00:00Z"),
$lt: new ISODate("2011-07-12T03:40:00Z")
}
}, { user: 0 }).sort( { millis: -1 } )
|