在linux下通过apt-get install mongo的方式一键式安装mongo

安装后mongo的配置文件位于/etc/mongodb.conf. 里面有mongo的各项配置,文件内容如下:重要的主要有下面几项:

1 dbpath:存放数据库文件的地方

2 logpath: 存放log的地方

3 bind_ip = 127.0.0.1。安装好之后默认只允许本地访问。限制访问的IP为127.0.0.1。如果要允许所有的地址访问。需要将此行注释掉

4 port:访问的端口号

5 auth=true (添加帐号,密码认证)

root@zhf-maple:/home/zhf# cat /etc/mongodb.conf

# mongodb.conf

# Where to store the data.

dbpath=/var/lib/mongodb

#where to log

logpath=/var/log/mongodb/mongodb.log

logappend=true

bind_ip = 127.0.0.1

#port = 27017

# Enable journaling, http://www.mongodb.org/display/DOCS/Journaling

journal=true

# Enables periodic logging of CPU utilization and I/O wait

#cpu = true

# Turn on/off security.  Off is currently the default

#noauth = true

#auth = true

# Verbose logging output.

#verbose = true

# Inspect all client data for validity on receipt (useful for

# developing drivers)

#objcheck = true

# Enable db quota management

#quota = true

# Set oplogging level where n is

#   0=off (default)

#   1=W

#   2=R

#   3=both

#   7=W+some reads

#oplog = 0

# Diagnostic/debugging option

#nocursors = true

# Ignore query hints

#nohints = true

# Disable the HTTP interface (Defaults to localhost:27018).

#nohttpinterface = true

# Turns off server-side scripting.  This will result in greatly limited

# functionality

#noscripting = true

# Turns off table scans.  Any query that would do a table scan fails.

#notablescan = true

# Disable data file preallocation.

#noprealloc = true

# Specify .ns file size for new databases.

# nssize = <size>

# Accout token for Mongo monitoring server.

#mms-token = <token>

# Server name for Mongo monitoring server.

#mms-name = <server-name>

# Ping interval for Mongo monitoring server.

#mms-interval = <seconds>

# Replication Options

# in replicated mongo databases, specify here whether this is a slave or master

#slave = true

#source = master.example.com

# Slave only: specify a single database to replicate

#only = master.example.com

# or

#master = true

#source = slave.example.com

# Address of a server to pair with.

#pairwith = <server:port>

# Address of arbiter server.

#arbiter = <server:port>

# Automatically resync if slave data is stale

#autoresync

# Custom size for replication operation log.

#oplogSize = <MB>

# Size limit for in-memory storage of op ids.

#opIdMem = <bytes>

# SSL options

# Enable SSL on normal ports

#sslOnNormalPorts = true

# SSL Key file and password

#sslPEMKeyFile = /etc/ssl/mongodb.pem

#sslPEMKeyPassword = pass

安装后之后输入mongo命令进入shell界面

root@zhf-maple:/home/zhf# mongo

MongoDB shell version v3.4.7

connecting to: mongodb://127.0.0.1:27017

MongoDB server version: 3.4.7

Server has startup warnings:

2017-12-12T20:16:00.856+0800 I STORAGE  [initandlisten]

2017-12-12T20:16:00.856+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine

2017-12-12T20:16:00.856+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem

2017-12-12T20:16:04.012+0800 I CONTROL  [initandlisten]

2017-12-12T20:16:04.012+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.

2017-12-12T20:16:04.012+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.

2017-12-12T20:16:04.012+0800 I CONTROL  [initandlisten]

可以通过db.system.users.find()来查询所有的用户

添加用户名和帐号

> db.createUser({user:'zhf',pwd:'123',roles:['userAdminAnyDatabase']})

