shell mongodb sharding index initialize

常用脚本备忘:mongodb initialize sharding & index

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash

if [ $# < 4 ]; then
echo " Usage: ./shell.sh [mongos_ip] [mongos_port] [username] [password] [drop?]"
exit
fi;

databases=(
"player"
)

# enablesharding|collection|element
shardings=(
"player|player|id"
)

# database|collection|indexEle|indexType|unique
indexs=(
"player|player|id|1|true"
)

mongos_ip=$1
mongos_port=$2
mongos_username=$3
mongos_password=$4
mongos_drop=1
if [ $# == 5 ]; then
mongos_drop=$5
fi;

echo "initialize mongodb's database & collection"
echo
echo "mongos ip: ${mongos_ip}"
echo "mongos port: ${mongos_port}"
echo "mongos username: ${mongos_username}"
echo


for database in ${databases[@]}; do
if [ $mongos_drop == 1 ]; then
read -p "!!!! mongodb's database **** ${database} **** will drop !!!!(y/n) " drop_yn
if echo "$drop_yn" | grep -iq "^y" ; then
echo "####### drop #######"
mongo --host $mongos_ip --port $mongos_port --username $mongos_username --password $mongos_password --authenticationDatabase admin --quiet << EOF
use $database;
db.dropDatabase();
EOF
fi;
fi;
done

while true; do
read -p "!!!! confirm mongodb not initialize before !!!!(y/n) " confirm_yn
if echo "$confirm_yn" | grep -iq "^y" ; then
echo
for item in ${shardings[@]}; do
database=`echo "$item" | awk -F '|' '{print $1}'`
collection=`echo "$item" | awk -F '|' '{print $2}'`
element=`echo "$item" | awk -F '|' '{print $3}'`
# printf "show dbs" | mongo --host $mongos_ip --port $mongos_port --username $mongos_username --password $mongos_password --authenticationDatabase admin;
echo

echo "####### sharding #######"
mongo --host $mongos_ip --port $mongos_port --username $mongos_username --password $mongos_password --authenticationDatabase admin --quiet << EOF
use admin;
sh.enableSharding("$database");
use $database;
sh.shardCollection("$database.$collection", {"$element": "hashed"});
EOF

# printf "use admin\\nsh.enableSharding(\"$database\")\\nuse $database\\nsh.shardCollection(\"$database.$collection\", {\"$element\": \"hashed\"})" | mongo --host $mongos_ip --port $mongos_port --username $mongos_username --password $mongos_password --authenticationDatabase admin;
echo
done
echo "####### index #######"
for item in ${indexs[@]}; do
database=`echo "$item" | awk -F '|' '{print $1}'`
collection=`echo "$item" | awk -F '|' '{print $2}'`
element=`echo "$item" | awk -F '|' '{print $3}'`
index=`echo "$item" | awk -F '|' '{print $4}'`
uniq=`echo "$item" | awk -F '|' '{print $5}'`
# printf "show dbs" | mongo --host $mongos_ip --port $mongos_port --username $mongos_username --password $mongos_password --authenticationDatabase admin;
echo
mongo --host $mongos_ip --port $mongos_port --username $mongos_username --password $mongos_password --authenticationDatabase admin --quiet << EOF
use $database;
db.$collection.createIndex({$element: $index},{unique:$uniq, background: true});
EOF
# printf "use $database\\ndb.$collection.createIndex({$element: $index})" | mongo --host $mongos_ip --port $mongos_port --username $mongos_username --password $mongos_password --authenticationDatabase admin;
echo
done
echo
fi;
exit
done

------ 本文结束 ------
------ 版权声明:转载请注明出处 ------