cavacn / Growth-notes

3 stars 0 forks source link

Mongodb 集群搭建(CentOS) #1

Open cavacn opened 8 years ago

cavacn commented 8 years ago

安装mongodb

更新系统

yum update

添加源

vim /etc/yum.repos.d/mongodb-org-2.6.repo

粘贴复制

[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=0
enabled=1

安装mongodb

yum install -y mongodb-org

cavacn commented 8 years ago

按照以上方式,搭建另外两台服务器

这样,我们就有三台服务器

es0

es1

es2

cavacn commented 8 years ago

复制service~

cp /etc/init.d/mongod /etc/init.d/mongod-config
cp /etc/init.d/mongod /etc/init.d/mongod-db1
cp /etc/init.d/mongod /etc/init.d/mongod-db2
cp /etc/init.d/mongod /etc/init.d/mongod-db3

然后修改这些文件

vim /etc/init.d/mongod-db*

找到CONFIGFILE=/etc/mongod.conf依次修改CONFIGFILE=/etc/mongod-**.conf

配置示例

mongod-db*.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-db1.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo-db1
  directoryPerDB: true
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod-db1.pid  # location of pidfile

# network interfaces
net:
  port: 10001
#  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.

#security:

#operationProfiling:

#replication:

#sharding:
sharding:
  clusterRole: shardsvr
replication:
  replSetName: shard1

## Enterprise-Only Options

#auditLog:

mongod-config.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-config.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo-config
  directoryPerDB: true
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod-config.pid  # location of pidfile

# network interfaces
net:
  port: 20000
#  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.

#security:

#operationProfiling:

#replication:

#sharding:
sharding:
  clusterRole: configsvr
#replication:
#  replSetName: shard1

## Enterprise-Only Options

#auditLog:
cavacn commented 8 years ago

启动服务三台服务器都要做

service mongod-db1 start

service mongod-db2 start

service mongod-db3 start

service mongod-config start

cavacn commented 8 years ago

启动mongos

nohup mongos --port 3000 --configdb host1.port,host2.port,host3.port &

cavacn commented 8 years ago

连接mongos

mongo host:3000

mongos>use admin

mongos>db.runCommand({"addShard":"shardId/host:port"})

mongos>db.runCommand({"addShard":"shardId/host:port"})

mongos>db.runCommand({"addShard":"shardId/host:port"})

mongos>db.runCommand({listshards:1}) #查看状态

开启分片功能

mongos>db.runCommand({"enablesharding":"库名"})

{ "ok" : 1 }

mongos> db.runCommand({"shardcollection":"库名.表名","key":{_id:'hashed'}})

{ "collectionsharded" : "库名.表名", "ok" : 1 }