下载地址:
https://www.mongodb.com/try/download/community
下载mongodb-linux-x86_64-rhel70-5.0.18
搭建集群
tar -zxvf mongodb-linux-x86_64-rhel70-5.0.18.tgz
mkdir -p data/dp
cd mongodb-linux-x86_64-rhel70-5.0.18
mkdir -p data/db
mkdir log
mkdir conf
cd log
touch mongodb.log
cd ../conf
touch mongodb.conf
mkdir key
openssl rand -base64 75 > ./access.keyfile(其他机器上的access.keyfile内容复制本机器的access.keyfile内容)
Chmod 600 access.keyfile
pwd 查看当前路径
vi mongodb.conf
systemLog:# MongoDB发送所有日志输出的目标指定为文件# The path of the log file to which mongod or mongos should send all diagnostic logging informationdestination: file# mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径path: "/home/admin/mongodb-linux-x86_64-rhel70-5.0.18/log/mongodb.log"# 当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。logAppend: true
storage:# mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。# The directory where the mongod instance stores its data.Default Value is "/data/db".dbPath: "/home/admin/mongodb-linux-x86_64-rhel70-5.0.18/data/db"journal:#启用或禁用持久性日志以确保数据文件保持有效和可恢复。enabled: true
processManagement:#启用在后台运行mongos或mongod进程的守护进程模式。fork: true
net:# 服务实例绑定的IP,默认是localhost(10.0.4.12:局域网IP)bindIp: 0.0.0.0# bindIp#绑定的端口,默认是27017port: 27017
security:keyFile: /var/local/mongodb/key/access.keyfileauthorization: enabled #开启认证登录
导入环境变量中
export PATH=/root/Desktop/mongodb-linux-x86_64-rhel70-5.0.18/bin:$PATH
以上操作在每个机器都做一遍
之后选择一个作为主节点,启动mongodb
启动:mongod -f ../conf/mongodb.conf --replSet "grap"
每个机器都做一边,最后连接一台机器
Mongo进去数据库中,(注意一定不要先执行rs.initiate(),否则后面的操作执行失败,显示已经初始化)
初始化数据集
rs.initiate( {_id: "grap",members:[{_id:1, host:'IP1:27017',priority:1},{_id:2, host:'IP2:27017',priority:1},{_id:3, host:'IP3:27017',priority:99}
] })
#priority越大越容易被选为主节点
创建用户数据库
Primary节点为主节点(可以创建用户),Secondary节点不可以创建用户
#修改admin密码
use admin
db.createUser({user: "admin", pwd: "yourpassword", roles: ["root"]})
db.auth("admin","yourpassword")
#创建graylog数据库并设置密码
rs0:PRIMARY> use graylog
rs0:PRIMARY> db.createUser( {
... user: "graylog",
... pwd: "yourpassword",
... roles: [ { role: "readWrite", db: "graylog" } ]
... });
rs0:PRIMARY> db.grantRolesToUser( "graylog" , [ { role: "dbAdmin", db: "graylog" } ])
rs0:PRIMARY> show users
rs0:PRIMARY> db.auth("graylog","yourpassword")