一、环境
操作系统:CentOS Linux release 7.9.2009 (Core)
mongodb版本: version v5.0.3
安装包:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.3.tgz
windows客户端:https://download.studio3t.com/robomongo/windows/robo3t-1.4.4-windows-x86_64-e6ac9ec5.exe
开发环境需求: 安装mongodb 5.0 的单机版
官方安装文档参考:https://docs.mongodb.com/guides/
二、安装
# 创建安装目录
mkdir -p /opt/mongo
# 进入目录下载
cd /opt/mongo
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.3.tgz
tar zxvf mongodb-linux-x86_64-rhel70-5.0.3.tgz
# 建立软连接
ln -s mongodb-linux-x86_64-rhel70-5.0.3 mongodb
# 配置环境变量
vim ~/.bash_profile
export PATH=$PATH:/opt/mongo/mongodb/bin
source ~/.bash_profile
# 检测配置是否成功
mongo -version
# 创建相应目录
cd /opt/mongo/mongodb
mkdir -p data/db # 数据目录:用于存储 mongodb 数据
mkdir log # 日志目录:用于存储 mongodb 日志
mkdir conf # 配置文件目录:用于存放配置文件
# 创建日志文件
cd /opt/mongo/mongodb/log
touch mongodb.log
# 创建配置文件
cd /opt/mongo/mongodb/conf
touch mongodb.conf
# 编辑内容
vim mongodb.conf
port=27017 # 端口
dbpath=/opt/mongo/mongodb/data/db # 数据库存文件存放目录
logpath=/opt/mongo/mongodb/log/mongodb.log # 日志文件存放路径
logappend=true # 使用追加的方式写日志
fork=true # 以守护进程的方式运行,创建服务器进程
maxConns=5000 # 最大同时连接数
journal=true # 每次写入会记录一条操作日志(通过journal可以重新构造出写入的数据)。
# 即使宕机,启动时wiredtiger会先将数据恢复到最近一次的checkpoint点,然后重放后续的journal日志来恢复。
storageEngine=wiredTiger # 存储引擎有mmapv1、wiretiger、mongorocks
bind_ip = 0.0.0.0 # 允许任意外部地址访问
auth=true # 启用验证
# 目录参考
tree -L 2 -I "LICENSE-Community.txt|MPL-2|THIRD-PARTY-NOTICES|README"
.
├── bin
│ ├── install_compass
│ ├── mongo
│ ├── mongod
│ └── mongos
├── data
│ └── db
├── conf
│ └── mongodb.conf
└── log
└── mongodb.log
# 启动
mongod --config /opt/mongo/mongodb/conf/mongodb.conf
# 启动成功参考
mongod --config /opt/mongo/mongodb/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 14298
child process started successfully, parent exiting
# 关闭
ps -ef | grep mongodb # 获取 mongodb 进程信息
kill -9 进程Id
# 首次连接,添加用户
mongo --host <HOSTNAME> --port <PORT>
# 由于在本机且已经配置好环境变量,因此直接mongo命令即可
mongo
# 样例参考
mongo
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("56adc0aa-ca81-4b9f-8824-07dadc181f7f") }
MongoDB server version: 5.0.3
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
>
// 使用admin数据库
use admin
// 给admin数据库添加管理员用户名和密码,用户名和密码请自行设置
db.createUser({user:"admin",pwd:"R7v4f1kZBO",roles:["root"]})
// 验证是否成功,返回1则代表成功
db.auth("admin", "R7v4f1kZBO")
// 切换其他设置数据库,例如:test
use test
// 为test创建用户,用户名和密码请自行设置。
db.createUser({user: "test", pwd: "R7v4f1kZBO", roles: [{ role: "dbOwner", db: "test" }]})
# 关闭
ps -ef | grep mongodb # 获取 mongodb 进程信息
kill -9 进程Id 或者 kill -9 进程Id
# 创建好用户,再次启动
mongod --config /opt/mongo/mongodb/conf/mongodb.conf
# 成功样例
mongod --config /opt/mongodb/mongodb/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 14397
child process started successfully, parent exiting
# 启用防火墙,则打开相应防火墙
firewall-cmd --zone=public --add-port=27017/tcp --permanet
firewall-cmd --reload
三、界面连接工具
下载地址,这里以windows为例,其它系统自行下载即可:
windows客户端:https://download.studio3t.com/robomongo/windows/robo3t-1.4.4-windows-x86_64-e6ac9ec5.exe
安装比较简单,默认一路next即可;

安装完成后,配置关键项目:
(1)在第一个选项中,输入地址
(2)在第二个选项中,输入用户名及密码


连接登录成功,如图所示:

四、附录
1.常见命令
# mongo --host <HOSTNAME> --port <PORT>
# 由于在本机且已经配置好环境变量,因此直接mongo命令即可
mongo
help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.mycoll.find() list objects in collection mycoll
db.mycoll.find( { a : 1 } ) list objects in mycoll where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
>db.version();
5.0.3
2.随机密码
cat /dev/urandom | LC_ALL=C tr -dc "[:alnum:]" | fold -w 10 |head -10
cat /dev/urandom | LC_ALL=C tr -dc "[:graph:]" | fold -w 10 |head -10
3、开机自启动
#!/bin/sh
#
#chkconfig: 2345 80 90
#description: mongodb
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
start() {
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf
}
stop() {
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
其它自启动
把这句话写到把 /etc/rc.d/rc.local,即可开机启动。
/opt/mongo/mongodb/bin --config /opt/mongo/mongodb/conf/mongodb.conf
网友评论