fit_mongo.sh和fit_mongo.sh在同一级目录下:
fit_mongo.sh内容:
#!/bin/bash
//进入mongo的bin目录下 cd mongo/mongodb-linux-x86_64-rhel62-4.2.3/bin/
//执行的脚本 ,其中的data为要操作的数据库 ./mongo --host 127.0.0.1:27017 data ../../../fit_mongo.js >> fit_mongo.log
fit_mongo.js的内容:
try{ var db = connect(‘data‘); if(db.system.users.find({‘user‘:‘username‘}).count() == 0){ print(‘Ready to addUser...‘); db.createUser({user: "admin",pwd: "cloudmap123",roles: ["readWrite"]}); }else{ print(‘Already exist user.‘); } }catch(err){ print(‘error message:‘+err); }