单机转副本集

10.2.210.2

mongo --host 10.2.210.2  --port 27017
use admin;
db.auth('root','root.COM2020');


mkdir -p /data/mongo_rep2
mkdir -p /data/mongo_rep3

touch /etc/mongod_rep2.cnf
touch /etc/mongod_rep3.cnf

chown -R mongod:mongod /data/mongo_rep2
chown -R mongod:mongod /data/mongo_rep3



配置文件 /etc/mongo/mongo.cnf 新增配置
security:
    authorization: enabled
    keyFile: /etc/mongod_rep1.key
    
replication:
  replSetName: dev_zt

 
vi   /etc/mongod.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: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  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: 27017
  bindIp: 10.2.210.2  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


#security:
security:
    authorization: enabled
#operationProfiling:

#replication:
replication:
  replSetName: dev_zt

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
                                 


复制配置文件给rep2:
cat > /etc/mongod_rep2.cnf  << EOF
# 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/mongo_rep2/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongo_rep2
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongo_rep2/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

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


#security:
security:
    authorization: enabled
    keyFile: /etc/mongod_rep1.key
#operationProfiling:

#replication:
replication:
  replSetName: dev_zt

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
EOF


复制配置文件给rep3:
cat > /etc/mongod_rep3.cnf  << EOF
# 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/mongo_rep3/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongo_rep3
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongo_rep3/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

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


#security:
security:
    authorization: enabled
    keyFile: /etc/mongod_rep1.key
    
#operationProfiling:

#replication:
replication:
  replSetName: dev_zt
  
#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
EOF


先把 /etc/mongod_rep2.cnf 和 /etc/mongod_rep3.cnf 的 security 和 replication 参数注释
#security:
#    authorization: enabled
#    keyFile: /etc/mongod_rep1.key


#replication:
#  replSetName: dev_zt
  
启动mongo
/usr/bin/mongod --config  /etc/mongod_rep2.cnf
/usr/bin/mongod --config  /etc/mongod_rep3.cnf


# 登录 mongo
mongo --host 10.2.210.2 --port 27018 admin
mongo --host 10.2.210.2 --port 27019 admin


添加超级管理员账号

> use admin;

> db.createUser({user:"root",pwd:"root.COM2020",roles:[{role:"root",db:"admin"}]})


把 /etc/mongod_rep2.cnf 和 /etc/mongod_rep3.cnf 的 security 和 replication 参数加上
security:
    authorization: enabled
    keyFile: /etc/mongod_rep1.key


replication:
  replSetName: dev_zt
  

准备: security.keyFile

cat > /etc/mongod_rep1.key   << EOF
5heBYl8onlA1pbkH5AWjgGmFDvB4QagyOc5R9q18cU6vuCm14L/31JlmqsdRY95I
Es8ZXRuVsJuae51RHR+Y3lsc4udWk6zVIOtTDgcREm5lHzDyJGZERFqdjLQMAGzd
T6gXNg==
EOF

cat > /etc/mongod_rep2.key   << EOF
5heBYl8onlA1pbkH5AWjgGmFDvB4QagyOc5R9q18cU6vuCm14L/31JlmqsdRY95I
Es8ZXRuVsJuae51RHR+Y3lsc4udWk6zVIOtTDgcREm5lHzDyJGZERFqdjLQMAGzd
T6gXNg==
EOF

cat > /etc/mongod_rep3.key   << EOF
5heBYl8onlA1pbkH5AWjgGmFDvB4QagyOc5R9q18cU6vuCm14L/31JlmqsdRY95I
Es8ZXRuVsJuae51RHR+Y3lsc4udWk6zVIOtTDgcREm5lHzDyJGZERFqdjLQMAGzd
T6gXNg==
EOF
 
 
chmod 600 /etc/mongod_rep1.key
chmod 600 /etc/mongod_rep2.key
chmod 600 /etc/mongod_rep3.key


关闭mongodb
/usr/bin/mongod --config /etc/mongod_rep2.cnf --shutdown 
/usr/bin/mongod --config /etc/mongod_rep3.cnf --shutdown 


启动mongo
/usr/bin/mongod --config  /etc/mongod_rep2.cnf
/usr/bin/mongod --config  /etc/mongod_rep3.cnf


关闭主 mongodb
/usr/bin/mongod --config /etc/mongod.conf --shutdown 
 
启动主 mongodb
/usr/bin/mongod --config /etc/mongod.conf 
 

# 登录 mongo
mongo --host 10.2.210.2  --port 27017
use admin;
db.auth('root','root.COM2020');
 

# 初始化复制集
config = {"_id":"dev_zt",
          "members":[
          {"_id":0,host:"10.2.210.2:27017"},
          {"_id":1,host:"10.2.210.2:27018"},
          {"_id":2,host:"10.2.210.2:27019"}
          ]
}

rs.initiate(config)


# 查看复制集状态
rs.status()

 

posted @ 2025-06-17 00:49  屠魔的少年  阅读(6)  评论(0)    收藏  举报