学习笔记0716----NOSQL之mongdb

mongdb

  • 1. mongodb简介
    • 1.1 mongodb介绍
    • 1.2 MongoDB和关系型数据库对比
    • 1.3 关系型数据库和mongodb数据结构对比
  • 2. mongodb安装
  • 3. mongodb的连接方法
  • 4. mongodb的用户管理
  • 5. mongodb创建集合
  • 6. php设置mongodb扩展
  • 7. php设置mongo扩展
  • 8. 测试mongo扩展
  • 9. MongoDB副本集
    • 9.1 副本集介绍
    • 9.2 副本集搭建
    • 9.3 副本集测试
    • 9.4 副本集更改权重模拟主宕机
  • 10. mongodb分片介绍
  • 11. mongodb分片搭建
    • 11.1 分片搭建–服务器规划
    • 11.2 分片搭建–创建目录
    • 11.3 分片搭建–config server配置
    • 11.4 分片搭建–启动config server
    • 11.5 分片搭建–分片配置
    • 11.6 分片搭建–启动分片
    • 11.7 分片搭建–配置路由服务器
    • 11.8 分片搭建–启用分片
    • 11.9 分片测试
  • 12. mongodb的备份和恢复
    • 12.1 备份
    • 12.2 恢复

1. mongodb简介

1.1 mongodb介绍

  • memcached和redis都是同一种nosql数据库,mongodb是一种文档型数据库
  • mongodb的官网www.mongodb.com,目前最新版本是4.0。
  • 在NoSQL中是最像关系型数据库的
  • MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档、数组及文档数组。
  • 关于JSON http://www.w3school.com.cn/json/index.asp
  • 因为基于分布式,所以很容易扩展

1.2 MongoDB和关系型数据库对比

学习笔记0716----NOSQL之mongdb_第1张图片

1.3 关系型数据库和mongodb数据结构对比

学习笔记0716----NOSQL之mongdb_第2张图片
学习笔记0716----NOSQL之mongdb_第3张图片

2. mongodb安装

官方安装文档 https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ 增加一个mongodb的yum源

[root@linux-02 SOURCES]# cd /etc/yum.repos.d/
[root@linux-02 yum.repos.d]# ls
Centos-7.repo     CentOS-Base.repo.bak  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo  epel-testing.repo  zabbix.repo
CentOS-Base.repo  CentOS-CR.repo        CentOS-fasttrack.repo  CentOS-Sources.repo  epel.repo          mariadb.repo
[root@linux-02 yum.repos.d]#
[root@linux-02 yum.repos.d]# vim mongodb-org-4.0.repo
[root@linux-02 yum.repos.d]# vim mongodb-org-4.0.repo

[mongodb-org-4.0]
name = MongoDB Repository
baseurl = https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck = 1
enabled = 1
gpgkey = https://www.mongodb.org/static/pgp/server-4.0.asc

[root@linux-02 yum.repos.d]# yum list |grep mongodb
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository contrib is listed more than once in the configuration
collectd-write_mongodb.x86_64           5.8.1-1.el7                    epel
mongodb.x86_64                          2.6.12-6.el7                   epel
mongodb-org.x86_64                      4.0.10-1.el7                   mongodb-org-4.0
mongodb-org-mongos.x86_64               4.0.10-1.el7                   mongodb-org-4.0
mongodb-org-server.x86_64               4.0.10-1.el7                   mongodb-org-4.0
mongodb-org-shell.x86_64                4.0.10-1.el7                   mongodb-org-4.0
mongodb-org-tools.x86_64                4.0.10-1.el7                   mongodb-org-4.0
mongodb-server.x86_64                   2.6.12-6.el7                   epel
mongodb-test.x86_64                     2.6.12-6.el7                   epel
nodejs-mongodb.noarch                   1.4.7-1.el7                    epel
php-mongodb.noarch                      1.0.4-1.el7                    epel
php-pecl-mongodb.x86_64                 1.1.10-1.el7                   epel
poco-mongodb.x86_64                     1.6.1-3.el7                    epel
syslog-ng-mongodb.x86_64                3.5.6-3.el7                    epel
[root@linux-02 yum.repos.d]# yum install -y mongodb-org
已加载插件:fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository contrib is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 mongodb-org.x86_64.0.4.0.10-1.el7 将被 安装
--> 正在处理依赖关系 mongodb-org-tools = 4.0.10,它被软件包 mongodb-org-4.0.10-1.el7.x86_64 需要
--> 正在处理依赖关系 mongodb-org-shell = 4.0.10,它被软件包 mongodb-org-4.0.10-1.el7.x86_64 需要
--> 正在处理依赖关系 mongodb-org-server = 4.0.10,它被软件包 mongodb-org-4.0.10-1.el7.x86_64 需要
--> 正在处理依赖关系 mongodb-org-mongos = 4.0.10,它被软件包 mongodb-org-4.0.10-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 mongodb-org-mongos.x86_64.0.4.0.10-1.el7 将被 安装
---> 软件包 mongodb-org-server.x86_64.0.4.0.10-1.el7 将被 安装
---> 软件包 mongodb-org-shell.x86_64.0.4.0.10-1.el7 将被 安装
---> 软件包 mongodb-org-tools.x86_64.0.4.0.10-1.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

===========================================================================================================================================================================================
 Package                                           架构                                  版本                                         源                                              大小
===========================================================================================================================================================================================
正在安装:
 mongodb-org                                       x86_64                                4.0.10-1.el7                                 mongodb-org-4.0                                5.8 k
为依赖而安装:
 mongodb-org-mongos                                x86_64                                4.0.10-1.el7                                 mongodb-org-4.0                                 12 M
 mongodb-org-server                                x86_64                                4.0.10-1.el7                                 mongodb-org-4.0                                 21 M
 mongodb-org-shell                                 x86_64                                4.0.10-1.el7                                 mongodb-org-4.0                                 13 M
 mongodb-org-tools                                 x86_64                                4.0.10-1.el7                                 mongodb-org-4.0                                 46 M

