MongoDBのデータベースセキュリティとGridFS

MongoDBデータベースのユーザー名とパスワードを設定する

/etc/xxx.conf

mongodb構成ファイルを編集してユーザー認証を有効にする

vim /etc/mongodb.conf
# IP
bind_ip = 0.0.0.0
# 端口
port = 27017

次のセキュリティ構成を変更します

# Turn on/off security.  Off is currently the default
#noauth = true
#auth = true

削除#auth=true前のコメントを#

auth = true

再起動mongodbサービス

service mongodb restart

mongoシェルを使用してサーバーに接続する

# 连接服务器
mongo

# 连接到 admin 数据库
use admin

# 创建管理员账号,用户名和密码可以任意,角色为 `root`
db.createUser({user:'root',pwd:'123456',roles:['root']})

# 登录
db.auth('root','123456')

管理データベースにログインした後

user admin

db.auth('root','hello')

show collections
> system.users
> system.version

# 显示用户信息
db.system.users.find().pretty()

特定のデータベースのユーザー名とパスワードを設計する

# 创建数据库
use hr

# 创建用户
db.createUser({
  user:'hr_user',
  pwd:'123',
  roles:[{db:'hr',role:'dbOwner'}]
  })

# 显示用户
show users

データベースに接続する

mongo 127.0.0.1:27017/demo -u demo -p
# 获得 mongo shell 更多信息
mongo --help

データモデリング

  • 文档埋め込み子文档
  • ドキュメント間引用$lookupリレーショナルデータベースのテーブル接続と同様)

MongoDBドキュメントは16Mを超えることはできません(ドキュメントをネストしすぎないでください)

MongoDB GridFS(グリッドファイルシステム)

大きなファイル(16M以上)を格納するために使用され、ファイル(デフォルト)は255kブロックストレージにカットされ、2つのセットに格納されます。1つはファイルのメタ情報を格納し、もう1つはファイルのデータを格納します

# 往数据库 myfile 中上传(put)文件
mongofiles -d myfile put 文件路径

# 显示数据库 myfile 中的文件
mongofiles -d myfile list

ファイルは2つのコレクションに保存されます

  • fs.filesファイルのメタデータ情報(ファイル名、サイズ、ブロックサイズ、日付、_id)
  • fs.chunksファイルの内容(files_id、content、format)

mongofiles リモートデータベースにアップロード

mongofiles -h ip -d db_name -u user -p password put file_path
mongofiles -h ip -d db_name -u user -p password list

mongofiles ツールはコマンドライン開発ツールです

138の元の記事を公開 394を賞賛 30,000以上のビュー

おすすめ

転載: blog.csdn.net/weixin_44364444/article/details/105687478