创建yum源
vim /etc/yum.repos.d/mongodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=0
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
安装
yum install mongodb-org -y
修改mongo配置
vim /etc/mongo.conf
replication:
oplogSizeMB: 81920
replSetName: ktmapp
systemctl start mongod
初始化集群
use admin
config={_id:"ktmapp",members:[{_id:1,host:"192.168.90.123:27017"}]}
rs.initiate(config);
添加节点(, priority:90为权重)
rs.add({_id:2, host:"192.168.90.124:27017"})
添加仲裁:
rs.addArb("192.168.90.125:27017")
查看
调整优先级
cfg = rs.conf()
cfg.members[1].priority = 85
rs.reconfig(cfg)
导出所有数据库
mongodump -h 127.0.0.1 -o /home/ktm
导入所有数据库mongodb中
mongorestore /home/ktm
备份mongo(同时备份期间的oplog)
mongodump -h 192.168.10.3 --port 27017 --oplog -o /var/data/
恢复mongo(同时回放oplog)
mongorestore -h 127.0.0.1 --authenticationDatabase admin --oplogReplay /var/data
单独备份oplog
mongodump -h 192.168.10.3 -d local -c oplog.rs --oplog -o /var/data/
单独恢复oplog
cp local/oplog.rs.bson22.232 /var/data/oplog.bson
cp local/*23.01 /var/data/local/
mongorestore -h 127.0.0.1 --authenticationDatabase admin --oplogReplay /var/data
# 认证登录
openssl rand -base64 741 > /var/ssh/mongodb.keyfile
chmod 600 /var/ssh/mongodb.keyfile
chown -R mongod:mongod 600 /var/ssh/mongodb.keyfile
vim /etc/mongod.conf
security:
keyFile: /var/ssh/mongodb.keyfile
authorization: enabled
mongo
添加密码
use admin
db.createUser(
{
user:"myadmin",
pwd:"test1",
roles:[{role:"root",db:"admin"}]
}
)
db.createUser(
{
user:"readonly",
pwd:"test2",
roles:[{role:"readAnyDatabase",db:"admin"}]
}
)
db.createUser(
{
user:"apply",
pwd:"test3",
roles:[{role:"readWriteAnyDatabase",db:"admin"}]
}
)
mongo集群分片
openssl rand -base64 741 > /var/ssh/mongodb.keyfile
chmod 600 /var/ssh/mongodb.keyfile
vim /var/opt/mongodb/config.conf
#数据目录
dbpath=/var/opt/mongodb/config/data/
#日志文件
logpath=/var/opt/mongodb/config/log/config.log
#日志追加
logappend=true
#端口
port = 20000
#最大连接数
maxConns = 5000
pidfilepath = /var/run/mongo_20000.pid
#日志,redo log
journal = true
#刷写提交机制
journalCommitInterval = 200
#守护进程模式
fork = true
#刷写数据到日志的频率
syncdelay = 60
#storageEngine = wiredTiger
#操作日志,单位M
oplogSize = 1000
#指定复制集
replSet=configs
#命名空间的文件大小,默认168M,最大2G。
nssize = 168
#noauth = true
unixSocketPrefix = /tmp
configsvr = true
auth=true
keyFile=/var/ssh/mongodb.keyfile
vim /var/opt/mongodb/shard1.conf
pidfilepath = /var/run/mongo_36000.pid
dbpath = /var/opt/mongodb/shard1/data
logpath = /var/opt/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 36000
fork = true
#打开web监控
httpinterface=true
rest=true
#副本集名称
replSet=shard1
#操作日志,单位M
oplogSize = 1000
#declare this is a shard db of a cluster;
shardsvr = true
#设置最大连接数
maxConns=20000
#日志,redo log
journal = true
#刷写提交机制
journalCommitInterval = 200
#刷写数据到日志的频率
syncdelay = 60
#noauth = true
auth=true
keyFile=/var/ssh/mongodb.keyfile
vim /var/opt/mongodb/mongo.conf
pidfilepath = /var/run/mongos.pid
logpath = /var/opt/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 30000
fork = true
configdb = configs/192.168.52.100:20000,192.168.52.101:20000,192.168.52.102:20000
#设置最大连接数
maxConns=20000
#auth=true
keyFile=/var/ssh/mongodb.keyfile
#启动服务
mongod -f /var/opt/mongodb/conf/config.conf
mongod -f /var/opt/mongodb/conf/shard1.conf
mongos -f /var/opt/mongodb/conf/mongo.conf
#登录用户
登录到其中一个monogs,然后添加一个用户
mongos> use admin
switched to db admin
------------------------------------------------------------------
db.createUser(
{
user:"readonly",
pwd:"test.123",
roles:[{role:"readAnyDatabase",db:"admin"}]
}
)
db.createUser(
{
user:"apply",
pwd:"test@123.qwe",
roles:[{role:"readWriteAnyDatabase",db:"admin"}]
}
)
--------------------------------------------------------------------------------
mongos> db.auth('root','root')
#删除用户
mongos> db.dropUser("readonly")
true
mongos> db.dropUser("apply")
true
-----------------------------------------------------
在各个sharing分片添加用户
mongo 192.168.20.35:27000
db.createUser(
{
user:"myadmin",
pwd:"123",
roles:[{role:"root",db:"admin"}]
}
)
-------------------------------
db.createUser(
{
user:"readonly",
pwd:"123",
roles:[{role:"readAnyDatabase",db:"admin"}]
}
)
db.createUser(
{
user:"apply",
pwd:"123",
roles:[{role:"readWriteAnyDatabase",db:"admin"}]
}
)