4.2.2-MongoDB-安装

优质
小牛编辑
134浏览
2023-12-01

1.1 运行命令

# cd /home/wlj
# wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.12.tgz?_ga=1.28205556.912007513.1487138979
# tar -zxvf mongodb-linux-x86_64-rhel70-3.2.12.gz
# mv mongodb-linux-x86_64-rhel70-3.2.12 mongodb
# mkdir mongodb/conf
# vim mongodb/conf/mongo.config

1.2 配置文件

dbpath=/data/mongodb/data/db
logpath=/data/mongodb/logs/mongodb.log
port=27017
fork=true #后台执行
logappend=true
wiredTigerCacheSizeGB=10 #设置进程的堆内存

1.3 创建数据目录和日志目录

# mkdir -p /data/mongodb/data/db
# mkdir -p /data/mongodb/logs/

1.4 配置 MongoDB 家目录和 PATH 环境变量

# vim /etc/profile.d/mongodb.sh
export MONGODB_HOME=/home/jj/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
# source /etc/profile.d/mongodb.sh    
# mongod -f /home/wlj/mongodb/conf/mongo.config
# mongo

2. 复制集

2.1 安装 numactl

# yum install numactl

2.2 修改系统变量

# echo 1 > /proc/sys/vm/overcommit_memory  
# echo never >> /sys/kernel/mm/transparent_hugepage/enabled 
# echo never >> /sys/kernel/mm/transparent_hugepage/defrag 
# 设置最大文件数
# ulimit -u 32767

2.3 Master 配置文件

dbpath=/data/volume_b/mongodb/data/db
logpath=/data/volume_b/mongodb/logs/mongodb.log
pidfilepath=/data/volume_b/mongodb/logs/mongod.pid
port=27017
#后台启动
fork=true 
logappend=true
journal=true
# 复制集名称
replSet=fbj
wiredTigerCacheSizeGB=10 #设置进程的堆内存

2.4 Master 启动

numactl --interleave=all ./bin/mongod -config /home/wlj/conf/mongod.conf

2.5 复制集配置(两台)

dbpath=/data/volume_b/mongodb/data/db
logpath=/data/volume_b/mongodb/logs/mongodb.log
pidfilepath = /data/volume_b/mongodb/logs/mongod.pid
port=27017
fork=true
logappend=true
journal=true
# 复制集名称
replSet=fbj
wiredTigerCacheSizeGB=10 #设置进程的堆内存

2.6 复制集启动(两台)

# numactl --interleave=all ./bin/mongod -config ./conf/mongod.conf

2.7 登录 Master

./mongo
> use admin
switched to db admin
>config=({_id:"fbj",members:[{_id:0,host:"192.168.20.100:27017"},{_id:1,host:"192.168.20.103:27017"},{_id
:2,host:"192.168.20.104:27017"}]})
{
"_id" : "fbj",
    "members" : [
        {
             "_id" : 0,
             "host" : "192.168.20.102:27017"
        },
        {
              "_id" : 1,
               "host" : "192.168.20.103:27017"
        },
        {
                "_id" : 2,
                "host" : "192.168.20.104:27017"
        }
    ]
}
> rs.initiate(config)
{ "ok" : 1 }
fbj:PRIMARY>rs.status()

2.8 测试

2.8.1 创建连接

fbj:PRIMARY> con = new Mongo("localhost:27017")
connection to localhost:27017

2.8.2 创建数据库

fbj:PRIMARY> priDB = con.getDB("test")
test

2.8.3 判断当前节点是否 Master

fbj:PRIMARY> priDB.isMaster()
{
    "hosts" : [
        "192.168.20.102:27017",
        "192.168.20.103:27017",
        "192.168.20.104:27017"
    ],
    "setName" : "fbj",
    "setVersion" : 1,
    "ismaster" : true,
    "secondary" : false,
    "primary" : "192.168.20.102:27017",
    "me" : "192.168.20.102:27017",
    "electionId" : ObjectId("7fffffff0000000000000001"),
    "maxBsonObjectSize" : 16777216,
    "maxMessageSizeBytes" : 48000000,
    "maxWriteBatchSize" : 1000,
    "localTime" : ISODate("2017-04-11T02:31:12.112Z"),
    "maxWireVersion" : 4,
    "minWireVersion" : 0,
    "ok" : 1
}

