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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
| #!/bin/bash
REPLICA_SET_NAME="rs0" MONGO_PORT=27017 DATA_DIR="/data/mongodb" LOG_DIR="/var/log/mongodb" CONFIG_DIR="/etc/mongodb" ADMIN_USER="admin" ADMIN_PASS="admin123"
create_directories() { echo "创建MongoDB副本集目录结构..." mkdir -p $DATA_DIR/{primary,secondary1,secondary2} chown -R mongodb:mongodb $DATA_DIR mkdir -p $LOG_DIR chown -R mongodb:mongodb $LOG_DIR mkdir -p $CONFIG_DIR chown -R mongodb:mongodb $CONFIG_DIR echo "目录结构创建完成" }
generate_config_files() { echo "生成MongoDB副本集配置文件..." cat > $CONFIG_DIR/mongod-primary.conf << EOF # MongoDB主节点配置文件 # @author 运维实战
# 存储配置 storage: dbPath: $DATA_DIR/primary journal: enabled: true commitIntervalMs: 100 wiredTiger: engineConfig: cacheSizeGB: 2 journalCompressor: snappy directoryForIndexes: true collectionConfig: blockCompressor: snappy indexConfig: prefixCompression: true
# 网络配置 net: port: $MONGO_PORT bindIp: 0.0.0.0 maxIncomingConnections: 1000 compression: compressors: snappy,zstd
# 日志配置 systemLog: destination: file path: $LOG_DIR/mongod-primary.log logAppend: true logRotate: reopen verbosity: 1 component: accessControl: verbosity: 1 command: verbosity: 1
# 进程管理 processManagement: fork: true pidFilePath: $DATA_DIR/primary/mongod.pid timeZoneInfo: /usr/share/zoneinfo
# 副本集配置 replication: replSetName: $REPLICA_SET_NAME oplogSizeMB: 1024
# 安全配置 security: authorization: enabled keyFile: $CONFIG_DIR/keyfile
# 性能优化 operationProfiling: slowOpThresholdMs: 100 mode: slowOp
# 监控配置 setParameter: enableLocalhostAuthBypass: false logLevel: 1 EOF
cat > $CONFIG_DIR/mongod-secondary1.conf << EOF # MongoDB从节点1配置文件 # @author 运维实战
# 存储配置 storage: dbPath: $DATA_DIR/secondary1 journal: enabled: true commitIntervalMs: 100 wiredTiger: engineConfig: cacheSizeGB: 2 journalCompressor: snappy directoryForIndexes: true collectionConfig: blockCompressor: snappy indexConfig: prefixCompression: true
# 网络配置 net: port: $((MONGO_PORT + 1)) bindIp: 0.0.0.0 maxIncomingConnections: 1000 compression: compressors: snappy,zstd
# 日志配置 systemLog: destination: file path: $LOG_DIR/mongod-secondary1.log logAppend: true logRotate: reopen verbosity: 1 component: accessControl: verbosity: 1 command: verbosity: 1
# 进程管理 processManagement: fork: true pidFilePath: $DATA_DIR/secondary1/mongod.pid timeZoneInfo: /usr/share/zoneinfo
# 副本集配置 replication: replSetName: $REPLICA_SET_NAME oplogSizeMB: 1024
# 安全配置 security: authorization: enabled keyFile: $CONFIG_DIR/keyfile
# 性能优化 operationProfiling: slowOpThresholdMs: 100 mode: slowOp
# 监控配置 setParameter: enableLocalhostAuthBypass: false logLevel: 1 EOF
cat > $CONFIG_DIR/mongod-secondary2.conf << EOF # MongoDB从节点2配置文件 # @author 运维实战
# 存储配置 storage: dbPath: $DATA_DIR/secondary2 journal: enabled: true commitIntervalMs: 100 wiredTiger: engineConfig: cacheSizeGB: 2 journalCompressor: snappy directoryForIndexes: true collectionConfig: blockCompressor: snappy indexConfig: prefixCompression: true
# 网络配置 net: port: $((MONGO_PORT + 2)) bindIp: 0.0.0.0 maxIncomingConnections: 1000 compression: compressors: snappy,zstd
# 日志配置 systemLog: destination: file path: $LOG_DIR/mongod-secondary2.log logAppend: true logRotate: reopen verbosity: 1 component: accessControl: verbosity: 1 command: verbosity: 1
# 进程管理 processManagement: fork: true pidFilePath: $DATA_DIR/secondary2/mongod.pid timeZoneInfo: /usr/share/zoneinfo
# 副本集配置 replication: replSetName: $REPLICA_SET_NAME oplogSizeMB: 1024
# 安全配置 security: authorization: enabled keyFile: $CONFIG_DIR/keyfile
# 性能优化 operationProfiling: slowOpThresholdMs: 100 mode: slowOp
# 监控配置 setParameter: enableLocalhostAuthBypass: false logLevel: 1 EOF
echo "配置文件生成完成" }
generate_keyfile() { echo "生成MongoDB副本集密钥文件..." openssl rand -base64 756 > $CONFIG_DIR/keyfile chmod 600 $CONFIG_DIR/keyfile chown mongodb:mongodb $CONFIG_DIR/keyfile echo "密钥文件生成完成" }
start_mongodb_services() { echo "启动MongoDB副本集服务..." echo "启动主节点..." mongod --config $CONFIG_DIR/mongod-primary.conf sleep 5 echo "启动从节点1..." mongod --config $CONFIG_DIR/mongod-secondary1.conf sleep 5 echo "启动从节点2..." mongod --config $CONFIG_DIR/mongod-secondary2.conf sleep 5 echo "MongoDB副本集服务启动完成" }
initialize_replica_set() { echo "初始化MongoDB副本集..." mongo --port $MONGO_PORT << EOF // 初始化MongoDB副本集 // @author 运维实战
// 初始化副本集配置 rs.initiate({ _id: "$REPLICA_SET_NAME", members: [ { _id: 0, host: "localhost:$MONGO_PORT", priority: 2, tags: { "dc": "primary", "role": "primary" } }, { _id: 1, host: "localhost:$((MONGO_PORT + 1))", priority: 1, tags: { "dc": "secondary", "role": "secondary" } }, { _id: 2, host: "localhost:$((MONGO_PORT + 2))", priority: 1, tags: { "dc": "secondary", "role": "secondary" } } ], settings: { heartbeatIntervalMillis: 2000, electionTimeoutMillis: 10000, catchUpTimeoutMillis: 2000, catchUpTakeoverDelayMillis: 30000 } });
// 等待副本集初始化完成 print("等待副本集初始化完成..."); while (rs.status().ok !== 1) { sleep(1000); }
// 检查副本集状态 print("副本集状态:"); rs.status();
print("副本集初始化完成"); EOF
echo "副本集初始化完成" }
create_users() { echo "创建MongoDB用户账户..." sleep 10 mongo --port $MONGO_PORT << EOF // 创建MongoDB用户账户 // @author 运维实战
use admin;
// 创建管理员用户 db.createUser({ user: "$ADMIN_USER", pwd: "$ADMIN_PASS", roles: [ { role: "root", db: "admin" }, { role: "clusterAdmin", db: "admin" }, { role: "userAdminAnyDatabase", db: "admin" }, { role: "dbAdminAnyDatabase", db: "admin" } ] });
// 创建应用用户 use appdb; db.createUser({ user: "appuser", pwd: "app123", roles: [ { role: "readWrite", db: "appdb" }, { role: "read", db: "appdb" } ] });
// 创建只读用户 use appdb; db.createUser({ user: "readonly", pwd: "read123", roles: [ { role: "read", db: "appdb" } ] });
print("用户账户创建完成"); EOF
echo "用户账户创建完成" }
configure_replica_set_optimization() { echo "配置副本集优化..." mongo --port $MONGO_PORT -u $ADMIN_USER -p $ADMIN_PASS --authenticationDatabase admin << EOF // 配置副本集优化 // @author 运维实战
use admin;
// 配置副本集设置 var config = rs.conf(); config.settings.heartbeatIntervalMillis = 2000; config.settings.electionTimeoutMillis = 10000; config.settings.catchUpTimeoutMillis = 2000; config.settings.catchUpTakeoverDelayMillis = 30000;
// 应用配置 rs.reconfig(config);
// 设置从节点读取偏好 rs.slaveOk();
// 配置读写分离 db.getMongo().setReadPref("secondaryPreferred");
print("副本集优化配置完成"); EOF
echo "副本集优化配置完成" }
main() { echo "开始部署MongoDB副本集..." create_directories generate_config_files generate_keyfile start_mongodb_services initialize_replica_set create_users configure_replica_set_optimization echo "MongoDB副本集部署完成!" echo "主节点: localhost:$MONGO_PORT" echo "从节点1: localhost:$((MONGO_PORT + 1))" echo "从节点2: localhost:$((MONGO_PORT + 2))" echo "管理员用户: $ADMIN_USER" echo "管理员密码: $ADMIN_PASS" }
main "$@"
|