Getting Start
官网的英文文档是最好的学习资料,以mongo4.2为例,阅读官网文档
What is MongoDB
Welcome to the MongoDB 4.2 Manual! MongoDB is a document database designed for ease of development and scaling. The Manual introduces key concepts in MongoDB, presents the query language, and provides operational and administrative considerations and procedures as well as a comprehensive reference section. The manual is also available as HTML tar.gz and EPUB.
MongoDB is a document database:MongoDB是一个database(数据库)
What is Document
A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents.
MongoDB中存储的记录称之为document(文档)
document和JSON对象类似,是一种key/value形式的数据结构,其中key称之为field(字段)
How to install MongoDB
为了进一步了解MongoDB,安装MongoDB来实际操作,以centos7下安装为例
MongoDB分为Community Edition(社区版)和Enterprise Edition(企业版),以安装社区版为例
- 官网安装教程(yum安装包方式安装):https://docs.mongodb.com/manu...
- 官网安装教程(tgz压缩包方式安装):https://docs.mongodb.com/manu...
Create a/etc/yum.repos.d/mongodb-org-4.2.repo
file so that you can install MongoDB directly usingyum
:
先在/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
To install the latest stable version of MongoDB, issue the following command:
安装最新的稳定版MongoDB,使用以下命令
sudo yum install -y mongodb-org
Alternatively, to install a specific release of MongoDB, specify each component package individually and append the version number to the package name, as in the following example:
安装特定版本的MongoDB,需要分别指定每个组件包并附带版本号,示例如下
sudo yum install -y mongodb-org-4.2.8 mongodb-org-server-4.2.8 mongodb-org-shell-4.2.8 mongodb-org-mongos-4.2.8 mongodb-org-tools-4.2.8
You can specify any available version of MongoDB. Howeveryum
upgrades the packages when a newer version becomes available. To prevent unintended upgrades, pin the package. To pin a package, add the followingexclude
directive to your/etc/yum.conf
file:
当执行yum upgrade或是yum update更新软件包时,会将MongoDB更新为最新版本
如果希望使用固定的版本,可以在/etc/yum.conf文件里排除MongoDB相关的更新,文件内容如下
exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools
安装完成后/usr/bin目录会添加MongoDB相关的命令
- mongod:用于启动MongoDB服务
- mongo:开启Mongo Shell连接MongoDB服务来操作MongoDB数据库
How to configure directory
安装完MongoDB后,先来了解关于MongoDB的目录配置
By default, MongoDB runs using the
mongod
user account and uses the following default directories:
/var/lib/mongo
(the data directory)/var/log/mongodb
(the log directory)- ➤ If you installed via the package manager,
The default directories are created, and the owner and group for these directories are set to
mongod
.- ➤ If you installed by downloading the tarballs,
The default MongoDB directories are not created. To create the MongoDB data and log directories:
sudo mkdir -p /var/lib/mongo sudo mkdir -p /var/log/mongodb
By default, MongoDB runs using themongod
user account. Once created, set the owner and group of these directories tomongod
:sudo chown -R mongod:mongod
默认情况下,MongoDB使用mongod用户来运行,且使用以下默认目录
- /var/lib/mongo:数据存放的路径
- /var/log/mongodb:日志存放的路径
通过yum软件包安装的方式会自动创建mongod用户和以上目录,并设置目录的相关权限为mongod用户
通过tgz压缩包安装的方式则需要自己手动创建用户和目录,具体操作见上述引用
如果需要自定义MongoDB相关的目录,也可以按照以下操作手动指定目录
To use a data directory and/or log directory other than the default directories:
- Create the new directory or directories.
Edit the configuration file
/etc/mongod.conf
and modify the following fields accordingly:
storage.dbPath
to specify a new data directory path (e.g./some/data/directory
)systemLog.path
to specify a new log file path (e.g./some/log/directory/mongod.log
)Ensure that the user running MongoDB has access to the directory or directories:
sudo chown -R mongod:mongod <directory>
If you change the user that runs the MongoDB process, you must give the new user access to these directories.
- Configure SELinux if enforced. See Configure SELinux.
通过配置/etc/mongod.conf文件来配置自定义的目录
- storage.dbPath:配置数据存放的路径
- systemLog.path:配置日志存放的路径
How to run MongoDB
通过mongod命令启动服务,具体参数使用方式通过mongod --help查看
# 启动服务
mongod -f /etc/mongod.conf
# 停止服务
mongod --shutdown -f /etc/mongod.conf
通过systemctl命令启动服务
# 启动服务
sudo systemctl start mongod
# 查看服务状态
sudo systemctl status mongod
# 开机启动服务
sudo systemctl enable mongod
# 关闭服务
sudo systemctl stop mongod
# 重启服务
sudo systemctl restart mongod
systemctl start mongod启动时可能会遇到的问题
Failed to start mongod.service: Unit mongod.service not found.
表示系统没有找到mongod服务,解决方法如下
-
systemctl daemon-reload
:先使mongod服务生效 -
systemctl start mongod
:再启动服务
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
表示启动服务时发生了错误,查看MongoDB的日志文件,出现类似/var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied
的错误表示权限不足,先查看该文件的权限
[root@centos-7 mongo]# ll /var/lib/mongo | grep WiredTiger.turtle
-rw-------. 1 root root 1249 Jul 8 14:56 WiredTiger.turtle
发现目标文件只有root用户有读写权限,这是由于
- mongod命令:使用当前用户启动服务,服务中生成的文件的权限所属为当前用户
- systemctl命令:使用mongod用户启动服务,服务中生成的文件的权限所属为mongod用户
导致这样的问题的发生,应该是之前使用了root用户通过mongod -f /etc/mongod.conf命令启动了服务,生成了root权限的WiredTiger.turtle文件。当使用systemctl start mongod启动服务时,使用mongod用户无法对WiredTiger.turtle文件进行操作,因而返回错误导致服务启动失败,解决方法如下
-
sudo chown -R mongod:mongod /var/lib/mongo
:先更改目录的权限为mongod -
systemctl start mongod
:再启动服务
出现ERROR: Cannot write pid file to /var/run/mongodb/mongod.pid: Permission denied
的错误也是类似的,修改mongod.pid的权限即可,如果文件不存在可以先手动touch创建再赋予权限
开始使用MongoDB
通过mongo命令开启Mongo Shell同MongoDB服务交互
- mongo命令的相关使用说明:https://docs.mongodb.com/manu...
- --host:主机名,默认为localhost
- --port:端口号,默认为27017
mongo
How to uninstall MongoDB
通过以下操作可以完整卸载MongoDB,包括MongoDB程序、配置文件以及数据、日志使用的目录
To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs. The following section guides you through the necessary steps.WARNING
This process will completely remove MongoDB, its configuration, and all databases. This process is not reversible, so ensure that all of your configuration and data is backed up before proceeding.
先停止MongoDB服务
sudo service mongod stop
再卸载所有的MongoDB包
sudo yum erase $(rpm -qa | grep mongodb-org)
最后移除数据、日志使用的目录
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo
About Localhost Binding
By default, MongoDB launches with
bindIp
set to127.0.0.1
, which binds to the localhost network interface. This means that themongod
can only accept connections from clients that are running on the same machine. Remote clients will not be able to connect to themongod
, and themongod
will not be able to initialize a replica set unless this value is set to a valid network interface.This value can be configured either:
默认情况下,MongoDB设置了本地IP绑定,即当前MongoDB服务只接受当前本机的访问请求
如果希望服务能接收其他机器的访问请求,需要绑定其他机器的IP地址
绑定IP地址的方法如下
- /etc/mongod.conf中配置bindIp属性
- mongod方式启动服务时添加参数 --bind_ip
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。