1、环境介绍
操作系统:centos 7.7
mongodb版本:5.0.2
mongodb兼容性:
cpu支持:
mongodb下载地址:
https://www.mongodb.com/try/download/community?tck=docs_server
2、关闭防火墙和selinux
3、安装依赖包
yum install libcurl openssl xz-libs
4、上传软件包,解压,并重名到/usr/local/mongodb
5、创建环境变量软连接
ln -s /usr/local/mongodb/bin/* /usr/local/bin/
6、安装mongosh
(1)下载mongosh
https://www.mongodb.com/try/download/shell?jmp=docs
(2)上传软件包,解压,并重名到/usr/local/mongosh
(3)添加执行权限
chmod +x /usr/local/mongosh/bin/mongocryptd-mongosh chmod +x /usr/local/mongosh/bin/mongosh
(4)创建环境变量软连接
ln -s /usr/local/mongosh/bin/* /usr/local/bin/
7、设置ulimit
vi /etc/security/limits.conf
添加如下:
mongod soft fsize unlimited mongod hard fsize unlimited mongod soft cpu unlimited mongod hard cpu unlimited mongod soft as unlimited mongod hard as unlimited mongod soft nofile 64000 mongod hard nofile 64000 mongod soft nproc 64000 mongod hard nproc 64000
8、创建用户和组并添加sudo权限
groupadd mongod useradd -m -g mongod mongod echo "mongod" | passwd --stdin mongod
visudo
添加如下
mongod ALL=(ALL) NOPASSWD:ALL
切换用户,查看第5步,已经生效
9、创建 MongoDB 数据和日志目录并更改权限
sudo mkdir -p /var/lib/mongo sudo mkdir -p /var/log/mongodb
sudo chown -R mongod:mongod /var/lib/mongo sudo chown -R mongod:mongod /var/log/mongodb
10、启动和停止数据库
启动
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --bind_ip 0.0.0.0 --fork
停止
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --bind_ip 0.0.0.0 --shutdown
11、用mongosh 连接数据库并创建管理员用户
mongosh --host localhost --port 27017
use admin; db.createUser( { user: "admin", pwd: passwordPrompt(), // or cleartext password roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { role: "readWriteAnyDatabase", db: "admin" } ] } )
12、启用安全连接
启动
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --bind_ip 0.0.0.0 --auth --fork
停止
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --bind_ip 0.0.0.0 --auth --shutdown
13、重新安全连接
mongosh -u admin -p Smtgbk_123 --host localhost --port 27017
Navicat连接