Linux系统下安装MongoDB


Linux系统下MongoDB的安装

1.使用RPM包(推荐)

1.1配置包管理系统

Create a /etc/yum.repos.d/mongodb-org-4.0.repo file so that you can install MongoDB directly using yum.
创建/etc/yum.repos.d/mongodb-org-4.0.repo文件以便直接使用yum安装MongoDB。

[[email protected] ~]$ sudo vi /etc/yum.repos.d/mongodb-org-4.0.repo

[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

1.2.安装MongoDB包

[[email protected] ~]$ sudo yum install -y mongodb-org
[[email protected] ~]$ sudo yum install -y mongodb-org-4.0.4 mongodb-org-server-4.0.4 mongodb-org-shell-4.0.4 mongodb-org-mongos-4.0.4 mongodb-org-tools-4.0.4

You can specify any available version of MongoDB. However yum upgrades the packages when a newer version becomes available. To prevent unintended upgrades, pin the package. To pin a package, add the following exclude directive to your /etc/yum.conf file:
exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

您可以指定任何可用的MongoDB版本。 但是,当更新版本可用时,yum会升级软件包。 为防止意外升级,将包固定。 要固定包,将以下exclude指令添加到/etc/yum.conf文件中

exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

1.3目录和权限

By default, MongoDB instance stores:

  • its data files in /var/lib/mongo
  • its log files in /var/log/mongodb

默认情况下,MongoDB实例存储,data数据文件和log日志文件存放目录

By default, MongoDB runs using the mongod user account. If you change the user that runs the MongoDB process, you must also modify the permission to the /var/lib/mongo and /var/log/mongodbdirectories to give this user access to these directories.

默认情况下,MongoDB使用mongod用户帐户运行。 如果更改运行MongoDB进程的用户,则还必须修改/var/lib/mongo/var/log/mongodb目录的权限,以授予此用户访问这些目录的权限。

To specify a different log file directory and data file directory, edit the systemLog.path and storage.dbPath settings in the /etc/mongod.conf. Ensure that the user running MongoDB has access to these directories.

要指定其他日志文件目录和数据文件目录,请编辑/etc/mongod.conf中的systemLog.pathstorage.dbPath设置。 确保运行MongoDB的用户可以访问这些目录。

1.4运行MongoDB过程步骤

1.4.1启动MongoDB

[[email protected] ~]$ sudo service mongod start
Redirecting to /bin/systemctl start mongod.service

1.4.2验证MongoDB启动成功

[[email protected] ~]$ sudo cat /var/log/mongodb/mongod.log

2018-11-10T20:33:23.906+0800 I NETWORK  [initandlisten] waiting for connections on port 27017

有一行日志内容

[initandlisten] waiting for connections on port <port>

where <port> is the port configured in /etc/mongod.conf, 27017 by default.

1.4.3开机自动启动

开机MongoDB自启动

You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:
sudo chkconfig mongod on
您可以选择通过发出以下命令来确保MongoDB将在系统重新启动后启动

sudo chkconfig mongod on

1.4.4停止MongoDB

[[email protected] ~]$ sudo service mongod stop

1.4.5重启MongoDB

[[email protected] ~]$ sudo service mongod restart

1.4.6使用mongo shell

[[email protected] ~]$ mongo

1.5卸载MongoDB过程步骤

1.5.1停止MongoDB

[[email protected] ~]$ sudo service mongod stop

1.5.2删除包

[[email protected] ~]$ sudo yum erase $(rpm -qa | grep mongodb-org)

1.5.3删除数据目录

删除日志存放目录和数据存放目录

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo
[[email protected] ~]$ sudo rm -r /var/log/mongodb
[[email protected] ~]$ sudo rm -r /var/lib/mongo

2.使用tar包

2.1安装依赖包

[[email protected] ~]$ sudo yum install libcurl openssl

2.2下载tar包

MongoDB Download Center

由于Linux系统使用的是CentOS7.4,操作系统选择RHEL 7.0 Linux 64-bit x64

Linux系统下安装MongoDB

[[email protected] ~]$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.4.tgz

2.3解压到安装目录

[[email protected] ~]$ tar -zxvf mongodb-linux-x86_64-rhel70-4.0.4.tgz -C /usr/local/app/

[[email protected] ~]$ cd /usr/local/app/
[[email protected] app]$ mv mongodb-linux-x86_64-rhel70-4.0.4/ mongodb-4.0.4/

2.4设置环境变量

[[email protected] ~]$ vi .bash_profile

export MONGODB_HOME=/usr/local/app/mongodb-4.0.4
export PATH=$MONGODB_HOME/bin:$PATH

[[email protected] ~]$ source .bash_profile

2.5运行MongoDB

If you installed manually by downloading the tarballs, you can create the directories using mkdir -p<directory> or sudo mkdir -p <directory> depending on the user that will run MongoDB. (See your linux man pages for information on mkdir and sudo.)
如果通过下载tar包手动安装,则可以使用mkdir -p <directory>sudo mkdir -p <directory>创建目录,具体取决于将运行MongoDB的用户。(有关mkdirsudo的信息,请参阅linux手册页。)

MongoDB的数据存储在data目录的db目录下,但是这个目录在安装过程不会自动创建,所以需要手动创建data目录,并在data目录中创建db目录。

注意:/data/db是MongoDB默认的启动的数据库路径(–dbpath)。如果你的数据库目录不是/data/db,可以通过mongod --dbpath 目录来指定。

[[email protected] ~]$ sudo mkdir -p /data/db

## 设置赋予目录读写权限
[[email protected] data]$ sudo chmod o+rwx /data/db

启动MongoDB

[[email protected] data]$ mongod

重新打开一个命令行窗口,启动mongo shell

[[email protected] ~]$ mongo

本文参考:

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/