MongoDB分片集群搭建及扩容

准备工作

在CentOS 7 上安装MongoDB分片集群

6 Servers 代表:6台服务器上都要操作.

环境准备 On 6 Servers

给虚拟机设置静态IP
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

修改以下
#BOOTPROTO=“dhcp”
BOOTPROTO=“static”
IPADDR=“.xx.xx.xx.xx”
NETMASK=“255.255.255.0”
GATEWAY=“.xx.xx.xx.xx”
DNS1=“.xx.xx.xx.xx”

systemctl restart network
reboot

如果连不上外网,可以尝试断开虚拟机的网络,再重新连接

ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub root@xx.xx.xx.24
ssh-copy-id -i ~/.ssh/id_rsa.pub root@xx.xx.xx.71
ssh-copy-id -i ~/.ssh/id_rsa.pub root@xx.xx.xx.73
ssh-copy-id -i ~/.ssh/id_rsa.pub root@xx.xx.xx.74
ssh-copy-id -i ~/.ssh/id_rsa.pub root@xx.xx.xx.75
ssh-copy-id -i ~/.ssh/id_rsa.pub root@xx.xx.xx.76

[root@mongo1 ~]# getenforce
Enforcing

[root@mongo1 ~]# sudo setenforce 0
[root@mongo1 lib]# vi /etc/selinux/config
SELINUX=disabled

systemctl stop firewalld
systemctl disable firewalld

sudo shutdown -r now

开始安装MongoDB On 6 Servers

[root@mongo1 ~]# sudo yum -y install libcurl openssl
[root@mongo1 ~]# curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.6.tgz
[root@mongo1 ~]# tar -zxvf mongodb-linux-*-4.2.6.tgz

[root@mongo1 ~]# adduser mongod
[root@mongo1 ~]# passwd mongod

为服务器设置域名 On 6 Servers

我们将使用域名来搭建分片集。使用域名也是搭建MongoDB集群的推荐方式,它在以后发生迁移时将带来很多便利。

vi /etc/hosts
xx.xx.xx.24 mongod1 member1.mongod.com
xx.xx.xx.71 mongod2 member2.mongod.com
xx.xx.xx.73 mongod3 member3.mongod.com
xx.xx.xx.74 mongod4 member4.mongod.com
xx.xx.xx.75 mongod5 member5.mongod.com
xx.xx.xx.76 mongod6 member6.mongod.com

服务器分工

在本例中,我们将使用:

  • member1/member3/member5搭建shard1config
  • member2/member4/member6搭建shard2mongos
member1member2member3member4member5member6
shard1
shard2
config
mongos

准备分片目录

在各服务器上创建数据目录,我们使用/data,请按自己需要修改为其他目录:

  • member1/member3/member5上执行以下命令:

    mkdir -p /var/lib/mongo/shard1/
    mkdir -p /var/lib/mongo/config/
    
  • member2/member4/member6上执行以下命令:

    mkdir -p /var/lib/mongo/shard2/
    mkdir -p /var/lib/mongo/mongos/
    
sudo chown -R mongod:mongod /var/lib/mongo

Ensure the binaries are in a directory listed in your PATH environment variable.

