最近项目需要使用 MongoDB,所以不得不搭建 MongoDB 环境,此文记录搭建过程及使用过程中需要了解的问题。

Linux + Windows 混合搭建调试 MongoDB 记录

版本介绍 :

windows : win10 64bit

linux:ubuntu 14.0.4

nodejs:6.11.2

MongoDB: 4.0.9

MongoDB安装

MongoDB下载地址:https://www.mongodb.com/download-center/community

ubuntu安装MongoDB

1. 下载:点击上方链接选择目标

可以直接点击 DownLoad 下载,或者使用上图链接下载

解压提取

将其中内容移动到/usr/local/mongodb

2. 创建用于存储的数据库和日志目录,数据都将存于此地

该路径可配置,配置文件中要相应参数与之对应

mkdir -p /data/db # 默认数据库目录路径,应手动予以创建
mkdir -p /data/logs

3. 配置环境变量

vi ~/.bashrc

在最后一行插入

export PATH=/usr/local/mongodb/bin:$PATH

4. 运行MongoDB 服务

root@qizhuang-virtual-machine:/# mongod

运行结果:

root@qizhuang-virtual-machine:/# mongod
--29T16::28.558+ I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
--29T16::28.660+ I CONTROL [initandlisten] MongoDB starting : pid= port= dbpath=/data/db -bit host=qizhuang-virtual-machine
--29T16::28.660+ I CONTROL [initandlisten] db version v4.0.9
--29T16::28.660+ I CONTROL [initandlisten] git version: fc525e2d9b0e4bceff5c2201457e564362909765
--29T16::28.660+ I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1f Jan
--29T16::28.660+ I CONTROL [initandlisten] allocator: tcmalloc
--29T16::28.660+ I CONTROL [initandlisten] modules: none
--29T16::28.660+ I CONTROL [initandlisten] build environment:
--29T16::28.660+ I CONTROL [initandlisten] distmod: ubuntu1404
--29T16::28.661+ I CONTROL [initandlisten] distarch: x86_64
--29T16::28.661+ I CONTROL [initandlisten] target_arch: x86_64
--29T16::28.661+ I CONTROL [initandlisten] options: {}
--29T16::28.708+ I STORAGE [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
--29T16::28.708+ I STORAGE [initandlisten]
--29T16::28.708+ I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
--29T16::28.708+ I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
--29T16::28.714+ I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=256M,session_max=,eviction=(threads_min=,threads_max=),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=),statistics_log=(wait=),verbose=(recovery_progress),
--29T16::40.351+ I STORAGE [initandlisten] WiredTiger message [:][:0x7f1436e1fa80], txn-recover: Main recovery loop: starting at / to /
--29T16::40.667+ I STORAGE [initandlisten] WiredTiger message [:][:0x7f1436e1fa80], txn-recover: Recovering log through
--29T16::40.756+ I STORAGE [initandlisten] WiredTiger message [:][:0x7f1436e1fa80], txn-recover: Recovering log through
--29T16::40.802+ I STORAGE [initandlisten] WiredTiger message [:][:0x7f1436e1fa80], txn-recover: Set global recovery timestamp:
--29T16::40.886+ I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(, )
--29T16::40.977+ I CONTROL [initandlisten]
--29T16::40.977+ I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
--29T16::40.977+ I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
--29T16::40.977+ I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
--29T16::40.978+ I CONTROL [initandlisten]
--29T16::40.978+ I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
--29T16::40.978+ I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
--29T16::40.978+ I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
--29T16::40.978+ I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
--29T16::40.978+ I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
--29T16::40.978+ I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
--29T16::40.978+ I CONTROL [initandlisten]
--29T16::40.978+ I CONTROL [initandlisten]
--29T16::40.978+ I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
--29T16::40.978+ I CONTROL [initandlisten] ** We suggest setting it to 'never'
--29T16::40.978+ I CONTROL [initandlisten]
--29T16::40.978+ I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
--29T16::40.978+ I CONTROL [initandlisten] ** We suggest setting it to 'never'
--29T16::40.979+ I CONTROL [initandlisten]
--29T16::41.217+ I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
--29T16::41.301+ I NETWORK [initandlisten] waiting for connections on port

5. mongo shell

如果你需要进入MongoDB后台管理,你需要先打开mongodb装目录的下的bin目录,然后执行mongo命令文件。

MongoDB Shell是MongoDB自带的交互式Javascript shell,用来对MongoDB进行操作和管理的交互式环境。

root@qizhuang-virtual-machine:/# mongo

