使用mongo-express图形化界面远程管理数据库

本人以前一直用Robo连接远程数据库管理,被黑客攻击后(见文章1文章2)再也不敢用了,因为我主机linux,又嫌mongo shell敲起来太累一直对命令行不感冒...,所以就想着找一个能跑在服务器的web端管理器。上github一搜,发现mongo-express星星比较多,于是就配置了一下,在服务器欢快的跑了起来,记录如下。

  1. 服务器随便找个地方建个文件夹,里面运行npm install mongo-express,注意不要按官网说的全局装,不然装了以后位置还真不好找。
  2. 修改默认配置文件:进入你的安装文件夹/node_modules/mongo-express,执行cp config.default.js config.js.
  3. 编辑config.js文件,以下几个地方要注意:
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: 4,

    //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,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
    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 登录密码',
  },

设置好了以后,进入你的安装文件夹/node_modules/mongo-express,执行npm start,就跑起来了。然后网页端访问服务器ip:你定义的端口号, 会提示登录,按照你设定的用密登录就可以管理了。

  1. 这样还是有点麻烦,每次都要做上面这个步骤,写个简单的shell脚本mongoadmin.sh:
cd /你的安装文件夹/node_modules/mongo-express & npm start

赋予权限:

chmod -x mongoadmin.sh

那么每次. mongoadmin.sh就行了。

还是那句话,数据库安全无小事,所以这个用的时候打开,用完就关闭吧,另外,做好数据库自动备份,我现在的频率是每天备份。

    原文作者:Angeladaddy
    原文地址: https://www.jianshu.com/p/437626dafad2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