Successfully added user: { "user" : "zhf", "roles" : [ "userAdminAnyDatabase" ]

设置远程连接:

配置文件修改如下:注释掉bind_ip,

#bind_ip = 127.0.0.1

port = 27017

添加路由开发27017端口

root@zhf-maple:/home/zhf# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 27017 -j ACCEPT

启动mong服务。

root@zhf-maple:/var/lib/mongodb# mongod --dbpath /var/lib/mongodb

2017-12-12T22:39:13.060+0800 I CONTROL  [initandlisten] MongoDB starting : pid=16691 port=27017 dbpath=/var/lib/mongodb 64-bit host=zhf-maple

2017-12-12T22:39:13.061+0800 I CONTROL  [initandlisten] db version v3.4.7

2017-12-12T22:39:13.061+0800 I CONTROL  [initandlisten] git version: cf38c1b8a0a8dca4a11737581beafef4fe120bcd

2017-12-12T22:39:13.061+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016

2017-12-12T22:39:13.061+0800 I CONTROL  [initandlisten] allocator: tcmalloc

2017-12-12T22:39:13.061+0800 I CONTROL  [initandlisten] modules: none

2017-12-12T22:39:13.061+0800 I CONTROL  [initandlisten] build environment:

2017-12-12T22:39:13.061+0800 I CONTROL  [initandlisten]     distarch: x86_64

2017-12-12T22:39:13.061+0800 I CONTROL  [initandlisten]     target_arch: x86_64

2017-12-12T22:39:13.061+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/var/lib/mongodb" } }

2017-12-12T22:39:13.079+0800 E NETWORK  [initandlisten] listen(): bind() failed Address already in use for socket: 0.0.0.0:27017

2017-12-12T22:39:13.079+0800 E NETWORK  [initandlisten]   addr already in use

2017-12-12T22:39:13.079+0800 E NETWORK  [initandlisten] Failed to set up sockets during startup.

2017-12-12T22:39:13.079+0800 E STORAGE  [initandlisten] Failed to set up listener: InternalError: Failed to set up sockets

2017-12-12T22:39:13.079+0800 I NETWORK  [initandlisten] shutdown: going to close listening sockets...

2017-12-12T22:39:13.079+0800 I NETWORK  [initandlisten] shutdown: going to flush diaglog...

2017-12-12T22:39:13.079+0800 I CONTROL  [initandlisten] now exiting

2017-12-12T22:39:13.079+0800 I CONTROL  [initandlisten] shutting down with code:48

这里提示端口和地址已经被使用。通过netstat命令可以查看到确实有一个

root@zhf-maple:/var/lib/mongodb# netstat -anp|more

激活Internet连接 (服务器和已建立连接的)

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      749/mongod

杀掉正在运行的进程:kill -9 749并再次重启服务:

root@zhf-maple:/var/lib/mongodb# mongod --dbpath /var/lib/mongodb

2017-12-12T22:41:25.966+0800 I CONTROL  [initandlisten] MongoDB starting : pid=16780 port=27017 dbpath=/var/lib/mongodb 64-bit host=zhf-maple

2017-12-12T22:41:25.966+0800 I CONTROL  [initandlisten] db version v3.4.7

2017-12-12T22:41:25.966+0800 I CONTROL  [initandlisten] git version: cf38c1b8a0a8dca4a11737581beafef4fe120bcd

2017-12-12T22:41:25.966+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016

2017-12-12T22:41:25.966+0800 I CONTROL  [initandlisten] allocator: tcmalloc

2017-12-12T22:41:25.966+0800 I CONTROL  [initandlisten] modules: none

2017-12-12T22:41:25.966+0800 I CONTROL  [initandlisten] build environment:

2017-12-12T22:41:25.966+0800 I CONTROL  [initandlisten]     distarch: x86_64

2017-12-12T22:41:25.966+0800 I CONTROL  [initandlisten]     target_arch: x86_64

2017-12-12T22:41:25.966+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/var/lib/mongodb" } }

2017-12-12T22:41:25.984+0800 I -        [initandlisten] Detected data files in /var/lib/mongodb created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.

2017-12-12T22:41:25.984+0800 I STORAGE  [initandlisten]

2017-12-12T22:41:25.984+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine

2017-12-12T22:41:25.984+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem

2017-12-12T22:41:25.984+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3417M,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),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),

2017-12-12T22:41:27.096+0800 I CONTROL  [initandlisten]

2017-12-12T22:41:27.096+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.

2017-12-12T22:41:27.096+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.

2017-12-12T22:41:27.096+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

2017-12-12T22:41:27.096+0800 I CONTROL  [initandlisten]

2017-12-12T22:41:27.103+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/var/lib/mongodb/diagnostic.data'

2017-12-12T22:41:27.104+0800 I NETWORK  [thread1] waiting for connections on port 27017