事务概要
===========================================================================================================================================================================================
安装  1 软件包 (+4 依赖软件包)

总下载量:93 M
安装大小:257 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/mongodb-org-4.0/packages/mongodb-org-4.0.10-1.el7.x86_64.rpm: 头V3 RSA/SHA1 Signature, 密钥 ID e52529d4: NOKEY             ] 111 kB/s | 127 kB  00:14:17 ETA
mongodb-org-4.0.10-1.el7.x86_64.rpm 的公钥尚未安装
(1/5): mongodb-org-4.0.10-1.el7.x86_64.rpm                                                                                                                          | 5.8 kB  00:00:08
(2/5): mongodb-org-mongos-4.0.10-1.el7.x86_64.rpm                                                                                                                   |  12 MB  00:00:57
(3/5): mongodb-org-shell-4.0.10-1.el7.x86_64.rpm                                                                                                                    |  13 MB  00:01:37
(4/5): mongodb-org-server-4.0.10-1.el7.x86_64.rpm                                                                                                                   |  21 MB  00:03:49
(5/5): mongodb-org-tools-4.0.10-1.el7.x86_64.rpm                                                                                                                    |  46 MB  00:07:06
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                                       164 kB/s |  93 MB  00:09:41
从 https://www.mongodb.org/static/pgp/server-4.0.asc 检索密钥
导入 GPG key 0xE52529D4:
 用户ID     : "MongoDB 4.0 Release Signing Key "
 指纹       : 9da3 1620 334b d75d 9dcb 49f3 6881 8c72 e525 29d4
 来自       : https://www.mongodb.org/static/pgp/server-4.0.asc
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : mongodb-org-shell-4.0.10-1.el7.x86_64                                                                                                                                  1/5
  正在安装    : mongodb-org-mongos-4.0.10-1.el7.x86_64                                                                                                                                 2/5
  正在安装    : mongodb-org-tools-4.0.10-1.el7.x86_64                                                                                                                                  3/5
  正在安装    : mongodb-org-server-4.0.10-1.el7.x86_64                                                                                                                                 4/5
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
  正在安装    : mongodb-org-4.0.10-1.el7.x86_64                                                                                                                                        5/5
  验证中      : mongodb-org-4.0.10-1.el7.x86_64                                                                                                                                        1/5
  验证中      : mongodb-org-server-4.0.10-1.el7.x86_64                                                                                                                                 2/5
  验证中      : mongodb-org-tools-4.0.10-1.el7.x86_64                                                                                                                                  3/5
  验证中      : mongodb-org-mongos-4.0.10-1.el7.x86_64                                                                                                                                 4/5
  验证中      : mongodb-org-shell-4.0.10-1.el7.x86_64                                                                                                                                  5/5

已安装:
  mongodb-org.x86_64 0:4.0.10-1.el7

作为依赖被安装:
  mongodb-org-mongos.x86_64 0:4.0.10-1.el7       mongodb-org-server.x86_64 0:4.0.10-1.el7       mongodb-org-shell.x86_64 0:4.0.10-1.el7       mongodb-org-tools.x86_64 0:4.0.10-1.el7

完毕!

3. mongodb的连接方法

systemctlstartmongod//启动服务
在本机可以直接运行命令mongo进入到mongodbshell中
如果mongodb监听端口并不是默认的27017,则在连接的时候需要加–port选项,例如
mongo–port27018
连接远程mongodb,需要加–host,例如
mongo–host127.0.0.1
如果设置了验证,则在连接的时候需要带用户名和密码
mongo-uusername-ppasswd–authenticationDatabasedb//这个和MySQL挺像

[root@linux-02 ~]# systemctl start mongod.service
[root@linux-02 ~]# ps aux |grep mongodb
root      37750  0.0  0.1 112724   984 pts/1    R+   07:32   0:00 grep --color=auto mongodb
[root@linux-02 ~]# ps aux |grep mongo
mongod    37722  3.6  6.1 1079736 50924 ?       Sl   07:32   0:00 /usr/bin/mongod -f /etc/mongod.conf
root      37752  0.0  0.1 112724   988 pts/1    S+   07:32   0:00 grep --color=auto mongo
[root@linux-02 ~]# netstat -lntp |grep mong
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      37722/mongod
[root@linux-02 ~]# mongo
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("07c1fa17-e3a2-48d7-bb18-567dde6c346f") }
MongoDB server version: 4.0.10
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten]
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten]
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten]
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten]
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 
bye

4. mongodb的用户管理

use admin//需要切换到admin库
db.createUser( { user: “admin”, customData: {description: “superuser”}, pwd: “admin122”, roles: [ { role: “root”, db: “admin” } ] } )
user指定用户,customData为说明字段,可以省略,pwd为密码,roles指定用户的角色,db指定库名
use admin //切换到admin库
db.system.users.find() //列出所有用户,需要切换到admin库
show users //查看当前库下所有的用户
db.dropUser(‘admin’) //删除用户
若要用户生效,还需要编辑启动脚本vim /usr/lib/systemd/system/mongod.service,在OPTIONS=后面增–auth
重启服务systemctl restart mongod
mongo -u “admin” -p “admin122” --authenticationDatabase “admin”

[root@linux-02 ~]# mongo
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("52bf6366-b35b-4178-ad24-814f32eca061") }
MongoDB server version: 4.0.10
Server has startup warnings:
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten]
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten]
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten]
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten]
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-17T07:32:19.977+0800 I CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> use admin
switched to db admin
> db.createUser( { user: "admin", customData: {description: "superuser"}, pwd: "123qwe", roles: [ { role: "root", db: "admin" } ] } )
2019-07-17T07:56:24.036+0800 E QUERY    [js] Error: couldn't add user: User "admin@admin" already exists :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1491:15
@(shell):1:1
> show users
{
	"_id" : "admin.admin",
	"userId" : UUID("0fadfb0e-5580-4bc3-b906-e31d834f3df1"),
	"user" : "admin",
	"db" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}
