-
测试环境
操作系统: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
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
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
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分布式集群搭建----副本集----PSS/PSA
- mongodb 分片集群切换单个分片副本集primary的正确步骤
- 学习MongoDB(二) Replica Set集群配置
- MongoDB Windows 集群配置
- ES 7.0版本集群配置报错,没发现master的异常。[hadoop-1] master not discovered or elected yet, an election requires
- MongoDB--配置副本集群权限文件keyfile
- 第40章:MongoDB-集群--Replica Sets(副本集)---副本集的管理
- MongoDB高可用集群配置的方案
- MongoDB 2.6配置副本集,支持端口号修改和用户登录认证
- MongoDB学习笔记~Mongo集群和副本集