一、安装
1)下载mongodb-linux-x86_64-3.0.5.tgz文件
2)解压:tar –xzvf mongodb-linux-x86_64-3.0.5.tgz
3)移动位置:mv mongodb-linux-x86_64-3.0.5 /usr/local/mongodb/
4)启动mongo:
#创建数据文件夹和日志文件
mkdir data log
touch mongodb.log
#启动mongodb,有通过配置文件或直接设置参数两种方式
./mongod --dbpath=./mongodb/data/ --logpath=./mongodb/data/mongodb.log --logappend --fork&
或者./mongod –f mongodb.conf
mongodb.conf文件的内容:
dbpath=/usr/local/mongodb/data/ #数据存储路径
logpath=/usr/local/mongodb/data/mongodb.log #日志存储路径
logappend=true
fork=on #后台启动
port=12345 #端口(默认端口27017)
auth=false #是否需要身份验证
5)关闭mongo
方式一.进入mongo命令行执行db.shutdownServer()
方式二. 直接用ps命令查询到mongodb对应的进程号,再使用kill命令杀死进程
6) 问题解决
如果在虚拟机上启动mongodb,很可能出现--fork子进程启动不了,查询日志发现提示没有足够的空间for journal
错误提示:child process failed,exited with error number 100
解决办法:启动时加上--smallfiles(./mongod--fork --dbpath=/usr/local/mongodb/data/--logpath=/usr/local/mongodb/data/mongodb.log –logappend--smallfiles)
二、连接和授权
1. 连接数据库
1)mongo localhost:27017/testdb -u myusername -p myusername
testdb:需要连接的数据库名
myusername:用户名
myusername:用户密码
localhost:连接的主机,也可以是其他机器的ip
如果mongodb启动时不需要身份验证,即auth=false,则可用方式2)进行连接。
2)mongo 192.168.1.127:12345
2.用户管理
1)添加用户
旧版本的mongodb:
db.adduser(“user1”)
db.adduser(“user1”,”mypassword”,true)(用户名,密码,是否只读)
db.removeUser(“user1”)
3.0以上版本的mongodb已废弃adduser方法,改用createUser方法:
db.createUser({“user”:”user1”,”pwd:”123”,”roles”:[“dbAdmin”]})
db.createUser({user:”iot1”,pwd:”iot2”,roles:[“dbOwner”]}
2)删除用户
db.dropUser(“user1”)
3) 查看所有用户或角色
show users
show roles
3. 授权方式
use admin
db.createUser({“user”:”testuser”,”mypassword:”iot”,”roles”:[“dbOwner”]})
db.auth(“testuser”,”mypassword”)(返回1表示已通过验证)
三、初始化
1. 创建数据库
mongodb没有直接创建数据库的命令,操作方式:
use newtestdb,之后做一些操作,例如创建一个集合(表):db.newtestdb.createCollection(“user”,{“name”:20,“password”:20}),即可成功创建一个名为newtestdb的数据库了。
2. 创建集合
db.createCollection(“user”)
db.createCollection(“user”,{capped: <Boolean>,autoIndexId: <Boolean>, size: <number>, max <number>}})
3. 导入导出数据
1) mongoimport、mongoexport导入导出数据(Json格式)
mongoimport导入文件:
mongoimport –u iot1 –p iot1 –d iot –c openapi_info --file openapi_info.json
mongoexport导出文件:
mongoexport –u iot1 –p iot1 –d iot –c openapi_info --file openapi_info.json
-u:mongodb用户名
-p:用户密码
-d:数据库名
-c:需要导出的数据集(表)名称
--file:导出的文件名
2) Mongorestore、Mongodump导入导出数据(bson格式)
Mongorestore导入文件夹内的数据
mongorestore.exe -h 192.168.1.203:20000 -u clib -p clib -d clib --dir library
-h:主机ip+mongodb启动端口
-u:mongodb用户名
-p:用户密码
--dir:即将导入的数据文件所在目录
mongodump导出文件
mongodump.exe –d library –o library
mongodump –d library
四、 修改、添加、删除集合数据
1、添加记录
db.users.save({name: ‘zhangsan’, age: 25, sex: true});
添加的数据的数据列,没有固定,根据添加的数据为准
2、更新记录
db.users.update({age: 25}, {$set: {name: 'newName'}},false, true);相当于:update users set name = ‘newName’ where age = 25;
db.users.update({name: 'Li'}, {$inc: {age: 50}}, false,true);
相当于:update users set age = age + 50 where name = ‘Li’;
db.users.update({name: 'Li'}, {$inc: {age: 50}, $set:{name: 'hoho'}}, false, true);
相当于:update users set age = age + 50, name = ‘newname’ wherename = ‘Li’;
3、删除记录
db.users.remove({age: 132});4、查询记录
1)查询所有记录
db.userInfo.find();
相当于:select* from userInfo;
默认每页显示20条记录。
2)过滤掉重复数据
db.userInfo.distinct("name");
会过滤掉name中的相同数据
相当于:select distict name from userInfo;
3)按条件查询
db.userInfo.find({"age": 22});
相当于: select * from userInfo where age = 22;
db.userInfo.find({age: {$gt: 22}});
相当于:select * from userInfo where age >22;
db.userInfo.find({age: {$lt: 22}});
相当于:select * from userInfo where age <22;
db.userInfo.find({name: /mongo/});
相当于:select * from userInfo where name like ‘%mongo%’;
db.userInfo.find({name: /^mongo/});
相当于:select * from userInfo where name like ‘mongo%’;
db.userInfo.find({}, {name: 1, age: 1});
相当于:select name, age from userInfo;