2.8.4 插入数据

fbj:PRIMARY> for(i=0;i<1000;i++){priDB.coll.insert({count:i})}
WriteResult({ "nInserted" : 1 })

2.8.5 count 查询

fbj:PRIMARY> priDB.coll.count()
1000

2.8.6 切换到 test 数据库

fbj:PRIMARY> use admin
fbj:SECONDARY> rs.slaveOk()
fbj:PRIMARY> use test
switched to db test

2.8.7 查看表

fbj:PRIMARY> show tables
coll

2.8.8 查询数据

fbj:PRIMARY> db.coll.find()
{ "_id" : ObjectId("58ec402c14ae530934f46135"), "count" : 0 }
{ "_id" : ObjectId("58ec402c14ae530934f46136"), "count" : 1 }
{ "_id" : ObjectId("58ec402c14ae530934f46137"), "count" : 2 }
{ "_id" : ObjectId("58ec402c14ae530934f46138"), "count" : 3 }
{ "_id" : ObjectId("58ec402c14ae530934f46139"), "count" : 4 }
{ "_id" : ObjectId("58ec402c14ae530934f4613a"), "count" : 5 }
{ "_id" : ObjectId("58ec402c14ae530934f4613b"), "count" : 6 }
{ "_id" : ObjectId("58ec402c14ae530934f4613c"), "count" : 7 }
{ "_id" : ObjectId("58ec402c14ae530934f4613d"), "count" : 8 }
{ "_id" : ObjectId("58ec402c14ae530934f4613e"), "count" : 9 }
{ "_id" : ObjectId("58ec402c14ae530934f4613f"), "count" : 10 }
{ "_id" : ObjectId("58ec402c14ae530934f46140"), "count" : 11 }
{ "_id" : ObjectId("58ec402c14ae530934f46141"), "count" : 12 }
{ "_id" : ObjectId("58ec402c14ae530934f46142"), "count" : 13 }
{ "_id" : ObjectId("58ec402c14ae530934f46143"), "count" : 14 }
{ "_id" : ObjectId("58ec402c14ae530934f46144"), "count" : 15 }
{ "_id" : ObjectId("58ec402c14ae530934f46145"), "count" : 16 }
{ "_id" : ObjectId("58ec402c14ae530934f46146"), "count" : 17 }
{ "_id" : ObjectId("58ec402c14ae530934f46147"), "count" : 18 }
{ "_id" : ObjectId("58ec402c14ae530934f46148"), "count" : 19 }
Type "it" for more

2.8.9 登录到从节点

./mongo

2.8.10 查看表

fbj:SECONDARY> show dbs
2017-04-11T10:35:51.680+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" :
"not master and slaveOk=false", "code" : 13435 } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:761:19
shellHelper@src/mongo/shell/utils.js:651:15
@(shellhelp2):1:1
fbj:SECONDARY> db.getMongo().setSlaveOk();
(use adminrs.slaveOk())
fbj:SECONDARY> show dbs
local 0.000GB
test 0.000GB

2.8.11 切换到 test 数据库

fbj:SECONDARY> use test
switched to db test

2.8.12 查看表

fbj:SECONDARY> show tables
coll

2.8.13 查询数据

