如何在Linux环境下安装mongodb数据库详细步骤
时间: 2024-05-21 21:02:53 浏览: 47
以下是在Linux环境下安装mongodb数据库的详细步骤:
1. 打开终端并更新软件包列表:
sudo apt-get update
2. 安装mongodb:
sudo apt-get install mongodb
3. 启动mongodb服务:
sudo systemctl start mongodb
4. 设置mongodb开机自启动:
sudo systemctl enable mongodb
5. 检查mongodb服务状态:
sudo systemctl status mongodb
6. 连接到mongodb:
mongo
7. 创建一个数据库:
use mydatabase
8. 创建一个集合:
db.createCollection("mycollection")
9. 向集合中插入数据:
db.mycollection.insertOne({name: "John", age: 25})
10. 查询集合中的数据:
db.mycollection.find()
以上就是在Linux环境下安装mongodb数据库的详细步骤。
相关问题
如何在Linux环境下安装mongodb数据库
在Linux环境下安装mongodb数据库可以按照以下步骤进行:
1. 打开终端,使用以下命令添加mongodb的官方源到系统中:
```
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
```
2. 更新系统包列表:
```
sudo apt-get update
```
3. 安装mongodb:
```
sudo apt-get install mongodb-org
```
4. 启动mongodb服务:
```
sudo systemctl start mongod
```
5. 验证mongodb服务是否正常运行:
```
sudo systemctl status mongod
```
6. 设置mongodb开机自启动:
```
sudo systemctl enable mongod
```
至此,mongodb已经安装完成,并且开机自启动。可以使用`mongo`命令进入mongo shell进行操作。
如何在Linux环境下安装mongodb_exporter并监控MongoDB数据库的详细步骤
以下是在Linux环境下安装并监控MongoDB数据库的详细步骤:
1. 下载mongodb_exporter二进制文件
在官方GitHub仓库中下载mongodb_exporter二进制文件,如:
```
$ wget https://github.com/percona/mongodb_exporter/releases/download/v0.20.3/mongodb_exporter-0.20.3.linux-amd64.tar.gz
```
2. 解压文件
解压刚刚下载的文件,如:
```
$ tar -zxvf mongodb_exporter-0.20.3.linux-amd64.tar.gz
```
3. 复制文件
将解压后的文件复制到/usr/local/bin目录下,如:
```
$ sudo cp mongodb_exporter-0.20.3.linux-amd64/mongodb_exporter /usr/local/bin/
```
4. 创建mongodb_exporter用户
为了安全起见,我们创建一个新用户来运行mongodb_exporter,如:
```
$ sudo useradd --no-create-home --shell /bin/false mongodb_exporter
```
5. 更改mongodb_exporter二进制文件的权限
为了让mongodb_exporter用户可以执行mongodb_exporter二进制文件,我们需要更改文件的权限,如:
```
$ sudo chown mongodb_exporter:mongodb_exporter /usr/local/bin/mongodb_exporter
```
6. 创建systemd服务
我们需要创建一个systemd服务来管理mongodb_exporter的运行,如:
```
$ sudo vi /etc/systemd/system/mongodb_exporter.service
```
在文件中添加以下内容:
```
[Unit]
Description=MongoDB Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=mongodb_exporter
Group=mongodb_exporter
Type=simple
ExecStart=/usr/local/bin/mongodb_exporter --web.listen-address=:9001 --mongodb.uri=mongodb://localhost:27017/
Restart=always
[Install]
WantedBy=multi-user.target
```
其中,`--web.listen-address`参数指定了mongodb_exporter的监听地址和端口,`--mongodb.uri`参数指定了要连接的MongoDB数据库的地址和端口。
7. 启动mongodb_exporter服务
启动mongodb_exporter服务并将其设置为开机自启,如:
```
$ sudo systemctl daemon-reload
$ sudo systemctl start mongodb_exporter
$ sudo systemctl enable mongodb_exporter
```
8. 配置Prometheus
将mongodb_exporter作为Prometheus的一个targets,如:
```
scrape_configs:
- job_name: 'mongodb_exporter'
metrics_path: '/metrics'
static_configs:
- targets: ['localhost:9001']
```
9. 启动Prometheus
启动Prometheus并访问其web界面,如:
```
$ prometheus --config.file=prometheus.yml
```
访问http://localhost:9090/graph,就可以看到mongodb_exporter的监控指标了。