windows docker mongodb

大家好,我是烤鸭:

   今天翻博客,发现4年前的一篇草稿,抽空给完善下。原本草稿写的是linux下mongo使用,还有java的一些api,现在就用容器实现下。

容器部署

官方网站:
https://www.mongodb.com/
windows安装:https://docs.docker.com/desktop/windows/install/

# 下载镜像
docker pull mongo
# 启动镜像
docker run -itd --name mongo --restart=always -v D:\\data\\docker\\mongo:/data/db -p 27017:27017 mongo --auth

-itd --name mongo:运行mongo

–restart=always:设置重启

-v D:\data\docker\mongo:/data/db :本地磁盘 D:\data\docker\mongo 作为存储目录

-p 27017:27017 : 设置端口

–auth : 需要密码访问

# 进入容器
docker exec -it mongo mongo admin
# 创建admin账号
db.createUser({ user:'admin',pwd:'admin',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
# 连接
db.auth("admin","admin");
# 创建city db
use city
# 查看db
db
# 创建city用户
db.createUser({ user:'city',pwd:'city',roles:[ { role:'userAdmin', db: 'city'}]});
# 删除city用户
db.dropUser('city')
# 由于刚才没有建权限,重新创建个
db.createUser({ user:'city',pwd:'city',roles:[ { role:'dbAdmin', db: 'city'},"readWrite"]});

在这里插入图片描述

用户创建相关可以参考这篇:
https://www.jianshu.com/p/02b7f89c7abf

工具-GUI

https://studio3t.com/download-studio3t-free

默认30天完整版试用,到期后自动转入免费版。

导入测试数据

https://download.csdn.net/download/Angry_Mills/85528602
在这里插入图片描述

CURD

# 全量查询
db.getCollection("city").find({})
# 条件查询
db.getCollection("city").find({"name":"北京"})
# 插入数据,返回 WriteResult({ "nInserted" : 1 }) 即为成功
db.city.insert({"label":"测试地区010","name":"北京","pinyin":"Beijing","zip":"010"})
# 修改,db.chartName.update(json1,{$set:json2}) , json1 是条件,json2 是指定更新的字段
db.city.update({"label":"测试地区010"},{$set:{"label":"测试地区020"}})
# 删除数据
db.city.remove({"label":"测试地区020","name":"北京","pinyin":"Beijing","zip":"010"})

Java 连接

demo项目:https://gitee.com/fireduck_admin/boot-mongo-demo

pom

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId><version>2.7.0</version>
</dependency>

application.properties

# spring.data.mongodb.uri=mongodb://用户名:密码@IP:端口/数据库?authSource=admin
spring.data.mongodb.uri=mongodb://city:city@localhost:27017/city?authSource=city#上面配置可写为以下配置
#spring.data.mongodb.database=city
#spring.data.mongodb.host=localhost
#spring.data.mongodb.port=27017
#spring.data.mongodb.username=city
#spring.data.mongodb.password=city
#spring.data.mongodb.authentication-database=city

City

@Data
@Document(collection = "city")
@AllArgsConstructor
public class City {private String label;private String name;private String pinyin;private String zip;
}

MongoController

@RestController
public class MongoController {@Autowiredprivate MongoTemplate mongoTemplate;@GetMapping("/test2")public void queryDemo(){System.out.println("=============查询所有====================");List<City> all = mongoTemplate.findAll(City.class);all.forEach(System.out::println);//方法引用System.out.println();System.out.println("===============条件查询=======================");// name是北京的String name="北京";Query query = new Query(Criteria.where("name").is(name));List<City> users = mongoTemplate.find(query, City.class);users.forEach(System.out::println);System.out.println();System.out.println("===============分页查询=======================");int pageSize=5,currentPage=1;long count = mongoTemplate.count(new Query(), City.class);List<City> userList = mongoTemplate.find(new Query().skip((currentPage - 1) * pageSize).limit(pageSize), City.class);System.out.println("记录数 = "+count);userList.forEach(System.out::println);}}

线上问题记录

记得是两个月前早上5点多被报警短信震醒,mongo的某个结点cpu打满。

在这里插入图片描述

在这里插入图片描述

后来经过排查是新上的查询未走索引,导致查询耗时升高, 引起cpu异常,当时记录的查询监控。

在这里插入图片描述

可以看到,下面的多条件查询,没走组合索引导致的。

在这里插入图片描述

其他常见问题

mongodb 启动失败:ERROR: child process failed, exited with error number 1

mongodb启动时报错ERROR: child process failed, exited with error number 1_SmallTankPy的博客-CSDN博客

数据备份恢复:

MongoDB备份-mongodump和恢复-mongorestore_朝闻道-夕死可矣的博客-CSDN博客

修改数据库名称:

MongoDB修改数据库名_其實很簡單的博客-CSDN博客_mongodb修改数据库名称

docker之mongo数据库备份、导入

https://www.gxlcms.com/sql_question-396380.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/412439.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[vue] 如何在子组件中访问父组件的实例?

[vue] 如何在子组件中访问父组件的实例&#xff1f; this.$parent拿到父组件实例 this.$children拿到子组件实例&#xff08;数组&#xff09;个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录…

[vue] watch的属性用箭头函数定义结果会怎么样?

[vue] watch的属性用箭头函数定义结果会怎么样&#xff1f; 因为箭头函数默绑定父级作用域的上下文&#xff0c;所以不会绑定vue实例&#xff0c;所以 this 是undefind个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢…

tensorflow的keras 与 原生keras几点比较

tensorflow的keras 与 原声keras几点比较&#xff0c;不是全面的比较&#xff0c;因为只是就使用时候发现的差异&#xff01; 使用函数式API时&#xff1a; 1. 定义模型模型时&#xff0c;用到输入的张量&#xff0c;也就是给Input的tensor赋值为你的inputs&#xff0c;在编译时…

[vue] 在vue项目中如何配置favicon?

[vue] 在vue项目中如何配置favicon&#xff1f; 也可以在当前项目部署的端口主目录下存放favicon.ico文件&#xff0c;默认就会显示该图标个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录 与…

Is the byte array a result of corresponding serialization for DefaultDeserializer

大家好&#xff0c;我是烤鸭&#xff1a; 问题记录&#xff0c;上线之后懵逼的问题。只能回滚?每次都是上线来暴击&#xff0c;不然多查查文章也不至于这么被动。 报错日志 org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nes…

[vue] 你有使用过babel-polyfill模块吗?主要是用来做什么的?

[vue] 你有使用过babel-polyfill模块吗&#xff1f;主要是用来做什么的&#xff1f; Babel默认只转换新的JavaScript句法&#xff08;syntax&#xff09;&#xff0c;而不转换新的API&#xff0c;比如Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise等全局对…

高级PHP工程师所应该具备的专业素养

高级PHP工程师所应该具备的专业素养 初次接触PHP&#xff0c;就为他的美所折服&#xff0c;于是一发不可收拾。 很多面试&#xff0c;很多人员能力要求都有“PHP高级工程师的字眼”&#xff0c;如果您真心喜欢PHP&#xff0c;并且您刚起步&#xff0c;那么我简单说说一个PHP高级…

容器环境 springcloud gateway grafana prometheus采集集成与问题

容器环境 springcloud gateway grafana prometheus采集集成与问题 大家好&#xff0c;我是烤鸭&#xff1a; 记录下网关上容器后&#xff0c;监控升级的过程。 原来的方式 grafana 和 prometheus 网上教程很多&#xff0c;就不细写了。 没上容器之前&#xff0c;可以在…

elasticsearch 问题

elasticsearch 的端口默认绑定到 127.0.0.1 上&#xff0c;对外开放 http 端口就配置 http.host&#xff0c;对外开放 tcp 端口就配置 network.host [1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]编辑 /etc/security…

[vue] 说说你对vue的错误处理的了解?

[vue] 说说你对vue的错误处理的了解&#xff1f; 分为errorCaptured与errorHandler。 errorCaptured是组件内部钩子&#xff0c;可捕捉本组件与子孙组件抛出的错误&#xff0c;接收error、vm、info三个参数&#xff0c;return false后可以阻止错误继续向上抛出。 errorHandler…

easyui Combotree 怎么加载数据 支持多选

1、开发环境vs2012 mvc4 c# 2、HTML前端代码 <% Page Language"C#" AutoEventWireup"true" CodeBehind"DataGridTest.aspx.cs" Inherits"MvcAppTest.DataGridTest" %><!DOCTYPE html><html xmlns"http://www.w3.…

[vue] 在vue事件中传入$event,使用e.target和e.currentTarget有什么区别?

[vue] 在vue事件中传入$event&#xff0c;使用e.target和e.currentTarget有什么区别&#xff1f; event.currentTarget指向事件所绑定的元素&#xff0c;而event.target始终指向事件发生时的元素。个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&am…

idea 错误: 找不到或无法加载主类(汇总贴)

大家好&#xff0c;我是烤鸭&#xff1a; 现在是采坑实录。 idea 错误: 找不到或无法加载主类 xxx.xxx.xxxxx JDK环境&#xff0c;maven项目还是ee还是web项目&#xff0c;是否都正常。 如果是用idea打开的话,在源码目录上点击右键,然后找到Mark directory as->source ro…

C语言——生命游戏(初始

#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<windows.h> #include<time.h>#define High 25 #define Width 50 //游戏画面的尺寸int cells[High][Width]; //所有位置细胞生为1&#xff0c;死亡为0void gotoxy(…

[vue] 在.vue文件中style是必须的吗?那script是必须的吗?为什么?

[vue] 在.vue文件中style是必须的吗&#xff1f;那script是必须的吗&#xff1f;为什么&#xff1f; style 不是必须的&#xff0c;script 是必须的&#xff0c;而且必须要写上个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定…

爬虫,关于 video 标签 src 带有blob:http的 一些想法

大家好&#xff0c;我是烤鸭&#xff1a; 之前玩爬虫的时候&#xff0c;看到过video标签中src属性引入的blob:http:xxxx&#xff0c;当时没找到解决思路&#xff0c;今天又遇到类似问题&#xff0c;就试着找了一下。 这是有人问过 https://vimeo.com/ 这个网站的视频怎么下载。…

4Python切片功能剖析

引用文章&#xff1a;https://mp.weixin.qq.com/s/NZ371nKs_WXdYPCPiryocw 切片基础法则&#xff1a; &#xff08;1&#xff09;公式[i : n : m]&#xff0c;i为起始位置索引(当i为首位0可省略)&#xff0c;in为结束位置索引(当n为长度len(li)可省略)&#xff0c;m为步长&…

[vue] vue怎么实现强制刷新组件?

[vue] vue怎么实现强制刷新组件&#xff1f; 强制重新渲染this.$forceUpdate()强制重新刷新某组件//模版上绑定key <SomeComponent :key"theKey"/> //选项里绑定data data(){return{theKey:0} } //刷新key达到刷新组件的目的 theKey;个人简介 我是歌谣&#x…

Success Rate CodeForces - 807C (数学+二分)

You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made y submissions, out of which x have been successful. Thus, your current success rate on Codeforces is equal to x / y. Your favorite rational …

[vue] 实际工作中,你总结的vue最佳实践有哪些?

[vue] 实际工作中&#xff0c;你总结的vue最佳实践有哪些&#xff1f; .babelrc 是目前 babel-polyfill 的最佳实践 { "presets": [ [ "babel/preset-env", { "corejs": "3", "modules": false, "useBuiltIns": …