How to deploy a MongoDB cluster using Docker

Source: Internet
Author: User
Tags mongo shell
MongoDB is a product between relational databases and non-relational databases. It is the most like a relational database in non-relational databases. Supports query languages similar to object-oriented

MongoDB is a product between relational databases and non-relational databases. It is the most like a relational database in non-relational databases. Supports query languages similar to object-oriented

[Editor's note] MongoDB is a product between a relational database and a non-relational database, which is the most similar to a relational database. Supports similar to the Object-Oriented Query Language, and supports almost most of the functions of querying a single table in a relational database. It also supports data indexing. This article describes how to use Docker to build a MongoDB cluster.

This article will introduce how to use Docker to deploy a MongoDB cluster as follows:

First, you need to prepare three running Docker servers, which means you need to prepare a local Vagrant Box Virtual Machine with Docker installed (the system can use CoreOS) or AWS or other methods you like.

Step 1

You need to get the IP addresses of the three Docker servers and configure the following IP addresses to all servers. Execute the following command on each server (remember to replace the IP address ):

Root @ node

Ideally, you do not need to do this. These IP addresses can be automatically configured through DNS. However, this is simpler. After all, this is only an installation test.

Step 2

Create a key file for each node. Run the following command on a server and copy the key file to the same location on the other two servers.

In this tutorial, I will put everything in the "/home/core" folder.

Coreroot @ node keyfileroot @ node

The owner of this key file is set to "999" because in the Docker container of MongoDB, this user must have the permission to operate the key file.

Step 3

Start the MongoDB container of node1 (the first Docker server. It starts a container without authentication mechanism, so we need to set a user.

Name mongo \ db \ keyfile \\\

Create an admin user. We can connect to the mongoDB container just started and enter an interactive shell environment.

At this time, we will enter the MongoDB Docker container, and then we will open a mongo shell environment:

The above command can open the mongo shell environment. After execution, you will see the following output:

Connecting to: test seehttphttp

Switch to admin user:

Adminswitched to db admin

Create a new site admin user

User pwd roles

If the creation is successful, you will see the following successful information:

{}]
}

Create a root user:

User pwd roles

You will see the following successful information:

{}]
}

We have created several users that we will use later. Now we will not exit the Interactive shell environment (the mongo and Docker container environments ).

Byeroot @ node1Step4

Stop the first MongoDB instance:

Step 5

This time, the key file is used to start the first MongoDB instance (still operating on node1 ).

Root @ node1: // # docker rm unzip root @ node1:/# docker run \ -- name mongo \ db \ keyfile \ \\\\ -- smallfiles \ keyfile \

Note:

  • -- The keyFile path is/opt/keyfile/mongodb-keyfile, which is correct. This is the address of the key file inside Docker. We use the-v option to map the key file to the path inside the container (that is,/opt/keyfile/mongodb-keyfile ).
  • -- Add-host: add the information to the/etc/hosts file of the Docker container, so we can use the domain name instead of the IP address. In the actual production environment, the information is DNS, and these parameters can be ignored.
  • Step 6

    Connect to the replica set and install and configure it. This is still performed on node1. We need to start another Interactive shell environment to enter the mongo container and start a mongo shell environment:

    Bashroot @ node1:/# mongo

    Switch to the admin user:

    Adminswitched to db admin

    Because we have set a password, we have to perform authentication this time. Set the password to: password.

    Now we can enable the replica set:

    Step 7

    Verify the configuration of the initialized replica set:

    > Rs0 r {}]
    } Step 8

    Start MongoDB on the remaining two nodes.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.