当前位置: 首页 > news >正文

简单网站开发实例教程奉化云优化seo

简单网站开发实例教程,奉化云优化seo,网站开发销售员,美橙互联网站建设好不好文章目录 Ubuntu 22.04 安装 MongoDB后台启动 MongoDBshell 连入 MongoDB 服务 MongoDB 用户权限认证创建 root 用户开启认证重启 MongoDB 服务创建其他用户查看用户信息验证用户权限删除用户 skynet.db.mongo 模块使用authensureIndexfind、findOneinsert、safe_insertdelete、…

文章目录

    • Ubuntu 22.04 安装 MongoDB
      • 后台启动 MongoDB
      • shell 连入 MongoDB 服务
    • MongoDB 用户权限认证
      • 创建 root 用户
      • 开启认证
      • 重启 MongoDB 服务
      • 创建其他用户
      • 查看用户信息
      • 验证用户权限
      • 删除用户
    • skynet.db.mongo 模块使用
      • auth
      • ensureIndex
      • find、findOne
      • insert、safe_insert
      • delete、safe_delete
      • update、safe_update
      • aggregate
      • safe_batch_insert、safe_batch_delete

Ubuntu 22.04 安装 MongoDB

其他平台安装教程可参考官网:https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/


  1. 确定主机运行哪个 Ubuntu 版本:(配置一致的继续往下看)
cat /etc/lsb-release

在这里插入图片描述

  1. 安装 mongodb 社区版的相关依赖
sudo apt-get install libcurl4 libgssapi-krb5-2 libldap-2.5-0 libwrap0 libsasl2-2 libsasl2-modules libsasl2-modules-gssapi-mit openssl liblzma5 gnupg curl
  1. 导入 MongoDB 公共 GPG 密钥
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \--dearmor
  1. 创建/etc/apt/sources.list.d/mongodb-org-7.0.list 列表文件
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
  1. 重新加载本地包数据库
sudo apt-get update
  1. 安装最新的稳定版本
sudo apt-get install -y mongodb-org

后台启动 MongoDB

  • 配置 /etc/mongod.conf

    • bindIp: 0.0.0.0
    • fork: true
# mongod.conf# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/# Where and how to store data.
storage:dbPath: /var/lib/mongodb
#  engine:
#  wiredTiger:# where to write logging data.
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod.log# network interfaces
net:port: 27017bindIp: 0.0.0.0# how the process runs
processManagement:fork: truetimeZoneInfo: /usr/share/zoneinfo#security:#operationProfiling:#replication:#sharding:## Enterprise-Only Options:#auditLog:
  • sudo mongod -f /etc/mongod.conf

执行后结果如下:

about to fork child process, waiting until server is ready for connections.
forked process: 361
child process started successfully, parent exiting
  • ps -ef | grep mongod:可以看到有mongod进程在后台运行

在这里插入图片描述


shell 连入 MongoDB 服务

  • mongosh:mongodb 客户端连接工具(安装时自带)

在这里插入图片描述

成功连入使用:

在这里插入图片描述


MongoDB 用户权限认证

创建 root 用户

在 MongoDB 中,root 账号是具有最高权限的账号,可以执行所有操作。

use admin
db.createUser({user:'root', pwd:'root',roles:['root']})

开启认证

我们需要开启 MongoDB 的认证功能,以确保只有经过认证的用户才能访问数据库。

  • /etc/mongod.conf

在启动配置文件中,添加以下配置:

security:authorization: enabled

重启 MongoDB 服务,认证功能才会生效。

重启 MongoDB 服务

官方描述:Sending a KILL signal kill -9 will probably cause damage as mongod will not be able to cleanly exit. (In such a scenario, run the repairDatabase command.)

可以采用在 mongosh 连入数据库后,执行下述指令来友好关闭服务进程。

use admin
db.shutdownServer()

创建其他用户

在MongoDB中,每个数据库都有自己的权限系统,可以为每个数据库创建不同的账号并赋予不同的角色。

db.createUser({user: 'cauchy', pwd: 'root', roles: [{ role: 'readWrite', db: 'test'}]})

readWrite: https://www.mongodb.com/docs/manual/reference/built-in-roles/#mongodb-authrole-readWrite

roles 可参考:https://www.mongodb.com/docs/manual/reference/built-in-roles/

查看用户信息

