mongodb的安装

https://repo.mongodb.org/yum/redhat/6Server/mongodb-org/3.4/x86_64/RPMS/

简介:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
一、简介
      mongodb是一个开源的,基于分布式的,面向文档存储的非关系型数据库。是非关系型数据库当中功能最丰富、最像关系数据库的。
      mongoDB由C++编写,其名字来源于"humongous"这个单词,其宗旨在于处理大量数据。
      mongoDB可以运行在Windows、unix、OSX、Solaris系统上,支持32位和64位应用,提供多种编程语言的驱动程序。
      mongoDB支持的数据结构非常松散,是类似json的BSON格式,通过键值对的形式存储数据,可以存储复杂的数据类型。
      mongoDB支持的数据类型有:null、boolean、String、objectId、32位整数、64位整数、64位浮点数、日期、正则表达式、js代码、二进制数据、数组、内嵌文档、最大值、最小值、未定义类型。
      其中,内嵌文档我理解的并不是.doc.txt等文件,这里所指的文档是mongoDB的一个存储单元(相当于关系型数据当中的记录),在mongoDB中的表现形式为{key1:value1,key2:value2},而内嵌文档则是这样的形式{key1:value1,key2:{key2.1:value2.1,key2.2:value2.2}}。
      mongoDB最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
      二、mongoDB的特性:
       1. 面向集合存储。数据被分组到若干集合,每个集合可以包含无限个文档,可以将集合想象成RDBMS的表,区别是集合不需要进行模式定义。
       2. 模式*。集合中没有行和列的概念,每个文档可以有不同的key,key的值不要求一致的数据类型。
       3. 支持动态查询。mongoDB支持丰富的查询表达式,查询指令使用json形式表达式。
       4. 完整的索引支持。mongoDB的查询优化器会分析查询表达式,并生成一个高效的查询计划。
       5. 高效的数据存储,支持二进制数据及大型对象(图片、视频等)。
       6. 支持复制和故障恢复。
       7. 自动分片以支持云级别的伸缩性,支持水平的数据库集群,可动态添加额外的服务器。
     三、 mongoDB的适用场景:
       1.  适合作为信息基础设施的持久化缓存层
        2. 适合实时的插入、更新与查询,并具备应用程序实时数据存储所需的复制及高度伸缩性
        3. 适合文档化格式的存储及查询
        4. 适合由数十或数百台服务器组成的数据库
      四、mongoDB不适用场景:
         1. 要求高度事务性的系统。例如对于银行或会计等需要大量原子性复杂事物的应用程序来说,还是需要关系型数据库的。
         2. 传统的商业智能应用
         3. 复杂的表级联查询

1.下载所有的rpm包

上传到服务器

1
2
3
4
5
6
7
8
9
[[email protected] ~]# ll
total 93528
-rw-------. 1 root root     1231 Feb 24 02:07 anaconda-ks.cfg
-rw-r--r--  1 root root     5900 Jul 22 06:42 mongodb-org-3.4.6-1.el6.x86_64.rpm
-rw-r--r--  1 root root 12182737 Jul 22 06:42 mongodb-org-mongos-3.4.6-1.el6.x86_64.rpm
-rw-r--r--  1 root root 20608908 Jul 22 06:42 mongodb-org-server-3.4.6-1.el6.x86_64.rpm
-rw-r--r--  1 root root 11767549 Jul 22 06:42 mongodb-org-shell-3.4.6-1.el6.x86_64.rpm
-rw-r--r--  1 root root 51195582 Jul 22 06:42 mongodb-org-tools-3.4.6-1.el6.x86_64.rpm

2.安装

1
yum  localinstall  mongodb-org-*

3.修改配置文件

1
vi /etc/mongod.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# mongod.conf
 
# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/
 
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /opt/mongo/log/mongo.log  #日志文件
 
# Where and how to store data.
storage:
  dbPath: /opt/mongo/data  #数据
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:
 
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /opt/mongo/mongopid/mongo.pid  # location of pidfile  #pid
 
# network interfaces
net:
  port: 62114 #端口和监听的IP
  bindIp: 192.168.56.15  # Listen to local interface only, comment to listen on all interfaces.
 
 
#security:
 
#operationProfiling:
 
#replication:
 
#sharding:
 
## Enterprise-Only Options
 
#auditLog:
 
#snmp:
 
 
chown  -R  mongod.mongod  /opt/mongo

4.启动mongo

1
systemctl  start  mongod

