(一)基础环境设置
操作系统版本 :centos-7.4
MongoDB版本:MongoDB 4.2 社区版
(1)关闭防火墙
# 关闭防火墙 [root@mongodbenterprise lib]# systemctl stop firewalld.service # 禁止firewall开机启动 [root@mongodbenterprise lib]# systemctl disable firewalld.service # 确认防火墙为not running状态 [root@mongodbenterprise lib]# firewall-cmd --state not running
(2)关闭selinux
[root@mongodbenterprise lib]# vim /etc/selinux/config
SELINUX=disabled
(二)安装MongoDB
使用yum安装,一共有2步:
STEP1:配置MongoDB的yum源
[root@mongoserver ~]# vim /etc/yum.repos.d/mongodb-org-4.2.repo # 添加下面内容 [mongodb-org-4.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
STEP2:安装MongoDB软件包和相关工具
[root@mongoServer1 ~]# yum install -y mongodb-org
此时会下载5个包(包含4个依赖包),如下:
====================================================================================== Package Arch Version Repository Size ====================================================================================== Installing: mongodb-org x86_64 4.2.7-1.el7 mongodb-org-4.2 5.8 k Installing for dependencies: mongodb-org-mongos x86_64 4.2.7-1.el7 mongodb-org-4.2 15 M mongodb-org-server x86_64 4.2.7-1.el7 mongodb-org-4.2 25 M mongodb-org-shell x86_64 4.2.7-1.el7 mongodb-org-4.2 17 M mongodb-org-tools x86_64 4.2.7-1.el7 mongodb-org-4.2 62 M
这些包的作用如下:
Package Name | Description |
mongodb-org | 一个存放下面安装包的元数据包 |
mongodb-org-server | 包含mongod守护进程和相关脚本以及配置文件“/etc/mongod.conf”,可以使用守护进程和配置文件初始化数据库 |
mongodb-org-mongos | 包含路由进程mongos |
mongodb-org-shell | 包含mongo shell |
mongodb-org-tools |
MongoDB的工具集,包含以下工具: mongodump |
(三)启动与关闭MongoDB
# 启动
service mongod start
# 关闭
service mongod stop
#查看状态
service mongod status
#重新启动
service mongod restart
查看mongodb进程:
[root@mongoserver ~]# ps -ef|grep mongod mongod 1745 1 2 00:05 ? 00:00:00 /usr/bin/mongod -f /etc/mongod.conf
默认mongodb日志信息位置:
/var/log/mongodb/mongod.log
启动mongodb 的日志信息如下:
[root@mongoserver ~]# cat /var/log/mongodb/mongod.log 2020-05-29T00:05:55.377+0800 I CONTROL [main] ***** SERVER RESTARTED ***** 2020-05-29T00:05:55.379+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols ‘none‘ 2020-05-29T00:05:56.194+0800 W ASIO [main] No TransportLayer configured during NetworkInterface startup 2020-05-29T00:05:56.194+0800 I CONTROL [initandlisten] MongoDB starting : pid=1745 port=27017 dbpath=/var/lib/mongo 64-bit host=mongoserver ... 2020-05-29T00:05:56.713+0800 I NETWORK [listener] Listening on /tmp/mongodb-27017.sock 2020-05-29T00:05:56.713+0800 I NETWORK [listener] Listening on 127.0.0.1 2020-05-29T00:05:56.713+0800 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with provided UUID: aeeaac99-a6f2-4692-9b8d-5d49fea8c3bb and options: { uuid: UUID("aeeaac99-a6f2-4692-9b8d-5d49fea8c3bb") } 2020-05-29T00:05:56.713+0800 I NETWORK [listener] waiting for connections on port 27017
(四)常用命令
(1)进入数据库
使用mongo进入MongoDB数据库
[root@mongodbserver1 ~]# mongo MongoDB shell version v3.4.23 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.23 Server has startup warnings: 2020-05-14T22:36:54.506+0800 I CONTROL [initandlisten] 2020-05-14T22:36:54.507+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2020-05-14T22:36:54.507+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2020-05-14T22:36:54.507+0800 I CONTROL [initandlisten] >
查看数据库:
show dbs;
查看数据库版本:
db.version();
查看帮助信息:
db.help();
(五)远程连接设置
MongoDB安装完成后,只能本地连接,如果要远程连接到服务器上,需要配置网络,配置方法如下:
[root@mongoserver ~]# vim /etc/mongod.conf #将bindIp改为“0.0.0.0”,表示任何远程机器都可以连接 net: bindIp: 0.0.0.0
【完】