执行下述指令,查看当前数据库系统中的所有用户信息:

use admin
db.system.users.find()

在这里插入图片描述

验证用户权限

在 test 数据库中,验证当前 cauchy 用户权限

use test
db.auth('cauchy', 'root')

删除用户

use test
db.dropUser('cauchy')

skynet.db.mongo 模块使用

本节主要讲解在 Skynet 框架中一些常用的 API,以及如何使用相应的 API 来执行 MongoDB 的 CRUD。

前置变量、方法:

host = "127.0.0.1"
port = 27017
username = "cauchy"
password = "root"
authdb = "test"
db_name = "test"function create_client()return mongo.client({host = host, port = port,username = username,password = password, authdb = authdb})
end 

auth

数据库连接认证

  • 用法:db:auth(user, pwd)

测试代码:

function test_auth()local ok, err, retlocal c = mongo.client({host = host, port = port,})local db = c[db_name]db:auth(username, password)db.testcol:dropIndex("*")db.testcol:drop()ok, err, ret = db.testcol:safe_insert({test_key = 1});assert(ok and ret and ret.n == 1, err)
end 

如果注释掉认证:-- db:auth(username, password),则会报错提示需要权限认证。

在这里插入图片描述


ensureIndex

创建索引

  • 用法:db.collection:ensureIndex({ key1 }, { option })

源码 mongo.lua 中,这个 API 实际上就是创建索引:

mongo_collection.ensureIndex = mongo_collection.createIndex

测试代码:

function test_insert_with_index()local ok, err, retlocal c = create_client()local db = c[db_name]db.testcol:dropIndex("*")db.testcol:drop()db.testcol:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"})--[[ mongoshdb.testcol.getIndexes()]]ok, err, ret = db.testcol:safe_insert({test_key = 1})assert(ok and ret and ret.n == 1, err)ok, err, ret = db.testcol:safe_insert({test_key = 1})assert(ok == false and string.find(err, "duplicate key error"))
end

执行结果:
在这里插入图片描述


find、findOne

查找符合条件的文档,find 查找所有,findOne 查找第一条

  • 用法:db.collection:find(query, projection)db.collection:findOne(query, projection)
  • projection:查询结果的投影

