MongoDB使用iscsi

2022-09-16 12:17:02 浏览数 (1)

Linux平台:尝试部署MogoDB,并使用iscsi作为其data目录

MongoDB 是由C 语言编写的,是一个基于分布式文件存储的开源数据库系统。 在高负载的情况下,添加更多的节点,可以保证服务器性能。 MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。

# 连接iscsi

代码语言:javascript复制
root in awCqlxb in ~ took 4s 
❯ iscsiadm -m discovery -t st -p 192.168.165.111
192.168.165.111:3260,1 iqn.2022-03.com.515:192.168.165.sn.1a801217e31855fd6e0d49fa
root in awCqlxb in ~ 
➜ iscsiadm -m node iqn.2022-03.com.515:192.168.165.sn.1a801217e31855fd6e0d49fa  -l
Logging in to [iface: default, target: iqn.2022-03.com.515:192.168.165.sn.1a801217e31855fd6e0d49fa, portal: 192.168.165.111,3260] (multiple)
Login to [iface: default, target: iqn.2022-03.com.515:192.168.165.sn.1a801217e31855fd6e0d49fa, portal: 192.168.165.111,3260] successful.
root in awCqlxb in ~ 
➜ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  1.8T  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    5G  0 part /usr/sysmag_vfs
└─sda3   8:3    0  1.8T  0 part /
sdb      8:16   0  3.7T  0 disk 
└─sdb1   8:17   0  3.7T  0 part 
sdc      8:32   0  3.7T  0 disk 
└─sdc1   8:33   0  3.7T  0 part 
sdd      8:48   0  3.7T  0 disk 
└─sdd1   8:49   0  3.7T  0 part 
sde      8:64   0  3.7T  0 disk 
└─sde1   8:65   0  3.7T  0 part 
sdf      8:80   0  3.7T  0 disk 
└─sdf1   8:81   0  3.7T  0 part 
sdg      8:96   0  3.7T  0 disk 
└─sdg1   8:97   0  3.7T  0 part 
sdh      8:112  0   50G  0 disk 
root in awCqlxb in ~ took 2s 
➜ fdisk /dev/sdh
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x5426b140.

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-104857599, default 8192): 
Using default value 8192
Last sector,  sectors or  size{K,M,G} (8192-104857599, default 104857599): 
Using default value 104857599
Partition 1 of type Linux and of size 50 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
root in awCqlxb in ~ took 11s 
➜ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  1.8T  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    5G  0 part /usr/sysmag_vfs
└─sda3   8:3    0  1.8T  0 part /
sdb      8:16   0  3.7T  0 disk 
└─sdb1   8:17   0  3.7T  0 part 
sdc      8:32   0  3.7T  0 disk 
└─sdc1   8:33   0  3.7T  0 part 
sdd      8:48   0  3.7T  0 disk 
└─sdd1   8:49   0  3.7T  0 part 
sde      8:64   0  3.7T  0 disk 
└─sde1   8:65   0  3.7T  0 part 
sdf      8:80   0  3.7T  0 disk 
└─sdf1   8:81   0  3.7T  0 part 
sdg      8:96   0  3.7T  0 disk 
└─sdg1   8:97   0  3.7T  0 part 
sdh      8:112  0   50G  0 disk 
└─sdh1   8:113  0   50G  0 part 
root in awCqlxb in ~ 
➜ mkfs.xfs /dev/sdh1
specified blocksize 4096 is less than device physical sector size 4194304
switching to logical sector size 512
meta-data=/dev/sdh1              isize=256    agcount=4, agsize=3276544 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0, sparse=0, rmapbt=0, reflink=0
data     =                       bsize=4096   blocks=13106176, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=6399, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
root in awCqlxb in ~ 
➜ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  1.8T  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    5G  0 part /usr/sysmag_vfs
└─sda3   8:3    0  1.8T  0 part /
sdb      8:16   0  3.7T  0 disk 
└─sdb1   8:17   0  3.7T  0 part 
sdc      8:32   0  3.7T  0 disk 
└─sdc1   8:33   0  3.7T  0 part 
sdd      8:48   0  3.7T  0 disk 
└─sdd1   8:49   0  3.7T  0 part 
sde      8:64   0  3.7T  0 disk 
└─sde1   8:65   0  3.7T  0 part 
sdf      8:80   0  3.7T  0 disk 
└─sdf1   8:81   0  3.7T  0 part 
sdg      8:96   0  3.7T  0 disk 
└─sdg1   8:97   0  3.7T  0 part 
sdh      8:112  0   50G  0 disk 
└─sdh1   8:113  0   50G  0 part 
root in awCqlxb in ~ 
➜ mkdir /iscsd-dir
root in awCqlxb in ~ 
➜ mount -t xfs /dev/sdh1 /iscsd-dir/
root in awCqlxb in ~ 
➜ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs         32G     0   32G   0% /dev
tmpfs            32G   20K   32G   1% /dev/shm
tmpfs            32G   26M   32G   1% /run
tmpfs            32G     0   32G   0% /sys/fs/cgroup
/dev/sda3       1.8T  145G  1.6T   9% /
/dev/sda1       976M  241M  669M  27% /boot
/dev/sda2       4.8G  1.4G  3.2G  30% /usr/sysmag_vfs
tmpfs           6.3G     0  6.3G   0% /run/user/1000
tmpfs           6.3G     0  6.3G   0% /run/user/0
/dev/sdh1        50G   33M   50G   1% /iscsd-dir
root in awCqlxb in ~ 

