Install platform dependency packages

sudo yum install libcurl openssl
Copy the code

Install the mongo

Download MongoDB source at www.mongodb.com/download-ce…

Download the TGZ installation package and decompress TGZ.

Wget $$tar ZXVF - https://fastdl.mongodb.org/linux/mongodb-shell-linux-x86_64-rhel70-4.4.2.tgz Mongo - the shell - Linux - x86_64 - rhel70-4.4.2. TGZRename the folder$mv mongo shell - Linux - x86_64 - rhel70-4.4.2 mongodb4Copy the code

Adding environment variables

# ${mongodb-install-directory} is the mongodb installation directory
$ export PATH=${mongodb-install-directory}/bin:$PATH
Copy the code

Creating a database directory

By default, MongoDB initializes the following two directories after startup

  • Data storage directory: /var/lib/mongodb
  • Log file directory: /var/log/mongodb

We can create these two directories and give the current user read and write permission before starting

$ sudo mkdir -p /var/lib/mongo
$ sudo mkdir -p /var/log/mongodb
$ sudo chown `whoami` /var/lib/mongo     # set permissions
$ sudo chown `whoami` /var/log/mongodb   # set permissions
Copy the code

Write a startup configuration file

Create the mongodb. Conf file and write the following

# Data store directory
dbpath=/var/lib/mongo
Log file directory
logpath=/var/log/mongodb/mongod.log
Start port
port=27017
# Enable process daemon
fork=true
# Enable permission verification
auth=true
# set to open to the outside world (127.0.0.1)Bind_ip = 0.0.0.0# Set the log to be automatically cut every day
logappend=true
Copy the code

Start the mongo

$ mongodb --config mongodb.conf
Copy the code

Add a user name and password

Log in mongo

$ mongo
Copy the code

Select the Admin library and add the username and password

$ use admin
$ db.createUser({ user: "root".pwd: "password", roles: [{ role: "userAdminAnyDatabase", db: "admin"}]})Copy the code

Login authentication

$ db.auth("root"."password")
Copy the code

Testing remote access

Use IP + port access, for example, 127.0.0.1:27017

If yes, the service is successfully started and can be accessed remotely.

usesystemdService management

Create a new file named mongod. Service in /etc/systemd/system. The contents are as follows:

[Unit]
Description=mongod-server
After=network.target

[Service]
Type=forking
ExecStart=${mongodb-install-directory}/bin/mongod --config ${mongodb-install-directory}/mongodb.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target
Copy the code

Refresh systemctl to check the MongoDB status

$ sudo systemctl daemon-reload
$ sudo systemctl status mongod
Copy the code

Setting boot

$ sudo systemctl enable mongod.service
Copy the code