• 测试环境

    操作系统:CentOS 7.2 最小化安装

    主服务器IP地址:192.168.197.21 mongo01

    从服务器IP地址:192.168.197.22 mongo02

    从服务器IP地址:192.168.197.23 mongo03

    关闭selinux,关闭防火墙。

    Mongodb版本:mongodb-linux-x86_64-3.4.10.tgz

  • 上传mongodb包到/software目录下,然后解压缩该包

    [root@localhost software]# tar -zxvf mongodb-linux-x86_64-3.4.10.tgz

  • 移动解压缩之后的文件至/usr/local/mongodb目录

    [root@localhost software]# mv mongodb-linux-x86_64-3.4.10 /usr/local/mongodb

  • 在mongodb下面创建data目录和logs目录,用于存放data和logs

    [root@localhost local]# mkdir -p /usr/local/mongodb/data

    [root@localhost local]# mkdir -p /usr/local/mongodb/logs

  • 增加环境变量,并使环境变量生效

    [root@localhost data]# vi /etc/profile

    export PATH=$PATH:/usr/local/mongodb/bin

    [root@localhost data]# source /etc/profile

  • 创建mongodb.conf文件并写入以下内容

    [root@localhost data]# touch /usr/local/mongodb/bin/mongodb.conf

    [root@localhost data]# vi /usr/local/mongodb/bin/mongodb.conf

  • 主服务器配置

    [root@mongo01 ~]# more /usr/local/mongodb/bin/mongodb.conf

    port=27017

    bind_ip=192.168.197.21

    dbpath=/usr/local/mongodb/data

    logpath=/usr/local/mongodb/logs/log.log

    pidfilepath=/usr/local/mongodb/mongo.pid

    fork=true

    logappend=true

    shardsvr=true

    directoryperdb=true

    #auth=true

    #keyFile=/usr/local/mongodb/keyfile

    replSet=hqmongodb

  • 从服务器配置

    [root@mongo02 ~]# more /usr/local/mongodb/bin/mongodb.conf

    port=27017

    bind_ip=192.168.197.22

    dbpath=/usr/local/mongodb/data

    logpath=/usr/local/mongodb/logs/log.log

    pidfilepath=/usr/local/mongodb/mongo.pid

    fork=true

    logappend=true

    shardsvr=true

    directoryperdb=true

    #auth=true #暂时注释掉这里,后期开启验证时再取消注释

    #keyFile=/usr/local/mongodb/keyfile #暂时注释掉这里,后期开启验证时再取消注释

    replSet=hqmongodb

    [root@mongo03 ~]# more /usr/local/mongodb/bin/mongodb.conf

    port=27017

    bind_ip=192.168.197.23

    dbpath=/usr/local/mongodb/data

    logpath=/usr/local/mongodb/logs/log.log

    pidfilepath=/usr/local/mongodb/mongo.pid

    fork=true

    logappend=true

    shardsvr=true

    directoryperdb=true

    #auth=true #暂时注释掉这里,后期开启验证时再取消注释

    #keyFile=/usr/local/mongodb/keyfile #暂时注释掉这里,后期开启验证时再取消注释

    replSet=hqmongodb

  • 使用配置文件启动三台服务器上的数据库

    mongod --config /usr/local/mongodb/bin/mongodb.conf

  • 登陆主节点初始化副本集

    [root@mongo01 software]# mongo 192.168.197.21:27017

    MongoDB shell version v3.4.10

    connecting to: 192.168.197.21:27017

    MongoDB server version: 3.4.10

    Welcome to the MongoDB shell.

    For interactive help, type "help".

    For more comprehensive documentation, see

    http://docs.mongodb.org/

    Questions? Try the support group

    http://groups.google.com/group/mongodb-user

    Server has startup warnings:

    2019-02-12T03:04:58.259-0500 I CONTROL [initandlisten]

    2019-02-12T03:04:58.259-0500 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.

    2019-02-12T03:04:58.259-0500 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.

    2019-02-12T03:04:58.259-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

    2019-02-12T03:04:58.259-0500 I CONTROL [initandlisten]

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten]

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten]

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten]

    > rs.initiate() #初始化副本集

    {

    "info2" : "no configuration specified. Using a default configuration for the set",

    "me" : "192.168.197.21:27017",

    "ok" : 1

    }

    hqmongodb:OTHER> rs.conf() #查看集群状态

    {

    "_id" : "hqmongodb",

    "version" : 1,

    "protocolVersion" : NumberLong(1),

    "members" : [

    {

    "_id" : 0,

    "host" : "192.168.197.21:27017",

    "arbiterOnly" : false,

    "buildIndexes" : true,

    "hidden" : false,

    "priority" : 1,

    "tags" : {

    },

    "slaveDelay" : NumberLong(0),

    "votes" : 1

    }

    ],

    "settings" : {

    "chainingAllowed" : true,

    "heartbeatIntervalMillis" : 2000,

    "heartbeatTimeoutSecs" : 10,

    "electionTimeoutMillis" : 10000,

    "catchUpTimeoutMillis" : 60000,

    "getLastErrorModes" : {

    },

    "getLastErrorDefaults" : {

    "w" : 1,

    "wtimeout" : 0

    },

    "replicaSetId" : ObjectId("5c627f1190e39641c3d0e91f")

    }

    }

    hqmongodb:PRIMARY> rs.add("192.168.197.22:27017") #添加节点到集群中

    { "ok" : 1 }

    hqmongodb:PRIMARY> rs.add("192.168.197.23:27017") #添加节点到集群中

    { "ok" : 1 }

    hqmongodb:PRIMARY> cfg = rs.conf() #查看集群中各节点的信息

    {

    "_id" : "hqmongodb",

    "version" : 3,

    "protocolVersion" : NumberLong(1),

    "members" : [

    {

    "_id" : 0,

    "host" : "192.168.197.21:27017",

    "arbiterOnly" : false,

    "buildIndexes" : true,

    "hidden" : false,

    时不会被选为主节点,越大越容易被选为主节点

    "tags" : {

    },

    "slaveDelay" : NumberLong(0),

    "votes" : 1

    },

    {

    "_id" : 1,

    "host" : "192.168.197.22:27017",

    "arbiterOnly" : false,

    "buildIndexes" : true,

    "hidden" : false,

    "priority" : 1,

    "tags" : {

    },

    "slaveDelay" : NumberLong(0),

    "votes" : 1

    },

    {

    "_id" : 2,

    "host" : "192.168.197.23:27017",

    "arbiterOnly" : false,

    "buildIndexes" : true,

    "hidden" : false,

    "priority" : 1,

    "tags" : {

    },

    "slaveDelay" : NumberLong(0),

    "votes" : 1

    }

    ],

    "settings" : {

    "chainingAllowed" : true,

    "heartbeatIntervalMillis" : 2000,

    "heartbeatTimeoutSecs" : 10,

    "electionTimeoutMillis" : 10000,

    "catchUpTimeoutMillis" : 60000,

    "getLastErrorModes" : {

    },

    "getLastErrorDefaults" : {

    "w" : 1,

    "wtimeout" : 0

    },

    "replicaSetId" : ObjectId("5c627f1190e39641c3d0e91f")

    }

    }

    hqmongodb:PRIMARY> rs.reconfig(cfg) #使配置生效

    { "ok" : 1 }

  • 登陆两台从节点操作

    [root@mongo02 software]# mongo 192.168.197.22:27017

    MongoDB shell version v3.4.10

    connecting to: 192.168.197.22:27017

    MongoDB server version: 3.4.10

    Welcome to the MongoDB shell.

    For interactive help, type "help".

    For more comprehensive documentation, see

    http://docs.mongodb.org/

    Questions? Try the support group

    http://groups.google.com/group/mongodb-user

    Server has startup warnings:

    2019-02-12T03:06:00.986-0500 I CONTROL [initandlisten]

    2019-02-12T03:06:00.986-0500 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.

    2019-02-12T03:06:00.986-0500 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.

    2019-02-12T03:06:00.986-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

    2019-02-12T03:06:00.986-0500 I CONTROL [initandlisten]

    2019-02-12T03:06:00.987-0500 I CONTROL [initandlisten]

    2019-02-12T03:06:00.987-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

    2019-02-12T03:06:00.987-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T03:06:00.987-0500 I CONTROL [initandlisten]

    2019-02-12T03:06:00.987-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

    2019-02-12T03:06:00.987-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T03:06:00.987-0500 I CONTROL [initandlisten]

    hqmongodb:SECONDARY> db.getMongo().setSlaveOk() #设置从节点为只读.注意从节点的前缀现在是SECONDARY。看清楚才设置,执行这个,否则后续从节点同步数据时会报错:"errmsg" : "not master and slaveOk=false"

    [root@mongo03 software]# mongo 192.168.197.23:27017

    MongoDB shell version v3.4.10

    connecting to: 192.168.197.23:27017

    MongoDB server version: 3.4.10

    Welcome to the MongoDB shell.

    For interactive help, type "help".

    For more comprehensive documentation, see

    http://docs.mongodb.org/

    Questions? Try the support group

    http://groups.google.com/group/mongodb-user

    Server has startup warnings:

    2019-02-12T03:06:41.579-0500 I CONTROL [initandlisten]

    2019-02-12T03:06:41.579-0500 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.

    2019-02-12T03:06:41.579-0500 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.

    2019-02-12T03:06:41.579-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

    2019-02-12T03:06:41.579-0500 I CONTROL [initandlisten]

    2019-02-12T03:06:41.580-0500 I CONTROL [initandlisten]

    2019-02-12T03:06:41.580-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

    2019-02-12T03:06:41.580-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T03:06:41.580-0500 I CONTROL [initandlisten]

    2019-02-12T03:06:41.580-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

    2019-02-12T03:06:41.580-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T03:06:41.580-0500 I CONTROL [initandlisten]

    hqmongodb:SECONDARY> db.getMongo().setSlaveOk() #设置从节点为只读.注意从节点的前缀现在是SECONDARY。看清楚才设置,执行这个,否则后续从节点同步数据时会报错:"errmsg" : "not master and slaveOk=false"

  • 设置管理员账号,开启登陆验证

    登陆主节点确定登陆后的前缀是PRIMARY

    [root@mongo01 software]# mongo 192.168.197.21:27017

    MongoDB shell version v3.4.10

    connecting to: 192.168.197.21:27017

    MongoDB server version: 3.4.10

    Server has startup warnings:

    2019-02-12T03:04:58.259-0500 I CONTROL [initandlisten]

    2019-02-12T03:04:58.259-0500 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.

    2019-02-12T03:04:58.259-0500 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.

    2019-02-12T03:04:58.259-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

    2019-02-12T03:04:58.259-0500 I CONTROL [initandlisten]

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten]

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten]

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T03:04:58.260-0500 I CONTROL [initandlisten]

    hqmongodb:PRIMARY> show dbs

    local 0.000GB

    hqmongodb:PRIMARY> use admin #mongodb3.0版本没有admin数据库,需要手动创建admin库,然后在admin库下添加管理员账户

    switched to db admin

    hqmongodb:PRIMARY> show collections

    hqmongodb:PRIMARY> db.createUser({user:"system",pwd:"123456",roles:[{role:"root",db:"admin"}]}) #添加系统管理员用户system,同来管理用户

    Successfully added user: {

    "user" : "system",

    "roles" : [

    {

    "role" : "root",

    "db" : "admin"

    }

    ]

    }

    hqmongodb:PRIMARY> db.auth('system','123456') #添加管理员用户认证,认证之后才可以管理数据库

    hqmongodb:PRIMARY> db.createUser({user:'administrator', pwd:'123456', roles:[{ role: "userAdminAnyDatabase", db: "admin"}]}); #添加数据库管理员administator,同来管理所有数据库

    Successfully added user: {

    "user" : "administrator",

    "roles" : [

    {

    "role" : "userAdminAnyDatabase",

    "db" : "admin"

    }

    ]

    }

    hqmongodb:PRIMARY> db.auth('administrator','123456') #添加管理员用户认证

    hqmongodb:PRIMARY> db

    admin

    hqmongodb:PRIMARY> show collections

    system.users

    system.version

    hqmongodb:PRIMARY> db.system.users.find()

    { "_id" : "admin.system", "user" : "system", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "YnrIFQ9fDFp5rUUePmkfSg==", "storedKey" : "7DtAOhrPS63eJCb7eEDaLJAMoqs=", "serverKey" : "diT9vhEInnsuI/PFgJD36VNgeyQ=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }

    { "_id" : "admin.administrator", "user" : "administrator", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "1c5T2GUBattsdn3bMs3kJw==", "storedKey" : "KjzcNnkCzJe6XKGF94gpwk6UZcQ=", "serverKey" : "J1WYtTwGZ/ZpUTl5P5ZMUHxFiuI=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }

  • 为集群开启登陆验证

    [root@mongo01 software]# cd /usr/local/mongodb/

    的倍数,否则生成的字符串可能含有非法字符会导致认证失败

    [root@mongo01 mongodb]# chmod 600 /usr/local/mongodb/keyfile #更改文件权限

    [root@mongo01 mongodb]# cat /usr/local/mongodb/keyfile #查看生成的字符串,并做记录

    ROiB0DZZXbpLg4D7lOMifa2g2Jd8

    取消所有节点上配置文件中之前注释的内容,如下内容

    [root@mongo01 mongodb]# vi /usr/local/mongodb/bin/mongodb.conf

    auth=true #取消这里的注释,开启登陆验证

    keyFile=/usr/local/mongodb/keyfile #取消这里的注释,开启登陆验证

  • 将之前主节点上生成的21位字符串复制到各节点上的keyfile文件中,并更改权限

    [root@mongo02 software]# vi /usr/local/mongodb/keyfile

    [root@mongo02 software]# cat /usr/local/mongodb/keyfile

    ROiB0DZZXbpLg4D7lOMifa2g2Jd8

    [root@mongo02 software]# chmod 600 /usr/local/mongodb/keyfile

    [root@mongo03 software]# vi /usr/local/mongodb/bin/mongodb.conf

    [root@mongo03 software]# vi /usr/local/mongodb/keyfile

    [root@mongo03 software]# cat /usr/local/mongodb/keyfile

    ROiB0DZZXbpLg4D7lOMifa2g2Jd8

    [root@mongo03 software]# chmod 600 /usr/local/mongodb/keyfile

  • 设置mongodb的开机启动,首先要在系统服务目录下创建启动文件mongodb.servicer然后写入一下内容

    [root@localhost system]# cd /lib/systemd/system

    [root@localhost system]# vi mongodb.service

    [Unit]

    Description=mongodb

    After=network.target remote-fs.target nss-lookup.target

    [Service]

    Type=forking

    ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf

    ExecReload=/bin/kill -s HUP $MAINPID

    ExecStop=/usr/local/mongodb/bin/mongod --shutdown

    PrivateTmp=true

    [Install]

    WantedBy=multi-user.target

    注册到开机启动

    [root@localhost system]# systemctl enable mongodb.service

    Created symlink from /etc/systemd/system/multi-user.target.wants/mongodb.service to /usr/lib/systemd/system/mongodb.service.

  • 重启所有服务器,然后使用administrator或者system登陆,然后写入20条数据。

    [root@mongo01 ~]# mongo 192.168.197.21:27017 -u system -p 123456 --authenticationDatabase admin

    MongoDB shell version v3.4.10

    connecting to: 192.168.197.21:27017

    MongoDB server version: 3.4.10

    Server has startup warnings:

    2019-02-12T04:18:16.929-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

    2019-02-12T04:18:16.929-0500 I CONTROL [initandlisten]

    2019-02-12T04:18:16.929-0500 I CONTROL [initandlisten]

    2019-02-12T04:18:16.929-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

    2019-02-12T04:18:16.929-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T04:18:16.929-0500 I CONTROL [initandlisten]

    2019-02-12T04:18:16.929-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

    2019-02-12T04:18:16.929-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T04:18:16.929-0500 I CONTROL [initandlisten]

    hqmongodb:PRIMARY>

    hqmongodb:PRIMARY>

    hqmongodb:PRIMARY> rs.status() #查看集群状态

    {

    "set" : "hqmongodb",

    "date" : ISODate("2019-02-12T09:28:30.936Z"),

    "myState" : 1,

    "term" : NumberLong(34),

    "heartbeatIntervalMillis" : NumberLong(2000),

    "optimes" : {

    "lastCommittedOpTime" : {

    "ts" : Timestamp(1549963707, 1),

    "t" : NumberLong(34)

    },

    "appliedOpTime" : {

    "ts" : Timestamp(1549963707, 1),

    "t" : NumberLong(34)

    },

    "durableOpTime" : {

    "ts" : Timestamp(1549963707, 1),

    "t" : NumberLong(34)

    }

    },

    "members" : [

    {

    "_id" : 0,

    "name" : "192.168.197.21:27017",

    "health" : 1,

    的代表该节点目前为主节点,为2代表从节点,为8代表离线

    "stateStr" : "PRIMARY",

    "uptime" : 615,

    "optime" : {

    "ts" : Timestamp(1549963707, 1),

    "t" : NumberLong(34)

    },

    "optimeDate" : ISODate("2019-02-12T09:28:27Z"),

    "electionTime" : Timestamp(1549963666, 1),

    "electionDate" : ISODate("2019-02-12T09:27:46Z"),

    "configVersion" : 4,

    "self" : true

    },

    {

    "_id" : 1,

    "name" : "192.168.197.22:27017",

    "health" : 1,

    "state" : 2,

    "stateStr" : "SECONDARY",

    "uptime" : 17,

    "optime" : {

    "ts" : Timestamp(1549963707, 1),

    "t" : NumberLong(34)

    },

    "optimeDurable" : {

    "ts" : Timestamp(1549963707, 1),

    "t" : NumberLong(34)

    },

    "optimeDate" : ISODate("2019-02-12T09:28:27Z"),

    "optimeDurableDate" : ISODate("2019-02-12T09:28:27Z"),

    "lastHeartbeat" : ISODate("2019-02-12T09:28:29.716Z"),

    "lastHeartbeatRecv" : ISODate("2019-02-12T09:28:29.133Z"),

    "pingMs" : NumberLong(0),

    "syncingTo" : "192.168.197.23:27017",

    "configVersion" : 4

    },

    {

    "_id" : 2,

    "name" : "192.168.197.23:27017",

    "health" : 1,

    "state" : 2,

    "stateStr" : "SECONDARY",

    "uptime" : 613,

    "optime" : {

    "ts" : Timestamp(1549963707, 1),

    "t" : NumberLong(34)

    },

    "optimeDurable" : {

    "ts" : Timestamp(1549963707, 1),

    "t" : NumberLong(34)

    },

    "optimeDate" : ISODate("2019-02-12T09:28:27Z"),

    "optimeDurableDate" : ISODate("2019-02-12T09:28:27Z"),

    "lastHeartbeat" : ISODate("2019-02-12T09:28:30.719Z"),

    "lastHeartbeatRecv" : ISODate("2019-02-12T09:28:29.385Z"),

    "pingMs" : NumberLong(0),

    "syncingTo" : "192.168.197.21:27017",

    "configVersion" : 4

    }

    ],

    "ok" : 1

    }

    hqmongodb:PRIMARY> function add(){var i = 0;for(;i<20;i++){db.persons.insert({"nameA":"li"+i})}} #插入数据的命令

    hqmongodb:PRIMARY> add() #插入

    hqmongodb:PRIMARY> db.persons.find() #查看插入的数据

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e5f"), "nameA" : "li0" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e60"), "nameA" : "li1" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e61"), "nameA" : "li2" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e62"), "nameA" : "li3" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e63"), "nameA" : "li4" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e64"), "nameA" : "li5" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e65"), "nameA" : "li6" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e66"), "nameA" : "li7" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e67"), "nameA" : "li8" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e68"), "nameA" : "li9" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e69"), "nameA" : "li10" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e6a"), "nameA" : "li11" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e6b"), "nameA" : "li12" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e6c"), "nameA" : "li13" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e6d"), "nameA" : "li14" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e6e"), "nameA" : "li15" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e6f"), "nameA" : "li16" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e70"), "nameA" : "li17" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e71"), "nameA" : "li18" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e72"), "nameA" : "li19" }

  • 登陆从节点查看同步过来的数据

    [root@mongo02 ~]# mongo 192.168.197.23:27017 -u system -p 123456 --authenticationDatabase admin

    MongoDB shell version v3.4.10

    connecting to: 192.168.197.23:27017

    MongoDB server version: 3.4.10

    Server has startup warnings:

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten]

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten]

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten]

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten]

    hqmongodb:SECONDARY>

    hqmongodb:SECONDARY>

    hqmongodb:SECONDARY>

    hqmongodb:SECONDARY> show dbs #这里报错,执行rs.slaveOk()命令后在执行show dbs即可

    2019-02-12T04:31:34.256-0500 E QUERY [thread1] Error: listDatabases failed:{

    "ok" : 0,

    "errmsg" : "not master and slaveOk=false",

    "code" : 13435,

    "codeName" : "NotMasterNoSlaveOk"

    } :

    _getErrorWithCode@src/mongo/shell/utils.js:25:13

    Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1

    shellHelper.show@src/mongo/shell/utils.js:781:19

    shellHelper@src/mongo/shell/utils.js:671:15

    @(shellhelp2):1:1

    hqmongodb:SECONDARY> rs.slaveOk()

    hqmongodb:SECONDARY> show dbs

    admin 0.000GB

    local 0.000GB

    test 0.000GB

    hqmongodb:SECONDARY> db.persons.find() #查看同步过来的数据

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e62"), "nameA" : "li3" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e5f"), "nameA" : "li0" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e61"), "nameA" : "li2" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e60"), "nameA" : "li1" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e64"), "nameA" : "li5" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e63"), "nameA" : "li4" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e68"), "nameA" : "li9" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e66"), "nameA" : "li7" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e65"), "nameA" : "li6" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e67"), "nameA" : "li8" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e6b"), "nameA" : "li12" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e69"), "nameA" : "li10" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e6a"), "nameA" : "li11" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e6c"), "nameA" : "li13" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e6e"), "nameA" : "li15" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e6f"), "nameA" : "li16" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e70"), "nameA" : "li17" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e71"), "nameA" : "li18" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e6d"), "nameA" : "li14" }

    { "_id" : ObjectId("5c62921369ee6b1f7b0c1e72"), "nameA" : "li19" }

  • 测试当前主节点上的网络,然后查看集群状态

    或者23任意和节点查看主节点是否已经改变

    [root@mongo02 ~]# mongo 192.168.197.23:27017 -u system -p 123456 --authenticationDatabase admin

    MongoDB shell version v3.4.10

    connecting to: 192.168.197.23:27017

    MongoDB server version: 3.4.10

    Server has startup warnings:

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten]

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten]

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten]

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-12T04:18:10.941-0500 I CONTROL [initandlisten]

    hqmongodb:PRIMARY> rs.status() #登陆后看到前缀是PRIMARY,即为当前登陆的是主节点

    {

    "set" : "hqmongodb",

    "date" : ISODate("2019-02-12T10:49:16.358Z"),

    "myState" : 1,

    "term" : NumberLong(35),

    "heartbeatIntervalMillis" : NumberLong(2000),

    "optimes" : {

    "lastCommittedOpTime" : {

    "ts" : Timestamp(1549968548, 1),

    "t" : NumberLong(35)

    },

    "appliedOpTime" : {

    "ts" : Timestamp(1549968548, 1),

    "t" : NumberLong(35)

    },

    "durableOpTime" : {

    "ts" : Timestamp(1549968548, 1),

    "t" : NumberLong(35)

    }

    },

    "members" : [

    {

    "_id" : 0,

    "name" : "192.168.197.21:27017",

    "health" : 0,

    ,代表此节点已经离线

    "stateStr" : "(not reachable/healthy)",

    "uptime" : 0,

    "optime" : {

    "ts" : Timestamp(0, 0),

    "t" : NumberLong(-1)

    },

    "optimeDurable" : {

    "ts" : Timestamp(0, 0),

    "t" : NumberLong(-1)

    },

    "optimeDate" : ISODate("1970-01-01T00:00:00Z"),

    "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),

    "lastHeartbeat" : ISODate("2019-02-12T10:49:09.228Z"),

    "lastHeartbeatRecv" : ISODate("2019-02-12T10:48:45.604Z"),

    "pingMs" : NumberLong(0),

    "lastHeartbeatMessage" : "Couldn't get a connection within the time limit",

    "configVersion" : -1

    },

    {

    "_id" : 1,

    "name" : "192.168.197.22:27017",

    "health" : 1,

    "state" : 2,

    "stateStr" : "SECONDARY",

    "uptime" : 4862,

    "optime" : {

    "ts" : Timestamp(1549968548, 1),

    "t" : NumberLong(35)

    },

    "optimeDurable" : {

    "ts" : Timestamp(1549968548, 1),

    "t" : NumberLong(35)

    },

    "optimeDate" : ISODate("2019-02-12T10:49:08Z"),

    "optimeDurableDate" : ISODate("2019-02-12T10:49:08Z"),

    "lastHeartbeat" : ISODate("2019-02-12T10:49:15.981Z"),

    "lastHeartbeatRecv" : ISODate("2019-02-12T10:49:15.383Z"),

    "pingMs" : NumberLong(0),

    "syncingTo" : "192.168.197.23:27017",

    "configVersion" : 4

    },

    {

    "_id" : 2,

    "name" : "192.168.197.23:27017",

    "health" : 1,

    ,代表23即为当前的主节点

    "stateStr" : "PRIMARY",

    "uptime" : 5468,

    "optime" : {

    "ts" : Timestamp(1549968548, 1),

    "t" : NumberLong(35)

    },

    "optimeDate" : ISODate("2019-02-12T10:49:08Z"),

    "electionTime" : Timestamp(1549968535, 1),

    "electionDate" : ISODate("2019-02-12T10:48:55Z"),

    "configVersion" : 4,

    "self" : true

    }

    ],

    "ok" : 1

    }

  • 登陆命令

    [root@mongo01 software]# mongo 192.168.197.21:27017 -u system -p 123456 --authenticationDatabase admin

    [root@mongo01 software]# mongo 192.168.197.21:27017 -u administrator -p 123456 --authenticationDatabase admin