# 安装配置

MongoDB安装文件下载路径

  1. 解压后移动文件存储位置并改名
代码语言:javascript复制
root in awCqlxb in ~ 
➜ tar -zxvf mongodb-linux-x86_64-rhel70-5.0.6.tgz 
mongodb-linux-x86_64-rhel70-5.0.6/LICENSE-Community.txt
mongodb-linux-x86_64-rhel70-5.0.6/MPL-2
mongodb-linux-x86_64-rhel70-5.0.6/README
mongodb-linux-x86_64-rhel70-5.0.6/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-5.0.6/bin/install_compass
mongodb-linux-x86_64-rhel70-5.0.6/bin/mongo
mongodb-linux-x86_64-rhel70-5.0.6/bin/mongod
mongodb-linux-x86_64-rhel70-5.0.6/bin/mongos
root in awCqlxb in ~ took 2s 
➜ mv mongodb-linux-x86_64-rhel70-5.0.6 /usr/local/mongodb

  1. 添加配置文件
代码语言:javascript复制
root in awCqlxb in local/mongodb/bin 
➜ cat /usr/local/mongodb/mongodb.conf 
#数据文件存放目录(iscsi挂载目录)
#dbpath = /usr/local/mongodb/data/db
dbpath = /iscsd-dir/db
#日志文件存放目录
logpath = /usr/local/mongodb/logs/mongodb.log
#默认端口27017
port = 27017
#以守护程序的方式启用,即在后台运行
fork = true
#允许远程连接,127.0.0.1只允许本地连接
bind_ip=0.0.0.0
#是否需要认证,如果启用,则需要创建mongodb账号密码,使用账号密码才可以远程访问(先注释不开启,创建密码后打开)
#auth = true

  1. 创建日志目录
代码语言:javascript复制
root in awCqlxb in ~ 
➜ cd  /usr/local/mongodb
root in awCqlxb in /usr/local/mongodb 
➜ ll
total 136
drwxr-xr-x 2 root root  4096 Mar  7 11:27 bin
-rw-r--r-- 1 root root 30608 Jan 28 00:21 LICENSE-Community.txt
-rw-r--r-- 1 root root 16726 Jan 28 00:21 MPL-2
-rw-r--r-- 1 root root  1977 Jan 28 00:21 README
-rw-r--r-- 1 root root 76823 Jan 28 00:21 THIRD-PARTY-NOTICES
root in awCqlxb in /usr/local/mongodb 
➜ vim  mongodb.conf
root in awCqlxb in /usr/local/mongodb took 27s 
➜ mkdir -p /usr/local/mongodb/data/db
root in awCqlxb in /usr/local/mongodb 
➜ mkdir -p /usr/local/mongodb/logs
root in awCqlxb in /usr/local/mongodb 
➜ touch /usr/local/mongodb/logs/mongodb.log
root in awCqlxb in /usr/local/mongodb 

  1. 启动服务
