MongoDB是一个基于分布式文件存储的数据库,是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的,旨在为WEB应用提供可扩展的高性能数据存储解决方案。支持的数据结构非常松散,可以存储比较复杂的数据类型。使用C++语言编写。
CentOS7进行MongoDB安装:配置MongoDB的yum源
vim /etc/yum.repos.d/mongodb-org-3.7.repo
添加以下内容:
[mongodb-org-3.7]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.7/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.7.asc
注:可以修改 gpgcheck=0,省去gpg验证
更新数据缓存:
yum clean all
yum makecache
安装命令:
yum -y install mongodb-org
MongoDB官方源中包含以下几个依赖包:
mongodb-org: MongoDB元数据包。
安装时自动安装下面四个组件包:
mongodb-org-server: 包含MongoDB守护进程和相关的配置和初始化脚本。
mongodb-org-mongos: 包含mongos的守护进程。
mongodb-org-shell: 包含mongo shell。
mongodb-org-tools: 包含MongoDB工具( mongoimport, bsondump, mongodump, mongoexport, mongofiles,mongooplog, mongoperf, mongorestore, mongostat, and mongotop等)
查看mongo安装位置 :
whereis mongod
查看修改配置文件:
vim /etc/mongod.conf
默认配置文件:
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
开机自动启动mongodb (默认已经设置):
systemctl enable mongod
启动mongodb :
systemctl start mongod
查看mongodb的状态:
systemctl status mongod
正常如下:
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2019-09-11 23:54:27 CST; 8s ago
Docs: https://docs.mongodb.org/manual
Process: 1896 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
Process: 1894 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 1891 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 1889 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Main PID: 1899 (mongod)
CGroup: /system.slice/mongod.service
└─1899 /usr/bin/mongod -f /etc/mongod.conf
Sep 11 23:54:26 mongodb systemd[1]: Starting MongoDB Database Server...
Sep 11 23:54:26 mongodb mongod[1896]: about to fork child process, waiting until server is ready for connections.
Sep 11 23:54:26 mongodb mongod[1896]: forked process: 1899
Sep 11 23:54:27 mongodb mongod[1896]: child process started successfully, parent exiting
Sep 11 23:54:27 mongodb systemd[1]: Started MongoDB Database Server.
停止mongodb :
systemctl stop mongod
启动mongo shell:
mongo
正常信息如下:
MongoDB shell version v3.6.14
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b523de85-0e1f-4f77-8862-731bd7081704") }
MongoDB server version: 3.6.14
Server has startup warnings:
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten]
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten]
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten]
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten]
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten]
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-09-11T23:54:27.508+0800 I CONTROL [initandlisten]
查看数据库:
show dbs
正常显示如下:
admin 0.000GB
config 0.000GB
local 0.000GB
设置mongodb远程访问:
vim /etc/mongod.conf
编辑mongod.conf,找到bindIp注释掉,并重启mongodb.(这句配置代表只能本机使用,所以需注释)
#bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
重启mongodb使修改生效:
systemctl restart mongod
注意:记得要开放端口以便远程访问。
firewall-cmd --permanent --zone=public --add-port=27017/tcp
firewall-cmd --reload
注:本文为Toyo Lau原创,未经许可不得在任何平台转载,侵权必究。如需转载,可关注微信公众号:技术训练营,在后台留言,与作者联系,取得许可才能转载~