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
| #!/bin/bash
MONGO_PORT=27017 DATA_DIR="/data/mongodb" LOG_DIR="/var/log/mongodb" CONFIG_DIR="/etc/mongodb" ADMIN_USER="admin" ADMIN_PASS="admin123"
start_mongodb_readonly() { echo "启动MongoDB只读节点服务..." systemctl start mongodb-readonly.service sleep 5 if systemctl is-active --quiet mongodb-readonly.service; then echo "MongoDB只读节点服务启动成功" else echo "MongoDB只读节点服务启动失败" systemctl status mongodb-readonly.service exit 1 fi }
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: "userAdminAnyDatabase", db: "admin" }, { role: "dbAdminAnyDatabase", db: "admin" } ] });
// 创建只读用户 use appdb; db.createUser({ user: "readonly", pwd: "read123", roles: [ { role: "read", db: "appdb" } ] });
// 创建分析用户 use analytics; db.createUser({ user: "analyst", pwd: "analyst123", roles: [ { role: "read", db: "analytics" }, { role: "read", db: "appdb" } ] });
print("用户账户创建完成"); EOF
echo "用户账户创建完成" }
configure_readonly_optimization() { echo "配置只读优化..." mongo --port $MONGO_PORT -u $ADMIN_USER -p $ADMIN_PASS --authenticationDatabase admin << EOF // 配置只读优化 // @author 运维实战
use admin;
// 设置只读模式 db.adminCommand({setParameter: 1, readOnly: true});
// 配置读取偏好 db.getMongo().setReadPref("primaryPreferred");
// 配置读取关注 db.getMongo().setReadConcern("majority");
// 禁用写入操作 db.adminCommand({setParameter: 1, enableFreeMonitoring: false}); db.adminCommand({setParameter: 1, diagnosticDataCollectionEnabled: false});
// 优化查询执行器 db.adminCommand({setParameter: 1, queryExecutor: "classic"});
// 配置缓存优化 db.adminCommand({ setParameter: 1, wiredTigerEngineRuntimeConfig: "cache_size=5GB,eviction=(threads_min=3,threads_max=6)" });
print("只读优化配置完成"); EOF
echo "只读优化配置完成" }
import_test_data() { echo "导入测试数据..." mongo --port $MONGO_PORT -u $ADMIN_USER -p $ADMIN_PASS --authenticationDatabase admin << EOF // 导入测试数据 // @author 运维实战
use testdb;
// 创建测试集合 db.createCollection("users"); db.createCollection("products"); db.createCollection("orders");
// 插入测试数据 for (var i = 1; i <= 1000; i++) { db.users.insert({ userId: i, username: "user" + i, email: "user" + i + "@example.com", age: Math.floor(Math.random() * 50) + 18, city: ["Beijing", "Shanghai", "Guangzhou", "Shenzhen"][Math.floor(Math.random() * 4)], createdAt: new Date() }); }
for (var i = 1; i <= 500; i++) { db.products.insert({ productId: i, name: "Product " + i, category: ["Electronics", "Clothing", "Books", "Home"][Math.floor(Math.random() * 4)], price: Math.floor(Math.random() * 1000) + 10, stock: Math.floor(Math.random() * 100), createdAt: new Date() }); }
for (var i = 1; i <= 2000; i++) { db.orders.insert({ orderId: i, userId: Math.floor(Math.random() * 1000) + 1, productId: Math.floor(Math.random() * 500) + 1, quantity: Math.floor(Math.random() * 5) + 1, total: Math.floor(Math.random() * 1000) + 10, status: ["pending", "completed", "cancelled"][Math.floor(Math.random() * 3)], createdAt: new Date() }); }
// 创建索引 db.users.createIndex({userId: 1}); db.users.createIndex({username: 1}); db.users.createIndex({email: 1}); db.users.createIndex({city: 1});
db.products.createIndex({productId: 1}); db.products.createIndex({category: 1}); db.products.createIndex({price: 1});
db.orders.createIndex({orderId: 1}); db.orders.createIndex({userId: 1}); db.orders.createIndex({productId: 1}); db.orders.createIndex({status: 1}); db.orders.createIndex({createdAt: 1});
print("测试数据导入完成"); EOF
echo "测试数据导入完成" }
verify_deployment() { echo "验证MongoDB只读节点部署结果..." mongo --port $MONGO_PORT -u $ADMIN_USER -p $ADMIN_PASS --authenticationDatabase admin << EOF // 验证MongoDB只读节点部署结果 // @author 运维实战
use admin;
// 检查服务器状态 print("=== 只读节点部署验证 ==="); var serverStatus = db.serverStatus(); print("服务器状态: " + serverStatus.ok);
// 检查只读模式 print("\n=== 只读模式检查 ==="); var readOnlyStatus = db.adminCommand({getParameter: 1, readOnly: 1}); print("只读模式: " + readOnlyStatus.readOnly);
// 检查内存使用情况 print("\n=== 内存使用情况 ==="); print("物理内存: " + serverStatus.mem.resident + " MB"); print("虚拟内存: " + serverStatus.mem.virtual + " MB"); print("映射内存: " + serverStatus.mem.mapped + " MB");
// 检查连接情况 print("\n=== 连接情况 ==="); print("当前连接数: " + serverStatus.connections.current); print("可用连接数: " + serverStatus.connections.available); print("总连接数: " + serverStatus.connections.totalCreated);
// 检查存储情况 print("\n=== 存储情况 ==="); print("数据大小: " + serverStatus.storageSize); print("索引大小: " + serverStatus.indexSize); print("总大小: " + (serverStatus.storageSize + serverStatus.indexSize));
// 检查测试数据 print("\n=== 测试数据检查 ==="); use testdb; print("用户数量: " + db.users.count()); print("产品数量: " + db.products.count()); print("订单数量: " + db.orders.count());
// 检查索引 print("\n=== 索引检查 ==="); print("用户索引: " + db.users.getIndexes().length); print("产品索引: " + db.products.getIndexes().length); print("订单索引: " + db.orders.getIndexes().length);
// 测试只读操作 print("\n=== 只读操作测试 ==="); var user = db.users.findOne(); print("查询用户: " + user.username);
var product = db.products.findOne(); print("查询产品: " + product.name);
var order = db.orders.findOne(); print("查询订单: " + order.orderId);
print("\n只读节点部署验证完成"); EOF
echo "只读节点部署验证完成" }
main() { echo "开始部署MongoDB单节点只读..." start_mongodb_readonly create_users configure_readonly_optimization import_test_data verify_deployment echo "MongoDB单节点只读部署完成!" echo "只读节点: localhost:$MONGO_PORT" echo "管理员用户: $ADMIN_USER" echo "管理员密码: $ADMIN_PASS" echo "只读用户: readonly" echo "只读密码: read123" }
main "$@"
|