Linux平台安装MongoDB,安装操作:https://www.runoob.com/mongodb/mongodb-linux-install.html
1、下载安装包:

2、配置系统参数,vi /etc/profile,添加以下命令:export PATH=<mongodb-install-directory>/bin:$PATH

3、创建数据库目录

4、设置远程,新建 /etc/mongodb.conf文件,执行命令:vim /etc/mongodb.conf;输入配置内容(bindIp要设置为0.0.0.0,这样才能外网访问)

5、设置登录的账号:db.createUser({user:"root",pwd:"root",roles:[{role:"readAnyDatabase",db:"admin" },{role: "readWrite", db: "canace" }]})
6、通过conf文件启动,执行命令:mongod --config /etc/mongodb.conf --fork(后台启动)
7、本地链接服务器mongodb命令: mongo -u root -p root 192.168.1.240:27017/admin

代码实现:
1、pom.xml配置:

2、application.properties配置:

3、上传用户参数:collection ="canace_collection"保存的库;

具体实现,findAll后面的collectionName是要查询的库;

常用操作:
授权: db.auth('admin','admin')
显示集合: show collections
集合插入: db.mycol2.insert({"name" : "菜鸟教程"})
显示集合内容: db.mycol2.find()
显示所有用户: db.system.users.find()
遇到问题:
提示错误:child process failed, exited with error number 100
原因:是没有正常关闭mongodb引起的
解决:删除var/log/mongo/中的 mongod.lock 和 _repair_incomplete文件
提示错误:"errmsg" : "not master and slaveOk=false"
解决: rs.slaveOk();
网友评论