21.40 mongodb分片測試

21.40 mongodb分片測試

分片搭建–測試mongodb

  • 登陸任何一臺20000端口
    mongo --port 20000
    use admin
    db.runCommand({ enablesharding : "testdb"}) 或者
    sh.enableSharding("testdb") //指定要分片的數據庫
    db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } ) 或者
    sh.shardCollection("testdb.table1",{"id":1} ) //#指定數據庫裏須要分片的集合和片鍵
    use testdb
    for (var i = 1; i <= 10000; i++) db.table1.save({id:i,"test1":"testval1"})//插入測試數據
    db.table1.stats()//查看table1狀態
[root@Dasoncheng ~]# mongo --port 20000
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:20000/
MongoDB server version: 3.4.9
mongos> use admin
switched to db admin
mongos> sh.enableSharding("testdb")     
##指定要分片的數據庫testdb,沒有回自動建立;
{ "ok" : 1 }
mongos> sh.shardCollection("testdb.table1",{"id":1} )    
##指定數據庫裏須要分片的集合和片鍵
{ "collectionsharded" : "testdb.table1", "ok" : 1 }
mongos> use  testdb
switched to db testdb
##下面插入一條數據;
mongos> for (var i = 1; i <= 10000; i++) db.table1.save({id:i,"test1":"testval1"})

WriteResult({ "nInserted" : 1 })
mongos> show dbs
admin   0.000GB
config  0.001GB
testdb  0.000GB
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
	"_id" : 1,
	"minCompatibleVersion" : 5,
	"currentVersion" : 6,
	"clusterId" : ObjectId("59e957b4b1833892fcc37ec7")
}
  shards:
	{  "_id" : "shard1",  "host" : "shard1/192.168.60.11:27001,192.168.60.12:27001",  "state" : 1 }
	{  "_id" : "shard2",  "host" : "shard2/192.168.60.12:27002,192.168.60.13:27002",  "state" : 1 }
	{  "_id" : "shard3",  "host" : "shard3/192.168.60.11:27003,192.168.60.13:27003",  "state" : 1 }
  active mongoses:
	"3.4.9" : 1
 autosplit:
	Currently enabled: yes
  balancer:
	Currently enabled:  yes
	Currently running:  no
		Balancer lock taken at Fri Oct 20 2017 14:44:12 GMT+0800 (CST) by ConfigServer:Balancer
	Failed balancer rounds in last 5 attempts:  0
	Migration Results for the last 24 hours: 
		No recent migrations
  databases:
	{  "_id" : "testdb",  "primary" : "shard1",  "partitioned" : true }
		testdb.table1
			shard key: { "id" : 1 }
			unique: false
			balancing: true
			chunks:
				shard1	1  ##看這裏,存在shard1裏面
			{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0) 
mongos> sh.enableSharding("db2")  ##再建立庫和表,查看存在哪一個shard裏面;
{ "ok" : 1 }
mongos> sh.shardCollection("db2.cl2",{"id":1} )
{ "collectionsharded" : "db2.cl2", "ok" : 1 }
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
	"_id" : 1,
	"minCompatibleVersion" : 5,
	"currentVersion" : 6,
	"clusterId" : ObjectId("59e957b4b1833892fcc37ec7")
}
  shards:
	{  "_id" : "shard1",  "host" : "shard1/192.168.60.11:27001,192.168.60.12:27001",  "state" : 1 }
	{  "_id" : "shard2",  "host" : "shard2/192.168.60.12:27002,192.168.60.13:27002",  "state" : 1 }
	{  "_id" : "shard3",  "host" : "shard3/192.168.60.11:27003,192.168.60.13:27003",  "state" : 1 }
  active mongoses:
	"3.4.9" : 1
 autosplit:
	Currently enabled: yes
  balancer:
	Currently enabled:  yes
	Currently running:  no
		Balancer lock taken at Fri Oct 20 2017 14:44:12 GMT+0800 (CST) by ConfigServer:Balancer
	Failed balancer rounds in last 5 attempts:  0
	Migration Results for the last 24 hours: 
		No recent migrations
  databases:
	{  "_id" : "testdb",  "primary" : "shard1",  "partitioned" : true }
		testdb.table1
			shard key: { "id" : 1 }
			unique: false
			balancing: true
			chunks:
				shard1	1
			{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0) 
	{  "_id" : "db2",  "primary" : "shard2",  "partitioned" : true }
		db2.cl2
			shard key: { "id" : 1 }
			unique: false
			balancing: true
			chunks:
				shard2	1    ##看這裏,存在shard2裏面
			{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 0) 