sudo cp /root/mongodb-linux-x86_64-rhel70-4.2.6/bin/* /usr/local/bin/

搭建分片

搭建shard1

member1/member3/member5上执行以下命令。注意以下参数:

  • shardsvr: 表示这不是一个普通的复制集,而是分片集的一部分;
  • wiredTigerCacheSizeGB: 该参数表示MongoDB能够使用的缓存大小。默认值为(RAM - 1GB) / 2
    • 不建议配置超过默认值,有OOM的风险;
    • 因为我们当前测试会在一台服务器上运行多个实例,因此配置了较小的值;
  • bind_ip: 生产环境中强烈建议不要绑定外网IP,此处为了方便演示绑定了所有IP地址。类似的道理,生产环境中应开启认证--auth,此处为演示方便并未使用;
mongod --bind_ip 0.0.0.0 --replSet shard1 --dbpath /var/lib/mongo/shard1 --logpath /var/lib/mongo/shard1/mongod.log --port 27010 --fork --shardsvr --wiredTigerCacheSizeGB 1

用这三个实例搭建shard1复制集:

  • 任意连接到一个实例,例如我们连接到member1.example.com

    mongo --host member1.mongod.com:27010
    
  • 初始化shard1复制集。我们使用如下配置初始化复制集:

    rs.initiate({
        _id: "shard1",
        "members" : [
            {
                "_id": 0,
                "host" : "member1.mongod.com:27010"
            },
            {
                "_id": 1,
                "host" : "member3.mongod.com:27010"
            },
            {
                "_id": 2,
                "host" : "member5.mongod.com:27010"
            }
        ]
    });
    
  • 查看初始化复制集状态:

    rs.status()
    

搭建config

shard1类似的方式,我们可以搭建config服务器。在member1/member3/member5上执行以下命令:

  • 运行config实例:

    mongod --bind_ip 0.0.0.0 --replSet config --dbpath /var/lib/mongo/config --logpath /var/lib/mongo/config/mongod.log --port 27019 --fork --configsvr --wiredTigerCacheSizeGB 1
    
  • 连接到member1

    mongo --host member1.mongod.com:27019
    
  • 初始化config复制集:

    rs.initiate({
        _id: "config",
        "members" : [
            {
                "_id": 0,
                "host" : "member1.mongod.com:27019"
            },
            {
                "_id": 1,
                "host" : "member3.mongod.com:27019"
            },
            {
                "_id": 2,
                "host" : "member5.mongod.com:27019"
            }
        ]
    });
    
  • 查看初始化config复制集状态:

    rs.status()
    

搭建mongos

mongos的搭建比较简单,我们在member2/member4/member6上搭建3个mongos。注意以下参数:

  • configdb: 表示config使用的集群地址;

开始搭建:

  • 运行mongos进程:

    mongos --bind_ip 0.0.0.0 --logpath /var/lib/mongo/mongos/mongos.log --port 27017 --configdb config/member1.mongod.com:27019,member3.mongod.com:27019,mongod.example.com:27019 --fork
    
  • 连接到任意一个mongos,此处我们使用member2

    mongo --host member2.mongod.com:27017
    
  • shard1加入到集群中:

    sh.addShard("shard1/member1.mongod.com:27010,member3.mongod.com:27010,member5.mongod.com:27010");
    
  • 查看shard1复制集状态:

    sh.status()
    

测试分片集

上述示例中我们搭建了一个只有1个分片的分片集。在继续之前我们先来测试一下这个分片集。

  • 连接到分片集:

    mongo --host member2.mongod.com:27017
    
    sh.status();
    
    mongos> sh.status()
    --- Sharding Status ---
      sharding version: {
            "_id" : 1,
            "minCompatibleVersion" : 5,
            "currentVersion" : 6,
            "clusterId" : ObjectId("5eb6523ae71638d514e84f79")
      }
      shards:
            {  "_id" : "shard1",  "host" : "shard1/member1.mongod.com:27010,member3.mongod.com:27010,member5.mongod.com:27010",  "state" : 1 }
      active mongoses:
            "4.2.6" : 3
      autosplit:
            Currently enabled: yes
      balancer:
            Currently enabled:  yes
            Currently running:  no
            Failed balancer rounds in last 5 attempts:  0
            Migration Results for the last 24 hours:
                    No recent migrations
      databases:
            {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
    
  • 创建一个分片表:

    sh.enableSharding("foo");
    sh.shardCollection("foo.bar", {_id: 'hashed'});
    sh.status();
    
    ...
            {  "_id" : "foo",  "primary" : "shard1",  "partitioned" : true,  "version" : {  "uuid" : UUID("838293c5-f083-4a3b-b75e-548cfe2f6087"),  "lastMod" : 1 } }
                    foo.bar
                            shard key: { "_id" : "hashed" }
                            unique: false
                            balancing: true
                            chunks:
                                    shard1  2
                            { "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong(0) } on : shard1 Timestamp(1, 0)
                            { "_id" : NumberLong(0) } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 1)
    
  • 任意写入若干数据:

    use foo
    for (var i = 0; i < 10000; i++) {
        db.bar.insert({i: i});
    }
    sh.status()
    
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5eb6523ae71638d514e84f79")
  }
  shards:
        {  "_id" : "shard1",  "host" : "shard1/member1.mongod.com:27010,member3.mongod.com:27010,member5.mongod.com:27010",  "state" : 1 }
  active mongoses:
        "4.2.6" : 3
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard1  1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
        {  "_id" : "foo",  "primary" : "shard1",  "partitioned" : true,  "version" : {  "uuid" : UUID("48bd1e44-b995-4ebb-bb82-b379ca38ef2a"),  "lastMod" : 1 } }
                foo.bar
                        shard key: { "_id" : "hashed" }
                        unique: false
                        balancing: true
                        chunks:
                                shard1  2
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong(0) } on : shard1 Timestamp(1, 0)
                        { "_id" : NumberLong(0) } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 1)

向分片集加入新的分片

下面我们搭建shard2并将其加入分片集中,观察发生的效果。

使用类似shard1的方式搭建shard2。在member2/member4/member6上执行以下命令:

mongod --bind_ip 0.0.0.0 --replSet shard2 --dbpath /var/lib/mongo/shard2 --logpath /var/lib/mongo/shard2/mongod.log --port 27010 --fork --shardsvr --wiredTigerCacheSizeGB 1

用这三个实例搭建shard2复制集:

  • 任意连接到一个实例,例如我们连接到member2.mongod.com

    mongo --host member2.mongod.com:27010
    
  • 初始化shard2复制集。我们使用如下配置初始化复制集:

    rs.initiate({
        _id: "shard2",
        "members" : [
            {
                "_id": 0,
                "host" : "member2.mongod.com:27010"
            },
            {
                "_id": 1,
                "host" : "member4.mongod.com:27010"
            },
            {
                "_id": 2,
                "host" : "member6.mongod.com:27010"
            }
        ]
    });
    
    rs.status()
    members里面看到primary就选举成功了
    
  • 连接到任意一个mongos。此处使用member1

    mongo --host member2.mongod.com:27017
    
  • shard2加入到集群中:

    sh.addShard("shard2/member2.mongod.com:27010,member4.mongod.com:27010,member6.mongod.com:27010");
    
    {
        "shardAdded" : "shard2",
        "ok" : 1,
        "operationTime" : Timestamp(1577498687, 6),
        "$clusterTime" : {
            "clusterTime" : Timestamp(1577498687, 6),
            "signature" : {
                "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                "keyId" : NumberLong(0)
            }
        }
    }
    
  • 观察sh.status()

    sh.status();
    
    ...
    {  "_id" : "foo",  "primary" : "shard1",  "partitioned" : true,  "version" : {  "uuid" : UUID("838293c5-f083-4a3b-b75e-548cfe2f6087"),  "lastMod" : 1 } }
        foo.bar
                shard key: { "_id" : "hashed" }
                unique: false
                balancing: true
                chunks:
                        shard1  1
                        shard2  1
                { "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong(0) } on : shard2 Timestamp(2, 0)
                { "_id" : NumberLong(0) } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(2, 1)
    

可以发现原本shard1上的两个chunk被均衡到了shard2上,这就是MongoDB的自动均衡机制。

其他参考

[root@mongod2 shard2]# netstat -anp | grep 27010
tcp        0      0 0.0.0.0:27010           0.0.0.0:*               LISTEN      1731/mongod
tcp        0      0 xx.xx.xx.71:34664       xx.xx.xx.24:27010       ESTABLISHED 1678/mongos
tcp        0      0 xx.xx.xx.71:39040       xx.xx.xx.75:27010       ESTABLISHED 1678/mongos
tcp        0      0 xx.xx.xx.71:43050       xx.xx.xx.73:27010       ESTABLISHED 1678/mongos
unix  2      [ ACC ]     STREAM     LISTENING     20336    1731/mongod          /tmp/mongodb-27010.sock
[root@mongod2 shard2]# netstat -anp | grep 27019
tcp        0      0 xx.xx.xx.71:40120       xx.xx.xx.24:27019       ESTABLISHED 1678/mongos
tcp        0      0 xx.xx.xx.71:49938       xx.xx.xx.75:27019       ESTABLISHED 1678/mongos
tcp        0      0 xx.xx.xx.71:49934       xx.xx.xx.75:27019       ESTABLISHED 1678/mongos
tcp        0      0 xx.xx.xx.71:40162       xx.xx.xx.24:27019       ESTABLISHED 1678/mongos
tcp        0      0 xx.xx.xx.71:37928       xx.xx.xx.73:27019       ESTABLISHED 1678/mongos
tcp        0      0 xx.xx.xx.71:37940       xx.xx.xx.73:27019       ESTABLISHED 1678/mongos
[root@mongod2 shard2]# netstat -anp | grep 27017
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      1678/mongos
unix  2      [ ACC ]     STREAM     LISTENING     19533    1678/mongos          /tmp/mongodb-27017.sock
[root@mongod2 shard2]# netstat -pnltu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1183/master
tcp        0      0 0.0.0.0:27010           0.0.0.0:*               LISTEN      1731/mongod
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      1678/mongos
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1027/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1183/master
tcp6       0      0 :::22                   :::*                    LISTEN      1027/sshd
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1582/dhclient
[root@mongod2 shard2]# netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:27010           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 xx.xx.xx.71:40120       xx.xx.xx.24:27019       ESTABLISHED
tcp        0      0 xx.xx.xx.71:34664       xx.xx.xx.24:27010       ESTABLISHED
tcp        0      0 xx.xx.xx.71:49938       xx.xx.xx.75:27019       ESTABLISHED
tcp        0      0 xx.xx.xx.71:49934       xx.xx.xx.75:27019       ESTABLISHED
tcp        0      0 xx.xx.xx.71:22          10.20.45.99:62782       ESTABLISHED
tcp        0      0 xx.xx.xx.71:40162       xx.xx.xx.24:27019       ESTABLISHED
tcp        0      0 xx.xx.xx.71:39040       xx.xx.xx.75:27010       ESTABLISHED
tcp        0      0 xx.xx.xx.71:37928       xx.xx.xx.73:27019       ESTABLISHED
tcp        0      0 xx.xx.xx.71:37940       xx.xx.xx.73:27019       ESTABLISHED
tcp        0      0 xx.xx.xx.71:43050       xx.xx.xx.73:27010       ESTABLISHED
tcp        0     36 xx.xx.xx.71:22          10.20.45.99:65457       ESTABLISHED
tcp6       0      0 ::1:25                  :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
为什么以下的各种方式登录mongo shell后的查询的结果都不一样呢,后面发现是初始化`shard2`复制集

[root@mongod2 shard2]# mongo localhost:27010
MongoDB shell version v4.2.6
connecting to: mongodb://localhost:27010/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("67c6510e-320d-4ded-9727-1134fb41684d") }
MongoDB server version: 4.2.6
Server has startup warnings:
2020-05-09T15:14:07.983+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> show dbs
2020-05-09T15:55:52.626+0800 E  QUERY    [js] uncaught exception: Error: listDatabases failed:{
        "operationTime" : Timestamp(0, 0),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(0, 0),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:135:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:87:12
shellHelper.show@src/mongo/shell/utils.js:906:13
shellHelper@src/mongo/shell/utils.js:790:15
@(shellhelp2):1:1
> exit
bye

[root@mongod2 shard2]# mongo
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ee8647bc-d9e0-4082-8878-d1aa931538b6") }
MongoDB server version: 4.2.6
Server has startup warnings:
2020-05-09T14:51:35.994+0800 I  CONTROL  [main]
2020-05-09T14:51:35.994+0800 I  CONTROL  [main] ** WARNING: Access control is not enabled for the database.
2020-05-09T14:51:35.994+0800 I  CONTROL  [main] **          Read and write access to data and configuration is unrestricted.
2020-05-09T14:51:35.994+0800 I  CONTROL  [main] ** WARNING: You are running this process as the root user, which is not recommended.
2020-05-09T14:51:35.994+0800 I  CONTROL  [main]
mongos> show dbs
admin   0.000GB
config  0.001GB
foo     0.000GB
mongos>exit
bye

[root@mongod2 shard2]# mongo 127.0.0.1:27010
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27010/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("4e8766b3-a911-479a-b316-faaebeb20d91") }
MongoDB server version: 4.2.6
Server has startup warnings:
2020-05-09T15:14:07.983+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> show dbs;
2020-05-09T16:00:02.809+0800 E  QUERY    [js] uncaught exception: Error: listDatabases failed:{
        "operationTime" : Timestamp(0, 0),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(0, 0),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:135:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:87:12
shellHelper.show@src/mongo/shell/utils.js:906:13
shellHelper@src/mongo/shell/utils.js:790:15
@(shellhelp2):1:1
>exit
bye

[root@mongod2 shard2]# mongo mongod2:27010
MongoDB shell version v4.2.6
connecting to: mongodb://mongod2:27010/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6741d5fe-71fb-446d-a515-5787e6cd162d") }
MongoDB server version: 4.2.6
Server has startup warnings:
2020-05-09T15:14:07.983+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> show dbs
2020-05-09T16:00:56.963+0800 E  QUERY    [js] uncaught exception: Error: listDatabases failed:{
        "operationTime" : Timestamp(0, 0),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(0, 0),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:135:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:87:12
shellHelper.show@src/mongo/shell/utils.js:906:13
shellHelper@src/mongo/shell/utils.js:790:15
@(shellhelp2):1:1
> exit
bye

[root@mongod2 shard2]# mongo --port 27010
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27010/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f8595dca-1918-41c8-84e2-87ddb955f961") }
MongoDB server version: 4.2.6
Server has startup warnings:
2020-05-09T15:14:07.983+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-05-09T15:14:07.984+0800 I  CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> show dbs
2020-05-09T16:01:46.964+0800 E  QUERY    [js] uncaught exception: Error: listDatabases failed:{
        "operationTime" : Timestamp(0, 0),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(0, 0),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:135:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:87:12
shellHelper.show@src/mongo/shell/utils.js:906:13
shellHelper@src/mongo/shell/utils.js:790:15
@(shellhelp2):1:1
> exit;
bye


[root@mongod6 ~]# pgrep mongo -l
1671 mongos
1720 mongod
[root@mongod6 ~]#  cat /var/lib/mongo/shard2/mongod.log
2020-05-09T16:22:03.590+0800 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-05-09T16:22:03.594+0800 W  ASIO     [main] No TransportLayer configured during NetworkInterface startup
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten] MongoDB starting : pid=1811 port=27010 dbpath=/var/lib/mongo/shard2 64-bit host=mongod6
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten] db version v4.2.6
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten] git version: 20364840b8f1af16917e4c23c1b5f5efd8b352f8
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten] allocator: tcmalloc
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten] modules: none
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten] build environment:
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten]     distmod: rhel70
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten]     distarch: x86_64
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten]     target_arch: x86_64
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten] options: { net: { bindIp: "0.0.0.0", port: 27010 }, processManagement: { fork: true }, replication: { replSet: "shard2" }, sharding: { clusterRole: "shardsvr" }, storage: { dbPath: "/var/lib/mongo/shard2", wiredTiger: { engineConfig: { cacheSizeGB: 1.0 } } }, systemLog: { destination: "file", path: "/var/lib/mongo/shard2/mongod.log" } }
2020-05-09T16:22:03.594+0800 E  STORAGE  [initandlisten] Failed to set up listener: SocketException: Address already in use
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten] now exiting
2020-05-09T16:22:03.594+0800 I  CONTROL  [initandlisten] shutting down with code:48

其他参考命令

pgrep mongo -l
ps -ef | grep mongod
mongo localhost:27010

sudo cp /root/mongodb-linux-x86_64-rhel70-4.2.6/bin/* /usr/local/bin/
sudo ln -s  /root/mongodb-linux-x86_64-rhel70-4.2.6/bin/* /usr/local/bin/
export PATH=$PATH:/root/mongodb-linux-x86_64-rhel70-4.2.6/bin

直到出现primary
rs.status()
sh.status()

版权声明:本文为GongMeiyan原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。