首页 » 技术分享 » M103: Basic Cluster Administration chapter 1 The Mongod学习记录

M103: Basic Cluster Administration chapter 1 The Mongod学习记录

 

M103: Basic Cluster Administration chapter 1 The Mongod学习记录

运行环境

操作系统:windows 10 家庭中文版
Mongodb :Mongodb 3.4

Mongodb安装路径:E:>MongoDB\Server\3.4\bin\
Mongodb存储路径:E:>MongoDB\data

课后问题

lab - Launching Mongod

In this lab, you’re going to launch your own mongod with a few basic command line arguments.

Applying what you’ve learned so far about the mongod process, launch a mongod instance, from the command line, with the following requirements:

  • run on port 27000
  • data files are stored in /data/db/
  • listens to connections from the IP address 192.168.103.100 and localhost
  • authentication is enabled

By default, a mongod that enforces authentication but has no configured users only allows connections through the localhost. Use the mongo shell on the Vagrant box to use the localhost exception and connect to this node.

You can use the following command to connect to the Mongo shell and create a user:

mongo admin --host localhost:27000 --eval '
  db.createUser({
    user: "m103-admin",
    pwd: "m103-pass",
    roles: [
      {role: "root", db: "admin"}
    ]
  })
'

The above command creates a user with the following credentials:

  • Role: root on admin database
  • Username: m103-admin
  • Password: m103-pass

When you’re finished, run the following validation script and enter the validation key you receive below. If you receive an error, it should give you some idea of what went wrong.

validate_lab_launch_mongod

Hint: You want to make sure all applicable command line options are set! Also, in case you need to restart the mongod daemon, you may need to kill the process using it’s pid.

You can use the following command to find the pid of the process:

ps -ef | grep mongod

To kill the process, you can use this command:

kill <pid>

解答

启动vagrant box(搭建方法见上一章:M103: Basic Cluster Administration chapter 0 Introduction学习记录):

C:\Users\Shinelon>e:

E:\>cd E:\MongoDB\m103\chapter_0_introduction_setup\m103-vagrant-env

E:\MongoDB\m103\chapter_0_introduction_setup\m103-vagrant-env>vagrant up --provision
Bringing machine 'mongod-m103' up with 'virtualbox' provider...
==> mongod-m103: Clearing any previously set forwarded ports...
==> mongod-m103: Clearing any previously set network interfaces...
···xxx···
···xxx···
···xxx···
···xxx···
    mongod-m103: Done: Downloaded Validation Scripts
    mongod-m103: + echo DONE
    mongod-m103: DONE

进入box:

E:\MongoDB\m103\chapter_0_introduction_setup\m103-vagrant-env>vagrant ssh
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-144-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Mon Apr 16 03:10:54 UTC 2018

  System load:  0.17              Processes:           90
  Usage of /:   5.0% of 39.34GB   Users logged in:     0
  Memory usage: 7%                IP address for eth0: 10.0.2.15
  Swap usage:   0%                IP address for eth1: 192.168.103.100

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

2 packages can be updated.
2 updates are security updates.

New release '16.04.4 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Mon Apr 16 03:10:55 2018 from 10.0.2.2

按指定要求执行mongod守护进程:

