minhhungit / mongodb-cluster-docker-compose

docker-compose for mongodb cluster sharded with replication

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No shards found

sandro971 opened this issue · comments

Hello
i have follow your instructions and when i try to create a database, i have this error :
**Failed to create database 'm'.

Error:
Unable to initialize targeter for write op for collection m.temp :: caused by :: Database m not found :: caused by :: No shards found**

My script :

def deploy(self, stack_name):
        try:
            #Step 1: Start all of the containers
            print(':::::: Step 1: Start all of the containers')
            os.system('docker stack deploy -c {0}/scripts/mongo/docker-compose.yml {1}'.format(root, stack_name))


            #Step 2: Initialize the replica sets (config servers and shards) and routers
            print(':::::: Step 2: Initialize the replica sets (config servers and shards) and routers')

            # time.sleep(60)
            configsvrs_results = self.get_containers([stack_name + '_configsvr01'])

            if len(list(configsvrs_results['containers'].keys())) == 0:
                raise ValueError(stack_name + '_configsvr01 not found')
            else:
                mongo_configsvr01 = configsvrs_results['containers'][
                    list( configsvrs_results['containers'].keys() )[0]
                ]

                print(mongo_configsvr01.exec_run('mongo --port 27017 --configsvr --replSet rs-config-server'))
                print('----' * 20)

                print(mongo_configsvr01.exec_run('mongo < /scripts/init-configserver.js'.format(root, stack_name)))
                print('----' * 20)
            

            shards_results = self.get_containers([stack_name + '_shard01-a', stack_name + '_shard02-a', stack_name + '_shard03-a'])
            if shards_results['counter'] != 3 :
                raise ValueError('all shards not founds')
            else:
                for name in shards_results['containers']:
                    ctn = shards_results['containers'][name]
                    print('<<<<<<<<<<<<>>>>>>>>>>>>>', name)
                    ref = re.match(r"_shard([0-9]+)-a", name)
                    if ref is not None:
                        print('<<<<<<<<<<<<>>>>>>>>>>>>>', ref.group(1))
                        print(ctn.exec_run('mongo < /scripts/scripts/init-shard{1}.js'.format(ref.group(1))))


            # time.sleep(60)
            #Step 3: Connect to the primary and add arbiters
            print(':::::: Step 3: Connect to the primary and add arbiters')
            for name in shards_results['containers']:
                ctn = shards_results['containers'][name]
                ref = re.match(r"_shard([0-9]+)-a", name)
                if ref is not None:
                    print('<<<<<<<<<<<<>>>>>>>>>>>>>', ref.group(1))
                    print(ctn.exec_run("echo 'rs.addArb(\""+stack_name+ "_shard{0}-x:27017\")' | mongo --port 27017".format(ref.group(1))))


            #Step 4: Initializing the router
            print('::::::: Step 4: Initializing the router')

            #Note: Wait a bit for the config server and shards to elect their primaries before initializing the router
            time.sleep(60)

            router01_results = self.get_containers([stack_name + '_router01'])
            if len(router01_results['containers']) == 0:
                raise ValueError(stack_name + '_router01 not founds')
            else:
                router01 = router01_results['containers'][list(router01_results['containers'].keys())[0]]
                print(router01.exec_run('mongo < /scripts/init-router.js'.format(root)))
            

            #Step 5: Enable sharding and setup sharding-key
            print('::::::: Step 5: Enable sharding and setup sharding-key')
            print(router01.exec_run('mongo < /scripts/enable-sharding.js'.format(root)))

            #Verify
            print('::::::: Verify')

            time.sleep(60)
            print('---- Verify the status of the sharded cluster')
            #Verify the status of the sharded cluster
            print(router01.exec_run("echo 'sh.status()' | mongo --port 27017"))


            print('---- Verify status of replica set for each shard')
            for name in shards_results['containers']:
                ctn = shards_results['containers'][name]
                #Verify status of replica set for each shard
                print('===================' )
                print(ctn.name)
                print('===================')
                print(ctn.exec_run("echo 'sh.status()' | mongo --port 27017"))
            

            print('---- Check database status')
            print(router01.exec_run("echo 'use MyDatabase' | mongo --port 27017"))
            print(router01.exec_run("echo 'db.stats()' | mongo --port 27017"))
            print(router01.exec_run("echo 'db.getUsers()' | mongo --port 27017"))
            print(router01.exec_run("echo 'db.changeUserPassword(\"admin\", \"6b65795f31\")' | mongo --port 27017"))
            print(router01.exec_run("echo 'db.MyCollection.getShardDistribution()' | mongo --port 27017"))
        except Exception as e:
            print(e)
            exit()
commented

Unlucky I don't know python

commented

no longer active