摘要
- 本文内容基于mongodb4.2.3
- 本文基于本地安装,也就是ip相同,端口不同
- 3个shard复制集(3台),1个config复制集(3台),2个router
安装MongoDB
下载地址 : https://www.mongodb.com/download-center
下载后解压即可,可以将bin目录配置到$PATH中
目录设计
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| ├── config │ ├── node1 │ │ ├── db │ │ ├── logs │ │ └── mongod.conf │ ├── node2 │ │ ├── db │ │ ├── logs │ │ └── mongod.conf │ └── node3 │ ├── db │ ├── logs │ └── mongod.conf ├── keyfile.key ├── router │ ├── node1 │ │ ├── logs │ │ └── mongos.conf │ └── node2 │ ├── logs │ └── mongos.conf ├── shard1 │ ├── node1 │ │ ├── db │ │ ├── logs │ │ └── mongod.conf │ ├── node2 │ │ ├── db │ │ ├── logs │ │ └── mongod.conf │ └── node3 │ ├── db │ ├── logs │ └── mongod.conf ├── shard2 │ ├── node1 │ │ ├── db │ │ ├── logs │ │ └── mongod.conf │ ├── node2 │ │ ├── db │ │ ├── logs │ │ └── mongod.conf │ └── node3 │ ├── db │ ├── logs │ └── mongod.conf └── shard3 ├── node1 │ ├── db │ ├── logs │ └── mongod.conf ├── node2 │ ├── db │ ├── logs │ └── mongod.conf └── node3 ├── db ├── logs └── mongod.conf
|
端口分配
1 2 3 4 5
| shard1 28011~28013 shard2 28021~28023 shard3 28031~28033 config 29011~29013 router 29021~29022
|
配置文件
shard
以shard1为例,三个node下都有mongod.conf,要注意替换文件路径和端口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| systemLog: destination: file path: "/Users/hanqf/myservice_dir/mongodb-mongos/shard1/node1/logs/mongo.log" logAppend: true storage: journal: enabled: true dbPath: "/Users/hanqf/myservice_dir/mongodb-mongos/shard1/node1/db" processManagement: fork: true net: bindIp: 0.0.0.0 port: 28011 setParameter: enableLocalhostAuthBypass: true replication: replSetName: "shard1" sharding: clusterRole: shardsvr security: authorization: "enabled" keyFile: /Users/hanqf/myservice_dir/mongodb-mongos/keyFile.key
|
config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| systemLog: destination: file path: "/Users/hanqf/myservice_dir/mongodb-mongos/config/node1/logs/mongo.log" logAppend: true storage: journal: enabled: true dbPath: "/Users/hanqf/myservice_dir/mongodb-mongos/config/node1/db" processManagement: fork: true net: bindIp: 0.0.0.0 port: 29011 setParameter: enableLocalhostAuthBypass: true replication: replSetName: "config" sharding: clusterRole: configsvr security: authorization: "enabled" keyFile: /Users/hanqf/myservice_dir/mongodb-mongos/keyFile.key
|
router
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| systemLog: destination: file path: "/Users/hanqf/myservice_dir/mongodb-mongos/router/node1/logs/mongos.log" logAppend: true processManagement: fork: true net: bindIp: 0.0.0.0 port: 29021 setParameter: enableLocalhostAuthBypass: true replication: localPingThresholdMs: 15 sharding: configDB: config/127.0.0.1:29011,127.0.0.1:29012,127.0.0.1:29013 security: keyFile: /Users/hanqf/myservice_dir/mongodb-mongos/keyFile.key
|
keyFile
1 2 3 4 5
| cd /Users/hanqf/myservice_dir/mongodb-mongos
openssl rand -base64 756 > keyFile.key
chmod 400 mongodb-keyfile
|
配置config复制集
启动
1 2 3
| mongod --config /Users/hanqf/myservice_dir/mongodb-mongos/config/node1/mongod.conf mongod --config /Users/hanqf/myservice_dir/mongodb-mongos/config/node2/mongod.conf mongod --config /Users/hanqf/myservice_dir/mongodb-mongos/config/node3/mongod.conf
|
登录及配置
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
| mongo --host 127.0.0.1:29011
>rs.initiate( { _id: "config", configsvr: true, members: [ { _id : 0, host : "127.0.0.1:29011" }, { _id : 1, host : "127.0.0.1:29012" }, { _id : 2, host : "127.0.0.1:29013" } ] } );
>rs.isMaster() >use admin >db.createUser( { user: "root", pwd: "password", roles: [ { role: "root", db: "admin" } ] } );
|
验证用户是否可以登录
1 2 3
| mongo --host 127.0.0.1:29011 >use admin >db.auth("root","password")
|
配置shard复制集
启动shard1服务
1 2 3
| mongod --config /Users/hanqf/myservice_dir/mongodb-mongos/shard1/node1/mongod.conf mongod --config /Users/hanqf/myservice_dir/mongodb-mongos/shard1/node2/mongod.conf mongod --config /Users/hanqf/myservice_dir/mongodb-mongos/shard1/node3/mongod.conf
|
登录及配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| mongo --host 127.0.0.1:28011
>rs.initiate( { _id: "shard1", members: [ { _id : 0, host : "127.0.0.1:28011" }, { _id : 1, host : "127.0.0.1:28012" }, { _id : 2, host : "127.0.0.1:28013" } ] } );
>rs.isMaster() >use admin >db.createUser( { user: "root", pwd: "password", roles: [ { role: "root", db: "admin" } ] } );
|
启动shard2服务
1 2 3
| mongod --config /Users/hanqf/myservice_dir/mongodb-mongos/shard2/node1/mongod.conf mongod --config /Users/hanqf/myservice_dir/mongodb-mongos/shard2/node2/mongod.conf mongod --config /Users/hanqf/myservice_dir/mongodb-mongos/shard2/node3/mongod.conf
|
登录及配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| mongo --host 127.0.0.1:28021
>rs.initiate( { _id: "shard2", members: [ { _id : 0, host : "127.0.0.1:28021" }, { _id : 1, host : "127.0.0.1:28022" }, { _id : 2, host : "127.0.0.1:28023" } ] } );
>rs.isMaster() >use admin >db.createUser( { user: "root", pwd: "password", roles: [ { role: "root", db: "admin" } ] } );
|
启动shard3服务
1 2 3
| mongod --config /Users/hanqf/myservice_dir/mongodb-mongos/shard3/node1/mongod.conf mongod --config /Users/hanqf/myservice_dir/mongodb-mongos/shard3/node2/mongod.conf mongod --config /Users/hanqf/myservice_dir/mongodb-mongos/shard3/node3/mongod.conf
|
登录及配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| mongo --host 127.0.0.1:28031
>rs.initiate( { _id: "shard3", members: [ { _id : 0, host : "127.0.0.1:28031" }, { _id : 1, host : "127.0.0.1:28032" }, { _id : 2, host : "127.0.0.1:28033" } ] } );
>rs.isMaster() >use admin >db.createUser( { user: "root", pwd: "password", roles: [ { role: "root", db: "admin" } ] } );
|
重启router服务
1 2
| mongos --config /Users/hanqf/myservice_dir/mongodb-mongos/router/node1/mongos.conf mongos --config /Users/hanqf/myservice_dir/mongodb-mongos/router/node2/mongos.conf
|
分片配置
登录任意router
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 28 29 30
| mongo --host 127.0.0.1:29021 >use admin >db.auth("root","password")
>sh.addShard( "shard1/127.0.0.1:28011,127.0.0.1:28012,127.0.0.1:28013") >sh.addShard( "shard2/127.0.0.1:28021,127.0.0.1:28022,127.0.0.1:28023") >sh.addShard( "shard3/127.0.0.1:28031,127.0.0.1:28032,127.0.0.1:28033")
>sh.status()
>use springboot >db.createUser( { user: "springboot", pwd: "123456", roles: [ { role: "dbOwner", db: "springboot" } ] } );
>sh.enableSharding("springboot")
>sh.shardCollection("springboot.user", { _id : "hashed" } )
>use springboot
>db.user.stats()
|
小贴士
注意
- 正式环境注意权限控制,使客户端只能连接router
- 可以创建任意多个router
- springboot连接方式:
spring.data.mongodb.uri=mongodb://springboot:123456@127.0.0.1:29021,127.0.0.1:29022/springboot?authSource=springboot