fbj:SECONDARY> db.coll.find()
{ "_id" : ObjectId("58ec402c14ae530934f46135"), "count" : 0 }
{ "_id" : ObjectId("58ec402c14ae530934f46136"), "count" : 1 }
{ "_id" : ObjectId("58ec402c14ae530934f46137"), "count" : 2 }
{ "_id" : ObjectId("58ec402c14ae530934f46138"), "count" : 3 }
{ "_id" : ObjectId("58ec402c14ae530934f4613a"), "count" : 5 }
{ "_id" : ObjectId("58ec402c14ae530934f46139"), "count" : 4 }
{ "_id" : ObjectId("58ec402c14ae530934f4613b"), "count" : 6 }
{ "_id" : ObjectId("58ec402c14ae530934f4613d"), "count" : 8 }
{ "_id" : ObjectId("58ec402c14ae530934f4613c"), "count" : 7 }
{ "_id" : ObjectId("58ec402c14ae530934f4613e"), "count" : 9 }
{ "_id" : ObjectId("58ec402c14ae530934f4613f"), "count" : 10 }
{ "_id" : ObjectId("58ec402c14ae530934f46141"), "count" : 12 }
{ "_id" : ObjectId("58ec402c14ae530934f46140"), "count" : 11 }
{ "_id" : ObjectId("58ec402c14ae530934f46144"), "count" : 15 }
{ "_id" : ObjectId("58ec402c14ae530934f46143"), "count" : 14 }
{ "_id" : ObjectId("58ec402c14ae530934f46145"), "count" : 16 }
{ "_id" : ObjectId("58ec402c14ae530934f46142"), "count" : 13 }
{ "_id" : ObjectId("58ec402c14ae530934f46146"), "count" : 17 }
{ "_id" : ObjectId("58ec402c14ae530934f46147"), "count" : 18 }
{ "_id" : ObjectId("58ec402c14ae530934f46148"), "count" : 19 }
Type "it" for more

2.8.14 切换到 ycsb 数据库

> use ycsb

2.8.15 创建索引

> db.usertable.ensureIndex({field0:1})
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}

3. 分片集

进程IP:PORT
mongod1192.168.1.231:27017
mongod2192.168.1.231:27018
mongod3192.168.1.231:27019
mongod1192.168.1.232:27017
mongod2192.168.1.232:27018
mongod3192.168.1.232:27019
mongo2192.168.1.231:27037
192.168.1.232:27037

3.1 配置文件

# vim mongod1.conf
dbpath=/data/volume_b/mongodb/data/db1
logpath=/data/volume_b/mongodb/logs/mongodb1.log
pidfilepath = /data/volume_b/mongodb/logs/mongod1.pid
port=27017
fork=true
logappend=true
journal=true
quiet=true 
replSet=fbj1
oplogSize=102400
wiredTigerCacheSizeGB=10 #设置进程的堆内存
# vim mongod2.conf
dbpath=/data/volume_b/mongodb/data/db2
logpath=/data/volume_b/mongodb/logs/mongodb2.log
pidfilepath = /data/volume_b/mongodb/logs/mongod2.pid
port=27018
fork=true
logappend=true
journal=true
quiet=true 
replSet=fbj2
oplogSize=102400
wiredTigerCacheSizeGB=10 #设置进程的堆内存
# vim mongod3.conf
dbpath=/data/volume_b/mongodb/data/db3
logpath=/data/volume_b/mongodb/logs/mongodb3.log
pidfilepath = /data/volume_b/mongodb/logs/mongod3.pid
port=27019
fork=true
logappend=true
journal=true
quiet=true 
replSet=fbj3
oplogSize=102400
wiredTigerCacheSizeGB=10 #设置进程的堆内存
# vim config.conf
dbpath=/data/volume_b/mongodb/data/config
logpath=/data/volume_b/mongodb/logs/config.log
pidfilepath = /data/volume_b/mongodb/logs/config.pid
port=27027
fork=true
logappend=true
journal=true
quiet=true
replSet=configs
# vim mongos.conf
mongos.conf
logpath=/data/volume_b/mongodb/logs/mongos.log
pidfilepath = /data/volume_b/mongodb/logs/mongos.pid
port=27037
fork=true
configdb=configs/192.168.20.100:27027,192.168.20.103:27027,192.168.20.104:27027

3.2 启动节点

3.2.1 启动 Master 节点

numactl --interleave=all mongod -config /home/es/soft/mongodb/config/mongod1.conf --setParameter
replWriterThreadCount=32
numactl --interleave=all mongod -config /home/es/soft/mongodb/config/mongod2.conf --setParameter
replWriterThreadCount=32
numactl --interleave=all mongod -config /home/es/soft/mongodb/config/mongod3.conf --setParameter
replWriterThreadCount=32

