Windows 平台安装 MongoDB
https://www.runoob.com/mongodb/mongodb-window-install.html
MongoDB 下载
MongoDB 提供了可用于 32 位和 64 位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoDB 预编译二进制包下载地址:https://www.mongodb.com/download-center/community
注意:在 MongoDB 2.2 版本后已经不再支持 Windows XP 系统。最新版本也已经没有了 32 位系统的安装文件。
- MongoDB for Windows 64-bit 适合 64 位的 Windows Server 2008 R2, Windows 7 , 及最新版本的 Window 系统。
- MongoDB for Windows 32-bit 适合 32 位的 Window 系统及最新的 Windows Vista。 32 位系统上 MongoDB 的数据库最大为 2GB。
- MongoDB for Windows 64-bit Legacy 适合 64 位的 Windows Vista, Windows Server 2003, 及 Windows Server 2008 。
下载 .msi 文件,下载后双击该文件,按操作提示安装即可。
安装过程中,你可以通过点击 "Custom(自定义)" 按钮来设置你的安装目录。
下一步安装 "install mongoDB compass" 不勾选(当然你也可以选择安装它,可能需要更久的安装时间),MongoDB Compass 是一个图形界面管理工具,我们可以在后面自己到官网下载安装,下载地址:https://www.mongodb.com/download-center/compass。
创建数据目录
添加配置文件“mongod.cfg”
systemLog:
destination: file
path: D:\Learn\MongoDB3.6\data\log\mongod.log
storage:
dbPath: D:\Learn\MongoDB3.6\data\db
启动服务
#以管理员身份运行
mongod.exe --config "D:\Learn\MongoDB3.6\mongod.cfg" --serviceName "MongoDB" --install
启动MongoDB服务
net start MongoDB
关闭MongoDB服务
net stop MongoDB
移除 MongoDB 服务
D:\Learn\MongoDB3.6\bin\mongod.exe --remove
至此单机版安装成功
MongoDB分布式安装
config.conf
net:
port: 27018 #config1的端口,mongo2下的为27028,mongo3下的为27038
bindIp: 0.0.0.0 #允许连接地址,这里统一设置为允许任何ip连接
systemLog:
destination: file
logAppend: true
path: D:\Learn\MongoDB3.6\mongo1\log\config.log #config的日志存储地址,mongo2,mongo3下路径调整即可
storage:
dbPath: D:\Learn\MongoDB3.6\mongo1\data\config #config的数据存储地址,mongo2、mongo3下做相应的调整
journal:
enabled: true #数据故障恢复和持久化,肯定是开启
#processManagement:
# fork: true #linux下打开此设置,Windows环境下后台启动需要注册服务,
# pidFilePath: /mongo/config/run/mongod.pid
#副本集名称设置
replication:
replSetName: configs
#分片集群角色,配置服务器的固定值
sharding:
clusterRole: configsvr
mongos.conf
systemLog:
destination: file
logAppend: true
path: D:\Learn\MongoDB3.6\mongo1\log\mongos.log #mongos没有数据存储地址,只有日志输出地址
# network interfaces
net:
port: 27019 # mongos的端口
bindIp: 0.0.0.0
setParameter:
enableLocalhostAuthBypass: false
#注意!此处配置的连接地址为配置服务器config Serve的副本集名称和所在服务器地址以及端口
sharding:
configDB: configs/127.0.0.1:27018
shard1.conf
net:
port: 27001 #设置端口
bindIp: 0.0.0.0
systemLog:
destination: file
logAppend: true
path: D:\Learn\MongoDB3.6\mongo1\log\shard1.log #日志文件所在目录
storage:
dbPath: D:\Learn\MongoDB3.6\mongo1\data\shard1 #数据存储目录
journal:
enabled: true
#processManagement:
# fork: true
# pidFilePath: /mongo/shard1/run/mongod.pid
replication:
replSetName: shard1 #副本集名称,下其他两个分片对应调整shard2为shard2,shard3为shard3
sharding:
clusterRole: shardsvr #集群中角色归属
shard2.conf
net:
port: 27002 #设置端口
bindIp: 0.0.0.0
systemLog:
destination: file
logAppend: true
path: D:\Learn\MongoDB3.6\mongo1\log\shard2.log #日值文件
storage:
dbPath: D:\Learn\MongoDB3.6\mongo1\data\shard2 #数据文件
journal:
enabled: true
#processManagement:
# fork: true
# pidFilePath: /mongo/shard1/run/mongod.pid
replication:
replSetName: shard2 #副本集名称,下其他两个分片对应调整shard2为shard2,shard3为shard3
sharding:
clusterRole: shardsvr #集群中角色归属
shard3.conf
net:
port: 27003 #设置端口
bindIp: 0.0.0.0
systemLog:
destination: file
logAppend: true
path: D:\Learn\MongoDB3.6\mongo1\log\shard3.log #日志文件
storage:
dbPath: D:\Learn\MongoDB3.6\mongo1\data\shard3 #数据文件
journal:
enabled: true
#processManagement:
# fork: true
# pidFilePath: /mongo/shard1/run/mongod.pid
replication:
replSetName: shard3 #副本集名称,下其他两个分片对应调整shard2为shard2,shard3为shard3
sharding:
clusterRole: shardsvr #集群中角色归属
##配置中心服务器
#启动配置中心服务器
##启动配置中心服务器(不要关闭,该窗体),上个图有验证是否成功
mongod -f D:\Learn\MongoDB3.6\mongo1\conf\config.conf
#连接到配置中心服务器(新打开命令窗体)
mongo -host 127.0.0.1 --port 27018
#配置服务器
config = { _id : "configs",
members : [{_id : 0, host : "127.0.0.1:27018" }]
}
#初始化副本集:
rs.initiate(config);
#查看副本集状态:
rs.status();
##分片副本服务器
##分片1
#启动分片1(不要关闭,该窗体),上个图有验证是否成功
mongod -f D:\Learn\MongoDB3.6\mongo1\conf\shard1.conf
#连接到分片1(新打开命令窗体)
mongo --port 27001
#配置
config = { _id : "shard1",
members : [{_id : 0, host : "127.0.0.1:27001"}]
}
#初始化分片1的副本集:
rs.initiate(config);
#查看副本集状态:
rs.status();
##分片2
#启动分片2(不要关闭,该窗体),上个图有验证是否成功
mongod -f D:\Learn\MongoDB3.6\mongo1\conf\shard2.conf
#连接到分片2(新打开命令窗体)
mongo --port 27002
#配置分片2
config = { _id : "shard2",
members : [{_id : 0, host : "127.0.0.1:27002"}]
}
#初始化分片2的副本集:
rs.initiate(config);
##分片3
#启动分片3(不要关闭,该窗体),上个图有验证是否成功
mongod -f D:\Learn\MongoDB3.6\mongo1\conf\shard3.conf
#连接到分片3
mongo --port 27003
#配置分片3
config = { _id : "shard3",
members : [{_id : 0, host : "127.0.0.1:27003"}]
}
#初始化分片3的副本集:
rs.initiate(config);
##配置mongos路由
#启动mongos路由(不要关闭,该窗体),上个图有验证是否成功
mongos -f D:\Learn\MongoDB3.6\mongo1\conf\mongos.conf
#连接到mongos路由(新打开命令窗体)
mongo --port 27019
#使用admin库,启用集群分片:
use admin;
#添加分片1
sh.addShard("shard1/127.0.0.1:27001");
#添加分片2
sh.addShard("shard2/127.0.0.1:27002");
#添加分片3
sh.addShard("shard3/127.0.0.1:27003");
#创建用户
db.createUser( { user: "admin", pwd: "admin", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
#退出
exit
#重新连接
mongo --port 27019 -u "admin" -p "admin" --authenticationDatabase "admin"
至此,配置完成。
验证分片
#设置一下chunk的大小:
use config;
# 设置块大小为1M是方便实验,不然需要插入海量数据
db.setting.save({"_id":"chunksize","value":1})
#模拟写入数据
use testdb;
#启用数据库分片
sh.enableSharding("testdb")
#创建集合
use testdb;
db.createCollection("user");
# 以"name"作哈希索引分片键
db.user.createIndex({"name":"hased"})
#对user集合启用分片
sh.shardCollection( "testdb.user", { "name" : "hashed" } )
#模拟往mytest数据库的user表写入5万数据
for(i=1;i<=50000;i++){db.user.insert({"id":i,"name":"zzx"+i})}
# 查看分片情况
sh.status()
# 查看sh下所有指令
sh.help()
# 查看数据库分片帮助指令
db.settings.help()
# 查看分片列表,所有分片
use admin
db.runCommand({listShards:1})