1.下载对应的mongo版本
1.查看linux版本:uname -a
2.下载mongodb包
下载地址: https://www.mongodb.com/try/download
本地测试,我们下载community版本即可
下载:wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.4.1.tgz
二、配置
1.创建数据库目录和log文件和所在目录
mkdir -p /data/db
mkdir -p /data/log/mongo/
touch /data/log/mongo/mongodb.log
2.编辑配置文件
vim /etc/mongod.conf
# 详细记录输出 verbose = true # 指定服务端口号,默认端口27017 port = 27017 # 指定MongoDB日志文件,注意是指定文件不是目录 logpath = /data/log/mongo/mongodb.log # 使用追加的方式写日志 logappend = true # 指定数据库路径 dbpath = /data/db/mongo/ #设置每个数据库将被保存在一个单独的目录 directoryperdb = true # 启用验证 auth = false #以守护进程的方式运行MongoDB,创建服务器进程 fork = true #安静输出 quiet = true
三、启动、验证
cd /usr/local/mongodb/bin
1.启动:./mongod -f /etc/mongod.conf 返回以下内容表示启动成功
2.验证:pgrep mongo -l 有返回结果则证明启动成功
四、常用命令
本文只为其他项目而简单搭建环境,具体命令请查看官网等!
1.进入shell模式:
./mongo
2.查看数据库列表:
> show dbs admin 0.000GB config 0.000GB local 0.000GB
3.进入并创建数据数据库
> use test
switched to db test
4.查看当前在哪个数据库
> db
test
5.创建集合(数据表)
> db.createCollection("test") { "ok" : 1 }
6.向集合插入数据
> db.test.insert({name:"哈哈",age:18}) WriteResult({ "nInserted" : 1 })
7.查看数据库中有哪些集合(数据表)
> show collections
test
test2
test3
8.删除集合
> db.test3.drop() true
五、开启认证
1.创建管理员账号
> use admin switched to db admin > show collections system.version > db.system.version.find() { "_id" : "featureCompatibilityVersion", "version" : "4.4" } > db.createUser({user:"admin",pwd:"123456",roles:["root"]}) Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
2.开启认证
vim /etc/mongod.conf
auth = true #将auth设置为true
3.重新登录
root@ubuntu1:/usr/local/mongodb/bin# ./mongo MongoDB shell version v4.4.1 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("738b99c4-a57e-467b-8d60-15f5e0007ce8") } MongoDB server version: 4.4.1 > use admin switched to db admin > db.auth("admin","123456") 1 > show dbs admin 0.000GB config 0.000GB local 0.000GB test 0.000GB
4.为数据库添加用户
> use test switched to db test > db.createUser({user:"test",pwd:"123456",roles:[{role:"dbOwner",db:"test"}]}) Successfully added user: { "user" : "test", "roles" : [ { "role" : "dbOwner", #权限 "db" : "test" #数据库 } ] }
5.用数据库用户test登录
root@ubuntu1:/usr/local/mongodb/bin# ./mongo MongoDB shell version v4.4.1 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("ca9ce18c-be3b-4f7a-af77-50db106c9676") } MongoDB server version: 4.4.1 > use test switched to db test > db.auth("test","123456") 1 > show dbs test 0.000GB > db test > show collections test test2
6.删除用户
> use admin switched to db admin > db.system.users.find() { "_id" : "admin.admin", "userId" : UUID("a3a3c4b2-6984-44a1-80d4-112bad3fd86f"), "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "Edm0oEf+DCxBvjIAbg9V6Q==", "storedKey" : "GY8/71b+NRD2IlNAtPJTjxGZ34o=", "serverKey" : "AoSfWXn3MkOJcax7vU02a4LmTmA=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "nc3gzY+tLBNy4HGE0SXBQz0+7QMjYpbkZ9I+1w==", "storedKey" : "dlEriAdL5XE5CyhOwXGhvm9pmKRtfIGyVXkz8x0YX3w=", "serverKey" : "Oxw7bKeEcZ1FftFLY3xON6tTZS7KS/VsR5bUPQwWlXM=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] } { "_id" : "test.test", "userId" : UUID("5ddc0a8e-d799-4900-ba58-4adc4f04d73f"), "user" : "test", "db" : "test", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "7JzzsLSOQbd3+/pfavT3aQ==", "storedKey" : "M/9gUQTJGEm+Jp/z2e8hPyHeBeo=", "serverKey" : "sCPbv9Aj7Pdub9mPmQHG7Fq6hbY=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "bLSGy+lL7nepxvbhOCxGejmGd04p2ZM2QU1YYw==", "storedKey" : "NAoLCNhAZ2MtVJk/LKTKD3JiX501SiG9AL5MDn9FzcA=", "serverKey" : "kQ3SK6QEquFuUrlPKE5s0xveQ4s5SIaii8w5hx4jDt4=" } }, "roles" : [ { "role" : "dbOwner", "db" : "test" } ] } > db.dropUser("test") #admin数据库内不能删除test数据库下面的test用户 false > use test switched to db test > db.dropUser("test") #必须切换到被删除用户所在数据库才能删除 true > use admin switched to db admin > db.system.users.find() { "_id" : "admin.admin", "userId" : UUID("a3a3c4b2-6984-44a1-80d4-112bad3fd86f"), "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "Edm0oEf+DCxBvjIAbg9V6Q==", "storedKey" : "GY8/71b+NRD2IlNAtPJTjxGZ34o=", "serverKey" : "AoSfWXn3MkOJcax7vU02a4LmTmA=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "nc3gzY+tLBNy4HGE0SXBQz0+7QMjYpbkZ9I+1w==", "storedKey" : "dlEriAdL5XE5CyhOwXGhvm9pmKRtfIGyVXkz8x0YX3w=", "serverKey" : "Oxw7bKeEcZ1FftFLY3xON6tTZS7KS/VsR5bUPQwWlXM=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] } >
数据库用户角色
- 数据库用户角色(Database User Roles)
read:授予User只读数据的权限
readWrite:授予User读写数据的权限
- 数据库管理角色(Database Administration Roles):
dbAdmin:在当前dB中执行管理操作
dbOwner:在当前DB中执行任意操作
userAdmin:在当前DB中管理User
- 备份和还原角色(Backup and Restoration Roles):
backup
restore
- 跨库角色(All-Database Roles):
readAnyDatabase:授予在所有数据库上读取数据的权限
readWriteAnyDatabase:授予在所有数据库上读写数据的权限
userAdminAnyDatabase:授予在所有数据库上管理User的权限
dbAdminAnyDatabase:授予管理所有数据库的权限
- 集群管理角色(Cluster Administration Roles):
clusterAdmin:授予管理集群的最高权限
clusterManager:授予管理和监控集群的权限,A user with this role can access the config and local databases, which are used in sharding and replication, respectively.
clusterMonitor:授予监控集群的权限,对监控工具具有readonly的权限
hostManager:管理Server