Mongodb-入门
一.Mongod基础运维部分
1.安装MongoDB
#软件下载
https://www.mongodb.com/try/download/community
#2.上传解压mongod软件
mkdir -p /mongodb && cd /mongodb
rz mongodb-linux-x86_64-rhel70-4.2.8.tgz
tar xf mongodb-linux-x86_64-rhel70-4.2.8.tgz
mv mongodb-linux-x86_64-rhel70-4.2.8.tgz /usr/local/mongodb
#3.添加环境变量
vim /etc/profile
export PATH=/usr/local/mongodb/bin:$PATH
source /etc/profile
#4.关闭THP
root用户下
在vim /etc/rc.local最后添加如下代码
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
查看:
[root@lss mongodb]# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
[root@lss mongodb]# cat /sys/kernel/mm/transparent_hugepage/defrag
[always] madvise never
其他系统关闭参照官方文档:
https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/
为什么要关闭?
Transparent Huge Pages (THP) is a Linux memory management system
that reduces the overhead of Translation Lookaside Buffer (TLB)
lookups on machines with large amounts of memory by using larger memory pages.
However, database workloads often perform poorly with THP,
because they tend to have sparse rather than contiguous memory access patterns.
You should disable THP on Linux machines to ensure best performance with
MongoDB.
透明巨型页面(THP)是一个Linux内存管理系统,这减少了转换查找缓冲区(TLB)的开销,通过使用更大的内存页查找具有大量内存的机器。然而,使用THP时,数据库工作负载通常表现不佳,因为它们往往具有稀疏而非连续的内存访问模式。您应该在Linux机器上禁用THP,以确保最佳性能MongoDB。
2.环境准备
(1)创建所需用户和组
useradd mongod
passwd mongod
输入mongod(密码)
(2)创建mongodb所需目录结构
mkdir -p /mongodb/conf
mkdir -p /mongodb/log
mkdir -p /mongodb/data
(3) 修改权限
chown -R mongod:mongod /mongodb
(4) 切换用户并设置环境变量
su - mongod
vi .bash_profile
export PATH=/usr/local/mongodb/bin:$PATH
source .bash_profile
3.启动数据库并初始化数据
#1.方式一:直接命令启动
su - mongod
mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork
[mongod@lss conf]$ mongo
MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f52da758-bd97-4c95-a54c-d6276d8c7dc1") }
MongoDB server version: 4.2.8
show databases;
use admin;
show tables;
#2.方式二:用配置文档启动
1.常规配置文档
vim /mongodb/conf/mongodb.conf
logpath=/mongodb/log/mongodb.log
dbpath=/mongodb/data
port=27017
logappend=true
fork=true
# 关闭mongodb
mongod -f /mongodb/conf/mongodb.conf --shutdown
使用配置文件启动mongodb
mongod -f /mongodb/conf/mongodb.conf
#登录mongo 需要切换到use admin
db.shutdownServer() 也可关闭mongod
2.yaml格式配置文档
# YAML 配置文件应用
--
NOTE:
YAML does not support tab characters for indentation: use spaces instead.
--系统日志有关
systemLog:
destination: file
path: "/mongodb/log/mongodb.log" --日志位置
logAppend: true --日志以追加模式记录
--数据存储有关
storage:
journal:
enabled: true
dbPath: "/mongodb/data" --数据路径的位置
-- 进程控制
processManagement:
fork: true --后台守护进程
pidFilePath: <string> --pid文件的位置,一般不用配置,可以去掉这行,自动
生成到data中
--网络配置有关
net:
bindIp: <ip> -- 监听地址,如果不配置这行是监听在0.0.0.0
port: <port> -- 端口号,默认不配置端口号,是27017
-- 安全验证有关配置
security:
authorization: enabled --是否打开用户名密码验证
------------------以下是复制集与分片集群有关----------------------
replication:
oplogSizeMB: <NUM>
replSetName: "<REPSETNAME>"
secondaryIndexPrefetch: "all"
sharding:
clusterRole: <string>
archiveMovedChunks: <boolean>
---for mongos only
replication:
localPingThresholdMs: <int>
sharding:
configDB: <string>
---
.........
++++++++++++++++++++++
#编写简易配置档
YAML例子
vim /mongodb/conf/mongo.conf
systemLog:
destination: file
path: "/mongodb/log/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/mongodb/data/"
processManagement:
fork: true
net:
port: 27017
bindIp: 10.0.0.150,127.0.0.1
#关闭:
mongod -f /mongodb/conf/mongo.conf --shutdown
#开启
mongod -f /mongodb/conf/mongo.conf
[mongod@lss conf]$ mongod -f /mongodb/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 8703
child process started successfully, parent exiting
++++++++++++++++++++++
#mongodb的关闭方式
mongod -f mongodb.conf --shutdown
#登录mongo
db.shutdownServer() 也可关闭mongod
4.用户基本管理
1.注意事项
用户管理 *****
注意:
验证库,建立用户时use到的库,在使用用户时,要加上验证库才能登陆。
对于管理员用户,必须在admin下创建.
1. 建用户时,use到的库,就是此用户的验证库
2. 登录时,必须明确指定验证库才能登录
3. 通常,管理员用的验证库是admin,普通用户的验证库一般是所管理的库设置为验证库
4. 如果直接登录到数据库,不进行use,默认的验证库是test,不是我们生产建议的.
2. 基本语法
3.用户实例
(1)--创建超级管理员:管理所有数据库(必须use admin再去创建)
$ mongo
> use admin
switched to db admin
> db.createUser(
... {
... user: "root",
... pwd: "root123",
... roles: [ { role: "root", db: "admin" } ]
... }
... )
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
#验证用户
> db.auth('root','root123')
1
(2).创建普通用户
db.createUser(
{
user: "app01",
pwd: "app01",
roles: [ { role: "readWrite" , db: "app" } ]
}
)
> db.auth('app01','app01')
1
(3).开启认证,将以下信息写入配置档中,重起mongodb
配置文件中,加入以下配置
security:
authorization: enabled
重启mongodb
mongod -f /mongodb/conf/mongo.conf --shutdown
mongod -f /mongodb/conf/mongo.conf
(4).重新登录mongo
[root@lss ~]# mongo -u root -p root123 10.0.0.150/admin
MongoDB shell version v4.2.8
connecting to: mongodb://10.0.0.150:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("13dd260c-8b0f-4e6c-b573-534cdbdc8f93") }
MongoDB server version: 4.2.8
#查看用户:
use admin #要查用户需要查好用户对用的
db.system.users.find().pretty()
> db.system.users.find().pretty()
{
"_id" : "admin.root",
"userId" : UUID("32c5bce8-6bbb-4e4d-be46-35f3d0adcdb6"),
"user" : "root",
"db" : "admin",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,
"salt" : "UvY7mHKBVOb6jFDWTvYrIg==",
"storedKey" : "QNaK1pyuQ4wFmjQf91dABFJgWuU=",
"serverKey" : "/iEKzPvH6gb43ZIFeAypcSoUlJo="
},
"SCRAM-SHA-256" : {
"iterationCount" : 15000,
"salt" : "5rclUKutvHdivPkkV5GNhq0txt+PRL3FiqSetQ==",
"storedKey" : "EJJDiRMzEUd0BH1R4uBfaA9QWB4cV6j6fuz+M9L50fI=",
"serverKey" : "uJTtn0d2/L3YtzJjSRC9X/L48AkC+ADZ6yFsZhdpgZM="
}
},
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
{
"_id" : "admin.app01",
"userId" : UUID("3171b30e-1864-4217-a575-41325818535a"),
"user" : "app01",
"db" : "admin",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,