MONGODB: mudanças entre as edições

De Wiki Clusterlab.com.br
Ir para navegação Ir para pesquisar
 
(Sem diferença)

Edição atual tal como às 20h21min de 18 de outubro de 2017

MongoDB

Instalação

Documentação


APT

  • Adicionando a chave
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
  • Adicionando o repositório
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/testing multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
  • Atualizando as informações do repositório
apt-get update
  • Instalando o MongoDB
apt-get install -y mongodb-org

FileSystem

lvcreate -n mongodb -L 4G ubuntu-vg
mkfs.ext4 /dev/ubuntu-vg/mongodb
mkdir -p /mongodb/run
mkdir -p /mongodb/log
echo UUID=$(blkid | grep mongodb | awk -F \" '{print $2}' ) /mongodb ext4 defaults 1 2 >> /etc/fstab
mount /mongodb
chown -R mongodb:mongodb /mongodb/

Configuração

  • /mongodb/mongod.conf
logpath=/mongodb/log/mongod.log
logappend=false
fork=true
dbpath=/mongodb/
storageEngine=wiredTiger
pidfilepath=/mongodb/run/mongod.pid
bind_ip=0.0.0.0
replSet=rs0
noscripting=true
#nohttpinterface=true
#rest=false
auth=false
keyFile=/mongodb/mongodb-keyfile
#verbose=true

Gerando Chave para replicaSet

openssl rand -base64 741 > /mongodb/mongodb-keyfile
chmod 600 /mongodb/mongodb-keyfile

Serviço

  • Habilitando para subir no boot
systemctl enable mongod
  • Iniciando o MongoDB
systemctl start mongod
  • Verificando status
systemctl status mongod

ReplicaSet

  • Conectar no mongo
mongo
  • Configurar ReplicaSet
rs.initiate( {
   _id : "rs0",
   members: [ { _id : 0, host : "cli-154.rocko83.com.br:27017" } ]
})

Comandos

Autenticar no MongoDB

mongo --username root -p --authenticationDatabase admin

Criar usuário Admin

use admin
db.createUser(
    {
      user: "root",
      pwd: "senha",
      roles: [ "root" ]
    }
)

Roles

  • User
    • readWrite
    • read
  • admin
    • dbAdmin
    • dbOwner
    • userAdmin
  • Cluster
    • clusterAdmin
    • clusterManager
    • clusterMonitor
    • hostManager
  • Backup
    • backup
    • restore
  • All-Database
    • readAnyDatabase
    • readWriteAnyDatabase
    • userAdminAnyDatabase
    • dbAdminAnyDatabase
  • Superuser
    • root
  • Internal
    • __system

Trocando a Senha

use products
db.changeUserPassword("accountUser", "senha")

Configurar ReplicaSet

config = {
    _id : "rs0",
     members : [
         {_id : 0, host : "mongo-db01:27016"},
         {_id : 1, host : "mongo-db02:27018"},
         {_id : 2, host : "mongo-db03:27017"},
     ]
}

rs.initiate(config)

Listar Usuários

rs0:PRIMARY> db.getUsers()
[
        {
                "_id" : "servername",
                "user" : "usuario",
                "db" : "database",
                "roles" : [
                        {
                                "role" : "readWrite",
                                "db" : "database"
                        }
                ]
        }
]

Dump de base

mongodump --username root --password root --authenticationDatabase admin --host rs0/servidor:27017 --db <nome da base>

Restore de Base

mongorestore --username root --password root --authenticationDatabase admin --host rs0/servidor:27017 --drop --db <nome da base> <diretório do dump>

Repare que o comando assima faz o drop da base antes de fazer o restore.

Conectando na Base

mongo --username root --password root --authenticationDatabase admin --host rs0/mongodb:27017

Criando usuários

use database
db.createUser({ user: "usuario1", pwd: "senha", roles: ["readWrite"] })
db.createUser({ user: "usuario2", pwd: "senha", roles: ["read"] })

Listando usuários

use database
db.getUsers()

Reconfigurar um replicaSet

cfg = rs.conf()
cfg.members[2].priority = 2
rs.reconfig(cfg)

Exemplos de cfg

rs0:SECONDARY> rs.conf()
{
        "_id" : "rs0",
        "version" : 4,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "mongodb01:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "mongodb02:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "mongodb03:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 0,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : 2000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("589c6f41b7030bf4a2738d9d")
        }
}