源码:(nextfindfindOne

local mongo_cursor = {}
local cursor_meta =	{__index	= mongo_cursor,
}
------------------------------------------------------------------------------------------------------
function mongo_cursor:next()if self.__ptr == nil thenerror "Call	hasNext	first"endlocal r	= self.__document[self.__ptr]self.__ptr = self.__ptr	+ 1if self.__ptr >	#self.__document thenself.__ptr = nilendreturn r
end
------------------------------------------------------------------------------------------------------
function mongo_collection:findOne(query, projection)local cursor = self:find(query, projection)if cursor:hasNext() thenreturn cursor:next()endreturn nil
end
------------------------------------------------------------------------------------------------------
function mongo_collection:find(query, projection)return setmetatable( {__collection = self,__query	= query	and	bson_encode(query) or empty_bson,__projection = projection and bson_encode(projection) or empty_bson,__ptr =	nil,__data = nil,__cursor = nil,__document = {},__flags	= 0,__skip = 0,__limit = 0,__sort = empty_bson,} ,	cursor_meta)
end
  1. 简单查看上述源码,可以发现 cursor:next 返回 __document 中的内容,即为实际找到的文档内容。

  2. find 返回一张表,表中有很多字段(__collection__cursor__document 等),这张表的元表是 cursor_meta,而 cursor_meta 的属性 __index 是表 mongo_cursor。所以在用 find 查找符合条件的文档时,返回的值应该使用 next 方法去一个个遍历获取所有的返回结果,即为 __document 中的内容。

  3. findOne 直接就是返回查找到的第一个文档,如上述 return cursor:next()


insert、safe_insert

插入一条文档

  • 用法:db.collection:insert(doc)db.collection:safe_insert(doc)

源码:

function mongo_collection:insert(doc)if doc._id == nil thendoc._id	= bson.objectid()endself.database:send_command("insert", self.name, "documents", {bson_encode(doc)})
end
------------------------------------------------------------------------------------------------------
function mongo_collection:safe_insert(doc)local r = self.database:runCommand("insert", self.name, "documents", {bson_encode(doc)})return werror(r)
end

如上述源码,safe_insert 会返回一些相关信息(由 werror 返回),而 insert 没有任何返回值。


delete、safe_delete

删除符合条件的一条或多条文档

  • 用法:db.collection:delete(query, single)db.collection:safe_delete(query, single)
  • single:删除条数(即limit限制)

源码:

function mongo_collection:delete(query, single)self.database:runCommand("delete", self.name, "deletes", {bson_encode({q = query,limit = single and 1 or 0,})})
end
------------------------------------------------------------------------------------------------------
function mongo_collection:safe_delete(query, single)local r = self.database:runCommand("delete", self.name, "deletes", {bson_encode({q = query,limit = single and 1 or 0,})})return werror(r)
end

如上述源码,safe_delete 会返回一些相关信息(由 werror 返回),而 delete 没有任何返回值。

测试代码:

function test_find_and_remove()local ok, err, retlocal c = create_client()local db = c[db_name]db.testcol:dropIndex("*")db.testcol:drop()local cursor = db.testcol:find()assert(cursor:hasNext() == false)db.testcol:ensureIndex({test_key = 1}, {test_key2 = -1}, {unique = true, name = "test_index"})ok, err, ret = db.testcol:safe_insert({test_key = 1, test_key2 = 1})assert(ok and ret and ret.n == 1, err)cursor = db.testcol:find()assert(cursor:hasNext() == true)local v = cursor:next()assert(v)assert(v.test_key == 1)ok, err, ret = db.testcol:safe_insert({test_key = 1, test_key2 = 2})assert(ok and ret and ret.n == 1, err)ok, err, ret = db.testcol:safe_insert({test_key = 2, test_key2 = 3})assert(ok and ret and ret.n == 1, err)ret = db.testcol:findOne({test_key2 = 1})assert(ret and ret.test_key2 == 1, err)ret = db.testcol:find({test_key2 = {['$gt'] = 0}}):sort({test_key = 1}, {test_key2 = -1}):skip(1):limit(1)--[[ mongoshdb.testcol.find({test_key2: {$gt: 0}}).sort({test_key: 1}, {test_key2: -1}).skip(1).limit(1)]]assert(ret:count() == 3)assert(ret:count(true) == 1)if ret:hasNext() thenret = ret:next()endassert(ret and ret.test_key2 == 1)db.testcol:delete({test_key = 1})db.testcol:delete({test_key = 2})ret = db.testcol:findOne({test_key = 1})assert(ret == nil)
end

上述代码中有调用了sortskiplimit,如源码所示,即为 find 返回的表中,__sort__skip__limit字段附上了值,而不是直接对数据执行排序,跳转、约束等操作。

  • count 比较特殊,会实际执行一次指令 runCommand,需要参数 with_limit_and_skip。如果参数为 nilfalse,则执行会忽略 skiplimit,反之,会加上。

源码(sortskiplimitcount ):

-- cursor:sort { key = 1 } or cursor:sort( {key1 = 1}, {key2 = -1})
function mongo_cursor:sort(key, key_v, ...)if key_v thenlocal key_list = unfold({}, key, key_v , ...)key = bson_encode_order(table.unpack(key_list))endself.__sort = keyreturn self
endfunction mongo_cursor:skip(amount)self.__skip = amountreturn self
endfunction mongo_cursor:limit(amount)self.__limit = amountreturn self
endfunction mongo_cursor:count(with_limit_and_skip)local cmd = {'count', self.__collection.name,'query', self.__query,}if with_limit_and_skip thenlocal len = #cmdcmd[len+1] = 'limit'cmd[len+2] = self.__limitcmd[len+3] = 'skip'cmd[len+4] = self.__skipendlocal ret = self.__collection.database:runCommand(table.unpack(cmd))assert(ret and ret.ok == 1)return ret.n
end

update、safe_update

更新一条文档

  • 用法:db.collection:update(query,update,upsert,multi)db.collection:safe_update(query,update,upsert,multi)
  • upsert : 默认是false。如果不存在 query 对应条件的文档,是 true 则插入一条新文档,false 则不插入。
  • multi : 默认是 false,只更新找到的第一条记录。是 true,就把按条件查出来多条记录全部更新。

示例代码:

function test_update()local ok, err, rlocal c = create_client()local db = c[db_name]db.testcol:dropIndex("*")db.testcol:drop()db.testcol:safe_insert({test_key = 1, test_key2 = 1})db.testcol:safe_insert({test_key = 1, test_key2 = 2})-- ok, err, r = db.testcol:safe_update({test_key2 = 2}, { ['$set'] = {test_key = 2} }, true, false)-- assert(ok and r and r.n == 1)ok, err, r = db.testcol:safe_update({test_key = 1}, { ['$set'] = {test_key2 = 3} }, true, true)assert(ok and r and r.n == 2)
end 

aggregate

聚合,将来自多个 doc 的 value 组合在一起,并通过对分组数据进行各种操作处理,返回计算后的数据结果,主要用于处理数据(例如统计平均值,求和等)。

  • 用法:db.collection:aggregate({ { ["$project"] = {tags = 1} } }, {cursor={}})
  • @param pipeline: array
  • @param options: map

测试代码:

function test_runcommand()local ok, err, retlocal c = create_client()local db = c[db_name]db.testcol:dropIndex("*")db.testcol:drop()ok, err, ret = db.testcol:safe_insert({test_key = 1, test_key2 = 1})assert(ok and ret and ret.n == 1, err)ok, err, ret = db.testcol:safe_insert({test_key = 1, test_key2 = 2})assert(ok and ret and ret.n == 1, err)ok, err, ret = db.testcol:safe_insert({test_key = 2, test_key2 = 3})assert(ok and ret and ret.n == 1, err)local pipeline = {{["$group"] = {_id = mongo.null,test_key_total = { ["$sum"] = "$test_key"},test_key2_total = { ["$sum"] = "$test_key2" },}}}ret = db:runCommand("aggregate", "testcol", "pipeline", pipeline, "cursor", {})assert(ret and ret.cursor.firstBatch[1].test_key_total == 4)assert(ret and ret.cursor.firstBatch[1].test_key2_total == 6)local res = db.testcol:aggregate(pipeline, {cursor={}})assert(res and res.cursor.firstBatch[1].test_key_total == 4)assert(res and res.cursor.firstBatch[1].test_key2_total == 6)
end

官方的 aggregate 接口手册:https://www.mongodb.com/docs/manual/reference/command/aggregate/


safe_batch_insert、safe_batch_delete

批量插入和删除

  • 用法:db.collection:safe_batch_insert(docs)db.collection:safe_batch_delete(docs)

测试代码:

function test_batch_insert_delete()local ok, err, retlocal c = create_client()local db = c[db_name]db.testcol:dropIndex("*")db.testcol:drop()local insert_docs = {}local insert_length = 10for i = 1, insert_length do table.insert(insert_docs, {test_key = i})end db.testcol:safe_batch_insert(insert_docs)ret = db.testcol:find()assert(insert_length == ret:count(), "test safe batch insert failed")local delete_docs = {}local delete_length = 5for i = 1, delete_length do table.insert(delete_docs, {test_key = i})end db.testcol:safe_batch_delete(delete_docs)assert(ret:count() == insert_length - delete_length, "test safe batch delete failed")
end 

更多的 skynet.db.mongo 模块用法可以去源码阅读查看。

附上 MongoDB 7.0 Manual


文章转载自:
http://doer.rqjL.cn
http://londonization.rqjL.cn
http://separative.rqjL.cn
http://spindle.rqjL.cn
http://solipsism.rqjL.cn
http://rossby.rqjL.cn
http://cease.rqjL.cn
http://diamorphine.rqjL.cn
http://wa.rqjL.cn
http://adenitis.rqjL.cn
http://lazyboots.rqjL.cn
http://uncrossed.rqjL.cn
http://rainproof.rqjL.cn
http://mothball.rqjL.cn
http://snowmelt.rqjL.cn
http://wilga.rqjL.cn
http://resume.rqjL.cn
http://darch.rqjL.cn
http://adherent.rqjL.cn
http://laryngology.rqjL.cn
http://shylock.rqjL.cn
http://spc.rqjL.cn
http://exaggerated.rqjL.cn
http://granitization.rqjL.cn
http://imitation.rqjL.cn
http://aloha.rqjL.cn
http://pillular.rqjL.cn
http://infinitely.rqjL.cn
http://facies.rqjL.cn
http://majlis.rqjL.cn
http://handover.rqjL.cn
http://coriander.rqjL.cn
http://palmistry.rqjL.cn
http://ricketic.rqjL.cn
http://oblong.rqjL.cn
http://irradiance.rqjL.cn
http://catastrophic.rqjL.cn
http://glossographer.rqjL.cn
http://etymologist.rqjL.cn
http://laxness.rqjL.cn
http://rudbeckia.rqjL.cn
http://goldy.rqjL.cn
http://soredium.rqjL.cn
http://loon.rqjL.cn
http://moleskin.rqjL.cn
http://parsoness.rqjL.cn
http://overhit.rqjL.cn
http://defenestration.rqjL.cn
http://equiprobably.rqjL.cn
http://saturnalia.rqjL.cn
http://traverse.rqjL.cn
http://intolerably.rqjL.cn
http://archipelago.rqjL.cn
http://iridectomize.rqjL.cn
http://notch.rqjL.cn
http://sonagram.rqjL.cn
http://cultrated.rqjL.cn
http://theistic.rqjL.cn
http://perdition.rqjL.cn
http://cense.rqjL.cn
http://approximately.rqjL.cn
http://minatory.rqjL.cn
http://panne.rqjL.cn
http://comparator.rqjL.cn
http://coati.rqjL.cn
http://quintroon.rqjL.cn
http://releasor.rqjL.cn
http://chill.rqjL.cn
http://antifoulant.rqjL.cn
http://diskcomp.rqjL.cn
http://troilism.rqjL.cn
http://clonidine.rqjL.cn
http://ducat.rqjL.cn
http://benison.rqjL.cn
http://verrucose.rqjL.cn
http://callipers.rqjL.cn
http://queenless.rqjL.cn
http://pons.rqjL.cn
http://precedable.rqjL.cn
http://limewash.rqjL.cn
http://anthracite.rqjL.cn
http://bonanza.rqjL.cn
http://pyrrhonic.rqjL.cn
http://asp.rqjL.cn
http://farinha.rqjL.cn
http://stipular.rqjL.cn
http://palter.rqjL.cn
http://fougasse.rqjL.cn
http://sleekly.rqjL.cn
http://dispassion.rqjL.cn
http://bangladeshi.rqjL.cn
http://flankerback.rqjL.cn
http://ironmonger.rqjL.cn
http://railway.rqjL.cn
http://bedaub.rqjL.cn
http://solifidian.rqjL.cn
http://pterosaurian.rqjL.cn
http://model.rqjL.cn
http://cembalist.rqjL.cn
http://humid.rqjL.cn
http://www.dt0577.cn/news/75162.html

相关文章:

  • 上海网站设计方案百度客服24小时电话
  • 北京网站开发培训中心网络广告策划
  • 网站首页ico怎么做搜索推广公司
  • 响应式网站建设特征bing搜索引擎国际版
  • 营销推广外包公司北京网站优化效果
  • wordpress网站制作价格百度推广关键词查询
  • 网站产品优化方案在哪里可以发布自己的广告
  • 鹤壁市城乡一体化示范区官网入口南昌网站seo
  • 网站克隆好后该怎么做爱站长工具综合查询
  • 有什么网站可以做简历百度大数据查询怎么用
  • 基础展示营销型型网站seo系统是什么
  • 备案网站建设方案书app推广全国代理加盟
  • 抖音上做我女朋友网站电商怎么做?如何从零开始学做电商赚钱
  • 九龙坡集团网站建设网络销售是什么工作内容
  • ftp上传php网站微信营销软件哪个好用
  • 做网站等保收费百度指数数据下载
  • 毕业设计网站开发的中期报告市场推广seo职位描述
  • 动态网站建设与规划怎么制作自己公司网站
  • wordpress laravel速度株洲seo优化推荐
  • 吉林省 网站建设网络营销ppt怎么做
  • 给人做logo的网站公司域名注册查询
  • 网站kv如何做全网品牌推广
  • 如何访问国外网站百度怎么发布自己的广告
  • 网站开发培训视频如何在网上推广自己
  • 服务器域名已有做网站app推广员好做吗
  • 电商类网站开发合同书seo少女
  • 柳州住房和城乡建设厅网站手机百度网址大全首页
  • 西安做网站 好运网络seo是什么服务
  • 自己编程怎么做网站教程清远市发布
  • 官网建设企业商丘网站优化公司