vagrant@m103:~$ mongod --port 27000 --dbpath /data/db --bind_ip 192.168.103.100,localhost --auth
2018-04-16T03:13:27.592+0000 I CONTROL  [initandlisten] MongoDB starting : pid=2179 port=27000 dbpath=/data/db 64-bit host=m103
2018-04-16T03:13:27.593+0000 I CONTROL  [initandlisten] db version v3.6.4-rc0
2018-04-16T03:13:27.593+0000 I CONTROL  [initandlisten] git version: d0181a711f7e7f39e60b5aeb1dc7097bf6ae5856
2018-04-16T03:13:27.593+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
2018-04-16T03:13:27.594+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2018-04-16T03:13:27.594+0000 I CONTROL  [initandlisten] modules: enterprise
2018-04-16T03:13:27.594+0000 I CONTROL  [initandlisten] build environment:
2018-04-16T03:13:27.595+0000 I CONTROL  [initandlisten]     distmod: ubuntu1404
2018-04-16T03:13:27.595+0000 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-16T03:13:27.595+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-16T03:13:27.596+0000 I CONTROL  [initandlisten] options: { net: { bindIp: "192.168.103.100,localhost", port: 27000 }, security: { authorization: "enabled" }, storage: { dbPath: "/data/db" } }
2018-04-16T03:13:27.597+0000 I STORAGE  [initandlisten]
2018-04-16T03:13:27.597+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-04-16T03:13:27.597+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-04-16T03:13:27.599+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=488M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),cache_cursors=false,log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-04-16T03:13:28.187+0000 I STORAGE  [initandlisten] WiredTiger message [1523848408:187321][2179:0x7ff3075d9a40], txn-recover: Set global recovery timestamp: 0
2018-04-16T03:13:28.264+0000 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: a0285c9e-85d5-492a-a46a-8f2da6946885
2018-04-16T03:13:28.292+0000 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.6
2018-04-16T03:13:28.296+0000 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: 902146ae-6549-4b36-8c2d-bb9c8a361b71
2018-04-16T03:13:28.314+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2018-04-16T03:13:28.315+0000 I NETWORK  [initandlisten] waiting for connections on port 27000
2018-04-16T03:13:55.815+0000 I NETWORK  [listener] connection accepted from 127.0.0.1:41635 #1 (1 connection now open)
2018-04-16T03:13:55.815+0000 I ACCESS   [conn1] note: no users configured in admin.system.users, allowing localhost access

进入一个新的box,执行mongo命令:

