mongo在哪创建管理员_MongoDB 创建副本集并添加管理员账号

这篇博客详细介绍了如何在MongoDB中创建一个一主二从的副本集,包括创建数据库目录、日志文件,启动mongod实例,配置文件设置,初始化副本集,查看副本集状态,以及在副本集中创建管理员账号的步骤。过程中提到了可能遇到的错误和解决方案。
摘要由CSDN通过智能技术生成

创建副本集

一主二从

创建数据库目录

mkdir -p /data/mongodb/rs0-0 /data/mongodb/rs0-1 /data/mongodb/rs0-2

创建日志文件

touch /data/mongodb/rs0-0.log /data/mongodb/rs0-1.log /data/mongodb/rs0-2.log

创建3个mongod实例(测试站)

mongod --bind_ip 0.0.0.0 --port 23317 --dbpath /data/mongodb/rs0-0 -fork --logpath /data/mongodb/rs0-0.log --replSet rs0 --oplogSize 128

mongod --bind_ip 0.0.0.0 --port 23318 --dbpath /data/mongodb/rs0-1 -fork --logpath /data/mongodb/rs0-1.log --replSet rs0 --oplogSize 128

mongod --bind_ip 0.0.0.0 --port 23319 --dbpath /data/mongodb/rs0-2 -fork --logpath /data/mongodb/rs0-2.log --replSet rs0 --oplogSize 128

创建3个mongodb配置文件(正式站)

vim /data/mongodb/rs0-0.conf

vim /data/mongodb/rs0-1.conf

vim /data/mongodb/rs0-2.conf

# mongod.conf

# for documentation of all options, see:

# http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.

systemLog:

destination: file

logAppend: true

path: /data/mongodb/rs0-0.log

# Where and how to store data.

storage:

dbPath: /data/mongodb/rs0-0

journal:

enabled: true

# engine:

# wiredTiger:

# how the process runs

processManagement:

fork: true # fork and run in background

pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile

timeZoneInfo: /usr/share/zoneinfo

# network interfaces

net:

port: 23317

bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

security:

keyFile: /data/mongodb/config/rs0/keyfile

authorization: enabled

#operationProfiling:

replication:

replSetName: rs0

启动mongodb

mongod -f /data/mongodb/rs0.conf

连接数据库

mongo --port 23317

初始化配置编写

rsconf = {

"_id" : "rs0",

"members" : [

{

"_id" : 0,

"host" : "127.0.0.1:23317"

},

{

"_id" : 1,

"host" : "127.0.0.1:23318"

},

{

"_id" : 2,

"host" : "127.0.0.1:23319"

}

]

}

初始化副本集

rs.initiate(rsconf)

查看当前配置

rs.conf()

查看副本集状态

rs.status()

查看所有mongod的进程命令

ps -ef | grep mongod

创建管理员

进入主副本集

mongo --port 23317

切换为admin数据库

use admin

创建管理员账号

db.createUser(

... {

... user:"root",

... pwd:"123123",

... roles:[{role:"root",db:"admin"}]

... })

db.auth("root","123123")

踩坑

报错Unrecognized option: replication

解决方法:

replication: 冒号前加一个空格

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值