代码语言:javascript复制
root in awCqlxb in local/mongodb/bin 
❯ ./mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1079984
child process started successfully, parent exiting
root in awCqlxb in local/mongodb/bin 
➜ ss -anlt | grep 27017
LISTEN  0        128              0.0.0.0:27017          0.0.0.0:*  

  1. 停止服务
代码语言:javascript复制
root in awCqlxb in local/mongodb/bin 
❯ ./mongod --dbpath /usr/local/mongodb/data/db --logpath /usr/local/mongodb/logs/mongodb.log --shutdown
{"t":{"$date":"2022-03-07T06:09:57.229Z"},"s":"I",  "c":"CONTROL",  "id":20697,   "ctx":"-","msg":"Renamed existing log file","attr":{"oldLogPath":"/usr/local/mongodb/logs/mongodb.log","newLogPath":"/usr/local/mongodb/logs/mongodb.log.2022-03-07T06-09-57"}}
killing process with pid: 1006202

  1. 创建用户
  • 切换到admin数据库use admin
  • 使用db.createUser()创建一个管理员用户admin,角色为root,指定具体数据库db.createUser({user:'admin',pwd:'admin',roles:[{role:'root',db:'admin'}]})
  • db.auth()认证一下获取权限,认证通过后可以操作数据库,成功则会输出1db.auth("admin","admin")
  • use 命令也可以是创建数据库,当指定数据库不存在时即为创建,切换到一个新的数据库xtestuse xtest
  • 创建用户xtest dbOwner权限db.createUser({user:'xtest',pwd:'xtest',roles:['dbOwner']})
  • 查看有哪儿些用户show users
  • 删除用户db.dropUser("user_name")
代码语言:javascript复制
root in awCqlxb in local/mongodb/bin 
➜ ./mongo 
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("cfebe895-18d2-4fcf-83ce-85f4b48551e6") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting: 
        2022-03-07T14:37:12.499 08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2022-03-07T14:37:12.499 08:00: You are running this process as the root user, which is not recommended
        2022-03-07T14:37:12.500 08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