mongodb集群配置副本集的更多相关文章

  1. 搭建mongodb集群(副本集+分片)

    搭建mongodb集群(副本集+分片) 转载自:http://blog.csdn.net/bluejoe2000/article/details/41323051 完整的搭建mongodb集群(副本集 ...

  2. MongoDB集群搭建-副本集

    MongoDB集群搭建-副本集 概念性的知识,可以参考本人博客地址: 一.Master-Slave方案: 主从: 二.Replica Set方案: 副本集: 步骤:(只要按步骤操作,100%成功) 1 ...

  3. mongodb集群配置分片集群

    测试环境 操作系统:CentOS 7.2 最小化安装 主服务器IP地址:192.168.197.21 mongo01 从服务器IP地址:192.168.197.22 mongo02 从服务器IP地址: ...

  4. MongoDB健壮集群——用副本集做分片

    1.    MongoDB分片+副本集 健壮的集群方案 多个配置服务器 多个mongos服务器  每个片都是副本集 正确设置w 架构图 说明: 1.   此实验环境在一台机器上通过不同port和dbp ...

  5. Mongodb集群之副本集

    上篇咱们遗留了几个问题 1主节点是否能自己主动切换连接? 眼下须要手动切换 2主节点读写压力过大怎样解决 3从节点每一个上面的数据都是对数据库全量拷贝,从节点压力会不会过大 4数据压力达到机器支撑不了 ...

  6. MongoDB 2.6配置副本集,支持端口号修改和用户登录认证

    mongoDB系列之(二):mongoDB 副本集 Mongodb2.6副本集验证部署和认证 副本集有以下特点: 1. 最小构成是:primary,secondary,arbiter,一般部署是:pr ...

  7. centos7下安装部署mongodb集群(副本集模式)

    环境需求:Mongodb集群有三种模式:  Replica Set, Sharding,Master-Slaver.  这里部署的是Replica Set模式. 测试环境: 这里副本集(Replica ...

  8. MongoDB学习笔记~Mongo集群和副本集

    回到目录 一些概念 对于Mongo在数据容灾上,推荐的模式是使用副本集模式,它有一个对外的主服务器Primary,还有N个副本服务器Secondary(N>=1,当N=1时,需要有一台仲裁服务器 ...

  9. MongoDB集群-主从复制(副本集)、failover

    1.概念 主从复制的目的:数据冗余.备份.读写分离 主从方式:一主一从(不推荐,只能实现复制,主节点挂掉且未重新启动的时候,无法提升从节点为master),一主一从一裁判,一主多从 复制方式:主节点记 ...

随机推荐

  1. 使用Jekins自动构建项目(GitLab+Java Maven)

    1. 登录Jekins 前提: Jekins 已经部署完成并能登录版本:Jenkins2.60.2 http://IP:端口/jenkins/view/all/newJob 2. 新建构建任务 选择项 ...

  2. VSCode汉化

    1.打开VSCode 点击箭头指示地方  在搜索框中输入chinese 然后安装中文简体 2.按住 Ctrl+shift+p 选择配置显示语言 然后会看见下面的样子 添加 "locale&q ...

  3. Putting Boxes Together CodeForces - 1030F (带权中位数)

    #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> ...

  4. .net获取程序根目录

    获取程序的根目录: var path = HttpRuntime.AppDomainAppPath;

  5. css实现0.5px

    使用缩放来0.5来实现,注意兼容各种浏览器 .frz-list li:after { content: ''; position: absolute; bottom:; height: 1px; wi ...

  6. 把旧系统迁移到.Net Core 2.0 日记 (19) --UI转用adminLTE

    之前用JEasyUI,它的datagrid比较强大,但region的布局界面就太简陋了. 把布局界面改成adminLTE 左边菜单要做成动态,从xml文件加载, 可以参考NOPCommerce的源码来 ...

  7. Php基本类型学习

    1.8种基本的数据类型 1)四种基本类型 boolean (布尔类型) integer(整形类型) double  (双精度类型) string  (字符串类型) 2)两种复合类型 array (数组 ...

  8. 防止asp马后门

    好多朋友都拿的有webshell吧,基本上都加了密的...  可是,没见到源码,很难测试它到底有没有后门, 指不定给别人打工了...  下面贴种很简单的方法,大家别扔蛋哈 (asp的哦)  在代码的最 ...

  9. ActiveMQ使用介绍及实例

    上一篇讲了rabbitmq的使用以及demo,activemq的思路也与之有相似的地方. activemq是基于Java实现的.activemq没有rabbitmq那么多的路由规则,其通信方式只有两种 ...

  10. 拉格朗日乘子法&KKT条件

    朗日乘子法(Lagrange Multiplier)和KKT(Karush-Kuhn-Tucker)条件是求解约束优化问题的重要方法,在有等式约束时使用拉格朗日乘子法,在有不等约束时使用KKT条件.前 ...