2017-12-12T22:41:28.028+0800 I FTDC     [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK

2017-12-12T22:41:50.776+0800 I NETWORK  [thread1] connection accepted from 192.168.0.11:35718 #1 (1 connection now open)

2017-12-12T22:46:10.454+0800 I -        [conn1] end connection 192.168.0.11:35718 (1 connection now open)

2017-12-12T22:46:54.889+0800 I NETWORK  [thread1] connection accepted from 192.168.0.11:35734 #2 (1 connection now open)

代表服务开启成功。

在客户端上通过mongo 192.168.0.12:27017/admin 访问服务器的admin数据库

root@zhf-linux:~# mongo 192.168.0.12:27017/admin

MongoDB shell version: 2.6.10

connecting to: 192.168.0.12:27017/admin

Server has startup warnings:

2017-12-12T22:41:25.984+0800 I STORAGE  [initandlisten]

2017-12-12T22:41:25.984+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine

2017-12-12T22:41:25.984+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem

2017-12-12T22:41:27.096+0800 I CONTROL  [initandlisten]

2017-12-12T22:41:27.096+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.

2017-12-12T22:41:27.096+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.

2017-12-12T22:41:27.096+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

2017-12-12T22:41:27.096+0800 I CONTROL  [initandlisten]

>

关闭服务器。主要有两种方法:

1 输入Ctrl+C关闭。这种关闭方式会等待当前进行中的的操作完成,所以依然是干净的关闭方式。

2 登录数据库。然后输入下面的命令进行关闭

> use admin

switched to db admin

> db.shutdownServer()

server should be down...

mongodb学习之:mongo安装以及远程访问的更多相关文章

  1. MongoDB学习记录(一) - 安装、启动与建立数据库

    简要说明一个基本概念:MongoDB中的三要素:数据库(database).集合(collection)和文档(document). 文档:类似于JSON对象,由字段(field)和值(value)组 ...

  2. MongoDB学习笔记——数据库安装及配置

    MongoDB数据库安装 MongoDB官方下载地址:https://www.mongodb.com/download-center 首先需要根据Windows版本选择正确的MongoDB版本进行安装 ...

  3. MongoDB 学习笔记一 安装以及基础命令

    一.MongoDB安装配置 1.获取最新版本号: wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.2.tgz 2.解压缩就可 ...

  4. MongoDB学习笔记~Mongo集群和副本集

    回到目录 一些概念 对于Mongo在数据容灾上,推荐的模式是使用副本集模式,它有一个对外的主服务器Primary,还有N个副本服务器Secondary(N>=1,当N=1时,需要有一台仲裁服务器 ...

  5. MongoDB学习笔记系列

    回到占占推荐博客索引 该来的总会来的,Ef,Redis,MVC甚至Sqlserver都有了自己的系列,MongoDB没有理由不去整理一下,这个系列都是平时在项目开发时总结出来的,希望可以为各位一些帮助 ...

  6. MongoDB学习笔记系列~目录

    MongoDB学习笔记~环境搭建 (2015-03-30 10:34) MongoDB学习笔记~MongoDBRepository仓储的实现 (2015-04-08 12:00) MongoDB学习笔 ...

  7. Failed global initialization:FileNotOpen: Failed to open "C:\MongoDB\data\log\mongo.log" 安装MongoDB时卡死

    在安装MongoDB的时候,下载了3.6版本,安装过程中发现到一半就卡死了,后面换了一个较低版本的才安装成功 这里是所有MongoDB版本的下载地址: https://www.mongodb.org/ ...

  8. MongoDB 学习记录(二)yum安装

    前言:接着上篇继续学习MongoDB,这次学习的是在Linux下安装MongoDB 环境:centos7.3 安装版本:MongoDB4.0 官网安装教程地址 https://docs.mongodb ...

  9. MongoDB学习(1)--安装,基本curd操作

    知识点: 1-MongoDB 安装,启动和卸载 2-基本概念 3-基本的增删改查操作(CURD) 来回顾总结一把学习的mongodb,如果有javascript基础,学习"芒果DB" ...

随机推荐

  1. 31深入理解C指针之---指针和字符串

    一.字符串与指针 1.定义:使用字符指针表示字符串 2.特征: 1).可以直接使用字符串字面量初始化字符指针 2).声明后,赋值就只能使用字符串操作函数strcpy函数赋值 3).可以使用类似于数组的 ...

  2. C#实现键盘钩子

    前言: 因为项目中需要使用到快捷键,所以上网找资料了解关于快捷键的实现技术,于是有了键盘钩子的使用学习.在网上了解到,键盘钩子其实只是很多种钩子中的其中一种.所谓钩子:请看下面关于钩子的描述(来自百度 ...

  3. LeetCode OJ——Convert Sorted List to Binary Search Tree

    http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 将一个按照元素升序排列的链表转换成BST.根据自身 ...

  4. Python Challenge 第二关

    第二关和第一关一样,还是一幅图和一行提示.提示说的是: recognize the characters. maybe they are in the book, but MAYBE they are ...

  5. Drupal 有用的模块

    投票模块drigg https://www.drupal.org/project/drigg

  6. JS-日历签到

    实现的功能: 首先这是前端显示的内容,没有后台的配置哈: 1.显示当前年月下的日历表: 2.今天的日期独有背景色: 3.当月今天之前的日子号数颜色变浅,表示日期已过: 4.点击日期签到:(只能点击当天 ...

  7. 洛谷——P2196 挖地雷

    题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径.当地窖及其连接的数据给出之后,某人可以从任一处 ...

  8. 信息收集渠道:文本分享类网站Paste Site

    信息收集渠道:文本分享类网站Paste Site Paste Site是一种专门的文本分享的网站.用户可以将一段文本性质的内容(如代码)上传到网站,然后通过链接分享给其他用户.这一点很类似于现在的优酷 ...

  9. luogu P3402 最长公共子序列

    题目背景 DJL为了避免成为一只咸鱼,来找Johann学习怎么求最长公共子序列. 题目描述 经过长时间的摸索和练习,DJL终于学会了怎么求LCS.Johann感觉DJL孺子可教,就给他布置了一个课后作 ...

  10. mybatis ----SqlSessionManager

    今天我们来看看这个类 有些写法还是很经典的 public class SqlSessionManager implements SqlSessionFactory, SqlSession { priv ...