3.2.2 启动副本节点

同上

3.3 配置 mongod 复制集

./mongo 192.168.20.100:27017
> use admin
switched to db admin
>config=({_id:"fbj1",members:[{_id:0,host:"192.168.20.100:27017"},{_id:1,host:"192.168.20.103:27017"},{_i
d:2,host:"192.168.20.104:27017"}]})
{
"_id" : "fbj1",
"members" : [
{
"_id" : 0,
"host" : "192.168.20.100:27017"
},
{
"_id" : 1,
"host" : "192.168.20.103:27017"
},
{
"_id" : 2,
"host" : "192.168.20.104:27017"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
./mongo 192.168.20.103:27018
> use admin
switched to db admin
>config=({_id:"fbj2",members:[{_id:0,host:"192.168.20.100:27018"},{_id:1,host:"192.168.20.103:27018"},{_i
d:2,host:"192.168.20.104:27018"}]})
{
"_id" : "fbj2",
"members" : [
{
"_id" : 0,
"host" : "192.168.20.100:27018"
},
{
"_id" : 1,
"host" : "192.168.20.103:27018"
},
{
"_id" : 2,
"host" : "192.168.20.104:27018"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
mongo 192.168.20.104:27019
> use admin
switched to db admin
>config=({_id:"fbj3",members:[{_id:0,host:"192.168.20.100:27019"},{_id:1,host:"192.168.20.103:27019"},{_i
d:2,host:"192.168.20.104:27019"}]})
{
"_id" : "fbj3",
"members" : [
{
"_id" : 0,
"host" : "192.168.20.100:27019"
},
{
"_id" : 1,
"host" : "192.168.20.103:27019"
},
{
"_id" : 2,
"host" : "192.168.20.104:27019"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }

3.4 启动 configdb

numactl --interleave=all ./bin/mongod --configsvr -config ./conf/config.conf

3.5 登录 configdb

# mongo 192.168.20.104:27027

3.6 配置副本集

>config=({_id:"configs",members:[{_id:0,host:"192.168.20.100:27027"},{_id:1,host:"192.168.20.103:27027"},{_i
d:2,host:"192.168.20.104:27027"}]})
> rs.initiate(config)

3.5 启动 mongos

numactl --interleave=all ./bin/mongos -config ./conf/mongos.conf

3.6 登录 mongos

./bin/mongo 192.168.20.100:27037
mongos> use admin
switched to db admin

3.7 添加分片

mongos> db.runCommand( { addshard :
"fbj1/192.168.20.100:27017,192.168.20.103:27017,192.168.20.104:27017"});
{ "shardAdded" : "fbj1", "ok" : 1 }
mongos> db.runCommand( { addshard :
"fbj2/192.168.20.100:27018,192.168.20.103:27018,192.168.20.104:27018"});
{ "shardAdded" : "fbj2", "ok" : 1 }
mongos> db.runCommand( { addshard :
"fbj3/192.168.20.100:27019,192.168.20.103:27019,192.168.20.104:27019"});
{ "shardAdded" : "fbj3", "ok" : 1 }
mongos> db.runCommand( { listshards : 1 } );
{
"shards" : [
{
"_id" : "fbj1",
"host" : "fbj1/192.168.20.100:27017,192.168.20.103:27017,192.168.20.104:27017"
},
{
"_id" : "fbj2",
"host" : "fbj2/192.168.20.100:27018,192.168.20.103:27018,192.168.20.104:27018"
},
{
"_id" : "fbj3",
"host" : "fbj3/192.168.20.100:27019,192.168.20.103:27019,192.168.20.104:27019"
}
],
"ok" : 1
}

3.8 登录其他 mongos 节点

./bin/mongo 192.168.20.100:27037
mongos> use admin
switched to db admin
mongos> db.runCommand( { enablesharding :"testdb"});
{ "ok" : 1 }
mongos> db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
{ "collectionsharded" : "testdb.table1", "ok" : 1 }
mongos> db.printShardingStatus() //
--- Sharding Status ---sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("58f42ee684fe49830067fdad")
}
shards:
{ "_id" : "fbj1", "host" :
"fbj1/192.168.20.100:27017,192.168.20.103:27017,192.168.20.104:27017" }
{ "_id" : "fbj2", "host" :
"fbj2/192.168.20.100:27018,192.168.20.103:27018,192.168.20.104:27018" }
{ "_id" : "fbj3", "host" :
"fbj3/192.168.20.100:27019,192.168.20.103:27019,192.168.20.104:27019" }
active mongoses:
"3.2.12" : 3
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "testdb", "primary" : "fbj1", "partitioned" : true }
testdb.table1
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
fbj1 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : fbj1
Timestamp(1, 0)

3.9 测试

mongos> for (var i = 1; i <= 10000; i++)db.table1.save({id:i,"test1":"testval1"});
WriteResult({ "nInserted" : 1 })
mongos> db.table1.stats();
mongos> db.runCommand( { enablesharding :"ycsb"});
{ "ok" : 1 }
mongos> db.runCommand( { shardcollection : "ycsb.usertable",key : {_id: "hashed"} } )
{ "collectionsharded" : "ycsb.usertable", "ok" : 1 }
mongos> db.printShardingStatus()
--- Sharding Status ---sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("58f42ee684fe49830067fdad")
}
shards:
{ "_id" : "fbj1", "host" :
"fbj1/192.168.20.100:27017,192.168.20.103:27017,192.168.20.104:27017" }
{ "_id" : "fbj2", "host" :
"fbj2/192.168.20.100:27018,192.168.20.103:27018,192.168.20.104:27018" }
{ "_id" : "fbj3", "host" :
"fbj3/192.168.20.100:27019,192.168.20.103:27019,192.168.20.104:27019" }
active mongoses:
"3.2.12" : 3
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 1
Last reported error: Couldn't get a connection within the time limit
Time of Reported error: Tue Apr 18 2017 10:15:11 GMT+0800 (CST)
Migration Results for the last 24 hours:
106 : Success
7 : Failed with error 'data transfer error', from fbj3 to fbj2
2 : Failed with error 'data transfer error', from fbj3 to fbj1
9 : Failed with error 'chunk too big to move', from fbj3 to fbj1
10 : Failed with error 'chunk too big to move', from fbj3 to fbj2
89 : Failed with error 'aborted', from fbj3 to fbj2
7904 : Failed with error 'aborted', from fbj3 to fbj1
databases:
{ "_id" : "testdb", "primary" : "fbj1", "partitioned" : true }
testdb.table1
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
fbj1 1
fbj2 1
fbj3 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : 2 } on : fbj2 Timestamp(2, 0)
{ "id" : 2 } -->> { "id" : 20 } on : fbj3 Timestamp(3, 0)
{ "id" : 20 } -->> { "id" : { "$maxKey" : 1 } } on : fbj1 Timestamp(3, 1)
{ "_id" : "ycsb", "primary" : "fbj3", "partitioned" : true }
ycsb.usertable
shard key: { "_id" : "hashed" }
unique: false
balancing: true
chunks:
fbj1 2
fbj2 2
fbj3 2
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong("-6148914691236517204")
} on : fbj1 Timestamp(3, 2)
{ "_id" : NumberLong("-6148914691236517204") } -->> { "_id" :
NumberLong("-3074457345618258602") } on : fbj1 Timestamp(3, 3)
{ "_id" : NumberLong("-3074457345618258602") } -->> { "_id" : NumberLong(0) } on
: fbj2 Timestamp(3, 4)
{ "_id" : NumberLong(0) } -->> { "_id" : NumberLong("3074457345618258602") } on
: fbj2 Timestamp(3, 5)
{ "_id" : NumberLong("3074457345618258602") } -->> { "_id" :
NumberLong("6148914691236517204") } on : fbj3 Timestamp(3, 6)
{ "_id" : NumberLong("6148914691236517204") } -->> { "_id" : { "$maxKey" : 1 } }
on : fbj3 Timestamp(3, 7)