mongodb 4.2单机部署
1.下载相应的版本
https://www.mongodb.com/download-center/community
我这里下载的是mongodb-linux-x86_64-rhel70-4.2.9.tgz
---------------------------------------安装部署---------------------------------------
1.创建安装目录
192.168.1.169:28001对应的安装目录
[root@test services]# mkdir -p /usr/local/services
[root@test services]# mkdir -p /home/middle/mongodb/data
[root@test services]# mkdir -p /home/middle/mongodb/log
[root@test services]# mkdir -p /home/middle/mongodb/key
[root@test services]# mkdir -p /home/middle/mongodb/conf
[root@test services]# mkdir -p /home/middle/mongodb/run
2.安装数据库
192.168.1.169:28001 安装
[root@test]# cd /soft
[root@test soft]# tar -xvf mongodb-linux-x86_64-rhel70-4.2.9.tgz
[root@test soft]# mv mongodb-linux-x86_64-rhel70-4.2.9 /usr/local/services/mongodb
3.产生秘钥验证
在其中一个机器上创建秘钥文件,我这里是在192.168.1.169:29001
[root@test key]# cd /home/middle/mongodb/key
[root@test key]# openssl rand -base64 741 >>keyfile
[root@test key]# chmod 700 keyfile
4.生成日志文件
192.168.1.169:28001
[root@test key]#echo>/home/middle/mongodb/log/mongodb.log
5.创建配置文件mongo.cnf
##主节点的配置参数
vi /home/middle/mongodb/conf/mongo.cnf
port=28001
fork=true
dbpath=/home/middle/mongodb/data
logpath=/home/middle/mongodb/log/mongodb.log
pidfilepath=/home/middle/mongodb/run/28001.pid
logappend=true
##shardsvr=true ##创建副本集才需要
##replSet=repltest ##创建副本集才需要
bind_ip=192.168.1.169,127.0.0.1
oplogSize=10000
logRotate=reopen
##auth=true
这里keyFile和auth先注释,因为等部署完初始化完集群后再启用
6.启动
主节点启动:
192.168.1.169:28001
[root@test key]# /usr/local/services/mongodb/bin/mongod -f /home/middle/mongodb/conf/mongo.cnf
7.创建管理员用户
在主节点上(我这里是节点1)上创建管理员账号test
[root@localhost bin]# /usr/local/services/mongodb/bin/mongo 192.168.1.169:28001
use admin
db.createUser({user:"yeemiao",pwd:"yeemiao123",roles:["root"]}); --创建用户
db.auth("yeemiao","yeemiao123"); --设置用户登陆权限,密码一定要和创建用户时输入的密码相同
show users; --查看创建的用户
8.重启mongodb然后启用认证参数
停掉mongodb
/usr/local/services/mongodb/bin/mongo localhost:28001
use admin
db.auth("yeemiao","yeemiao123");
db.shutdownServer()
修改配置文件,将之前注释的两行,启用
auth = true
再次启动
/usr/local/services/mongodb/bin/mongod -f /home/middle/mongodb/conf/mongo.cnf
9.再次启动后创建普通账号,并写入记录
[root@test key]#/usr/local/services/mongodb/bin/mongo 192.168.1.169:28001
> use admin
switched to db admin
> db.auth("yeemiao","yeemiao123");
>use mgdb_zhibo
>db.createUser({user:'threedev',pwd:'threedev123',roles:[{role:'dbOwner',db:'mgdb_zhibo'}]})
10.加入自动重启服务
参考
http://blog.chinaunix.net/uid-77311-id-5791282.html
-- The End --