5.进入mongo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
mongo  --host  192.168.56.15  --port 62114
Server has startup warnings: 
2017-07-22T07:07:43.607+0800 I CONTROL  [initandlisten] 
2017-07-22T07:07:43.607+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-07-22T07:07:43.608+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-07-22T07:07:43.608+0800 I CONTROL  [initandlisten] 
2017-07-22T07:07:43.608+0800 I CONTROL  [initandlisten] 
2017-07-22T07:07:43.608+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-07-22T07:07:43.608+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-07-22T07:07:43.608+0800 I CONTROL  [initandlisten] 
2017-07-22T07:07:43.608+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-07-22T07:07:43.608+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-07-22T07:07:43.608+0800 I CONTROL  [initandlisten] 
2017-07-22T07:07:43.608+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 1024000 files. Number of processes should be at least 512000 : 0.5 times number of files.
2017-07-22T07:07:43.608+0800 I CONTROL  [initandlisten] 
 
解决办法
[[email protected] ~]# tail -n 4  /etc/security/limits.conf 
mongod soft nofile 64000
mongod hard nofile 64000
mongod soft nproc 32000
mongod hard nproc 32000
[[email protected] ~]# echo "never" >/sys/kernel/mm/transparent_hugepage/enabled
[[email protected] ~]# echo "never" >/sys/kernel/mm/transparent_hugepage/defrag 
 never 此处系统重启失效 
所以把这个两个echo 放在/etc/rc.local 

6.增加用户名和密码

1
2
3
4
增加超级管理员
mongo  --host  192.168.56.15  --port 62114 
 
db.createUser({user:"admin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})

7.退出 让mongo用户认证登陆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
vim  /etc/mongod.conf
security:    #打开注释
  authorization: enabled   #增加一行
 systemctl restart  mongod
 测试:
 [[email protected] ~]# mongo  --host  192.168.56.15  --port 62114  
MongoDB shell version v3.4.6
connecting to: mongodb://192.168.56.15:62114/
MongoDB server version: 3.4.6
> show  dbs;
2017-07-22T07:43:14.962+0800 E QUERY    [thread1] Error: listDatabases failed:{
    "ok" : 0,
    "errmsg" "not authorized on admin to execute command { listDatabases: 1.0 }",
    "code" : 13,
    "codeName" "Unauthorized"
} :
[email protected]/mongo/shell/utils.js:25:13
[email protected]/mongo/shell/mongo.js:62:1
[email protected]/mongo/shell/utils.js:769:19
[email protected]/mongo/shell/utils.js:659:15
@(shellhelp2):1:1
测试通过
[[email protected] ~]# mongo  --host  192.168.56.15  --port 62114    -u admin  -p 123456
MongoDB shell version v3.4.6
connecting to: mongodb://192.168.56.15:62114/
MongoDB server version: 3.4.6
> show dbs;
admin  0.000GB
local  0.000GB
>

8.管理员增加一个库,并赋予特定的用户(库是创建了 默认不写入东西看不见)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
> db.createUser({ user: "test",
...     pwd"123456",
...     roles:[{"role":"readWrite","db":"mall"},
...       {"role":"dbAdmin","db":"mall"},
...       {"role":"dbOwner","db":"mall"},
...       {"role":"read","db":"mall"}]})
Successfully added user: {
    "user" "test",
    "roles" : [
        {
            "role" "readWrite",
            "db" "mall"
        },
        {
            "role" "dbAdmin",
            "db" "mall"
        },
        {
            "role" "dbOwner",
            "db" "mall"
        },
        {
            "role" "read",
            "db" "mall"
        }
    ]
}
 
> show dbs;  #管理员登陆也看不见
admin  0.000GB
local  0.000GB
mongo  --host  192.168.56.15  --port 62114    -u test  -p 123456
> db.auth("test","123456")
1
> use  mall;  #test 用户登陆可以switch
switched to db mall
>

9.插入数据 用test用户登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
test用户
> db.inventory.insert( { _id: 10, type"misc", item: "card", qty: 15 } )
WriteResult({ "nInserted" : 1 })
admin用户  插入数据后可以看到了
[[email protected] ~]# mongo  --host  192.168.56.15  --port 62114    -u admin  -p 123456
MongoDB shell version v3.4.6
connecting to: mongodb://192.168.56.15:62114/
MongoDB server version: 3.4.6
> show dbs;
admin  0.000GB
local  0.000GB
mall   0.000GB
>

10服务没关闭 直接关闭服务器 导致mongodb启动不起来

mongodb的安装


解决办法:

1
2
3
4
[[email protected] mongopid]# mv  mongo.pid   mongo.pid.ori 
[[email protected] mongopid]# pwd
/opt/mongo/mongopid
[[email protected] mongopid]#

11.linux客户端连接mongo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
linux 客户端链接mongo只需要安装mongodb-org-shell-3.4.6-1.el6.x86_64.rpm 即可
我在192.168.56.102/24  安装这个包,并测试
yum  localinstall  mongodb-org-shell-3.4.6-1.el6.x86_64.rpm
 which mongo 
管理员测试没问题
[[email protected] ~]# mongo   --port  62114  --host 192.168.56.15   -u admin  -p 123456
MongoDB shell version v3.4.6
connecting to: mongodb://192.168.56.15:62114/
MongoDB server version: 3.4.6
> show dbs;
admin  0.000GB
local  0.000GB
mall   0.000GB
>

12.windows客户端连接  mongo server  这里采用mongoboster/robmongo

这次我链接的时候用用户名密码链接 链接不上auth认证失败

最后我把mongo改为非认证的方式 才登陆上去的  !具体在查下,应该是协议的问题

mongodb的安装

mongodb的安装

问题解决办法:

安装mongodb之后开启非认证模式(默认就是非认证模式)

①增加一个用户 看出加密算法和版本(SCRAM-SHA-1)

1
2
3
mongo  --host 192.168.56.15   -port 27017  
use admin
db.createUser({user:"admin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
1
2
3
4
可以看出加密算法
> db.system.users.find()
"_id" "admin.admin""user" "admin""db" "admin""credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" "SysMVW5zoTXdbp6P5oTJmQ==""storedKey" "M51sy/NCeM0sE0tKEGfmzslOCQY=""serverKey" "qB9CLecOEf3vhQxzJgTWiciu7+o=" } }, "roles" : [ { "role" "userAdminAnyDatabase""db" "admin" } ] }
>
1
2
3
4
5
可以看出版本currentVersion 5
> db.system.version.find()
"_id" "featureCompatibilityVersion""version" "3.4" }
"_id" "authSchema""currentVersion" : 5 }
>

