準備目錄
rm -rf /compile/cluster-restore/cfg*
mkdir -p /compile/cluster-restore/cfg1/data/db
mkdir -p /compile/cluster-restore/cfg1/log
mkdir -p /compile/cluster-restore/cfg2/data/db
mkdir -p /compile/cluster-restore/cfg2/log
mkdir -p /compile/cluster-restore/cfg3/data/db
mkdir -p /compile/cluster-restore/cfg3/log
操作步驟
步驟 1 準(zhun)備單節點(dian)配(pei)置文件(jian)和數(shu)據目錄,以(yi)單節點(dian)方式(shi)啟動進(jin)程。
- 配置文件如下(restoreconfig/single_40303.yaml)。
net:
bindIp: 127.0.0.1
port: 40303
unixDomainSocket: {enabled: false}
processManagement: {fork: true, pidFilePath: /compile/cluster-restore/cfg1/configsvr.pid}
storage:
dbPath: /compile/cluster-restore/cfg1/data/db/
directoryPerDB: true
engine: wiredTiger
wiredTiger:
collectionConfig: {blockCompressor: snappy}
engineConfig: {directoryForIndexes: true, journalCompressor: snappy}
indexConfig: {prefixCompression: true}
systemLog: {destination: file, logAppend: true, logRotate: reopen, path: /compile/cluster-restore/cfg1/log/configsingle.log}
準備數據,將解壓后的configsvr文件拷貝到單節點dbPath目錄下。
cp -aR
/compile/download/backups/cac1efc8e65e42ecad8953352321bfeein02_41c8a32fb10245899708dea453a8c5c9no02/* /compile/cluster-restore/cfg1/data/db/
啟動進程。
./mongod -f restoreconfig/single_40303.yaml
步驟 2 連接單(dan)節點(dian),執行配置命令(ling)。
連接命令:./mongo --host 127.0.0.1 --port 40303
執行如下命令,修改副本集配置信息。
var cf=db.getSiblingDB('local').system.replset.findOne();
cf['members'][0]['host']='127.0.0.1:40303';
cf['members'][1]['host']='127.0.0.1:40304';
cf['members'][2]['host']='127.0.0.1:40305';
cf['members'][0]['hidden']=false;
cf['members'][1]['hidden']=false;
cf['members'][2]['hidden']=false;
cf['members'][0]['priority']=1;
cf['members'][1]['priority']=1;
cf['members'][2]['priority']=1;
db.getSiblingDB('local').system.replset.remove({});
db.getSiblingDB('local').system.replset.insert(cf)
執行如下命令,清理內置賬號。
db.getSiblingDB('admin').dropAllUsers();
db.getSiblingDB('admin').dropAllRoles();
執行如下命令,更新mongos和shard信息。
db.getSiblingDB('config').mongos.remove({});
先(xian)查(cha)詢(xun)config.shards表中的(de)(de)多個shard的(de)(de)_id信息,用(yong)于下面語句的(de)(de)_id的(de)(de)查(cha)詢(xun)條件。逐個更(geng)新每(mei)條記錄(lu)。
db.getSiblingDB('config').shards.update({'_id' : 'shard_1'},{$set: {'host': 'shard_1/127.0.0.1:40306,127.0.0.1:40307,127.0.0.1:40308'}})
db.getSiblingDB('config').shards.update({'_id' : 'shard_2'},{$set: {'host': 'shard_2/127.0.0.1:40309,127.0.0.1:40310,127.0.0.1:40311'}})
db.getSiblingDB('config').mongos.find({});
db.getSiblingDB('config').shards.find({});
執行如下命令,關閉單節點進程。
db.getSiblingDB('admin').shutdownServer();
步驟 3 搭建configsvr副本集。
準備副本集配置文件和目錄,將configsvr1節點的dbPath文件拷貝到其他兩個節點目錄下。
cp -aR /compile/cluster-restore/cfg1/data/db/ /compile/cluster-restore/cfg2/data/db/
cp -aR /compile/cluster-restore/cfg1/data/db/ /compile/cluster-restore/cfg3/data/db/
修改configsvr-1節點配置文件,增加副本集配置屬性(restoreconfig/configsvr_40303.yaml)。
net:
bindIp: 127.0.0.1
port: 40303
unixDomainSocket: {enabled: false}
processManagement: {fork: true, pidFilePath: /compile/cluster-restore/cfg1/configsvr.pid}
replication: {replSetName: config}
sharding: {archiveMovedChunks: false, clusterRole: configsvr}
storage:
dbPath: /compile/cluster-restore/cfg1/data/db/
directoryPerDB: true
engine: wiredTiger
wiredTiger:
collectionConfig: {blockCompressor: snappy}
engineConfig: {directoryForIndexes: true, journalCompressor: snappy}
indexConfig: {prefixCompression: true}
systemLog: {destination: file, logAppend: true, logRotate: reopen, path: /compile/cluster-restore/cfg1/log/configsvr.log}
啟動進程。
./mongod -f restoreconfig/configsvr_40303.yaml
修改configsvr-2節點配置文件,增加副本集配置屬性(restoreconfig/configsvr_40304.yaml)。
net:
bindIp: 127.0.0.1
port: 40304
unixDomainSocket: {enabled: false}
processManagement: {fork: true, pidFilePath: /compile/cluster-restore/cfg2/configsvr.pid}
replication: {replSetName: config}
sharding: {archiveMovedChunks: false, clusterRole: configsvr}
storage:
dbPath: /compile/cluster-restore/cfg2/data/db/
directoryPerDB: true
engine: wiredTiger
wiredTiger:
collectionConfig: {blockCompressor: snappy}
engineConfig: {directoryForIndexes: true, journalCompressor: snappy}
indexConfig: {prefixCompression: true}
systemLog: {destination: file, logAppend: true, logRotate: reopen, path: /compile/cluster-restore/cfg2/log/configsvr.log}
啟動進程
./mongod -f restoreconfig/configsvr_40304.yaml
修改configsvr-3節點配置文件,增加副本集配置屬性(restoreconfig/configsvr_40305.yaml)。
net:
bindIp: 127.0.0.1
port: 40304
unixDomainSocket: {enabled: false}
processManagement: {fork: true, pidFilePath: /compile/cluster-restore/cfg2/configsvr.pid}
replication: {replSetName: config}
sharding: {archiveMovedChunks: false, clusterRole: configsvr}
storage:
dbPath: /compile/cluster-restore/cfg2/data/db/
directoryPerDB: true
engine: wiredTiger
wiredTiger:
collectionConfig: {blockCompressor: snappy}
engineConfig: {directoryForIndexes: true, journalCompressor: snappy}
indexConfig: {prefixCompression: true}
systemLog: {destination: file, logAppend: true, logRotate: reopen, path: /compile/cluster-restore/cfg2/log/configsvr.log}
啟動進程。
./mongod -f restoreconfig/configsvr_40305.yaml
步(bu)驟 4 等(deng)待選主(zhu)成功。
./mongo --host 127.0.0.1 --port 40303
執行命令(ling)rs.status(),查看是否已存在(zai)主節點Primary。