最近项目需要使用 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
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
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-express:https://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: 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();
}
来源:oschina
链接:https://my.oschina.net/u/4313197/blog/3554228