②接着删除admin用户  ,不添加用户加密算法无法更新

1
db.system.users.remove({user:"admin"})

③更新版本

1
db.system.version.update({"_id":"authSchema"},{$set:{"currentVersion":3}})

④接着再次添加admin用户,可以看出算法已经改变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> use admin 
switched to db admin
> db.createUser({user:"admin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
Successfully added user: {
    "user" "admin",
    "roles" : [
        {
            "role" "userAdminAnyDatabase",
            "db" "admin"
        }
    ]
}
> db.system.users.find()
"_id" "admin.admin""user" "admin""db" "admin""credentials" : { "MONGODB-CR" "95ec4261124ba5951720b199908d892b" }, "roles" : [ { "role" "userAdminAnyDatabase""db" "admin" } ] }
>

⑤开启用户认证模式

1
2
3
4
vim  /etc/mongod.conf 
security:
  authorization: enabled
systemctl  restart mongod

⑥测试管理员能否登陆

1
2
3
4
5
6
7
8
[[email protected] ~]# mongo  --host 192.168.56.15   --authenticationDatabase  admin    -port 27017  -uadmin  -p123456
MongoDB shell version v3.4.6
connecting to: mongodb://192.168.56.15:27017/
MongoDB server version: 3.4.6
> show  dbs
admin  0.000GB
local  0.000GB
>

⑦新建一个test用户

1
2
3
4
5
6
7
8
9
10
11
12
use mall  #第一步先use
db.createUser({ user: "test",
    pwd"123456",
    roles:[{"role":"readWrite","db":"mall"},
      {"role":"dbAdmin","db":"mall"},
      {"role":"dbOwner","db":"mall"},
      {"role":"read","db":"mall"}]})
 插入一条数据方便查看,默认库里面没有数据,管理员也看不见这个库
  
 > use mall
switched to db mall
> db.inventory.insert( { _id: 10, type"misc", item: "card", qty: 15 } )

⑧测试test用户命令行是否可以链接

1
2
3
4
5
 [[email protected] ~]# mongo  --host 192.168.56.15   --authenticationDatabase  mall    -port 27017  -utest  -p123456
MongoDB shell version v3.4.6
connecting to: mongodb://192.168.56.15:27017/
MongoDB server version: 3.4.6
>

⑨windows客户端连接

mongodb的安装

可以看出可以了










本文转自 小小三郎1 51CTO博客,原文链接:http://blog.51cto.com/wsxxsl/1949913,如需转载请自行联系原作者