更新依赖包
安装前我们需要安装各个 Linux 平台依赖包。
Red Hat/CentOS:
sudo yum install libcurl openssl
Ubuntu 18.04 LTS ("Bionic")/Debian 10 "Buster":
sudo apt-get install libcurl4 openssl
Ubuntu 16.04 LTS ("Xenial")/Debian 9 "Stretch":
sudo apt-get install libcurl3 openssl
下载Mongodb源码,解压、配置
MongoDB 提供了 linux 各个发行版本 64 位的安装包,可以在官网下载安装包。
源码下载地址: https://www.mongodb.com/try/download/community
这里我们选择 tgz 下载,下载完安装包,并解压 tgz(以下演示的是 64 位 Linux上的安装) 。
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-4.2.8.tgz # 下载
tar -zxvf mongodb-linux-x86_64-ubuntu1604-4.2.8.tgz # 解压
mv mongodb-src-r4.2.8 /usr/local/mongodb4 # 将解压包拷贝到指定目录
MongoDB 的可执行文件位于 bin 目录下,所以可以将其添加到 PATH 路径中:
[root@itroot mongodb]# vim /etc/profile
# 添加以下配置
export MONGODB_HOME=/usr/local/mongodb
export PATH=$PATH:$MONGODB_HOME/bin
刷新配置,使其生效
source /etc/profile
创建数据库目录
默认情况下 MongoDB 启动后会初始化以下两个目录:
- 数据存储目录:/var/lib/mongodb
- 日志文件目录:/var/log/mongodb
我们在启动前可以先创建这两个目录并设置当前用户有读写权限:
sudo mkdir -p /var/lib/mongo
sudo mkdir -p /var/log/mongodb
sudo chmod -R 777 /var/lib/mongo
sudo chmod -R 777 /var/log/mongodb
接下来启动 Mongodb 服务:
有两种方式,一种是直接输入命令参数,一种是指定配置文件
输入命令参数启动
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
指定配置文件
[root@itroot mongodb]# vim mongodb.conf
#添加以下配置
dbpath=/usr/local/mongodb/db #数据文件保存地址
logpath=/usr/local/mongodb/logs/mongodb.log #日志保存地址
port=27017 #端口
fork=true #是否后台启动
auth=true #是否开启权限,访问需要用户名和密码
bind_ip=0.0.0.0 #任意ip都能访问
logappend=true
启动mongodb
mongod --config /usr/local/mongodb/mongodb.conf
打开 /var/log/mongodb/mongod.log 文件看到有输出信息,说明启动成功。
# tail -10f /var/log/mongodb/mongod.log
添加用户名密码
连接mongodb,打开bin目录下的mongo执行文件
bin/mongo
[root@iZ6we4yxap93y2r0clg3g8Z mongodb4]# bin/mongo
MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1a7e772c-cb50-41c7-b0ef-4a74b3a5f78f") }
MongoDB server version: 4.4.4
---
The server generated these startup warnings when booting:
2021-11-23T11:34:24.647+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2021-11-23T11:34:24.647+08:00: You are running this process as the root user, which is not recommended
2021-11-23T11:34:24.648+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>
连接admin库:
>use admin
添加用户密码:
>db.createUser({user:"test",pwd:"test123",roles:["userAdminAnyDatabase"]})
登录验证:
>db.auth("test","test")
1
重启 mongodb,使用 Robo3T远程连接
ps -ef | grep mongodb # 查看mongodb进程
kill -9 124523 # 指定mongdb进程号,杀掉进程
mongod --config /usr/local/mongodb/mongodb.conf # 重启
使用ip+端口访问,看看能不能访问到,注意关闭防火墙
能访问到,就说明服务开启了,并且可以远程访问了
至此,mongodb安装完成