部署mongodb replset全流程


走了不少弯路,因此做个记录。

安装

推荐直接下载

注意:不同的系统下载链接不同,请到http://www.mongodb.org/downloads选择系统,以便获得不同系统的下载链接,替换下面的https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.0.2.tgz

cd ~/apps
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.0.2.tgz
# visit https://www.mongodb.org/downloads for more urls
tar xzf mongodb-linux-x86_64-ubuntu1404-3.0.2.tgz
mv mongodb-linux-x86_64-ubuntu1404-3.0.2 mongodb

#for db and log storage
#mkdir -p mongodb/data/db
#mkdir mongodb/log

加入路径, 编辑 ~/.bashrc ,加入

#mongodb path
export PATH=~/apps/mongodb/bin:$PATH

指定 配置文件

mkdir ~/confs
vim ~confs/mongodb.conf

加入rpm包里自带的默认配置文件,注意 ### 部分

# mongod.conf

# Where to store the data.

# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn't mutable by the mongodb user.
dbpath=/home/long/apps/mongodb/data/db

#where to log
logpath=/home/long/apps/mongodb/log/mongod.log

logappend=true

#port = 27017

### engine for 3.0+
storageEngine = wiredTiger

# Listen to local interface only. Comment out to listen on all interfaces. 
#### 注意务必绑定 对外ip,否则默认绑定127.0.0.1,收不到外来请求
bind_ip = 100.100.10.100,127.0.0.1

# Disables write-ahead journaling
# nojournal = true

# Enables periodic logging of CPU utilization and I/O wait
#cpu = true

# Turn on/off security.  Off is currently the default
#noauth = true
#auth = true

# Verbose logging output.
#verbose = true

# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true

# Enable db quota management
#quota = true

# Set oplogging level where n is
#   0=off (default)
#   1=W
#   2=R
#   3=both
#   7=W+some reads
#diaglog = 0

# Ignore query hints
#nohints = true

# Enable the HTTP interface (Defaults to port 28017).
#httpinterface = true

# Turns off server-side scripting.  This will result in greatly limited
# functionality
#noscripting = true

# Turns off table scans.  Any query that would do a table scan fails.
#notablescan = true

# Disable data file preallocation.
#noprealloc = true

# Specify .ns file size for new databases.
# nssize = {size}

# Replication Options

# in replicated mongo databases, specify the replica set name here
#replSet=setname

### replSet名字,所有mongodb实例都要一样
replSet=rs

# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#keyFile=/path/to/keyfile

重新打开shell, 运行 mongod 实例

mongod --config ~/confs/mongodb.conf

每一台都启动 mongod 实例,至少三台,一定要奇数台

登陆到其中一个进行配置

mongo 100.100.10.100:27017

初始化

rs.initiate({
  _id: 'rs'
  ,members: [
    { _id: 0, host: '100.100.10.100:27017' }
    ,{ _id: 1, host: '100.100.10.101:27017' }
    ,{ 
      _id: 2
      ,host: '100.100.10.102:27017'
      ,arbiterOnly: true
    }
  ]
})

查看状态

rs.status()

done.