0、配置文件
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod1.logstorage:dbPath: /data/mongod1journal:enabled: truewiredTiger:engineConfig:directoryForIndexes: trueprocessManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod1.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27019bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.bindIpAll: truemaxIncomingConnections: 500unixDomainSocket:enabled: truepathPrefix: /tmp/mongod1filePermissions: 0700security:keyFile: /etc/mongo/mongo.key //所有副本集中的keyfile需要一样authorization: enabledreplication:replSetName: BigBoss //副本集需要配sharding: //分片集群需要配clusterRole: configsvr //配置服务器 shardsvr:数据服务器
1、单机版
1.1、windows
1.1.1、下载
下载地址win32/mongodb-win32-x86_64-2008plus-ssl-4.0.0-signed.msiwin32/mongodb-win32-x86_64-2008plus-ssl-4.0.0.zip
1.1.2、启动
.\mongod --dbpath="" --logpath=""
不指定dapath默认C:\data\db\,logpath可以不指定,输出到控制台。
.\mongod -f mongod.conf
以配置文件启动
1.2、Linux
1.2.1、tar包安装
绿色版:下载地址linux/mongodb-linux-x86_64-4.0.0.tgz
1.2.2、rpm安装
rpm包安装:阿里云镜像:https://mirrors.aliyun.com/mongodb/yum/redhat/
下载:
mongodb-org-tools-4.0.0-1.el6.x86_64.rpm 导入导出mongodump mongoexport …等等工具包
mongodb-org-shell-4.0.0-1.el6.x86_64.rpm 客户端mongo连接工具包
mongodb-org-server-4.0.0-1.el6.x86_64.rpm 服务端mongod程序包
mongodb-org-mongos-4.0.0-1.el6.x86_64.rpm 部署集群需要得包
1.安装rpm -ivh mongodb*.rpm
2.修改配置
vim /etc/mongod.conf
net:port: 27117bindIp: 0.0.0.0
1.2.3、启动
systemctl start mongod
或**mongod -f /etc/mongod.conf**
1.3、连接
mongo --host 192.168.189.100 --port 27017
2、副本集
2.1、安装
2.2、配置
修改配置文件mongod.conf
replication: # 这行解除屏蔽replSetName: rs0 # 这行加上,代表集合名字为tcl,其余的保持默认配置就好 (同一个副本集名字一样)
2.3、启动三台服务器
mongo --host 192.168.189.100 --port 27017>cfg = {_id: "rs0",protocolVersion: 1, //协议版本,默认为0members: [{_id: 0, host: "192.168.56.102:27017"},{_id: 1, host: "192.168.56.104:27017"},{_id: 2, host: "192.168.56.101:27017"}]}rs.initiate(cfg) 初始化副本集 // 定义配置文件初始化rs.status() 查看状态rs.status().membersrs.conf() 查看副本集配置rs.isMaster() 是否为主db.isMaster()rs.slaveOk() allow queries on secondary nodes,从节点可以读,但不能写(默认不能读写) 在从客户端操作,重启失效db.getMongo().setSlaveOk() 和rs.slaveOk()效果类似rs.reconfig(cfg) 重新配置 只能在主服务器上执行,但状态被删除,可以使用rs.reconfig(cfg,{"force":true}) 强制重新配置,4.0.0不支持版本0,需要协议版本为1
rs.initiate() //使用默认配置初始化,后加入副本集rs.status()rs.add( "192.168.56.102:27017" )rs.add( "192.168.56.102:27018" )
注意:默认secondary不能读写,
rs.slaveOk()可以读,但不能写
rs.reconfig(cfg):重新配置副本集名字要一样
cfg = {_id: "rs0",members: [{_id: 0, host: "192.168.189.100:27017"},{_id: 1, host: "192.168.189.101:27017"},{_id: 2, host: "192.168.189.102:27017"}]}
3、分片集群
集群架构:
mongos:数据库请求路由。负责接收所有客户端应用程序的连接查询请求,并将请求路由到集群内部对应的分片上。”mongos”可以有1个或多个。
config server: 配置服务,负责保存集群的元数据信息,比如集群的分片信息、用户信息。
MongoDB 3.4 版本以后,“config server” 必须是副本集!
shard: 分片存储。将数据分片存储在多个服务器上。
有点类似关系数据库”分区表”的概念,只不过分区表是将数据分散存储在多个文件中,而sharding将数据分散存储在多个服务器上。一个集群可以有一个或多个分片。
MongoDB 3.6以后,每个分片都必须是副本集
mongos192.168.189.100:4000configServer:192.168.189.100:27019192.168.189.101:27019192.168.189.102:27019shard1:192.168.189.100:27017192.168.189.101:27017192.168.189.102:27017shard2:192.168.189.100:27018192.168.189.101:27018192.168.189.102:27018
3.1、configServer副本集搭建
1.先把data和log目录建好
2.配置mongod.conf,其他服务器配置文件一样,端口改为27018、27019 。
注意:config服务器需要—configsvr
systemLog:destination: filelogAppend: truepath: /usr/local/mongodb/log/mongod.logstorage:dbPath: /usr/local/mongodb/datajournal:enabled: trueprocessManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27017bindIp: 0.0.0.0replication:replSetName: rsConfigsharding:clusterRole: configsvr
3.启动三台mongodmongod -f mongod.conf
4.连接任一台mongod,配置副本集
mongo --host 192.168.189.100 --port 27017>cfg = {_id: "rsConfig",members: [{_id: 0, host: "192.168.189.100:27019"},{_id: 1, host: "192.168.189.101:27019"},{_id: 2, host: "192.168.189.102:27019"}]}>rs.initiate(cfg)>rs.status()
3.2、shard1、shard2副本集搭建
1.建好相应目录
2.配置mongod.conf,其他的端口改下即可,shard2配置一样
systemLog:destination: filelogAppend: truepath: /usr/local/mongodb/log/mongod.logstorage:dbPath: /usr/local/mongodb/datajournal:enabled: trueprocessManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 27017bindIp: 0.0.0.0replication:replSetName: rsConfigsharding:clusterRole: shardsvr
3.启动三台mongodmongod -f mongod.conf
4.连接任一台mongod,配置副本集
mongo --host 192.168.189.100 --port 27017>cfg = {_id: "rs1",members: [{_id: 0, host: "192.168.189.100:27018"},{_id: 1, host: "192.168.189.101:27018"},{_id: 2, host: "192.168.189.102:27018"}]}>rs.initiate(cfg)>rs.status()
3.3、搭建mongos
1.建好相应目录,mongos没有data目录
2.配置route.conf,没有dbpath属性
systemLog:destination: filelogAppend: truepath: /usr/local/mongodb/log/route.logprocessManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/route.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfonet:port: 40000bindIp: 0.0.0.0sharding:configDB: rsConfig/192.168.189.100:27017,192.168.189.100:27018,192.168.189.100:27019
3.启动mongosmongos -f route.conf
4.连接并添加分片
>mongo --port 40000>sh.addShard("shard1/192.168.189.100:27018,192.168.189.101:27018,192.168.189.102:27018")>sh.addShard("shard1/192.168.189.100:27019,192.168.189.101:27019,192.168.189.102:27019")>sh.status()
5.为test数据库开启分片,选择一个片键age并指定一个集合mycoll对其进行分片
>sh.enableSharding("test")>sh.shardCollection("test.mycoll", {"age": 1})sh.status()