##分片分佈在各個shard裏面,測試成功;

21.41 mongodb備份恢復

  • 備份全部庫
    mongodump --host 127.0.0.1 --port 20000 -o /tmp/mongobak/alldatabase
  • 備份指定庫
    mongodump --host 127.0.0.1 --port 20000 -d mydb -o /tmp/mongobak
    它會在/tmp/目錄下面生成一個mydb的目錄
  • 指定備份集合
    mongodump --host 127.0.0.1 --port 20000 -d mydb -c c1 -o /tmp/mongobak/
    它依然會生成mydb目錄,再在這目錄下面生成兩個文件
  • 導出集合爲json文件
    mongoexport --host 127.0.0.1 --port 20000 -d mydb -c c1 -o /tmp/mydb2/1.json
##備份全部庫:
[root@Dasoncheng ~]# mongodump --host 127.0.0.1 --port 20000 -o /tmp/mongobak/alldatabase  
##備份全部庫;-o指定文件夾,若是沒有回自動建立
2017-10-20T17:00:15.154+0800	writing admin.system.version to 
2017-10-20T17:00:15.160+0800	done dumping admin.system.version (1 document)
2017-10-20T17:00:15.160+0800	writing testdb.table1 to 
2017-10-20T17:00:15.160+0800	writing config.lockpings to 
2017-10-20T17:00:15.161+0800	writing config.changelog to 
2017-10-20T17:00:15.161+0800	writing config.locks to 
2017-10-20T17:00:15.169+0800	done dumping config.lockpings (13 documents)
2017-10-20T17:00:15.169+0800	writing config.shards to 
2017-10-20T17:00:15.178+0800	done dumping config.shards (3 documents)
2017-10-20T17:00:15.178+0800	writing config.collections to 
2017-10-20T17:00:15.192+0800	done dumping config.collections (2 documents)
2017-10-20T17:00:15.192+0800	writing config.databases to 
2017-10-20T17:00:15.195+0800	done dumping config.changelog (7 documents)
2017-10-20T17:00:15.195+0800	writing config.chunks to 
2017-10-20T17:00:15.203+0800	done dumping config.locks (5 documents)
2017-10-20T17:00:15.203+0800	writing config.mongos to 
2017-10-20T17:00:15.203+0800	done dumping config.chunks (2 documents)
2017-10-20T17:00:15.203+0800	writing config.version to 
2017-10-20T17:00:15.206+0800	done dumping config.version (1 document)
2017-10-20T17:00:15.206+0800	writing config.tags to 
2017-10-20T17:00:15.209+0800	done dumping config.tags (0 documents)
2017-10-20T17:00:15.209+0800	writing config.migrations to 
2017-10-20T17:00:15.213+0800	done dumping config.migrations (0 documents)
2017-10-20T17:00:15.213+0800	writing db2.cl2 to 
2017-10-20T17:00:15.215+0800	done dumping config.databases (2 documents)
2017-10-20T17:00:15.226+0800	done dumping config.mongos (2 documents)
2017-10-20T17:00:15.227+0800	done dumping db2.cl2 (0 documents)
2017-10-20T17:00:15.260+0800	done dumping testdb.table1 (10000 documents)
[root@Dasoncheng ~]# ll /tmp/mongobak/alldatabase/
total 4
drwxr-xr-x 2 root root   69 Oct 20 17:00 admin
drwxr-xr-x 2 root root 4096 Oct 20 17:00 config
drwxr-xr-x 2 root root   47 Oct 20 17:00 db2
drwxr-xr-x 2 root root   53 Oct 20 17:00 testdb
[root@Dasoncheng ~]# ll /tmp/mongobak/alldatabase/testdb/
total 532
-rw-r--r-- 1 root root 540000 Oct 20 17:00 table1.bson  
##這個文件是主要數據.bson
-rw-r--r-- 1 root root    145 Oct 20 17:00 table1.metadata.json
##這個文件是建表信息.json