vagrant@m103:~$ mongo admin --host localhost:27000 --eval '
>   db.createUser({
>     user: "m103-admin",
>     pwd: "m103-pass",
>     roles: [
>       {role: "root", db: "admin"}
>     ]
>   })
> '
MongoDB shell version v3.6.4-rc0
connecting to: mongodb://localhost:27000/admin
MongoDB server version: 3.6.4-rc0
Successfully added user: {
        "user" : "m103-admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}

执行脚本,求出结果:

vagrant@m103:~$ validate_lab_launch_mongod
5a21c6dd403b6546001e79c0

lab - Configuration File

In this lab, you’re going to launch a mongod instance with the same exact settings as the previous lab. However this time, those settings will be defined in a configuration file instead of the command line. It is required for this lab that you use the YAML format to construct this config file.

As a reminder, here are the requirements of your mongod instance:

  • run on port 27000
  • data files are stored in /data/db/
  • listens to connections from the IP address 192.168.103.100 and localhost
  • authentication is enabled

If you created the user from the previous lab, you don’t need to create any new users. If you did not create the user, do so now. Here are the requirements for that user:

  • Role: root on admin database
  • Username: m103-admin
  • Password: m103-pass.

When your config file is complete, launch mongod with the –config command line option with the filepath to your config file in place of config_filepath (you can also use the -f option instead of –config).

When you’re finished, run the following validation script and enter the validation key you receive below. If you receive an error, it should give you some idea of what went wrong.

validate_lab_configuration_file

解答

先在box中查找mongod.conf文件的路径:

vagrant@m103:/$ find / -name 'mongod.conf'
find: `/lost+found': Permission denied
find: `/etc/chatscripts': Permission denied
find: `/etc/ssl/private': Permission denied
find: `/etc/ppp/peers': Permission denied
find: `/etc/sudoers.d': Permission denied
find: `/etc/polkit-1/localauthority': Permission denied
/etc/init/mongod.conf
/etc/mongod.conf
···xxx···
···xxx···

文件存放在默认位置,其中 YAML格式的是/etc/mongod.conf,停止mongod进程:

2018-04-16T03:37:59.406+0000 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2018-04-16T03:37:59.407+0000 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27000.sock
2018-04-16T03:37:59.408+0000 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
2018-04-16T03:37:59.410+0000 I STORAGE  [signalProcessingThread] WiredTigerKVEngine shutting down
2018-04-16T03:37:59.527+0000 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2018-04-16T03:37:59.527+0000 I CONTROL  [signalProcessingThread] now exiting
2018-04-16T03:37:59.528+0000 I CONTROL  [signalProcessingThread] shutting down with code:0

对conf文件进行修改:

vagrant@m103:~$ sudo vim /etc/mongod.conf

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /data/db
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27000
  bindIp: 192.168.103.100,localhost


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
  authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

启动守护进程mongod:

vagrant@m103:~$ mongod --config /etc/mongod.conf

切换至另一个box,执行答案脚本:

vagrant@m103:/etc/init$ validate_lab_configuration_file
5a2f0e41ae3c4e2f7427ee8f

lab - Change the Default DB Path

In this lab, you’re going to edit the config file from the previous lab to include a different DB path than the default path /data/db. Mongod will now store data files in this new directory instead.

Using what you know about the configuration file and Linux user groups, please complete the following:

  • create a new folder /var/mongodb/db/ and allow mongod to write files to this directory

    • create this directory with sudo, because /var is owned by root
    • use chown to change the owner of this directory to vagrant:vagrant
  • edit your config file to use this new directory as the dbpath

Here are the updated requirements for your mongod instance:

  • runs on port 27000
  • stores its data files in /var/mongodb/db/
  • listens to connections from the IP address 192.168.103.100 and localhost
  • uses authentication

Now that your config file has changed, you have to restart mongod so the server will reflect those changes. As a reminder, here is the way to safely shutdown from the mongo shell:

use admin
db.shutdownServer()
quit()

Once your mongod is safely stopped, you can launch it again with your new config file. But because your m103-admin user was written to the old DB path /data/db/, it won’t be available in our new mongod! You have to connect using the mongo shell and create a user with the same requirements:

  • Role: root on admin database
  • Username: m103-admin
  • Password: m103-pass

When you’re finished, run the following validation script and enter the validation key you receive below. If you receive an error, it should give you some idea of what went wrong.

validate_lab_change_dbpath

解答

根据题目要求创建目录:

vagrant@m103:~$ sudo mkdir -p /var/mongodb/db

修改文件所有者:

vagrant@m103:~$ sudo chown -R vagrant:vagrant /var/mongodb/

修改mongod.conf文件:

vagrant@m103:~$ sudo vim /etc/mongod.conf

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/mongodb/db/
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27000
  bindIp: 192.168.103.100,localhost


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
  authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

重新启动mongod守护进程:

vagrant@m103:~$ mongod --config /etc/mongod.conf

在另一个box重新配置账号密码:

vagrant@m103:~$ mongo admin --host localhost:27000 --eval '
  db.createUser({
    user: "m103-admin",
    pwd: "m103-pass",
    roles: [
      {role: "root", db: "admin"}
    ]
  })
'
MongoDB shell version v3.6.4-rc0
connecting to: mongodb://localhost:27000/admin
MongoDB server version: 3.6.4-rc0
Successfully added user: {
        "user" : "m103-admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}

执行答案函数脚本:

vagrant@m103:~$ validate_lab_change_dbpath
5a2f973bcb6b357b57e6bf43

lab - Logging to a Different Facility

By default, mongod sends diagnostic logging information to the standard output, occupying the terminal window where mongod was started. In this lab, you will tell mongod to send those logs to a file so you can fork the mongod process and continue to use the terminal window where mongod was started. You’ll also change some of the default log settings.

Your task for this lab is to change the config file such that:

  • mongod sends logs to /var/mongodb/db/mongod.log
  • mongod is forked and run as a daemon (this will not work without a logpath)
  • any process that takes 50ms or longer is logged

All of the other information in your config file should stay the same as in the previous lab.

When you’re finished, run the following validation script and enter the validation key you receive below. If you receive an error, it should give you some idea of what went wrong.

validate_lab_different_logpath

解答

按要求修改mongod.conf文件:

vagrant@m103:~$ sudo vim /etc/mongod.conf

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/mongodb/db/
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/mongodb/db/mongod.log

# network interfaces
net:
  port: 27000
  bindIp: 192.168.103.100,localhost


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo
  fork: true

security:
  authorization: enabled

operationProfiling:
     slowOpThresholdMs: 50
     mode: all

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

重启mongod守护进程:

vagrant@m103:~$ mongod --config /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3851
child process started successfully, parent exiting

执行答案脚本:

vagrant@m103:/data/db$ validate_lab_different_logpath
5a32e5835d7a25685155aa61

lab - Creating First Application User

In the first lab, you created a user with the root role on the admin database. The root role is one of the most powerful roles in a Mongo cluster, and has many privileges which are never used by a typical application. In this lab, you will create a new user for an application that has the readWrite role, because the application does not need to monitor the cluster or create users - it only needs to read and write data.

The requirements for this new user are:

  • Role: readWrite on applicationData database
  • Authentication source: admin
  • Username: m103-application-user
  • Password: m103-application-pass

You don’t need to make any changes to your mongod configuration.

When you’re finished, run the following validation script and enter the validation key you receive below. If you receive an error, it should give you some idea of what went wrong.

validate_lab_first_application_user

解答

由于设定了登陆权限,必须使用之前创建的用户以admin身份进入数据库:

vagrant@m103:/data/db$  mongo admin  --host localhost:27000 -u m103-admin -p m103-pass --eval '
>   db.createUser({
>     user: "m103-application-user",
>     pwd: "m103-application-pass",
>     roles: [
>       {role: "readWrite", db: "applicationData"}
>     ]
>   })
> '
MongoDB shell version v3.6.4-rc0
connecting to: mongodb://localhost:27000/admin
MongoDB server version: 3.6.4-rc0
Successfully added user: {
        "user" : "m103-application-user",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "applicationData"
                }
        ]
}