> db.changeUserPassword("admin","123qwe")     //默认系统已经创建了admin的用户,用此命令修改admin的密码
> use testdb    //如果系统中没有此数据库,会自动创建这个数据库
switched to db testdb
> db.createUser( { user: "test", customData: {description: "superuser"}, pwd: "123qwe", roles: [ { role: "read", db: "testdb" } ] } )
Successfully added user: {
	"user" : "test",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "read",
			"db" : "testdb"
		}
	]
}
> show users
{
	"_id" : "testdb.test",
	"userId" : UUID("f66f04a4-9050-4aae-8624-d2a681a306b1"),
	"user" : "test",
	"db" : "testdb",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "read",
			"db" : "testdb"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}
>exit
bye

修改vim /usr/lib/systemd/system/mongod.service文件,如下图,上图我们已经创建新用户,使用新用户登录mongdb。
学习笔记0716----NOSQL之mongdb_第4张图片
使用创建好的用户和密码测试登录。

[root@linux-02 ~]# systemctl daemon-reload
[root@linux-02 ~]# systemctl restart mongod.service
[root@linux-02 ~]# mongo -u "admin" -p "123qwe"  --authenticationDatabase "admin"
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ffbd6b73-b3e6-4689-b1fb-c2c788af53c5") }
MongoDB server version: 4.0.10
Server has startup warnings:
2019-07-17T08:12:58.475+0800 I CONTROL  [initandlisten]
2019-07-17T08:12:58.475+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-07-17T08:12:58.475+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-17T08:12:58.475+0800 I CONTROL  [initandlisten]
2019-07-17T08:12:58.475+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-07-17T08:12:58.475+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-17T08:12:58.475+0800 I CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---user

>exit
bye


##我们使用创建的test用户登录mongodb,发现无法访问admin数据库的users ##
[root@linux-02 ~]# mongo -u test  -p "123qwe"  --authenticationDatabase "testdb"
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?authSource=testdb&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("078bd9f7-3f6f-490b-ac95-7a9c93f2f15d") }
MongoDB server version: 4.0.10
> use admin
switched to db admin
> show users
2019-07-17T08:05:39.615+0800 E QUERY    [js] Error: not authorized on admin to execute command { usersInfo: 1.0, lsid: { id: UUID("078bd9f7-3f6f-490b-ac95-7a9c93f2f15d") }, $db: "admin" } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1763:1
shellHelper.show@src/mongo/shell/utils.js:859:9
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
> exit
bye

use db1
db.createUser( { user: “test1”, pwd: “123aaa”, roles: [ { role: “readWrite”, db: “db1” }, {role: “read”, db: “db2” } ] } )
test1用户对db1库读写,对db2库只读。
之所以先use db1,表示用户在 db1 库中创建,就一定要db1库验证身份,即用户的信息跟随随数据库。比如上述 test1虽然有 db2 库的读取权限,但是一定要先在db1库进行身份验证,直接访问会提示验证失败。
use db1
db.auth(“test1”, “123aaa”)

学习笔记0716----NOSQL之mongdb_第5张图片

5. mongodb创建集合

db.createCollection(“mycol”, { capped : true, size : 6142800, max : 10000 } ) //语法:db.createCollection(name,options)
name就是集合的名字,options可选,用来配置集合的参数,参数如下
capped true/false (可选)如果为true,则启用封顶集合。封顶集合是固定大小的集合,当它达到其最大大小,会自动覆盖最早的条目。如果指定true,则也需要指定尺寸参数。
autoindexID true/false (可选)如果为true,自动创建索引_id字段的默认值是false。
size (可选)指定最大大小字节封顶集合。如果封顶如果是 true,那么你还需要指定这个字段。单位B
max (可选)指定封顶集合允许在文件的最大数量。
show collections //查看集合,或者使用show tables
db.Account.insert({AccountID:1,UserName:“123”,password:“123456”}) //如果集合不存在,直接插入数据,则mongodb会自动创建集合
db.Account.update({AccountID:1},{"$set":{“Age”:20}}) //更新
db.Account.find() //查看所有文档
db.Account.find({AccountID:1}) //根据条件查询
db.Account.remove({AccountID:1}) //根据条件删除
db.Account.drop() //删除所有文档,即删除集合
use dbname //先进入对应的库
db.printCollectionStats() // 然后查看集合状态

