Exemplos de connection string
Nesta página
- Exemplos de connection string de sistema do MongoDB Atlas
- Agrupamento do Atlas do MongoDB que se autentica com um nome de usuário e senha
- MongoDB Atlas Cluster que autentica com credenciais AWS IAM
- Exemplos de strings de conexão de implantação auto-hospedada
- Agrupamento auto-hospedado que se autentica com um nome de usuário e senha
- Servidor de banco de dados auto-hospedado em execução localmente
- Banco de dados
admin
auto-hospedado executando localmente - Banco de dados
records
auto-hospedado executando localmente - Soquete de domínio UNIX auto-hospedado
- Conjunto de réplica auto-hospedada com membros em diferentes máquinas
- Conjunto de réplica auto-hospedada com membros ligados
localhost
- Conjunto de réplica auto-hospedada com distribuição de leitura
- Conjunto de réplica auto-hospedada com um alto nível de preocupação de escrita
- Cluster fragmentado auto-hospedado
➤ Use o menu suspenso Selecione a linguagem no canto superior direito para definir a linguagem dos exemplos a seguir.
Você pode usar strings de conexão para definir conexões entre instâncias do MongoDB e os seguintes destinos:
Seus aplicativos quando você se conecta usando o drivers.
Ferramentas como MongoDB Compass e o MongoDB Shell (mongosh).
Esta página fornece exemplo de strings de URI para destinos de conexão comuns.
Exemplos de connection string de sistema do MongoDB Atlas
As cadeias de conexão de exemplo a seguir se conectam a uma implantação do MongoDB Atlas.
Agrupamento do Atlas do MongoDB que se autentica com um nome de usuário e senha
mongosh "mongodb+srv://cluster0.example.mongodb.net myFirstDatabase" --apiVersion 1 --username myDatabaseUser
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority
MongoDB Atlas Cluster que autentica com credenciais AWS IAM
O seguinte se conecta a um MongoDB Atlas cluster do que foi configurado para suportar autenticação por meio Amazon Web Services de credenciais IAM:
mongosh 'mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS'
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
A conexão ao Atlas utilizando credenciais Amazon Web Services IAM desta maneira utiliza o MONGODB-AWS
authentication mechanism
e o $external
authSource
, como mostrado neste exemplo.
Se estiver usando um Amazon Web Services token de sessão, também forneça o AWS_SESSION_TOKEN
authMechanismProperties
valor , como segue:
mongosh 'mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>'
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
Observação
Se o ID da chave de acesso da AWS, a chave de acesso secreta ou o token de sessão incluírem os seguintes caracteres:
$ : / ? # [ ] @
esses caracteres devem ser convertidos usando codificação percentual.
Você também pode definir essas credenciais em sua plataforma usando Amazon Web Services variáveis de ambiente padrão IAM . mongosh
verifica as seguintes variáveis de ambiente quando você usa o MONGODB-AWS
authentication mechanism
:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_SESSION_TOKEN
Se definidas, essas credenciais não precisam ser especificadas na cadeia de conexão.
O exemplo a seguir define essas variáveis de ambiente no shell bash
:
export AWS_ACCESS_KEY_ID='<aws access key id>' export AWS_SECRET_ACCESS_KEY='<aws secret access key>' export AWS_SESSION_TOKEN='<aws session token>'
A sintaxe para definir variáveis de ambiente em outros shells será diferente. Consulte a documentação da sua plataforma para obter mais informações.
Você pode verificar se essas variáveis de ambiente foram definidas com o seguinte comando:
env | grep AWS
Depois de definido, o exemplo a seguir se conecta a um MongoDB Atlas cluster usando estas variáveis de ambiente:
mongosh 'mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS'
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
Exemplos de strings de conexão de implantação auto-hospedada
As cadeias de conexão de exemplo a seguir se conectam a uma implantação auto-hospedada.
Agrupamento auto-hospedado que se autentica com um nome de usuário e senha
A cadeia de conexão do conjunto de réplicas a seguir inclui estes elementos:
A opção
replicaSet
O(s) nome(s) de host da(s) instância(s)
mongod
conforme listado(s) na configuração do conjunto de réplicasEle se autentica como usuário
myDatabaseUser
com a senhaD1fficultP%40ssw0rd
para forçar o controle de acesso
mongosh "mongodb+srv://mongodb0.example.com/?authSource=admin&replicaSet=myRepl" --apiVersion 1 --username myDatabaseUser
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
Se o nome de usuário ou senha incluir os seguintes caracteres, esses caracteres devem ser convertidos usando codificação percentual:
$ : / ? # [ ] @
A seguinte string de conexão de cluster fragmentado inclui os seguintes elementos:
Os hosts
mongos
na string de conexãoEle se autentica como usuário
myDatabaseUser
com a senhaD1fficultP%40ssw0rd
para forçar o controle de acesso
mongosh "mongodb+srv://mongos0.example.com/?authSource=admin" --apiVersion 1 --username myDatabaseUser
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
Se o nome de usuário ou senha incluir os seguintes caracteres, esses caracteres devem ser convertidos usando codificação percentual:
$ : / ? # [ ] @
A seguinte string de conexão é autenticada como usuário myDatabaseUser
com a senha D1fficultP%40ssw0rd
para impor o controle deacesso:
mongosh "mongodb+srv://mongodb0.example.com/?authSource=admin" --apiVersion 1 --username myDatabaseUser
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
Se o nome de usuário ou senha incluir os seguintes caracteres, esses caracteres devem ser convertidos usando codificação percentual:
$ : / ? # [ ] @
A cadeia de conexão do conjunto de réplicas a seguir inclui estes elementos:
A opção
replicaSet
O(s) nome(s) de host da(s) instância(s)
mongod
conforme listado(s) na configuração do conjunto de réplicasUm nome de usuário e senha para impor o controle de acesso:
mongosh "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl" --apiVersion 1 --username myDatabaseUser
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
Se o nome de usuário ou senha incluir os seguintes caracteres, esses caracteres devem ser convertidos usando codificação percentual:
$ : / ? # [ ] @
A seguinte string de conexão de cluster fragmentado inclui os seguintes elementos:
Os hosts
mongos
na string de conexãoUm nome de usuário e uma senha para impor o controle de acesso
mongosh "mongodb://mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin" --apiVersion 1 --username myDatabaseUser
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
Se o nome de usuário ou senha incluir os seguintes caracteres, esses caracteres devem ser convertidos usando codificação percentual:
$ : / ? # [ ] @
A seguinte sequência de conexão autônoma impõe o controle de acesso:
mongosh "mongodb://@mongodb0.example.com:27017/?authSource=admin" --apiVersion 1 --username myDatabaseUser
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
Se o nome de usuário ou senha incluir os seguintes caracteres, esses caracteres devem ser convertidos usando codificação percentual:
$ : / ? # [ ] @
Servidor de banco de dados auto-hospedado em execução localmente
O seguinte se conecta a um servidor de banco de dados em execução localmente na porta padrão:
mongosh "mongodb://localhost" --apiVersion 1 --username myDatabaseUser
mongodb://localhost
mongodb://localhost
mongodb://localhost
mongodb://localhost
mongodb://localhost
mongodb://localhost
mongodb://localhost
mongodb://localhost
mongodb://localhost
mongodb://localhost
mongodb://localhost
mongodb://localhost
mongodb://localhost
Banco de dados auto-hospedado executando localmente admin
O seguinte conecta e registra no banco de dados do admin
como usuário myDatabaseUser
com a senha D1fficultP%40ssw0rd
:
mongosh "mongodb://localhost" --apiVersion 1 --username myDatabaseUser
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
Banco de dados auto-hospedado executando localmente records
O seguinte conecta e registra no banco de dados do records
como usuário myDatabaseUser
com a senha D1fficultP%40ssw0rd
:
mongosh "mongodb://localhost/records" --apiVersion 1 --username myDatabaseUser
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
Soquete de domínio UNIX auto-hospedado
Use uma cadeia de conexão codificada em URL ao conectar-se a um soquete de domínio UNIX.
O seguinte se conecta a um soquete de domínio UNIX com o caminho de arquivo /tmp/mongodb-27017.sock
como usuário myDatabaseUser
com a senha D1fficultP%40ssw0rd
:
mongosh "mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock" --apiVersion 1 --username myDatabaseUser
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
Observação
Nem todos os drivers são compatíveis com soquetes de domínio UNIX. Para obter informações sobre o driver, consulte a documentação Drivers .
Conjunto de réplica auto-hospedada com membros em diferentes máquinas
O seguinte conecta a um conjunto de réplica com dois membros, um no db1.example.net
e outro no db2.example.net
, como usuário myDatabaseUser
com a senha D1fficultP%40ssw0rd
:
Observação
Para um conjunto de réplica, especifique o(s) nome(s) de host da(s) instância(s) do mongod
como listado na configuração do conjunto de réplicas.
mongosh "mongodb://db1.example.net,db2.example.com/?replicaSet=test" --apiVersion 1 --username myDatabaseUser
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
Conjunto de réplica auto-hospedada com membros ligados localhost
O seguinte conecta a uma réplica configurada com três membros executando no localhost
nas portas 27017
, 27018
e 27019
como usuário myDatabaseUser
com a senha D1fficultP%40ssw0rd
:
Observação
Para um conjunto de réplica, especifique o(s) nome(s) de host da(s) instância(s) do mongod
como listado na configuração do conjunto de réplicas.
mongosh "mongodb://localhost,localhost:27018,localhost:27019/?replicaSet=test" --apiVersion 1 --username myDatabaseUser
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost,localhost:27018,localhost:27019/?replicaSet=test
Conjunto de réplica auto-hospedada com distribuição de leitura
O seguinte conecta a um conjunto de réplicas com três membros e distribui leituras para os secundários como usuário myDatabaseUser
com a senha D1fficultP%40ssw0rd
:
Observação
Para um conjunto de réplica, especifique o(s) nome(s) de host da(s) instância(s) do mongod
como listado na configuração do conjunto de réplicas.
mongosh "mongodb://example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary" --apiVersion 1 --username myDatabaseUser
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
Conjunto de réplica auto-hospedada com um alto nível de preocupação de escrita
O seguinte se conecta a um conjunto de réplicas com write concern configurado para aguardar que a replicação seja bem-sucedida na maioria dos membros votantes portadores de dados, com um tempo limite de dois segundos. Ele autentica como usuário myDatabaseUser
com a senha D1fficultP%40ssw0rd
.
Observação
Para um conjunto de réplica, especifique o(s) nome(s) de host da(s) instância(s) do mongod
como listado na configuração do conjunto de réplicas.
mongosh "mongodb://example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000" --apiVersion 1 --username myDatabaseUser
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
Cluster fragmentado auto-hospedado
O seguinte se conecta a um cluster fragmentado com três instâncias mongos
como usuário myDatabaseUser
com a senha D1fficultP%40ssw0rd
:
mongosh "mongodb://router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/" --apiVersion 1 --username myDatabaseUser
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/