当启动的时候,mongo检查用户HOME目录下的一个JavaScript文件.mongorc.js。如果找到,mongo在首次显示提示信息前解析.mongorc.js的内容。
如果你使用shell执行一个JavaScript文件或计算表达式,要么通过在命令行使用--eval选项或者指定一个.js文件给mongo,mongo会在完成JavaScript的处理后读取.mongorc.js文件。
可以使用--norc选项来阻止加载.mongorc.js。
官方文档有这么一句话:
When starting, mongo checks the user’s HOME directory for a JavaScript file named .mongorc.js. If found, mongo interprets the content of .mongorc.js before displaying the prompt for the first time. If you use the shell to evaluate a JavaScript file or expression, either by using the --eval option on the command line or by specifying a .js file to mongo, mongo will read the .mongorc.js file after the JavaScript has finished processing. You can prevent .mongorc.js from being loaded by using the --norc option.
当我们启动MongoDB时,MongoDB 在启动用户的 HOME 目录中检查名为 .mongorc.js 的 JavaScript 文件。如果找到,mongo 会在第一次显示返回客户请求之前应用 .mongorc.js 的内容。 我们可以使用 --norc 选项阻止加载 .mongorc.js。
我们可以将rs.slaveOk()这条命令,写入到这个.mongors.js文件中,这样,就可以让mongo-shell客户端为我们执行这个命令了。
我的MongoDB实例的启动用户是root,所以默认的js文件就在:/root/.mongors.js 路径下。只需要向其中键入:echo 'rs.slaveOk()' > /root/.mongors.js 即可
后续登录时候,就不需要手工输入这个rs.slaveOk()的命令了,效果如下:
当然,这个.mongors.js文件,你还可以写上其他的命令。
它都可以在你登录mongodb服务的第一时间,自动帮你执行。
参考
Docs Home → Develop Applications → MongoDB Manual/Configure the mongo Shell
https://docs.mongodb.com/manual/tutorial/configure-mongo-shell/
mongo Shell的使用小技巧
https://blog.csdn.net/u011235983/article/details/9768179
MongoDB中文手册|官方文档中文版
https://docs.mongoing.com/