[root@linux-02 ~]# mongo -u admin -p 123qwe
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("040120ed-08f3-43cd-a768-cbe4a38f4e60") }
MongoDB server version: 4.0.10
Server has startup warnings:
2019-07-18T03:58:18.731+0800 I CONTROL  [initandlisten]
2019-07-18T03:58:18.731+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-07-18T03:58:18.731+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-18T03:58:18.731+0800 I CONTROL  [initandlisten]
2019-07-18T03:58:18.731+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-07-18T03:58:18.731+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-18T03:58:18.731+0800 I CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> use db1
switched to db db1
>  db.createUser( { user: "test", customData: {description: "superuser"}, pwd: "123qwe", roles: [ { role: "readWrite", db: "db1" } ] } )
Successfully added user: {
	"user" : "test",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "db1"
		}
	]
}
>  db.createCollection("mycol", { capped : true, size : 6142800, max : 10000 } )
{ "ok" : 1 }
> show tables
mycol
> show collections
mycol
>  db.Account.insert({AccountID:1,UserName:"123",password:"123456"})
WriteResult({ "nInserted" : 1 })
> show collections
Account
mycol
> db.Account.insert({AccountID:2,UserName:"456",password:"123456"})
WriteResult({ "nInserted" : 1 })
> show collections
Account
mycol
>  db.Account.update({AccountID:1},{"$set":{"Age":20}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>  db.Account.find()
{ "_id" : ObjectId("5d2f7fc6707406cb96dd04e1"), "AccountID" : 1, "UserName" : "123", "password" : "123456", "Age" : 20 }
{ "_id" : ObjectId("5d2f7ff3707406cb96dd04e2"), "AccountID" : 2, "UserName" : "456", "password" : "123456" }
> db.Account.find({AccountID:1})
{ "_id" : ObjectId("5d2f7fc6707406cb96dd04e1"), "AccountID" : 1, "UserName" : "123", "password" : "123456", "Age" : 20 }
> db.Account.remove({AccountID:1})
WriteResult({ "nRemoved" : 1 })
> db.Account.find()
{ "_id" : ObjectId("5d2f7ff3707406cb96dd04e2"), "AccountID" : 2, "UserName" : "456", "password" : "123456" }
>  db.Account.drop()
true
> show tables
mycol
>  db.printCollectionStats()
mycol
{
	"ns" : "db1.mycol",
	"size" : 0,
	"count" : 0,
	"storageSize" : 4096,
	"capped" : true,
	"max" : 10000,
	"maxSize" : 6142976,
	"sleepCount" : 0,
	"sleepMS" : 0,
	"wiredTiger" : {
		"metadata" : {
			"formatVersion" : 1
		},
		"creationString" : "access_pattern_hint=none,allocation_size=4KB,app_metadata=(formatVersion=1),assert=(commit_timestamp=none,read_timestamp=none),block_allocation=best,block_compressor=snappy,cache_resident=false,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=false,extractor=,format=btree,huffman_key=,huffman_value=,ignore_in_memory_cache_size=false,immutable=false,internal_item_max=0,internal_key_max=0,internal_key_truncate=true,internal_page_max=4KB,key_format=q,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=32KB,leaf_value_max=64MB,log=(enabled=true),lsm=(auto_throttle=true,bloom=true,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=false,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_custom=(prefix=,start_generation=0,suffix=),merge_max=15,merge_min=0),memory_page_image_max=0,memory_page_max=10m,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=false,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=90,type=file,value_format=u",
		"type" : "file",
		"uri" : "statistics:table:collection-0-4559356897001839117",
		"LSM" : {
			"bloom filter false positives" : 0,
			"bloom filter hits" : 0,
			"bloom filter misses" : 0,
			"bloom filter pages evicted from cache" : 0,
			"bloom filter pages read into cache" : 0,
			"bloom filters in the LSM tree" : 0,
			"chunks in the LSM tree" : 0,
			"highest merge generation in the LSM tree" : 0,
			"queries that could have benefited from a Bloom filter that did not exist" : 0,
			"sleep for LSM checkpoint throttle" : 0,
			"sleep for LSM merge throttle" : 0,
			"total size of bloom filters" : 0
		},
		"block-manager" : {
			"allocations requiring file extension" : 0,
			"blocks allocated" : 0,
			"blocks freed" : 0,
			"checkpoint size" : 0,
			"file allocation unit size" : 4096,
			"file bytes available for reuse" : 0,
			"file magic number" : 120897,
			"file major version number" : 1,
			"file size in bytes" : 4096,
			"minor version number" : 0
		},
		"btree" : {
			"btree checkpoint generation" : 15,
			"column-store fixed-size leaf pages" : 0,
			"column-store internal pages" : 0,
			"column-store variable-size RLE encoded values" : 0,
			"column-store variable-size deleted values" : 0,
			"column-store variable-size leaf pages" : 0,
			"fixed-record size" : 0,
			"maximum internal page key size" : 368,
			"maximum internal page size" : 4096,
			"maximum leaf page key size" : 2867,
			"maximum leaf page size" : 32768,
			"maximum leaf page value size" : 67108864,
			"maximum tree depth" : 0,
			"number of key/value pairs" : 0,
			"overflow pages" : 0,
			"pages rewritten by compaction" : 0,
			"row-store internal pages" : 0,
			"row-store leaf pages" : 0
		},
		"cache" : {
			"bytes currently in the cache" : 182,
			"bytes dirty in the cache cumulative" : 0,
			"bytes read into cache" : 0,
			"bytes written from cache" : 0,
			"checkpoint blocked page eviction" : 0,
			"data source pages selected for eviction unable to be evicted" : 0,
			"eviction walk passes of a file" : 0,
			"eviction walk target pages histogram - 0-9" : 0,
			"eviction walk target pages histogram - 10-31" : 0,
			"eviction walk target pages histogram - 128 and higher" : 0,
			"eviction walk target pages histogram - 32-63" : 0,
			"eviction walk target pages histogram - 64-128" : 0,
			"eviction walks abandoned" : 0,
			"eviction walks gave up because they restarted their walk twice" : 0,
			"eviction walks gave up because they saw too many pages and found no candidates" : 0,
			"eviction walks gave up because they saw too many pages and found too few candidates" : 0,
			"eviction walks reached end of tree" : 0,
			"eviction walks started from root of tree" : 0,
			"eviction walks started from saved location in tree" : 0,
			"hazard pointer blocked page eviction" : 0,
			"in-memory page passed criteria to be split" : 0,
			"in-memory page splits" : 0,
			"internal pages evicted" : 0,
			"internal pages split during eviction" : 0,
			"leaf pages split during eviction" : 0,
			"modified pages evicted" : 0,
			"overflow pages read into cache" : 0,
			"page split during eviction deepened the tree" : 0,
			"page written requiring cache overflow records" : 0,
			"pages read into cache" : 0,
			"pages read into cache after truncate" : 0,
			"pages read into cache after truncate in prepare state" : 0,
			"pages read into cache requiring cache overflow entries" : 0,
			"pages requested from the cache" : 0,
			"pages seen by eviction walk" : 0,
			"pages written from cache" : 0,
			"pages written requiring in-memory restoration" : 0,
			"tracked dirty bytes in the cache" : 0,
			"unmodified pages evicted" : 0
		},
		"cache_walk" : {
			"Average difference between current eviction generation when the page was last considered" : 0,
			"Average on-disk page image size seen" : 0,
			"Average time in cache for pages that have been visited by the eviction server" : 0,
			"Average time in cache for pages that have not been visited by the eviction server" : 0,
			"Clean pages currently in cache" : 0,
			"Current eviction generation" : 0,
			"Dirty pages currently in cache" : 0,
			"Entries in the root page" : 0,
			"Internal pages currently in cache" : 0,
			"Leaf pages currently in cache" : 0,
			"Maximum difference between current eviction generation when the page was last considered" : 0,
			"Maximum page size seen" : 0,
			"Minimum on-disk page image size seen" : 0,
			"Number of pages never visited by eviction server" : 0,
			"On-disk page image sizes smaller than a single allocation unit" : 0,
			"Pages created in memory and never written" : 0,
			"Pages currently queued for eviction" : 0,
			"Pages that could not be queued for eviction" : 0,
			"Refs skipped during cache traversal" : 0,
			"Size of the root page" : 0,
			"Total number of pages currently in cache" : 0
		},
		"compression" : {
			"compressed pages read" : 0,
			"compressed pages written" : 0,
			"page written failed to compress" : 0,
			"page written was too small to compress" : 0
		},
		"cursor" : {
			"bulk-loaded cursor-insert calls" : 0,
			"close calls that result in cache" : 0,
			"create calls" : 1,
			"cursor operation restarted" : 0,
			"cursor-insert key and value bytes inserted" : 0,
			"cursor-remove key bytes removed" : 0,
			"cursor-update value bytes updated" : 0,
			"cursors reused from cache" : 0,
			"insert calls" : 0,
			"modify calls" : 0,
			"next calls" : 0,
			"open cursor count" : 0,
			"prev calls" : 1,
			"remove calls" : 0,
			"reserve calls" : 0,
			"reset calls" : 2,
			"search calls" : 0,
			"search near calls" : 0,
			"truncate calls" : 0,
			"update calls" : 0
		},
		"reconciliation" : {
			"dictionary matches" : 0,
			"fast-path pages deleted" : 0,
			"internal page key bytes discarded using suffix compression" : 0,
			"internal page multi-block writes" : 0,
			"internal-page overflow keys" : 0,
			"leaf page key bytes discarded using prefix compression" : 0,
			"leaf page multi-block writes" : 0,
			"leaf-page overflow keys" : 0,
			"maximum blocks required for a page" : 0,
			"overflow values written" : 0,
			"page checksum matches" : 0,
			"page reconciliation calls" : 0,
			"page reconciliation calls for eviction" : 0,
			"pages deleted" : 0
		},
		"session" : {
			"object compaction" : 0
		},
		"transaction" : {
			"update conflicts" : 0
		}
	},
	"nindexes" : 1,
	"totalIndexSize" : 4096,
	"indexSizes" : {
		"_id_" : 4096
	},
	"ok" : 1
}
---
> exit
bye

6. php设置mongodb扩展

可以使用如图上的方法安装mongodb扩展,也可以使用源码包的方式,可以在 http://pecl.php.net/package/ 找到mongodb的扩展包。

学习笔记0716----NOSQL之mongdb_第6张图片

[root@linux-02 ~]# cd /usr/local/src/
[root@linux-02 src]# wget http://pecl.php.net/get/mongodb-1.5.5.tgz
省略过程

[root@linux-02 src]# tar -zxvf mongodb-1.5.5.tgz
省略过程

[root@linux-02 src]# cd mongodb-1.5.5/

[root@linux-02 mongodb-1.5.5]# /usr/local/php-fpm/bin/phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226

[root@linux-02 mongodb-1.5.5]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config
省略过程

[root@linux-02 mongodb-1.5.5]# make
省略过程

[root@linux-02 mongodb-1.5.5]# make install
Installing shared extensions:     /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/

[root@linux-02 mongodb-1.5.5]# ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
memcache.so  mongodb.so  opcache.a  opcache.so  redis.so

[root@linux-02 mongodb-1.5.5]# vim /usr/local/php-fpm/etc/php.ini
##添加一下内容## 
extension=mongodb.so


[root@linux-02 mongodb-1.5.5]# /usr/local/php-fpm/bin/php -m | grep mongodb
mongodb

7. php设置mongo扩展

.安装mongo扩展和安装mongodb扩展类似。

[root@linux-02 ~]# cd /usr/local/src/

[root@linux-02 src]# wget http://pecl.php.net/get/mongo-1.6.16.tgz
省略

[root@linux-02 src]# tar -zxvf mongo-1.6.16.tgz
省略

[root@linux-02 src]# cd mongo-1.6.16/
[root@linux-02 mongo-1.6.16]# /usr/local/php-fpm/bin/phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226

[root@linux-02 mongo-1.6.16]#  ./configure --with-php-config=/usr/local/php-fpm/bin/php-config
省略

[root@linux-02 mongo-1.6.16]# make && make install
省略

[root@linux-02 mongo-1.6.16]# ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
memcache.so  mongodb.so  mongo.so  opcache.a  opcache.so  redis.so

[root@linux-02 mongo-1.6.16]# vim /usr/local/php-fpm/etc/php.ini
##添加一下内容##
extension=mongo.so

[root@linux-02 mongo-1.6.16]# /usr/local/php-fpm/bin/php -m | grep mongo
mongo
mongodb

8. 测试mongo扩展

由于我们在之前的mongo启动设置了用户名和密码的认证,所以去 /usr/lib/systemd/system/mongod.service 把–auth认证给删除。

[root@linux-02 mongo-1.6.16]# vim /usr/lib/systemd/system/mongod.service

[root@linux-02 mongo-1.6.16]# systemctl daemon-reload

[root@linux-02 mongo-1.6.16]# systemctl restart mongod.service

[root@linux-02 mongo-1.6.16]# vim /data/wwwroot/test.com/3.php
test; // 获取名称为 "test" 的数据库
$collection = $db->createCollection("runoob");
echo "集合创建成功";
?>


[root@linux-02 mongo-1.6.16]# curl localhost/3.php
集合创建成功

[root@linux-02 mongo-1.6.16]# mongo
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a25a861a-ab12-4911-baf5-b6140bc35338") }
MongoDB server version: 4.0.10
Server has startup warnings:
2019-07-18T04:44:16.063+0800 I CONTROL  [initandlisten]
2019-07-18T04:44:16.063+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-18T04:44:16.063+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-07-18T04:44:16.063+0800 I CONTROL  [initandlisten]
2019-07-18T04:44:16.063+0800 I CONTROL  [initandlisten]
2019-07-18T04:44:16.063+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-07-18T04:44:16.063+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-18T04:44:16.063+0800 I CONTROL  [initandlisten]
2019-07-18T04:44:16.063+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-07-18T04:44:16.063+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-18T04:44:16.063+0800 I CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> use test
switched to db test
> show tables
runoob
> exit
bye

9. MongoDB副本集

9.1 副本集介绍

  • 早期版本使用master-slave,一主一从和MySQL类似,但slave在此架构中为只读,当主库宕机后,从库不能自动切换为主
  • 目前已经淘汰master-slave模式,改为副本集,这种模式下有一个主(primary),和多个从(secondary),只读。支持给它们设置权重,当主宕掉后,权重最高的从切换为主
  • 在此架构中还可以建立一个仲裁(arbiter)的角色,它只负责裁决,而不存储数据
  • 再此架构中读写数据都是在主上,要想实现负载均衡的目的需要手动指定读库的目标server
    学习笔记0716----NOSQL之mongdb_第7张图片
    学习笔记0716----NOSQL之mongdb_第8张图片

9.2 副本集搭建

  • 三台机器: 192.168.48.128(primary) 192.168.48.130(secondary) 192.168.48.134(secondary)

  • 编辑三台机器的配置文件,更改或增加:

    • replication://把此行前面的#删除
    • ##oplog大小
      -oplogSizeMB: 20 //前面有两个空会报错,注意oplogSizeMB:和20之间有空格
    • ##复制集名称
    • replSetName: mglinux //前面有两个空格
  • 检查IPtables,selinux

  • 分别重启三台机器

首先分别把三台机器的mongdb安装好,配置每台机器的配置文件,如下:

学习笔记0716----NOSQL之mongdb_第9张图片

[root@linux-001 ~]# mongo
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("da07c5f0-213c-4fbb-b5b1-94bf1e136b46") }
MongoDB server version: 4.0.10
Server has startup warnings: 
2019-07-18T00:15:33.581+0800 I CONTROL  [initandlisten] 
2019-07-18T00:15:33.581+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-18T00:15:33.581+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-07-18T00:15:33.581+0800 I CONTROL  [initandlisten] 
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] 
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] 
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> use admin
switched to db admin
> config={_id:"mglinux",members:[{_id:0,host:"192.168.48.128:27017"},{_id:1,host:"192.168.48.130:27017"},{_id:2,host:"192.168.48.134:27017"}]}
{
	"_id" : "mglinux",
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.48.128:27017"
		},
		{
			"_id" : 1,
			"host" : "192.168.48.130:27017"
		},
		{
			"_id" : 2,
			"host" : "192.168.48.134:27017"
		}
	]
}
> rs.initiate(config)  //初始化,如果出现以下情况,请检查防火墙和selinux。
{
	"operationTime" : Timestamp(0, 0),
	"ok" : 0,
	"errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: 192.168.48.134:27017 failed with Error connecting to 192.168.48.134:27017 :: caused by :: No route to host",
	"code" : 74,
	"codeName" : "NodeNotFound",
	"$clusterTime" : {
		"clusterTime" : Timestamp(0, 0),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
> rs.initiate(config)
{
	"ok" : 1,
	"operationTime" : Timestamp(1563381568, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1563381568, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mglinux:SECONDARY> rs.status()   //查看状态
{
	"set" : "mglinux",
	"date" : ISODate("2019-07-17T16:40:11.439Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1563381610, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1563381610, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1563381610, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1563381610, 1),
			"t" : NumberLong(1)
		}
	},
	"lastStableCheckpointTimestamp" : Timestamp(1563381580, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.48.128:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 1479,
			"optime" : {
				"ts" : Timestamp(1563381610, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2019-07-17T16:40:10Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1563381579, 1),
			"electionDate" : ISODate("2019-07-17T16:39:39Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.48.130:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 42,
			"optime" : {
				"ts" : Timestamp(1563381600, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1563381600, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2019-07-17T16:40:00Z"),
			"optimeDurableDate" : ISODate("2019-07-17T16:40:00Z"),
			"lastHeartbeat" : ISODate("2019-07-17T16:40:09.549Z"),
			"lastHeartbeatRecv" : ISODate("2019-07-17T16:40:09.536Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.48.128:27017",
			"syncSourceHost" : "192.168.48.128:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.48.134:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 42,
			"optime" : {
				"ts" : Timestamp(1563381600, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1563381600, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2019-07-17T16:40:00Z"),
			"optimeDurableDate" : ISODate("2019-07-17T16:40:00Z"),
			"lastHeartbeat" : ISODate("2019-07-17T16:40:09.565Z"),
			"lastHeartbeatRecv" : ISODate("2019-07-17T16:40:09.491Z"),
			"pingMs" : NumberLong(1),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.48.128:27017",
			"syncSourceHost" : "192.168.48.128:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1563381610, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1563381610, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mglinux:PRIMARY>exit
bye

9.3 副本集测试

测试创建一个库和集合,先在主上操作,如下

[root@linux-001 ~]# mongo
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e1810685-523b-430c-b4a1-d06ec52d55eb") }
MongoDB server version: 4.0.10
Server has startup warnings: 
2019-07-18T00:15:33.581+0800 I CONTROL  [initandlisten] 
2019-07-18T00:15:33.581+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-18T00:15:33.581+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-07-18T00:15:33.581+0800 I CONTROL  [initandlisten] 
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] 
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] 
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-07-18T00:15:33.582+0800 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

mglinux:PRIMARY> use mydb
switched to db mydb
mglinux:PRIMARY> db.acc.insert({AccountID:1,UserName:"123",password:"123456"})
WriteResult({ "nInserted" : 1 })
mglinux:PRIMARY>  show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mydb    0.000GB
mglinux:PRIMARY> use mydb
switched to db mydb
mglinux:PRIMARY> show tables
acc
mglinux:PRIMARY> 

我们去从上测试看看是否有这个库,可以从下文中看到从上可以查看到刚才主上创建的库。

[root@linux-02 ~]# mongo
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("4bae3df4-edf9-45c1-853f-783f5b195944") }
MongoDB server version: 4.0.10
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2019-07-18T00:15:18.956+0800 I CONTROL  [initandlisten] 
2019-07-18T00:15:18.957+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-18T00:15:18.957+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-07-18T00:15:18.957+0800 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

mglinux:SECONDARY> show dbs
2019-07-18T00:51:06.753+0800 E QUERY    [js] Error: listDatabases failed:{
	"operationTime" : Timestamp(1563382260, 1),
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1563382260, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:139:1
shellHelper.show@src/mongo/shell/utils.js:882:13
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1  
mglinux:SECONDARY> rs.slaveOk()  //如果出现上面的错误,我们需要执行这个命令。
mglinux:SECONDARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mydb    0.000GB
mglinux:SECONDARY> use mydb
switched to db mydb
mglinux:SECONDARY> show tables
acc
mglinux:SECONDARY> 

9.4 副本集更改权重模拟主宕机

默认三台机器权重都为1,如果任何一个权重设置为比其他的高,则该台机器马上切换为primary角色,所以我们预设三台机器的权重分别为:128:3,130:2,134:1
在128上执行 iptables -I INPUT -p tcp --dport 27017 -j DROP,把27017端口数据禁掉。
在两台从上,有一台它的角色又SECONDARY变为了PRIMARY。那如何把之前的从给恢复过去呢?
在新的主上执行以下命令:
cfg = rs.conf()
cfg.members[0].priority = 3
cfg.members[1].priority = 2
cfg.members[2].priority = 1
rs.reconfig(cfg)
这样的话,第二个节点将会成为候选主节点。

10. mongodb分片介绍

  • 分片就是将数据库进行拆分,将大型集合分隔到不同服务器上。比如,本来100G的数据,可以分割成10份存储到10台服务器上,这样每台机器只有10G的数据。

  • 通过一个mongos的进程(路由)实现分片后的数据存储与访问,也就是说mongos是整个分片架构的核心,对客户端而言是不知道是否有分片的,客户端只需要把读写操作转达给mongos即可。

  • 虽然分片会把数据分隔到很多台服务器上,但是每一个节点都是需要有一个备用角色的,这样能保证数据的高可用。

  • 当系统需要更多空间或者资源的时候,分片可以让我们按需方便扩展,只需要把mongodb服务的机器加入到分片集群中即可
    学习笔记0716----NOSQL之mongdb_第10张图片

  • mongos: 数据库集群请求的入口,所有的请求都通过mongos进行协调,不需要在应用程序添加一个路由选择器,mongos自己就是一个请求分发中心,它负责把对应的数据请求请求转发到对应的shard服务器上。在生产环境通常有多mongos作为请求的入口,防止其中一个挂掉所有的mongodb请求都没有办法操作。

  • config server: 配置服务器,存储所有数据库元信息(路由、分片)的配置。mongos本身没有物理存储分片服务器和数据路由信息,只是缓存在内存里,配置服务器则实际存储这些数据。mongos第一次启动或者关掉重启就会从 config server 加载配置信息,以后如果配置服务器信息变化会通知到所有的 mongos 更新自己的状态,这样 mongos 就能继续准确路由。在生产环境通常有多个 config server 配置服务器,因为它存储了分片路由的元数据,防止数据丢失!

  • shard: 存储了一个集合部分数据的MongoDB实例,每个分片是单独的mongodb服务或者副本集,在生产环境中,所有的分片都应该是副本集。

11. mongodb分片搭建

11.1 分片搭建–服务器规划

  • 三台机器 A B C
  • A搭建:mongos、config server、副本集1主节点、副本集2仲裁、副本集3从节点
  • B搭建:mongos、config server、副本集1从节点、副本集2主节点、副本集3仲裁
  • C搭建:mongos、config server、副本集1仲裁、副本集2从节点、副本集3主节点
  • 端口分配:mongos 20000、config 21000、副本集1 27001、副本集2 27002、副本集3 27003
  • 三台机器全部关闭firewalld服务和selinux,或者增加对应端口的规则

11.2 分片搭建–创建目录

  • 分别在三台机器上创建各个角色所需要的目录
  • mkdir -p /data/mongodb/mongos/log
  • mkdir -p /data/mongodb/config/{data,log}
  • mkdir -p /data/mongodb/shard1/{data,log}
  • mkdir -p /data/mongodb/shard2/{data,log}
  • mkdir -p /data/mongodb/shard3/{data,log}

11.3 分片搭建–config server配置

  • mongodb3.4版本以后需要对config server创建副本集
  • 添加配置文件(三台机器都操作)
  • mkdir /etc/mongod/
  • vim /etc/mongod/config.conf //加入如下内容
pidfilepath = /var/run/mongodb/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
fork = true
configsvr = true #declare this is a config db of a cluster;
replSet=configs #副本集名称
maxConns=20000 #设置最大连接数

11.4 分片搭建–启动config server

  • 启动三台机器的config server
  • mongod -f /etc/mongod/config.conf //三台机器都要操作
  • 登录任意一台机器的21000端口,初始化副本集
  • mongo --port 21000
  • config = { _id: “configs”, members: [ {_id : 0, host : “192.168.133.130:21000”},{_id : 1, host : “192.168.133.132:21000”},{_id : 2, host : “192.168.133.133:21000”}] }
    -rs.initiate(config)
    { “ok” : 1 }

11.5 分片搭建–分片配置

  • 添加shard1配置文件(三台机器都操作)
  • vim /etc/mongod/shard1.conf //加入如下内容
pidfilepath = /var/run/mongodb/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
httpinterface=true #打开web监控
rest=true
replSet=shard1 #副本集名称
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #设置最大连接数
  • 添加shard2配置文件(三台机器都操作)
  • vim /etc/mongod/shard2.conf //加入如下内容
pidfilepath = /var/run/mongodb/shard2.pid
dbpath = /data/mongodb/shard2/data
logpath = /data/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 0.0.0.0
port = 27002
fork = true
httpinterface=true #打开web监控
rest=true
replSet=shard2 #副本集名称
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #设置最大连接数
  • 添加shard3配置文件(三台机器都操作)
  • vim /etc/mongod/shard3.conf //加入如下内容
pidfilepath = /var/run/mongodb/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 27003
fork = true
httpinterface=true #打开web监控
rest=true
replSet=shard3 #副本集名称
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #设置最大连接数

11.6 分片搭建–启动分片

  • 启动shard1

  • mongod -f /etc/mongod/shard1.conf //三台机器都要操作

  • 登录130或者132任何一台机器的27001端口初始化副本集,133之所以不行,是因为shard1我们把133这台机器的27001端口作为了仲裁节点

  • mongo --port 27001

  • use admin

  • config = { _id: “shard1”, members: [ {_id : 0, host : “192.168.133.130:27001”}, {_id: 1,host : “192.168.133.132:27001”},{_id : 2, host : “192.168.133.133:27001”,arbiterOnly:true}] }

  • rs.initiate(config)

  • shard1是第二台仲裁,shard2第三台仲裁,shard3第一台仲裁,设置仲裁的机器不可以登陆此副本集,设置的副本集那台为主,就需要在哪台登陆此副本。shard1在第一台登陆mongo,shard2在第二台登陆mongo,shard3在第三台登陆mongo。

11.7 分片搭建–配置路由服务器

-添加配置文件(三台机器都操作)

  • vim /etc/mongod/mongos.conf //加入如下内容
pidfilepath = /var/run/mongodb/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
configdb = configs/192.168.133.130:21000, 192.168.133.132:21000, 192.168.133.133:21000 #监听的配置服务器,只能有1个或者3个,configs为配置服务器的副本集名字
maxConns=20000 #设置最大连接数
  • 启动mongos服务,注意命令,前面都是mongod,这里是mongos
  • mongos -f /etc/mongod/mongos.conf

11.8 分片搭建–启用分片

 登录任何一台20000端口
 mongo --port 20000  
 把所有分片和路由器串联
sh.addShard("shard1/192.168.133.130:27001,192.168.133.132:27001,192.168.133.133:27001")
sh.addShard("shard2/192.168.133.130:27002,192.168.133.132:27002,192.168.133.133:27002")
sh.addShard("shard3/192.168.133.130:27003,192.168.133.132:27003,192.168.133.133:27003")
 查看集群状态
 sh.status()

11.9 分片测试

登录任何一台20000端口
 mongo --port 20000  
 use admin
 db.runCommand({ enablesharding : "testdb"}) 或者
 sh.enableSharding("testdb") //指定要分片的数据库
 db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } ) 或者
 sh.shardCollection("testdb.table1",{"id":1} ) //#指定数据库里需要分片的集合和片键
 use  testdb
 for (var i = 1; i <= 10000; i++) db.table1.save({id:i,"test1":"testval1"})//插入测试数据
 db.table1.stats()//查看table1状态

12. mongodb的备份和恢复

12.1 备份

 备份指定库
 mongodump --host 127.0.0.1 --port 20000  -d mydb -o /tmp/mongobak
 它会在/tmp/目录下面生成一个mydb的目录
 备份所有库
 mongodump --host 127.0.0.1 --port 20000 -o /tmp/mongobak/alldatabase
 指定备份集合
 mongodump --host 127.0.0.1 --port 20000 -d mydb -c c1 -o /tmp/mongobak/
 它依然会生成mydb目录,再在这目录下面生成两个文件
 导出集合为json文件
 mongoexport --host 127.0.0.1 --port 20000 -d mydb -c c1 -o /tmp/mydb2/1.json

12.2 恢复

恢复所有库
 mongorestore -h 127.0.0.1 --port 20000 --drop dir/ //其中dir是备份所有库的目录名字,其中--drop可选,意思是当恢复之前先把之前的数据删除,不建议使用
 恢复指定库
 mongorestore -d mydb dir/  //-d跟要恢复的库名字,dir就是该库备份时所在的目录
 恢复集合
mongorestore -d mydb -c testc dir/mydb/testc.bson // -c后面跟要恢复的集合名字,dir是备份mydb库时生成文件所在路径,这里是一个bson文件的路径
 导入集合
 mongoimport -d mydb -c testc --file /tmp/testc.json

你可能感兴趣的:(运维学习文档,mongodb)