MongoDB + mongo-express 環境搭建記

最近項目須要使用 MongoDB,因此不得不搭建 MongoDB 環境,此文記錄搭建過程及使用過程當中須要瞭解的問題。html

Linux + Windows 混合搭建調試 MongoDB 記錄前端

 

版本介紹 : 

windows : win10 64bitnode

linux:ubuntu 14.0.4linux

nodejs:6.11.2git

MongoDB: 4.0.9web

 

MongoDB安裝

MongoDB下載地址:https://www.mongodb.com/download-center/communitymongodb

 

ubuntu安裝MongoDB

1. 下載:點擊上方連接選擇目標docker

能夠直接點擊 DownLoad 下載,或者使用上圖連接下載shell

 

解壓提取數據庫

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

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

參考: http://www.javashuo.com/article/p-nozgxmgv-ko.html

 

mongodb 角色預受權

注意:1. root角色    2. 在某個庫下建立的用戶隸屬於這個庫

參考:http://www.javashuo.com/article/p-fcefgjrk-bk.html

 

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          | 27017           | 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: 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,則登陸認證只做用於部分庫
admin: truefalse,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();
        }
相關文章
相關標籤/搜索