文章目录
一、安装配置
1. mongodb安装
官网参考: https://docs.mongodb.com/v3.6/tutorial/install-mongodb-on-red-hat/
//1.设置安装源
vim /etc/yum.repos.d/mongodb-org-3.6.repo
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
//2.安装
sudo yum install -y mongodb-org
//3.启动
sudo systemctl start mongod
//4.开机启动
sudo systemctl enable mongod
//5.关闭|重启
sudo systemctl stop mongod
sudo systemctl restart mongod
//连接
mongo --host 127.0.0.1:27017
2. 配置 Disable Transparent Huge Pages (THP)
官网参考: https://docs.mongodb.com/v3.6/tutorial/transparent-huge-pages/
3. 配置文件mongod.conf
外网访问和密码验证,文件路径/etc/mongod.cnf
官网参考: https://docs.mongodb.com/v3.6/administration/configuration/
添加修改如下内容:
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
security:
authorization: enabled
4. 连接数设置
官方推荐设置:
https://docs.mongodb.com/v3.6/reference/ulimit/#recommended-ulimit-settings
-f (file size): unlimited
-t (cpu time): unlimited
-v (virtual memory): unlimited [1]
-l (locked-in-memory size): unlimited
-n (open files): 64000
-m (memory size): unlimited [1] [2]
-u (processes/threads): 64000
5. 数据定时备份
参考:https://www.jb51.net/article/135847.htm
//1.创建存储目录
cd /data
mkdir mongo
mkdir backup
mkdir backup/now
mkdir backup/list
//2.编辑脚本
vim backup.sh
#!/bin/sh
OUT_DIR=/data/mongo/backup/now #临时备份目录
TAR_DIR=/data/mongo/backup/list #备份存放路径
DATE=`date +%Y_%m_%d` #获取当前系统时间
DB_USER=root #数据库账号
DB_PASS=123456 #数据库密码
DAYS=7 #DAYS=7代表删除7天前的备份,即只保留最近7天的备份
TAR_BAK="mongo_backup_$DATE.tar.gz" #最终保存的数据库备份文件名
cd $OUT_DIR
rm -rf $OUT_DIR/*
mkdir -p $OUT_DIR/$DATE
mongodump -u $DB_USER -p $DB_PASS -o $OUT_DIR/$DATE #备份全部数据库
tar -zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE #压缩为.tar.gz格式
find $TAR_DIR/ -mtime +$DAYS -delete #删除7天前的备份文件
//3.添加脚本执行权限
chmod +x backup.sh
//4.添加定时任务
crontab -e
//每天凌晨2点30进行备份
30 2 * * * sh /data/mongo/backup.sh
crontab -l
二、命令操作
1. mongodb账号密码
参考文档:
https://www.jianshu.com/p/d21cdf929a2e
https://docs.mongodb.com/v3.6/tutorial/enable-authentication/
//1.创建管理员账号
use admin
db.createUser(
{
user: "root",
pwd: "123456",
roles: [
{ role: "userAdminAnyDatabase", db: "admin" }
]
}
)
//2.登录
db.auth('root','123456')
mongo -u root -p 123456
//3.授权
db.grantRolesToUser(
"root",
[
{ role: "root", db: "admin" },
]
)
db.grantRolesToUser("root",[{role:"readWrite",db:"common"}])
//4.读写权限账号
db.createUser(
{
user: "admin",
pwd: "123456",
roles: [
{ role: "readWriteAnyDatabase", db: "admin" }
]
}
)
db.auth('admin','123456')
//5.查看权限账号
db.createUser(
{
user: "read",
pwd: "123456",
roles: [
{ role: "readAnyDatabase", db: "admin" }
]
}
)
//6.数据库权限账号
use hall
db.createUser({user:"test",pwd:"123456",roles:[{role:"dbOwner",db:"test"}]})
2. 性能统计设置
参考文档:
https://docs.mongodb.com/v3.6/tutorial/manage-the-database-profiler/#database-profiling-view-status
db.setProfilingLevel(1)
//最近10条慢操作
db.system.profile.find().limit(10).sort( { ts : -1 } ).pretty()
3. 数据备份
参考文档:
https://docs.mongodb.com/v3.6/tutorial/backup-and-restore-tools/
cd /data/mongo/backup
mkdir 2020****
cd 2020****
mongodump --username root --password 123456
//显示备份目录大小
du -h --max-depth=1 /data/mongo/backup/
//数据还原
mongorestore --host mongodb1.example.net --port 3017 --username user --password 'pass' /opt/backup/mongodump-2013-10-24
4. 常用命令
// 1.查询 昵称包含指定字符串,显示指定字段
db.role.find({"nick":{$regex:"Helen"}},{nick:1,rid:1,gold:1})