---
---
        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',pwd:'admin',roles:[{role:'root',db:'admin'}]})
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
> db.auth("admin","admin")
1
> db.createUser({user:'xtest',pwd:'xtest',roles:['dbOwner']})
Successfully added user: { "user" : "xtest", "roles" : [ "dbOwner" ] }
> show users
{
	"_id" : "admin.admin",
	"userId" : UUID("0a24981d-da73-4eaa-812a-bb5b3a7882fb"),
	"user" : "admin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}
{
	"_id" : "admin.xtest",
	"userId" : UUID("b73df449-0857-434f-a803-a6ab5b5e5eb8"),
	"user" : "xtest",
	"db" : "admin",
	"roles" : [
		{
			"role" : "dbOwner",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}
> 

  1. 创建用户完成后打开auth认证,并重启服务
代码语言:javascript复制
> exit
bye
root in awCqlxb in local/mongodb/bin took 5m50s 
➜ cat ../mongodb.conf 
#数据文件存放目录
#dbpath = /usr/local/mongodb/data/db
dbpath = /iscsd-dir/db
#日志文件存放目录
logpath = /usr/local/mongodb/logs/mongodb.log
#默认端口27017
port = 27017
#以守护程序的方式启用,即在后台运行
fork = true
#允许远程连接,127.0.0.1只允许本地连接
bind_ip=0.0.0.0
#是否需要认证,如果启用,则需要创建mongodb账号密码,使用账号密码才可以远程访问
#auth = true

root in awCqlxb in local/mongodb/bin 
➜ ./mongod --dbpath /iscsd-dir/db/ --logpath /usr/local/mongodb/logs/mongodb.log --shutdown
{"t":{"$date":"2022-03-07T06:44:31.143Z"},"s":"I",  "c":"CONTROL",  "id":20697,   "ctx":"-","msg":"Renamed existing log file","attr":{"oldLogPath":"/usr/local/mongodb/logs/mongodb.log","newLogPath":"/usr/local/mongodb/logs/mongodb.log.2022-03-07T06-44-31"}}
killing process with pid: 1087919
root in awCqlxb in local/mongodb/bin 
➜ vim ../mongodb.conf 
root in awCqlxb in local/mongodb/bin took 5s 
➜ cat ../mongodb.conf 
#数据文件存放目录
#dbpath = /usr/local/mongodb/data/db
dbpath = /iscsd-dir/db
#日志文件存放目录
logpath = /usr/local/mongodb/logs/mongodb.log
#默认端口27017
port = 27017
#以守护程序的方式启用,即在后台运行
fork = true
#允许远程连接,127.0.0.1只允许本地连接
bind_ip=0.0.0.0
#是否需要认证,如果启用,则需要创建mongodb账号密码,使用账号密码才可以远程访问
auth = true

root in awCqlxb in local/mongodb/bin 
➜ ./mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1090671
child process started successfully, parent exiting
root in awCqlxb in local/mongodb/bin 
➜ ss -anlt | grep 27017
LISTEN  0        128              0.0.0.0:27017          0.0.0.0:*              
root in awCqlxb in local/mongodb/bin 
➜ 

# 测试连接

  1. 命令行连接
代码语言:javascript复制
root in awCqlxb in local/mongodb/bin 
❯ ./mongo -u admin -p admin
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("fa03aebf-3285-4d91-b955-7c0f273676ee") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting: 
        2022-03-07T14:45:00.145 08:00: You are running this process as the root user, which is not recommended
        2022-03-07T14:45:00.145 08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
---
---
        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()
---
> exit
bye

  1. 工具远程连接

# 读写测试脚本

代码语言:javascript复制
# -*- coding: utf-8 -*-
"""
@Time    : 2022/3/4 18:04
@Author  : summer
@File    : mongo_test.py
@Software: PyCharm
"""
import random
import pymongo
import time, datetime

# 时间记录器
def func_time(func):
    def _wrapper(*args, **kwargs):
        start = time.time()
        func(*args, **kwargs)
        print(func.__name__, 'run:', time.time() - start)
    return _wrapper


@func_time
def insert(num):
    posts = db.userinfo
    for x in range(num):
        post = {"_id": str(x),
                "author": str(x)   "Mike",
                "text": "My first blog post!",
                "tags": ["mongodb", "python", "pymongo"],
                "date": datetime.datetime.utcnow()}
        posts.insert_one(post)


# @func_time
def randy():
    rand = random.randint(1, 5000000)
    return rand


@func_time
def mread(num):
    find = db.userinfo
    for i in range(num):
        rand = randy()
        # 随机数查询
        find.find({"author": str(rand)   "Mike"})


if __name__ == "__main__":
    # client = pymongo.MongoClient("mongodb://xtest:xtest@192.168.150.20:27017/")
    client = pymongo.MongoClient("mongodb://admin:admin@192.168.150.20:27017/")
    db_list = client.list_database_names()
    # 清理数据
    if "ddd" in db_list:
        db = client.ddd
        collection_names = db.list_collection_names()
        for i in collection_names:
            db["userinfo"].drop()
    else:
        db = client.ddd
    # 设定循环500万次
    num = 20
    insert(num)
    # 设定循环100万次
    num1 = 100
    mread(num1)

0 人点赞