### 操作系统版本
[root@vm7-121 ~]# cat /etc/redhat-release;uname -r;uname -m
CentOS Linux release 7.9.2009 (Core)
3.10.0-1160.el7.x86_64
x86_64
### 网卡及配置
[root@vm7-121 ~]# ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:8d:d6:eb brd ff:ff:ff:ff:ff:ff
inet 10.4.7.121/24 brd 10.4.7.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe8d:d6eb/64 scope link
valid_lft forever preferred_lft forever
### 能否访问互联网
[root@vm7-121 ~]# ping -l 10.4.7.121 -c 2 www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=128 time=34.2 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=128 time=34.4 ms
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 34.239/34.359/34.480/0.221 ms, pipe 2
### 更改主机名
hostnamectl set-hostname \
vm$(ip addr show ens33|awk -F "[ /]" 'NR==3{print $6}'|awk -F "." '{print $3"-"$4}').host.com
### 更改base源和epel源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install -y epel-release
### 安装常用工具
yum install -y \
tree telnet lrzsz wget ntpdate vim nc namp dos2unix tcpdump expect elinks \
lsof net-tools iproute bridge-utils \
bind-utils nscd \
gcc gcc-c++ make cmake libaio zlib-devel pcre-devel \
psmisclsof sysstat yum-utils
### 关闭SELINUX
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
setenforce 0
### 设置支持中文字符
echo "LANG=\"zh_CN.UTF-8\"" >/etc/locale.conf
### 资源描述符
cat >>/etc/security/limits.conf<<EOF
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
* soft stack 65535
* hard stack 65535
EOF
echo " * - nproc 65535" >/etc/security/limits.d/20-nproc.conf
echo "session required pam_limits.so" >>/etc/pam.d/login
################ 关闭大内存页
## 立即关闭
echo "never" >/sys/kernel/mm/transparent_hugepage/defrag
echo "never" >/sys/kernel/mm/transparent_hugepage/enabled
## 让其下次开机时执行后生效
cat >>/etc/rc.local<<'EOF'
#### Transparent Huge Pages(THP)
echo "never" >/sys/kernel/mm/transparent_hugepage/defrag
echo "never" >/sys/kernel/mm/transparent_hugepage/enabled
EOF
官方的yum源怎么配置从哪里找(官方地址为:https://docs.mongodb.com/v4.0/tutorial/install-mongodb-on-red-hat/ )有没有看到我给出来官方地址中是有mongodb版本的哈。创建/etc/yum.repos.d/mongodb-org-4.0.repo文件,并将以下内容放在里面。
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
我是用我的帐号chenliang登录的服务器,这个帐号我在root用户下对它进行了visudo授权 chenliang ALL=(ALL) NOPASSWD:ALL
#### 添加mongodb的官方源
cat >>/etc/yum.repos.d/mongodb-org-4.0.repo<<'EOF'
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
EOF
#### 查看是否添加成功
[root@vm7-121 ~]#
[root@vm7-121 ~]# cat /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
#### 查看当前可以安装的mongodb版本有哪些(只会有mongodb 4.0.x版本的,因为我添加的yum源只是4.0的)
[root@vm7-121 ~]#
[root@vm7-121 ~]# yum list mongodb-org --showduplicates
已加载插件:fastestmirror
Determining fastest mirrors
* base: mirrors.aliyun.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
可安装的软件包
mongodb-org.x86_64 4.0.0-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.1-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.2-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.3-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.4-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.5-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.6-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.7-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.8-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.9-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.10-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.11-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.12-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.13-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.14-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.15-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.16-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.17-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.18-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.19-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.20-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.21-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.22-1.el7 mongodb-org-4.0
mongodb-org.x86_64 4.0.23-1.el7 mongodb-org-4.0
#### 安装mongodb的指定版本(我这里安装mongodb 4.0.2版本)
chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow
yum install -y mongodb-org-4.0.2-1.el7
# 在安装的过程中会安装如下的软件包,
# mongodb-org-4.0.2-1.el7.x86_64.rpm
# mongodb-org-mongos-4.0.2-1.el7.x86_64.rpm
# mongodb-org-shell-4.0.2-1.el7.x86_64.rpm
# mongodb-org-tools-4.0.2-1.el7.x86_64.rpm
# mongodb-org-server-4.0.2-1.el7.x86_64.rpm
#### 安装后的相关工具在哪
[root@vm7-121 ~]#
[root@vm7-121 ~]# which mongo mongod mongodump
/bin/mongo
/bin/mongod
/bin/mongodump
#### 安装后有创建一个傀儡用户mongod,我们不用它,也不管它。
[root@vm7-121 tools]#
[root@vm7-121 tools]# id mongod
uid=998(mongod) gid=996(mongod) 组=996(mongod)
[root@vm7-121 tools]#
#### 我们另外创建一个app用户,后面在app用户下启动mongod服务
chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow
useradd app
id app
echo "app123456"|passwd --stdin app
#### 安装后有配置文件(/etc/mongodb.conf),我不用这个配置文件,我会给删除掉(我这是测试环境),
find /etc -maxdepth 1 -type f -name "mongod.conf"
find /etc -maxdepth 1 -type f -name "mongod.conf"|xargs rm -f
#### 安装后会有一个启动脚本,我也会删除掉(我这是测试环境),我会用我自己写的脚本
find /usr/lib/systemd/system -maxdepth 1 -type f -name "mongod.service"
find /usr/lib/systemd/system -maxdepth 1 -type f -name "mongod.service"|xargs rm -f
/data/mongodb/27017/ # 以端口区分每个实例
├── conf # 存放27017实例的配置文件
├── data # 存放27017实例的数据文件
├── logs # 存放27017实例的日志文件
├── run # 存放27017实例的pid和socket文件
└── scripts # 存放27017实例的相关脚本(启停脚本、数据备份脚本等)
这里只是部署服务,但不启动服务。
#### 创建相关的目录
mkdir -p /data/mongodb/27017/{data,conf,logs,run,scripts}
tree /data/mongodb/27017
#### 准备27017实例的配置文件(简化版,注意,IP地址填你自己服务器的IP地址,请把127.0.0.1也监听上,因为我开了访问控制的)
cat >>/data/mongodb/27017/conf/mongod.conf<<'EOF'
## network
net:
port: 27017
ipv6: false
bindIpAll: false
bindIp: 10.4.7.121,127.0.0.1
maxIncomingConnections: 3000
wireObjectCheck: true
unixDomainSocket:
enabled: true
pathPrefix: /data/mongodb/27017/run
filePermissions: 0700
## storage
storage:
dbPath: /data/mongodb/27017/data
journal:
enabled: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 6
## log
systemLog:
destination: file
logAppend: true
path: /data/mongodb/27017/logs/mongod.log
quiet: false
## process
processManagement:
fork: true
pidFilePath: /data/mongodb/27017/run/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
## security
security:
authorization: enabled
javascriptEnabled: false
EOF
#### 更改/data/mongodb/目录极其子目录的属主/组
chown -R app:app /data/mongodb/
准备启停脚本 /data/mongodb/27017/scripts/mongod.sh
#!/bin/bash
#
# Explain: MongoDB start、stop scripts
#
# Define variables
RETVAL=0
MonPort="27017"
MonConf="/data/mongodb/$MonPort/conf/mongod.conf"
MonCommPath="/usr/bin"
# Load local funcation
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
# Define functions
start(){
MonStatusCheck="$(ss -lntup|grep -w "$MonPort"|wc -l)"
if [ "$MonStatusCheck" -eq 0 ];then
$MonCommPath/mongod -f $MonConf --fork >/dev/null 2>&1
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "Start mongodb service[$MonPort]" /bin/true
else
action "Start mongodb service[$MonPort]" /bin/false
fi
else
echo "mongodb service[$MonPort] is running"
fi
return $RETVAL
}
stop(){
MonStatusCheck="$(ss -lntup|grep -w "$MonPort"|wc -l)"
if [ "$MonStatusCheck" -ne 0 ];then
$MonCommPath/mongod --shutdown -f $MonConf >/dev/null 2>&1
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "Stop mongodb service[$MonPort]" /bin/true
else
action "Stop mongodb service[$MonPort]" /bin/false
fi
else
echo "mongodb service[$MonPort] is not running"
fi
return $RETVAL
}
status(){
MonStatusCheck="$(ss -lntup|grep -w "$MonPort"|wc -l)"
if [ "$MonStatusCheck" -ne 0 ];then
echo "mongodb service[$MonPort] is running"
else
echo "mongodb service[$MonPort] is not running"
fi
return $RETVAL
}
# case call define functiosn
case "$1" in
start)
start
RETVAL=$?
;;
stop)
stop
RETVAL=$?
;;
restart)
stop
sleep 5
start
RETVAL=$?
;;
status)
status
RETVAL=$?
;;
*)
echo "USAGE:$0{start|stop|restart|status}"
exit 1
esac
给启停脚本加权限,更改相应的属主/组
#### 更改属主/组为app用户
chown -R app:app /data/mongodb
#### 给脚本加可执行权限
ll /data/mongodb/27017/scripts/mongod.sh
chmod 744 /data/mongodb/27017/scripts/mongod.sh
ll /data/mongodb/27017/scripts/mongod.sh
测试启停脚本
#### 检查shell脚本(重启mongodb实例)
[root@vm7-121 ~]#
[root@vm7-121 ~]# sudo su - app -s /bin/bash -c "/data/mongodb/27017/scripts/mongod.sh"
USAGE:/data/mongodb/27017/scripts/mongod.sh{start|stop|restart|status}
[root@vm7-121 ~]#
[root@vm7-121 ~]# sudo su - app -s /bin/bash -c "/data/mongodb/27017/scripts/mongod.sh status"
mongodb service[27017] is not running
[root@vm7-121 ~]#
[root@vm7-121 ~]# sudo su - app -s /bin/bash -c "/data/mongodb/27017/scripts/mongod.sh start"
Start mongodb service[27017] [ 确定 ]
[root@vm7-121 ~]#
[root@vm7-121 ~]# sudo su - app -s /bin/bash -c "/data/mongodb/27017/scripts/mongod.sh status"
mongodb service[27017] is running
[root@vm7-121 ~]#
[root@vm7-121 ~]# sudo su - app -s /bin/bash -c "/data/mongodb/27017/scripts/mongod.sh stop"
Stop mongodb service[27017] [ 确定 ]
[root@vm7-121 ~]#
[root@vm7-121 ~]# sudo su - app -s /bin/bash -c "/data/mongodb/27017/scripts/mongod.sh start"
Start mongodb service[27017] [ 确定 ]
#### 检查mongodb服务是否正常运行
[root@vm7-121 ~]#
[root@vm7-121 ~]# ps -ef|grep mongod|grep -v grep
app 4314 1 5 16:55 ? 00:00:01 /usr/bin/mongod -f /data/mongodb/27017/conf/mongod.conf --fork
[root@vm7-121 ~]#
[root@vm7-121 ~]# lsof -i :27017
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 4314 app 11u IPv4 37441 0t0 TCP vm7-121.host.com:27017 (LISTEN)
mongod 4314 app 12u IPv4 37442 0t0 TCP localhost:27017 (LISTEN)
将脚本加入开机自启动(我是放在/etc/rc.local文件中)
#### 将脚本放在/etc/rc.local文件中
cat >>/etc/rc.local<<EOF
# Boot start mongodb service. USER:chenliang TIME:$(date +%F)
chown -R app:app /data/mongodb/
su - app -s /bin/bash -c "/data/mongodb/27017/scripts/mongod.sh start"
EOF
#### 查看/etc/rc.local文件的后三行
[root@vm7-121 ~]#
[root@vm7-121 ~]# sudo tail -3 /etc/rc.local
# Boot start mongodb service. USER:chenliang TIME:2021-11-17
chown -R app:app /data/mongodb/
su - app -s /bin/bash -c "/data/mongodb/27017/scripts/mongod.sh start"
#### 给/etc/rc.local文件所指定的文件加执行权限
[root@vm7-121 ~]#
[root@vm7-121 ~]# chmod 744 /etc/rc.d/rc.local
[root@vm7-121 ~]# ll /etc/rc.d/rc.local
-rwx------. 1 root root 614 3月 28 21:36 /etc/rc.d/rc.local
给MongoDB实例创建一个超级用户
// 为啥要这样连接,因为我启动前开了访问控制的,你不这样连接,你进行后无法创建用户哈;
mongo 127.0.0.1:27017
use admin;
db.createUser(
{
user:"root",
pwd:"root123456",
roles:[
{role:"root",db:"admin"}
]
}
);
db.auth("root","root123456"); // 进行认证
exit; // 退出当前连接
给我这个管理员创建一个超级用户
// 用之前创建的root用户连接mongod实例
mongo --host=10.4.7.121 --port=27017 --username=root --password=root123456 --authenticationDatabase=admin
db.getName();
use admin;
db.createUser(
{
user:"chenliang",
pwd:"chenliang123456",
roles:[
{role:"root",db:"admin"}
]
}
);
exit;
// 验证chenliang用户
mongo --host=10.4.7.121 --port=27017 --username=chenliang --password=chenliang123456 --authenticationDatabase=admin
db.getName();
show dbs;
exit;