Linux下MongoDB的入门安装、配置与启动

mongo非关系性数据库 查询性能好,数据性不是特别重要的情况下使用

[root@test ~]# tar xf mongodb-linux-x86_64-rhel70-5.0.7.tgz
[root@test ~]# mv mongodb-linux-x86_64-rhel70-5.0.7 /usr/local/mongodb
[root@test ~]# cd /usr/local/mongodb
[root@test mongodb]# ls
bin  LICENSE-Community.txt  MPL-2  README  THIRD-PARTY-NOTICES
[root@test mongodb]# mkdir data logs
[root@test mongodb]# touch /usr/local/mongodb/logs/mongodb.log
[root@test mongodb]# vim /etc/mongodb.conf
dbpath=/usr/local/mongodb/data             #数据库路径
logpath=/usr/local/mongodb/logs/mongodb.log            #MongoDB日志文件
logappend=true         # 使用追加的方式写日志
port=27017         #端口号
bind_ip=0.0.0.0 #绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定则默认本地所有IP
fork=true # 以守护进程的方式运行MongoDB,创建服务器进程
#auth=true #启用用户验证
[root@test mongodb]# vim /etc/profile
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
[root@test mongodb]# source /etc/profile
[root@test ~]# mongod -f /etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1094
child process started successfully, parent exiting
[root@test ~]# mongo
# MongoDB 自带的交互式 Javascript shell,用来对 MongoDB 进行操作和管理的交互式环境。
MongoDB shell version v5.0.7
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("cd90aabc-f30e-4ea7-be0d-76c6039ffab8") }
MongoDB server version: 5.0.7
================
> use admin;
switched to db admin
> db.createUser({user:'root', pwd:'123456', roles:[{role:'root', db:'admin'}]});
Successfully added user: {"user" : "root","roles" : [{"role" : "root","db" : "admin"}]
}
> exit
bye
[root@test ~]# mongo
> use admin
switched to db admin
> db.createUser({ user: "test",pwd: "1234",customData:{name:"test"},roles:[{ role: "userAdminAnyDatabase",db: "admin" }]})
Successfully added user: {              //创建带密码的角色test
> db.auth('test','12345')
Error: Authentication failed.
0
> db.auth('test','1234')
1
> mongodb://test:1234@localhost/test          //使用用户名和密码连接登录到指定数据库
...

MongoDB

数据库创建删除

> use testdb              //如果数据库不存在,则创建数据库
switched to db testdb
> db             //当前库
testdb
> show dbs               //查看所有数据库,插入数据时才会显示创建的库
admin   0.000GB
config  0.000GB
local   0.000GB
> db.testdb.insert({"name":"dachui"})         //插入数据
WriteResult({ "nInserted" : 1 })
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
testdb  0.000GB             //新增的库
> use testdb              //选中库
switched to db testdb
> db.dropDatabase()          //删除库
{ "ok" : 1 }
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

集合创建删除

> use testdb
switched to db testdb
> db.createCollection("table")         //创建集合,注意区分大小写
{ "ok" : 1 }
> show collections               //查看集合
table
> use table              //使用集合
switched to db table

创建带有参数的 createCollection()

> db.createCollection("mycol",{capped:true,autoIndexId:true,size:10000,max:9999})
{"note" : "The autoIndexId option is deprecated and will be removed in a future release","ok" : 1
}

可以是以下参数:

字段 类型 描述
capped 布尔 (可选)如果为 true,则创建固定集合。固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。 当该值为 true 时,必须指定 size 参数。
autoIndexId 布尔 3.2 之后不再支持该参数。(可选)如为 true,自动在 _id 字段创建索引。默认为 false。
size 数值 (可选)为固定集合指定一个最大值,即字节数。 如果 capped 为 true,也需要指定该字段。
max 数值 (可选)指定固定集合中包含文档的最大数量。
> use testdb
switched to db testdb
> show collections           //查看
table
> db.table.drop()            //删除集合table
true
> show collections

插入文档

> db.mycol.insert({name:"dachui",sex:"boy"})        //插入数据
WriteResult({ "nInserted" : 1 })
> show collections
mycol
> db.mycol.find()                //查看数据
{ "_id" : ObjectId("62676e80a3c1f22ef0404878"), "name" : "dachui", "sex" : "boy" }

Linux下Mongodb的主从配置

主从配置方法

1. 主机器(master)  192.168.1.11
2. 从机器(slave)  192.168.1.12
mongodb home: /usr/local/mongodb/
data file: /usr/local/mongodb/data
log file: /usr/local/mongodb/logs3. 启动master
mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs
--master --oplogSize 64 --logappend  --port=27017 --fork4. 启动slave
mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs
--slave --source 192.168.1.11 --only test --slavedelay 10
--logappend  --port=27017 --fork

互为主从

启动master
mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs   \
--master --slave --source 192.168.1.12   --logappend  --port=27017 --fork启动slave
mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs   \
--master --slave --source 192.168.1.11 --only test --slavedelay 10    \
--logappend  --port=27017 --fork测试
在主机器上添加数据
db.foo.save({"id":123456,"name":'dachui'});在从服务器看数据:
db.foo.find({"id":123})

Replica Pairs的配置方法

master
cd /usr/local/mongodb/
./bin/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs\
--pairwith 192.168.1.11 --arbiter <arbiterserver> --logappend  --port=27017 --fork4. 启动slave
cd /usr/local/mongodb/
./bin/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs\
--pairwith 192.168.1.12 --arbiter <arbiterserver> --logappend  --port=27017 --fork
三 参数解释参数解释: --dbpath 数据库路径(数据文件)
--logpath 日志文件路径
--master 指定为主机器
--slave 指定为从机器
--source 指定主机器的IP地址
--pologSize 命令行参数(与--master一同使用)配置用于存储给从节点可用的更新信息占用的磁盘空间(M为单位),如果不指定这个参数,默认大小为当前可用磁盘空间的5%(64位机器最小值为1G,32位机器为50M)。
--logappend 日志文件末尾添加
--port 启用端口号
--fork 在后台运行
--only 指定只复制哪一个数据库
--slavedelay 指从复制检测的时间间隔
--auth 是否需要验证权限登录(用户名和密码)-h [ --help ]             show this usage information
--version                 show version information
-f [ --config ] arg       configuration file specifying additional options
--port arg                specify port number
--bind_ip arg             local ip address to bind listener - all local ipsbound by default
-v [ --verbose ]          be more verbose (include multiple times for moreverbosity e.g. -vvvvv)
--dbpath arg (=/data/db/) directory for datafiles    指定数据存放目录
--quiet                   quieter output   静默模式
--logpath arg             file to send all output to instead of stdout   指定日志存放目录
--logappend               appnd to logpath instead of over-writing 指定日志是以追加还是以覆盖的方式写入日志文件
--fork                    fork server process   以创建子进程的方式运行
--cpu                     periodically show cpu and iowait utilization 周期性的显示cpu和io的使用情况
--noauth                  run without security 无认证模式运行
--auth                    run with security 认证模式运行
--objcheck                inspect client data for validity on receipt 检查客户端输入数据的有效性检查
--quota                   enable db quota management   开始数据库配额的管理
--quotaFiles arg          number of files allower per db, requires --quota 规定每个数据库允许的文件数
--appsrvpath arg          root directory for the babble app server
--nocursors               diagnostic/debugging option 调试诊断选项
--nohints                 ignore query hints 忽略查询命中率
--nohttpinterface         disable http interface 关闭http接口,默认是28017
--noscripting             disable scripting engine 关闭脚本引擎
--noprealloc              disable data file preallocation 关闭数据库文件大小预分配
--smallfiles              use a smaller default file size 使用较小的默认文件大小
--nssize arg (=16)        .ns file size (in MB) for new databases 新数据库ns文件的默认大小
--diaglog arg             0=off 1=W 2=R 3=both 7=W+some reads 提供的方式,是只读,只写,还是读写都行,还是主要写+部分的读模式
--sysinfo                 print some diagnostic system information 打印系统诊断信息
--upgrade                 upgrade db if needed 如果需要就更新数据库
--repair                  run repair on all dbs 修复所有的数据库
--notablescan             do not allow table scans 不运行表扫描
--syncdelay arg (=60)     seconds between disk syncs (0 for never) 系统同步刷新磁盘的时间,默认是60sReplication options:
--master              master mode 主复制模式
--slave               slave mode 从复制模式
--source arg          when slave: specify master as <server:port> 当为从时,指定主的地址和端口
--only arg            when slave: specify a single database to replicate 当为从时,指定需要从主复制的单一库
--pairwith arg        address of server to pair with
--arbiter arg         address of arbiter server 仲裁服务器,在主主中和pair中用到
--autoresync          automatically resync if slave data is stale 自动同步从的数据
--oplogSize arg       size limit (in MB) for op log 指定操作日志的大小
--opIdMem arg         size limit (in bytes) for in memory storage of op ids指定存储操作日志的内存大小Sharding options:
--configsvr           declare this is a config db of a cluster 指定shard中的配置服务器
--shardsvr            declare this is a shard db of a cluster 指定shard服务器

Linux下MongoDB的入门安装、配置与启动相关推荐

  1. Windows和Linux下apache-artemis-2.10.0安装配置

    window下安装配置 一.官网下载 http://activemq.apache.org/artemis/download.html 二.百度网盘下载 链接:https://pan.baidu.co ...

  2. linux下svn服务器的安装配置和使用

    2019独角兽企业重金招聘Python工程师标准>>> 安装环境: centos 一,安装必须的软件包. yum install subversion mod_dav_svn 二,基 ...

  3. linux下的openmeetings的安装配置…

    linux下openmeetings安装配置方法 目录 目录... 1 硬件条件:... 2 需要的相关软件:... 3 安装配置... 3 第一步       安装mysql数据库... 3 1.1 ...

  4. linux下xampp的安装和配置文件,linux下xampp集成包安装配置方法

    1.查看你linux系统的位数,是32位的还是64位的.使用uname -a命令查看. 显示有 x86_64则说明你是64位内核, 跑的是64位的系统. i386, i686说明你是32位的内核, 跑 ...

  5. linux下c 链接mongodb,Linux下mongoDB下载与安装

    百度网盘下载:https://pan.baidu.com/s/1r0JoOtoYzJEC_HOe-NALwg 提取码:rm12 此处提供的是mongodb-linux-x86_64-4.0.11.tg ...

  6. 如何在虚拟linux环境运行python_Python 虚拟环境 | Mac/Linux下如何避坑安装配置Virtualenv...

    1.为什么要使用虚拟环境 在Python中,不同的应用可能需要用到不同版本的第三方包,而这些第三方包被统一存放到目录site-packages中,不同版本的包容易相互覆盖,如安装Django 2.1时 ...

  7. 【教程】Linux下MySQL 8.0安装配置

    1.编译安装MySQL8.0 版本信息 #cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) 安装依赖包 yum -y insta ...

  8. Linux下svn服务端安装配置

    早上看到一篇文章把VC(版本控制)讲的很好,狂神聊Git,SVN是集中式版本控制中心,git是分布式版本控制,Svn配置和使用: 安装svn yum -y install subversion 输入s ...

  9. linux vbox安装mac os,超简单的linux下virtualbox4.3.26安装配置黑苹果 OSX 10.9的办法

    为了在虚拟机上装个osx,前前后后折腾了7天,终于搞定了. 发现虚拟机装osx有个最大的好处是,可以直接下载别人现成的虚拟机镜像,我就是在屡屡失败后,下载了个vdi镜像了事,虽然比较偷懒,但对于没时间 ...

最新文章

  1. 【ACM】POJ 3069
  2. 用脑电波玩游戏,这款VR体验逆天了
  3. C++11 正则表达式——实例1
  4. python代码需要背吗-Python代码需要缩进吗
  5. Python教程:json中encode与decode区别
  6. 史上最难逻辑题!据说99.9%的人都做不出来……
  7. python 获取系统相关编码的函数
  8. python的使用说明_Python 的基本使用说明
  9. 数据结构之字典序全排列
  10. 【codevs1246】丑数,STL与取模大质数的好处
  11. 什么是利用计算机化的知识进行自动推理,基于实例模型的知识推理及其在自动阅卷系统中的应用...
  12. power bi图表_Power BI中的图表类型概述
  13. igs无法分配驱动器映射表_左神算法基础:哈希函数和哈希表
  14. 【React】 lazy 和 Suspense
  15. Dynamic Programming(1)
  16. Java将一张图片放在另一张图片上(位置可选)
  17. Nginx目录结构、编译参数、状态码概述
  18. 全网详细解决:无法将 “xxx” 项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次
  19. 寒假日报(1.18)
  20. 淘宝 商品 货品 sku 科目 分类

热门文章

  1. 黑苹果热补丁hotpatch来禁用笔记本独显
  2. [Jquery]天气接口简单使用
  3. 16进制与byte的转换
  4. 语音识别基础算法——动态时间规整算法
  5. windows 任务栏桌面消失怎么办
  6. vue项目 打包文件大小分析
  7. Tracert 抓包测试
  8. FPGA交通灯 Verilog Modelsim
  9. blt功能_C++中BitBlt的使用方法详解
  10. 3.28 将文字转换为路径并进行艺术再加工 [原创Ps教程]