MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

कैसे उत्तरदायी mongodb_user, mongodb_replicaset मॉड्यूल काम कर रहे हैं?

इसके बजाय printjson(rs.initiate()) कोशिश करें

rs.initiate(
  {
    _id: "configRS",
    configsvr: true,
    members: [
      { _id: 0, host: "10.0.1.141:27017" },
      { _id: 1, host: "10.0.2.229:27017" },
      { _id: 2, host: "10.0.3.30:27017" }
    ]
  }
);
rs.status();
while (! db.isMaster().ismaster ) { sleep(1000) }

फिर आपको किसी सदस्य को जोड़ने की आवश्यकता नहीं है।

सीएसआरएस के लिए मैं इस तरह की एक प्लेबुक का उपयोग करता हूं:

- hosts: config
  tasks:
  - name: Compose variables
    set_fact:
      rs_initiate: |      
        {% set members = [] %}
        {% for host in groups['config']  | sort %}
        {% set m = {'_id': loop.index0 } %}
        {% set _ = m.update({'host': host + '.' + ansible_domain + ':' + ports.config | string }) %}
        {% set _ = members.append(m) %}
        {% endfor %}
        {% set init = {'_id': replica_set.conf} %}
        {% set _ = init.update({'members': members}) %}
        {% set _ = init.update({'configsvr': true}) %}
        {{ init }}
      rs_members: |
        {% set members = [] %}
        {% for host in groups['config'] | sort %}
        {% set _ = members.append(host + '.' + ansible_domain + ':' + ports.config | string) %}
        {% endfor %}
        {{ members }}
      replicaSetURI: "mongodb://{{ groups['config'] | product([ports.config]) | map('join', ':') | join(',') }}/admin?authSource=admin&replicaSet={{ replica_set.conf }}" 

  - name: Check if Config Replicaset is initiated
    shell: 
      cmd: "/usr/bin/mongo --norc --quiet localhost:{{ ports.config }}"
      executable: /bin/bash
      stdin: "rs.status().codeName" 
    register: result
    changed_when: false
    check_mode: no
  
  
  - set_fact:
# Needed to ensure that the Config Server Replica Set (CSRS) is initiated only once
      rs: |
        {% set i = (result.stdout == 'NotYetInitialized') %}
        {% for host in ansible_play_hosts %}
        {% set i = i and (hostvars[host].result.stdout == 'NotYetInitialized') %}
        {% endfor %}
        {{ {'NotYetInitialized': i} }}
  
  
  
  - name: Initiate Config Replicaset
    shell: 
      cmd: "/usr/bin/mongo --norc --quiet localhost:{{ ports.config }}"
      executable: /bin/bash
      stdin: |
        var i = rs.initiate({{ rs_initiate | to_json }})
        if (i.ok != 1) print(i.errmsg)
        var _ = rs.status()
        while (! db.isMaster().ismaster ) sleep(1000)
        rs.status().members.map(x => x.name)
        if (i.ok == 1) {print(rs.status().ok)} else {print(0)}
    register: ret
    failed_when: ret.stdout_lines | last != "1"
    when: rs.NotYetInitialized and inventory_hostname_short == groups['config'] | sort | first)
  
  - debug:
      msg: "{{ ret.stdout_lines }}"
    when: not ansible_check_mode and rs.NotYetInitialized and inventory_hostname_short == (groups['config'] | sort | first) and ret.stdout != ''

मौजूदा सीएसआरएस में मेजबानों को जोड़ने के लिए मैं इसका उपयोग करता हूं:

- hosts: config
  tasks:
  
  - meta: end_play
    when: ansible_check_mode or rs.NotYetInitialized | default(false)

  - name: Check current Config Server Replica Set members
    shell: 
      cmd: "/usr/bin/mongo -u admin -p {{ password.admin }} --authenticationDatabase admin --norc --quiet localhost:{{ ports.config }}"
      executable: /bin/bash
      stdin: "rs.status().members.map(x => x.name)"
    register: result
    changed_when: false
    when: inventory_hostname_short == (groups['config'] | sort | first)

  - set_fact:
      current_members: "{{ result.stdout | from_json }}"
    when: inventory_hostname_short == (groups['config'] | sort | first)

  - name: Add host to Config Server Replica Set
    shell: 
      cmd: "/usr/bin/mongo -u admin -p {{ password.admin }} --authenticationDatabase admin --norc --quiet localhost:{{ ports.config }}"
      executable: /bin/bash
      stdin: "rs.add('{{ item }}')"
    when: inventory_hostname_short == (groups['config'] | sort | first)
    loop: "{{ rs_members | difference(current_members) | sort }}"
    register: ret
    failed_when: ret.stdout != ""

मेरे द्वारा इस प्लेबुक से बनाए गए उपयोगकर्ता

- hosts: application
  tasks:

  - name: Check if authentication is enabled
    shell: 
      cmd: "/usr/bin/mongo -u admin -p {{ password.admin }} --authenticationDatabase admin --norc --quiet localhost:{{ ports.router }}"
      executable: /bin/bash
      stdin: exit 
    register: authenticate
    failed_when: false
    changed_when: false
    check_mode: no
    when: inventory_hostname_short == (groups['application'] | sort | first)

  - name: Create admin user
    shell: 
      cmd: "/usr/bin/mongo {{ (authenticate.rc == 0) | ternary('-u admin -p ' + password.admin + ' --authenticationDatabase admin', '') }} --norc --quiet localhost:{{ ports.router }}"
      executable: /bin/bash
      stdin: |
        const admin = db.getSiblingDB("admin")
        {% if authenticate.rc != 0 %}
        admin.createUser({ user: "admin", pwd: "{{ password.admin }}", roles: ["root"] })
        var _ = admin.auth("admin", "{{ password.admin }}")
        {% endif %}
        // Create more users if needed
    when: inventory_hostname_short == (groups['application'] | sort | first)
    register: ret_createUser
    changed_when: ret_createUser.stdout != ''

  - debug:
      msg: "{{ ret_createUser.stdout.split('\n') }}"
    when: not ansible_check_mode and inventory_hostname_short == (groups['application'] | sort | first) and ret_createUser.stdout != '' 



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. मोंगोडब कनेक्शन त्रुटि हालांकि स्प्रिंगबूट में डोकर

  2. मोंगो निर्यात क्वेरी का उपयोग करने में असमर्थ

  3. एक ही दस्तावेज़ के लिए MongoDB समवर्ती अद्यतन परमाणु व्यवहार नहीं कर रहा है

  4. एंबेडमोंगो रिएक्टिवमोंगो प्रक्रिया के साथ बाहर नहीं निकलता है

  5. नोड.जेएस एक्सप्रेस पासपोर्ट रूटिंग