执行答案脚本:

vagrant@m103:/data/db$ validate_lab_first_application_user
5a32fdd630bff1f2fcb87acf

lab - Importing a Dataset

Download Handouts:

m103/products.json

Now that you have some background about MongoDB’s server tools, use mongoimport to import a JSON dataset into MongoDB. You can find the dataset inside the Vagrant box in /dataset/products.json, or in the lesson handout.

Import the whole dataset with your application’s user m103-application-user into a collection called products, in the database applicationData.

When you’re finished, run the following validation script and enter the validation key you receive below. If you receive an error, it should give you some idea of what went wrong.

validate_lab_import_dataset

解答

查看json脚本位置:

vagrant@m103:~$ ls /dataset/
products.json

就在box的指定位置,继续执行插入命令:

vagrant@m103:/data/db$ mongoimport  --host localhost:27000 -u m103-application-user -p m103-applicatio
n-pass -d applicationData /dataset/products.json
2018-04-16T08:18:58.448+0000    no collection specified
2018-04-16T08:18:58.448+0000    using filename 'products' as collection
2018-04-16T08:18:58.451+0000    Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
2018-04-16T08:18:58.452+0000    imported 0 documents

发现报错 error connecting to db server: server returned error on SASL authentication step: Authentication failed,分析是权限问题,加上–authenticationDatabase=admin后执行成功:

vagrant@m103:/data/db$ mongoimport  --host localhost:27000 --authenticationDatabase=admin -u m103-application-user -p m103-application-pass -d applicationData -c products /dataset/products.json
2018-04-16T08:22:23.817+0000    connected to: localhost:27000
2018-04-16T08:22:26.807+0000    [######..................] applicationData.products     24.3MB/87.9MB (27.6%)
2018-04-16T08:22:29.807+0000    [#############...........] applicationData.products     48.8MB/87.9MB (55.4%)
2018-04-16T08:22:32.807+0000    [###################.....] applicationData.products     72.8MB/87.9MB (82.8%)
2018-04-16T08:22:34.671+0000    [########################] applicationData.products     87.9MB/87.9MB (100.0%)
2018-04-16T08:22:34.672+0000    imported 516784 documents

执行答案脚本:

vagrant@m103:/data/db$ validate_lab_import_dataset
5a383323ba6dbcf3cbcaec97

转载自原文链接, 如需删除请联系管理员。

原文链接:M103: Basic Cluster Administration chapter 1 The Mongod学习记录,转载请注明来源!

0