##備份指定庫
[root@Dasoncheng ~]# mongodump --host 127.0.0.1 --port 20000 -d testdb -o /tmp/mongobak  
##-d指定庫;-o指定備份文件夾 沒有則自動建立;
2017-10-20T16:56:15.484+0800	writing testdb.table1 to 
2017-10-20T16:56:15.633+0800	done dumping testdb.table1 (10000 documents)
[root@Dasoncheng ~]# ll /tmp/mongobak/testdb/  ##由於這個庫只有一個集合testdb,因此
total 532
-rw-r--r-- 1 root root 540000 Oct 20 16:56 table1.bson
-rw-r--r-- 1 root root    145 Oct 20 16:56 table1.metadata.json

##指定備份集合
[root@Dasoncheng ~]# mongodump --host 127.0.0.1 --port 20000 -d testdb -c table1 -o /tmp/mongobak2
2017-10-20T16:59:03.087+0800	writing testdb.table1 to 
2017-10-20T16:59:03.166+0800	done dumping testdb.table1 (10000 documents)
[root@Dasoncheng ~]# ll /tmp/mongobak2/testdb/
total 532
-rw-r--r-- 1 root root 540000 Oct 20 16:59 table1.bson
-rw-r--r-- 1 root root    145 Oct 20 16:59 table1.metadata.json

##導出集合爲json文件
[root@Dasoncheng ~]# mongoexport --host 127.0.0.1 --port 20000 -d testdb -c table1 -o /tmp/testdb/1.json
2017-10-20T17:02:09.210+0800	connected to: 127.0.0.1:20000
2017-10-20T17:02:09.593+0800	exported 10000 records
[root@Dasoncheng ~]# ll /tmp/testdb/1.json 
-rw-r--r-- 1 root root 748894 Oct 20 17:02 /tmp/testdb/1.json
[root@Dasoncheng ~]# tail /tmp/testdb/1.json 
{"_id":{"$oid":"59e9b0bb4bdfc034c89123b2"},"id":9991.0,"test1":"testval1"}
{"_id":{"$oid":"59e9b0bb4bdfc034c89123b3"},"id":9992.0,"test1":"testval1"}
{"_id":{"$oid":"59e9b0bb4bdfc034c89123b4"},"id":9993.0,"test1":"testval1"}
{"_id":{"$oid":"59e9b0bb4bdfc034c89123b5"},"id":9994.0,"test1":"testval1"}
{"_id":{"$oid":"59e9b0bb4bdfc034c89123b6"},"id":9995.0,"test1":"testval1"}
{"_id":{"$oid":"59e9b0bb4bdfc034c89123b7"},"id":9996.0,"test1":"testval1"}
{"_id":{"$oid":"59e9b0bb4bdfc034c89123b8"},"id":9997.0,"test1":"testval1"}
{"_id":{"$oid":"59e9b0bb4bdfc034c89123b9"},"id":9998.0,"test1":"testval1"}
{"_id":{"$oid":"59e9b0bb4bdfc034c89123ba"},"id":9999.0,"test1":"testval1"}
{"_id":{"$oid":"59e9b0bb4bdfc034c89123bb"},"id":10000.0,"test1":"testval1"}

MongoDB恢復:shell

  • 恢復全部庫
    mongorestore -h 127.0.0.1 --port 20000 --drop dir/ //其中dir是備份全部庫的目錄名字,其中--drop可選,意思是當恢復以前先把以前的數據刪除,不建議使用
  • 恢復指定庫
    mongorestore -d mydb dir/ //-d跟要恢復的庫名字,dir就是該庫備份時所在的目錄
  • 恢復集合
    mongorestore -d mydb -c testc dir/mydb/testc.bson // -c後面跟要恢復的集合名字,dir是備份mydb庫時生成文件所在路徑,這裏是一個bson文件的路徑
  • 導入集合
    mongoimport -d mydb -c testc --file /tmp/testc.json
