1、下载mg
https://www.mongodb.com/download-center/community
RHEL 7.0 Linux 64-bit x64 4.2 下载下来
2、准备机器、挂硬盘,格式化,网上有教程优化
#https://bbs.aqzt.com/thread-153-1-1.html 这里有服务的优化,自己改改运行就ok了
3、准备3台机器
237,238,239
机器1 | 机器2 | 机器3 | |
mongos | 172.30.1.237:62222 | 172.30.1.238:62222 | 172.30.1.239:62222 |
config | 172.30.1.237:21000 | 172.30.1.238:21000 | 172.30.1.237:21000 |
shard01 | 172.30.1.237:27001(仲裁) | 172.30.1.238:27001 | 172.30.1.239:27001 |
shard02 | 172.30.1.237:27002 | 172.30.1.238:27002(仲裁) | 172.30.1.239:27002 |
shard03 | 172.30.1.238:27003 | 172.30.1.238:27003 | 172.30.1.238:27004(仲裁) |
4、准备配置文件和路径
mkdir /opt/mongodb4.2/mongos mkdir /opt/mongodb4.2/mongos/log mkdir /opt/mongodb4.2/mongos/wtdb mkdir /opt/mongodb4.2/shard1 mkdir /opt/mongodb4.2/shard1/log mkdir /opt/mongodb4.2/shard1/wtdb mkdir /opt/mongodb4.2/shard2 mkdir /opt/mongodb4.2/shard2/log mkdir /opt/mongodb4.2/shard2/wtdb mkdir /opt/mongodb4.2/shard3 mkdir /opt/mongodb4.2/shard3/log mkdir /opt/mongodb4.2/shard3/wtdb mkdir /opt/mongodb4.2/config mkdir /opt/mongodb4.2/config/log mkdir /opt/mongodb4.2/config/wtdb
#准备 sh 文件放到根目录 /opt/mongodb4.2下
#准备 mongod.conf 放到对应节点的(mongos,config,shardxx)文件夹下
##这个偷懒用
ln -s /opt/mongodb4.2/bin/mongo /user/bin/mongo
--cfStart.sh --mgsStart.sh --1shardStart.sh --2shardStart.sh --3shardStart.sh --config ----mongod.conf ----log ----wtdb --mongos ----mongod.conf ----log ----wtdb --shard1 ----mongod.conf ----log ----wtdb --shard2 ----mongod.conf ----log ----wtdb --shard3 ----mongod.conf ----log ----wtdb
conf 放到对应 config . mongos shard1 shard2 shard3 路径下
#keyFile 放到key 目录下
##也可以自己生成个,实际里面内容可以乱写的
openssl rand -base64 756 > ./keyFile.file chmod 400 ./keyFile.file
testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest
#通用启动那个脚本
##注意修改路径,如果你需要新用户自己创建,并授权文件夹
22 MONGO_USER=root
23 MONGO_GROUP=root
24 MONGO_HOME="/opt/mongodb4.2"
1 # 2 # init.d / servicectl compatibility (openSUSE) 3 # 4 if [ -f /etc/rc.status ]; then 5 . /etc/rc.status 6 rc_reset 7 fi 8 9 # 10 # Source function library. 11 # 12 if [ -f /etc/rc.d/init.d/functions ]; then 13 . /etc/rc.d/init.d/functions 14 fi 15 16 # 17 # 18 # 19 #pids=("shard1" "shard2" "shard3" "config" "mongos") 20 pids=("config") 21 # 22 MONGO_USER=root 23 MONGO_GROUP=root 24 MONGO_HOME="/opt/mongodb4.2" 25 # 26 mongod="$MONGO_HOME/bin/mongod" 27 mongos="$MONGO_HOME/bin/mongos" 28 29 # 30 # 31 # 32 MAX_OPEN_FILES=65535 33 MAX_MAP_COUNT=262144 34 35 start() { 36 37 ini 38 echo "begin start" 39 ulimit -f unlimited 40 ulimit -t unlimited 41 ulimit -v unlimited 42 ulimit -n 64000 43 ulimit -m unlimited 44 ulimit -u 64000 45 46 if [ -n "$MAX_OPEN_FILES" ]; then 47 ulimit -n $MAX_OPEN_FILES 48 fi 49 if [ -n "$MAX_LOCKED_MEMORY" ]; then 50 ulimit -l $MAX_LOCKED_MEMORY 51 fi 52 if [ -n "$MAX_MAP_COUNT" -a -f /proc/sys/vm/max_map_count ]; then 53 sysctl -q -w vm.max_map_count=$MAX_MAP_COUNT 54 fi 55 for pgName in ${pids[@]}; 56 do 57 pidfile="$MONGO_HOME/$pgName/mongodb_$pgName.pid" 58 pgLock=/var/lock/subsys/mongodb_$pgName 59 60 61 if [ -n "$pidfile" ] && [ ! -e "$pidfile" ]; then 62 touch "$pidfile" && chown "$MONGO_USER":"$MONGO_GROUP" "$pidfile" 63 fi 64 echo $pidfile 65 66 # CONF_FILE setting was removed 67 pgConfigFile="$MONGO_HOME/$pgName/mongod.conf" 68 69 if [ ! -f "$pgConfigFile" ]; then 70 echo "pgConfigFile $pgName 没有找到配置文件 $pgConfigFile" 71 exit 1 72 fi 73 OPTIONS=" -f $pgConfigFile" 74 75 echo $mongod 76 echo $OPTIONS 77 78 echo -n $"Starting mongod: " 79 if [ "$pgName" = "mongos" ]; then 80 echo "start mongos --0" 81 daemon --user "$MONGO_USER" --check $mongos "$NUMACTL $mongos $OPTIONS >/dev/null 2>&1" 82 else 83 daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1" 84 fi 85 RETVAL=$? 86 echo 87 [ $RETVAL -eq 0 ] && touch $pgLock 88 done 89 } 90 91 stop() { 92 for pgName in ${pids[@]}; 93 do 94 pidfile="$MONGO_HOME/$pgName/mongod_$pgName.pid" 95 pgLock=/var/lock/subsys/mongodb_$pgName 96 97 98 echo $pidfile 99 # stop it here, often "killproc $prog" 100 pgId=`cat $pidfile` 101 echo -n "Stopping MongoDB server id= $pgId" 102 if [ ! -z "$pgId" ]; then 103 kill -15 $pgId 104 echo -n "Stopped -----> $pgId" 105 fi 106 retval=$? 107 echo 108 [ $retval -eq 0 ] && rm -f $pgLock 109 done 110 } 111 112 restart () { 113 stop 114 start 115 } 116 117 stopall() 118 { 119 MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'` 120 #echo "$MONGOPID" 121 echo -n "Stopping MongoDB server" 122 if [ ! -z "$MONGOPID" ]; then 123 kill -15 $MONGOPID 124 echo -n "Stopped -----> $MONGOPID" 125 fi 126 RETVAL=$? 127 echo 128 [ $RETVAL -eq 0 ] && rm -f $MONGO_LCOK 129 } 130 131 ini(){ 132 ### 133 echo "ini" 134 if [ -d /sys/kernel/mm/transparent_hugepage ]; then 135 thp_path=/sys/kernel/mm/transparent_hugepage 136 elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then 137 thp_path=/sys/kernel/mm/redhat_transparent_hugepage 138 else 139 return 0 140 fi 141 142 echo 'never' > ${thp_path}/enabled 143 echo 'never' > ${thp_path}/defrag 144 145 re='^[0-1]+$' 146 if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]] 147 then 148 # RHEL 7 149 echo 0 > ${thp_path}/khugepaged/defrag 150 else 151 # RHEL 6 152 echo 'no' > ${thp_path}/khugepaged/defrag 153 fi 154 155 unset re 156 unset thp_path 157 158 # disable NUMA 159 sysctl -w vm.zone_reclaim_mode=0 160 ### 161 } 162 RETVAL=0 163 164 case "$1" in 165 start) 166 start 167 ;; 168 stop) 169 stop 170 ;; 171 stopall) 172 stopall 173 ;; 174 restart|reload|force-reload) 175 restart 176 ;; 177 condrestart) 178 [ -f $MONGO_LCOK ] && restart || : 179 ;; 180 status) 181 status $mongod 182 RETVAL=$? 183 ;; 184 *) 185 echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}" 186 RETVAL=1 187 esac 188 189 exit $RETVAL
#config
##cfStart.sh
修改启动脚本底20行改成
"config"
##对应配置文件
## 配置文件内容 systemLog: destination: file path: "/opt/mongodb4.2/config/log/mongod.log" storage: dbPath: "/opt/mongodb4.2/config/db" net: bindIp: 0.0.0.0 port: 21000 processManagement: fork: true pidFilePath: "/opt/mongodb4.2/config/mongod_config.pid" sharding: clusterRole: configsvr ##如果需要修改 replication: replSetName: cfgSet ###key security: keyFile: "/opt/mongodb4.2/key/keyFile.key" authorization: enabled
#mongos
##对应 mongos 路径
##mgsStart.sh
修改启动脚本底20行改成 "mongos"
##对应配置文件
注意修改 和上面的名称和config 地址一致
configDB: cfgSet/172.30.1.237:21000,172.30.1.238:21000,172.30.1.239:21000
## 配置文件内容 systemLog: destination: file path: "/opt/mongodb4.2/mongos/log/mongos.log" net: port: 62222 processManagement: fork: true pidFilePath: "/opt/mongodb4.2/mongos/mongod_mongos.pid" ##注意修改下面的ip地址 sharding: configDB: cfgSet/172.30.1.237:21000,172.30.1.238:21000,172.30.1.239:21000 #sharding: # configDB: 10.210.74.170:21000 ###key security: keyFile: "/opt/mongodb4.2/key/keyFile.key"
#shard1
##1shardStart.sh
对应 shard1 路径
修改启动脚本底20行改成 "shard1"
##对应通用配置文件
注意修改存储路径
## 配置文件内容 systemLog: destination: file path: "/opt/mongodb4.2/shard1/log/mongod.log" processManagement: fork: true pidFilePath: "/opt/mongodb4.2/shard1/mongod_shard1.pid" net: port: 27001 storage: ##journal配置 journal: enabled: true ##数据文件存储位置 dbPath: "/opt/mongodb4.2/shard1/wtdb" ##是否一个库一个文件夹 directoryPerDB: true ##数据引擎 engine: wiredTiger ##WT引擎配置 wiredTiger: engineConfig: ##WT最大使用cache(根据服务器实际情况调节) cacheSizeGB: 6 ##是否将索引也按数据库名单独存储 directoryForIndexes: true ##表压缩配置 collectionConfig: blockCompressor: zlib ##索引配置 indexConfig: prefixCompression: true replication: replSetName: "shard01" sharding: clusterRole: shardsvr ###key security: keyFile: "/opt/mongodb4.2/key/keyFile.key" authorization: enabled
#注意打开每个配置文件,让其他ip可以访问
net: bindIp: 0.0.0.0
##其他修改share1 里面
1、数字
2、端口
3、数据库的存储路径
##注意修改mongos 配置文件里面
的config ip地址
##先启动 config
./cfStart.sh start
##配置集群
###进入
./bin/mongo localhost:21000
###找任意一台config
cfg = {_id : "cfgSet", configsvr: true,members : [{_id : 0, host : "172.30.1.237:21000" },{_id : 1, host : "172.30.1.238:21000" },{_id : 2, host : "172.30.1.239:21000" }]}; rs.initiate(cfg);
### 找台机器
####这个必须弄哦,忘记掉要死人的。
use admin db.createUser({user: "admin",pwd: "123456",roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]}) db.auth("admin","123456"); db.createUser({user: "root",pwd: "123456",roles: [ { role: "root", db: "admin" } ]})
##其他机器按照上面顺序重复,启动shard01 到 shard03
./1shardStart.sh
###特别权限不要忘记弄
cfg = {_id : "shard01",members : [ {_id : 0, host : "172.30.1.237:27001",arbiterOnly:true ,"priority":0 }, {_id : 1, host : "172.30.1.238:27001","priority":3}, {_id : 2, host : "172.30.1.239:27001" ,"priority":2} ]}; rs.initiate(cfg); cfg = {_id : "shard02",members : [ {_id : 0, host : "172.30.1.237:27002","priority":2 }, {_id : 1, host : "172.30.1.238:27002",arbiterOnly:true ,"priority":0 }, {_id : 2, host : "172.30.1.239:27002" ,"priority":3} ]}; rs.initiate(cfg); cfg = {_id : "shard03",members : [ {_id : 0, host : "172.30.1.237:27003","priority":3}, {_id : 1, host : "172.30.1.238:27003","priority":2}, {_id : 2, host : "172.30.1.239:27003" ,arbiterOnly:true ,"priority":0 } ]}; rs.initiate(cfg);
#现在启动mongos
./mgStart.sh
use admin db.auth("root","123456")
#我是逐个添加的 sh.addShard("shard01/172.30.1.237:27001")
sh.addShard("shard01/172.30.1.238:27002")
...
...
sh.addShard("shard03/172.30.1.239:27003" )
#下面这个一次全部添加的没有成功,不知道为啥 sh.addShard("shard01/172.30.1.237:27001,172.30.1.238:27001,172.30.1239:27001") sh.addShard("shard02/172.30.1237:27002,172.30.1.238:27002,172.30.1.239:27002") sh.addShard("shard03/172.30.1.237:27003,172.30.1.238:27003,172.30.1.239:27003")