运行结果:

root@qizhuang-virtual-machine:/# mongo
MongoDB shell version v4.0.9
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9eb5b47f-f872-4525-8906-8ea79bacd38b") }
MongoDB server version: 4.0.9
> # 此处可以进行一些交互式操作

mongod 配置文件

注意:1. ip     2. auth

参考: https://www.cnblogs.com/zhoujinyi/p/3130231.html

mongodb 角色预授权

注意:1. root角色    2. 在某个库下创建的用户隶属于这个库

参考:https://blog.csdn.net/kk185800961/article/details/45619863

mongo-express

参考 centos7下使用mongo-express/adminMongo通过WEB管理MongoDB(可视化)https://blog.csdn.net/Algorithmguy/article/details/81905224

docker mongo-expresshttps://docs.docker-cn.com/samples/mongo-express/

Configuration

Environment vairables are passed to the run command for configuring a mongo-express container.

Name                            | Default         | Description
--------------------------------|-----------------|------------
ME_CONFIG_BASICAUTH_USERNAME | '' | mongo-express web username
ME_CONFIG_BASICAUTH_PASSWORD | '' | mongo-express web password
ME_CONFIG_MONGODB_ENABLE_ADMIN | 'true' | Enable admin access to all databases. Send strings: `"true"` or `"false"`
ME_CONFIG_MONGODB_ADMINUSERNAME | '' | MongoDB admin username
ME_CONFIG_MONGODB_ADMINPASSWORD | '' | MongoDB admin password
ME_CONFIG_MONGODB_PORT | | MongoDB port
ME_CONFIG_MONGODB_SERVER | 'mongo' | MongoDB container name. Use comma delimited list of host names for replica sets.
ME_CONFIG_OPTIONS_EDITORTHEME | 'default' | mongo-express editor color theme, [more here](http://codemirror.net/demo/theme.html)
ME_CONFIG_REQUEST_SIZE | '100kb' | Maximum payload size. CRUD operations above this size will fail in [body-parser](https://www.npmjs.com/package/body-parser).
ME_CONFIG_SITE_BASEURL | '/' | Set the baseUrl to ease mounting at a subdirectory. Remember to include a leading and trailing slash.
ME_CONFIG_SITE_COOKIESECRET | 'cookiesecret' | String used by [cookie-parser middleware](https://www.npmjs.com/package/cookie-parser) to sign cookies.
ME_CONFIG_SITE_SESSIONSECRET | 'sessionsecret' | String used to sign the session ID cookie by [express-session middleware](https://www.npmjs.com/package/express-session).
ME_CONFIG_SITE_SSL_ENABLED | 'false' | Enable SSL.
ME_CONFIG_SITE_SSL_CRT_PATH | '' | SSL certificate file.
ME_CONFIG_SITE_SSL_KEY_PATH | '' | SSL key file.

The following are only needed if ME_CONFIG_MONGODB_ENABLE_ADMIN is “false”

Name                            | Default         | Description
--------------------------------|-----------------|------------
ME_CONFIG_MONGODB_AUTH_DATABASE | 'db' | Database name
ME_CONFIG_MONGODB_AUTH_USERNAME | 'admin' | Database username
ME_CONFIG_MONGODB_AUTH_PASSWORD | 'pass' | Database password

config.js 参考:https://www.jianshu.com/p/437626dafad2

module.exports = {
mongodb: {
server: 'localhost',
port: 你的mongodb端口,为安全起见,最好修改默认端口 //ssl: connect to the server using secure SSL
ssl: process.env.ME_CONFIG_MONGODB_SSL || mongo.ssl, //sslValidate: validate mongod server certificate against CA
sslValidate: process.env.ME_CONFIG_MONGODB_SSLVALIDATE || true, //sslCA: array of valid CA certificates
sslCA: [], //autoReconnect: automatically reconnect if connection is lost
autoReconnect: true, //poolSize: size of connection pool (number of connections to use)
poolSize: , //set admin to true if you want to turn on admin features
//if admin is true, the auth list below will be ignored
//if admin is true, you will need to enter an admin username/password below (if it is needed)
  //如果 admin 设置为true,则登陆认证作用于所有的库;设置为false,则登录认证只作用于部分库
admin: true 或 false,true为admin登录,安全性考虑建议设为false // >>>> If you are using regular accounts, fill out auth details in the section below
// >>>> If you have admin auth, leave this section empty and skip to the next section
  // 当 admin 设置为false,在此处添加作用的库及其用户认证
auth: [
/*
* Add the name, username, and password of the databases you want to connect to
* Add as many databases as you want!
*/
{
database: '要管理的数据库名称',
username: '此数据库管理员',
password: '管理员密码',
},
], // >>>> If you are using an admin mongodb account, or no admin account exists, fill out section below
// >>>> Using an admin account allows you to view and edit all databases, and view stats //如果上面的admin为true,这个地方填入admin信息
adminUsername: '*******',
adminPassword: '*******', //whitelist: hide all databases except the ones in this list (empty list for no whitelist)
whitelist: [], //blacklist: hide databases listed in the blacklist (empty list for no blacklist)
blacklist: [],
}, site: {
// baseUrl: the URL that mongo express will be located at - Remember to add the forward slash at the start and end!
baseUrl: process.env.ME_CONFIG_SITE_BASEURL || '/',
cookieKeyName: 'mongo-express',
cookieSecret: process.env.ME_CONFIG_SITE_COOKIESECRET || 'cookiesecret',
host: process.env.VCAP_APP_HOST || '你的服务器ip地址',//这个地方弄了很久,最后搞清楚是不带http的服务器地址
port: process.env.VCAP_APP_PORT || mongo-express 跑起来以后䣌端口号,默认是8081,最好改一下,
requestSizeLimit: process.env.ME_CONFIG_REQUEST_SIZE || '50mb',
sessionSecret: process.env.ME_CONFIG_SITE_SESSIONSECRET || 'sessionsecret',
sslCert: process.env.ME_CONFIG_SITE_SSL_CRT_PATH || '',
sslEnabled: process.env.ME_CONFIG_SITE_SSL_ENABLED || false,
sslKey: process.env.ME_CONFIG_SITE_SSL_KEY_PATH || '',
}, //set useBasicAuth to true if you want to authenticate mongo-express loggins
//if admin is false, the basicAuthInfo list below will be ignored
//this will be true unless ME_CONFIG_BASICAUTH_USERNAME is set and is the empty string
useBasicAuth: process.env.ME_CONFIG_BASICAUTH_USERNAME !== '', basicAuth: {
username: process.env.ME_CONFIG_BASICAUTH_USERNAME || 'mongo-express 登录用户名,最好改一下',
password: process.env.ME_CONFIG_BASICAUTH_PASSWORD || 'mongo-express 登录密码',
},

Java mongodb

使用MongoCollection,BasicDBObject 条件查询

        //链接数据库
MongoClient mongoClient = new MongoClient( "172.26.xxx.xxx" , 27017 ); MongoDatabase mongoDatabase =mongoClient.getDatabase("xxxx"); MongoCollection<Document> collection = mongoDatabase.getCollection("test_logs"); //加入查询条件
BasicDBObject query = new BasicDBObject();
//时间区间查询 记住如果想根据这种形式进行时间的区间查询 ,存储的时候 记得把字段存成字符串,就按yyyy-MM-dd HH:mm:ss 格式来
query.put("times", new BasicDBObject("$gte", "2018-06-02 12:20:00").append("$lte","2018-07-04 10:02:46"));
//模糊查询
Pattern pattern = Pattern.compile("^.*王.*$", Pattern.CASE_INSENSITIVE);
query.put("userName", pattern);
//精确查询
query.put("id", "11");
//skip 是分页查询,从第0条开始查10条数据。 Sorts是排序用的。有descending 和ascending
MongoCursor<Document> cursor = collection.find(query).sort(Sorts.orderBy(Sorts.descending("times"))).skip(0).limit(10).iterator();//
int unm=0;
try {
while (cursor.hasNext()) {
UserBehaviorLogs userBehaviorLogs = new UserBehaviorLogs();
//查询出的结果转换成jsonObject,然后进行封装或者直接返回给前端处理。我这是封装成对象了
JSONObject jsonObject = JSONObject.parseObject( cursor.next().toJson().toString());
userBehaviorLogs.setId(jsonObject.getString("id"));//id
userBehaviorLogs.setUserId(jsonObject.getString("userId"));//用户id
userBehaviorLogs.setUserName(jsonObject.getString("userName"));//用户名称
userBehaviorLogs.setParams(jsonObject.getString("params"));//参数
userBehaviorLogs.setException(jsonObject.getString("Exception"));//异常信息
userBehaviorLogs.setTimes(jsonObject.getString("times")+"");//创建时间
unm++;
System.out.println(unm+"="+userBehaviorLogs.getTimes()+"==="+userBehaviorLogs.getId());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
}

MongoDB + Express 环境搭建记的更多相关文章

  1. MongoDB的环境搭建及启动

    MongoDB环境搭建及配置 一.环境搭建 Mac:brew install mongodb 常见问题: Error: Permission denied @ unlink_internal 解决方案 ...

  2. Docker学习之——Node.js+MongoDB+Nginx环境搭建(一)

    最近在学习Node.js相关知识,在环境搭建上耗费了不少功夫,故此把这个过程写下来同大家分享一下,今天我先来介绍一下Docker,有很多人都写过相关知识,还有一些教程,在此我只想写一下,我的学习过程中 ...

  3. mongodb基础环境搭建

    一.准备工具 (1)mongodb(https://www.mongodb.com/dr/fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus- ...

  4. C#与mongoDB初始环境搭建

    mongoDB官网https://www.mongodb.com/ mongoDB默认安装路径(Windows x64平台) C:\Program Files\MongoDB\Server\3.4\b ...

  5. MongoDB从环境搭建到代码编程(Window 环境)

    本人开发环境: window Server 2008 , 64位系统 服务端 MongoDB下载地址:http://www.mongodb.org/downloads  (本人己下好的在百度网盘 : ...

  6. MongoDB Windows环境搭建

    简介 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供可扩展的高性能数据存 ...

  7. MongoDB学习--环境搭建记录

    Mongo安装教程,参考英文官网 基本命令, 索引的引用,索引基于地理位置的数据, win10 64位 系统中安装虚拟机 win10 系统中安装虚拟机VMwareWorkstation11 并安装 L ...

  8. Node+Express+MongoDB + Socket.io搭建实时聊天应用实战教程(二)--node解析与环境搭建

    前言 本来开始写博客的时候只是想写一下关于MongoDB的使用总结的,后来觉得还不如干脆写一个node项目实战教程实战.写教程一方面在自己写的过程中需要考虑更多的东西,另一方面希望能对node入门者有 ...

  9. Node+Express+MongoDB + Socket.io搭建实时聊天应用

    Node+Express+MongoDB + Socket.io搭建实时聊天应用 前言 本来开始写博客的时候只是想写一下关于MongoDB的使用总结的,后来觉得还不如干脆写一个node项目实战教程实战 ...

随机推荐

  1. LNMP 支持路由重写

    server    {        listen 80;        #listen [::]:80;        server_name www.xxx.com;        index i ...

  2. tomcat Server启动带profile文件

    一般mvn如果配置有环境,直接在tomcat server中无法直接启动,需要在vm或者tomcat启动中添加profile启动. pom.xml中添加: 在<profile>中添加默认p ...

  3. 运行UMAT:+ABQ和VS、IVF绑定

    运行UMAT: 1.run-script----xxxx.py2.属性---编辑材料---通用---非独立变量---用户材料3.job---编辑作业---通用----用户子程序.for4.parall ...

  4. Tex和LaTeX认识

    TeX (文本排版系统) 利用TeX可以很容易地生成高质量的dvi文件,打印输出.特别是在处理复杂的数学公式时.利用诸如是LaTeX等终端软件,TeX就能够排版出精美的文本. LaTeX LaTeX( ...

  5. lanya

    var app = getApp()   Page({   data: {   motto: 'Hello World',   openBLE:'打开蓝牙设备',   startBLEDiscover ...

  6. jenkins pipline 发送邮件

    推荐一个好网站https://www.w3cschool.cn/jenkins/jenkins-e7bo28ol.html 获取git 用户信息// Get checkout output value ...

  7. C博客01--顺序、分支结构

    1.本章学习总结 1.1 思维导图 1.2 本章学习体会及代码量学习体会 1.2.1 学习体会 经过一周的初步学习,对C语言我有了一定的认识,也体验到了代码的乐趣,这应该为我以后的学习开了一个好头.在 ...

  8. 使用autohotkey修改方向键、回车和启动程序

    具体步骤 下载并安装autohotkey. 在你觉得合适的地方鼠标右键-新建-autohotkey script(脚本):或者创建一个别的文件,再把后缀改成ahk也可以 一个新建的ahk文档里面会有这 ...

  9. Oracel递归查询start with ...connect by prior在Mysql中的实现

    Oracle是一个强大的数据库,有很多的函数和语法可以带来很多便利,有些函数和语法在Mysql中有代替的,但是有些没有现成可用的方法,比如Oracle的递归,在Mysql中怎么实现呢? 例子: Ora ...

  10. fyi

    <div> <h5 class="page-header">人件费预测项目管理</h5> <hr></div><d ...