##回覆全部庫;
[root@Dasoncheng ~]# ls /tmp/mongobak/alldatabase/   ##備份時指定的文件夾;
admin  config  db2  testdb
[root@Dasoncheng ~]# mongo --port 20000
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:20000/
MongoDB server version: 3.4.9
mongos> show dbs
admin   0.000GB
config  0.001GB
db2     0.000GB
testdb  0.000GB
mongos> use db2
switched to db db2
mongos> db.dropDatabase()
{ "dropped" : "db2", "ok" : 1 }
mongos> use testdb
switched to db testdb
mongos> db.dropDatabase()
{ "dropped" : "testdb", "ok" : 1 }
mongos> show dbs
admin   0.000GB
config  0.001GB
[root@Dasoncheng ~]# mongorestore -h 127.0.0.1 --port 20000 /tmp/mongobak/alldatabase  
##還原的時候使用備份的文件夾便可;
2017-10-20T17:50:44.598+0800	preparing collections to restore from
2017-10-20T17:50:44.599+0800	Failed: cannot do a full restore on a sharded system - remove the 'config' directory from the dump directory first  
##這裏報錯,config庫不能還原(config庫很重要 不能隨便還原),提示刪除 咱們接下來刪除;
[root@Dasoncheng ~]# rm -rf /tmp/mongobak/alldatabase/config/
[root@Dasoncheng ~]# mongorestore -h 127.0.0.1 --port 20000 /tmp/mongobak/alldatabase
2017-10-20T17:51:20.512+0800	preparing collections to restore from
2017-10-20T17:51:20.517+0800	reading metadata for testdb.table1 from /tmp/mongobak/alldatabase/testdb/table1.metadata.json
2017-10-20T17:51:20.522+0800	reading metadata for db2.cl2 from /tmp/mongobak/alldatabase/db2/cl2.metadata.json
2017-10-20T17:51:20.587+0800	restoring testdb.table1 from /tmp/mongobak/alldatabase/testdb/table1.bson
2017-10-20T17:51:20.604+0800	restoring db2.cl2 from /tmp/mongobak/alldatabase/db2/cl2.bson
2017-10-20T17:51:20.616+0800	restoring indexes for collection db2.cl2 from metadata
2017-10-20T17:51:20.644+0800	finished restoring db2.cl2 (0 documents)
2017-10-20T17:51:21.876+0800	restoring indexes for collection testdb.table1 from metadata
2017-10-20T17:51:22.105+0800	finished restoring testdb.table1 (10000 documents)
2017-10-20T17:51:22.105+0800	done
##還原成功,查看還原後的庫:
[root@Dasoncheng ~]# mongo --port 20000
mongos> show dbs
admin   0.000GB
config  0.001GB
db2     0.000GB
testdb  0.000GB
##老鐵,沒毛病;接下來恢復庫 以及集合

##恢復指定庫;
[root@Dasoncheng ~]# ls /tmp/mongobak/testdb/
table1.bson  table1.metadata.json
[root@Dasoncheng ~]# mongorestore -d testdb /tmp/mongobak/testdb/
##指定包含庫文件的目錄就行;
2017-10-20T17:57:21.077+0800	the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2017-10-20T17:57:21.078+0800	building a list of collections to restore from /tmp/mongobak/testdb dir
2017-10-20T17:57:21.081+0800	reading metadata for testdb.table1 from /tmp/mongobak/testdb/table1.metadata.json
2017-10-20T17:57:21.104+0800	restoring testdb.table1 from /tmp/mongobak/testdb/table1.bson
2017-10-20T17:57:22.389+0800	restoring indexes for collection testdb.table1 from metadata
2017-10-20T17:57:22.474+0800	finished restoring testdb.table1 (10000 documents)
2017-10-20T17:57:22.474+0800	done
##還原庫沒毛病!

##恢復集合;
[root@Dasoncheng ~]# mongorestore -d testdb -c table1 /tmp/mongobak/testdb/table1.bson
……
2017-10-20T18:00:05.663+0800	restoring indexes for collection testdb.table1 from metadata
2017-10-20T18:00:05.673+0800	finished restoring testdb.table1 (10000 documents)
2017-10-20T18:00:05.673+0800	done
相關文章
相關標籤/搜索