List all MongoDB databases from Linux bash terminal
Can you list all databases in MongoDB from a linux bash terminal? I cannot find the correct command. I am trying to list all so as that I can write a bash script to backup all DB's individually. Is there a better way?
mongodb-3.0 linux
add a comment |
Can you list all databases in MongoDB from a linux bash terminal? I cannot find the correct command. I am trying to list all so as that I can write a bash script to backup all DB's individually. Is there a better way?
mongodb-3.0 linux
See this question. stackoverflow.com/questions/25947929/…
– SqlWorldWide
Oct 12 '17 at 10:53
add a comment |
Can you list all databases in MongoDB from a linux bash terminal? I cannot find the correct command. I am trying to list all so as that I can write a bash script to backup all DB's individually. Is there a better way?
mongodb-3.0 linux
Can you list all databases in MongoDB from a linux bash terminal? I cannot find the correct command. I am trying to list all so as that I can write a bash script to backup all DB's individually. Is there a better way?
mongodb-3.0 linux
mongodb-3.0 linux
asked Oct 12 '17 at 10:32
eekfonkyeekfonky
327
327
See this question. stackoverflow.com/questions/25947929/…
– SqlWorldWide
Oct 12 '17 at 10:53
add a comment |
See this question. stackoverflow.com/questions/25947929/…
– SqlWorldWide
Oct 12 '17 at 10:53
See this question. stackoverflow.com/questions/25947929/…
– SqlWorldWide
Oct 12 '17 at 10:53
See this question. stackoverflow.com/questions/25947929/…
– SqlWorldWide
Oct 12 '17 at 10:53
add a comment |
3 Answers
3
active
oldest
votes
This is a bit tricky, as built-in command show dbs
will show databases with sizes. Plus this command cannot be eval
ed.
However commands can be passed with bash heredoc syntax. And to get rid of sizes, only even values can be displayed.
To skip showing welcome message use --quiet
option.
Resulting script:
#!/bin/bash
# $dbs will contain db names and sizes mixed together
# Use --quiet to skip connection information
dbs=$(mongo --quiet <<EOF
show dbs
quit()
EOF
)
i=0
for db in ${dbs[*]}
do
# Odd values are db names
# Even values are sizes
i=$(($i+1))
# Show db name, ignore size
if (($i % 2)); then
echo "$db"
fi
done
add a comment |
Use --eval
parameter
mongo --eval 'show dbs'
2
mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier
– eekfonky
Oct 12 '17 at 10:43
add a comment |
"show dbs" calculate the databases sizes and that may take some time.
You can pass this command to the mongo shell, then clean the response:
echo "db.getMongo().getDBNames()"|mongo --quiet |tr -d [] | tr , "n"|cut -c3-| tr -d "
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f188305%2flist-all-mongodb-databases-from-linux-bash-terminal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a bit tricky, as built-in command show dbs
will show databases with sizes. Plus this command cannot be eval
ed.
However commands can be passed with bash heredoc syntax. And to get rid of sizes, only even values can be displayed.
To skip showing welcome message use --quiet
option.
Resulting script:
#!/bin/bash
# $dbs will contain db names and sizes mixed together
# Use --quiet to skip connection information
dbs=$(mongo --quiet <<EOF
show dbs
quit()
EOF
)
i=0
for db in ${dbs[*]}
do
# Odd values are db names
# Even values are sizes
i=$(($i+1))
# Show db name, ignore size
if (($i % 2)); then
echo "$db"
fi
done
add a comment |
This is a bit tricky, as built-in command show dbs
will show databases with sizes. Plus this command cannot be eval
ed.
However commands can be passed with bash heredoc syntax. And to get rid of sizes, only even values can be displayed.
To skip showing welcome message use --quiet
option.
Resulting script:
#!/bin/bash
# $dbs will contain db names and sizes mixed together
# Use --quiet to skip connection information
dbs=$(mongo --quiet <<EOF
show dbs
quit()
EOF
)
i=0
for db in ${dbs[*]}
do
# Odd values are db names
# Even values are sizes
i=$(($i+1))
# Show db name, ignore size
if (($i % 2)); then
echo "$db"
fi
done
add a comment |
This is a bit tricky, as built-in command show dbs
will show databases with sizes. Plus this command cannot be eval
ed.
However commands can be passed with bash heredoc syntax. And to get rid of sizes, only even values can be displayed.
To skip showing welcome message use --quiet
option.
Resulting script:
#!/bin/bash
# $dbs will contain db names and sizes mixed together
# Use --quiet to skip connection information
dbs=$(mongo --quiet <<EOF
show dbs
quit()
EOF
)
i=0
for db in ${dbs[*]}
do
# Odd values are db names
# Even values are sizes
i=$(($i+1))
# Show db name, ignore size
if (($i % 2)); then
echo "$db"
fi
done
This is a bit tricky, as built-in command show dbs
will show databases with sizes. Plus this command cannot be eval
ed.
However commands can be passed with bash heredoc syntax. And to get rid of sizes, only even values can be displayed.
To skip showing welcome message use --quiet
option.
Resulting script:
#!/bin/bash
# $dbs will contain db names and sizes mixed together
# Use --quiet to skip connection information
dbs=$(mongo --quiet <<EOF
show dbs
quit()
EOF
)
i=0
for db in ${dbs[*]}
do
# Odd values are db names
# Even values are sizes
i=$(($i+1))
# Show db name, ignore size
if (($i % 2)); then
echo "$db"
fi
done
answered Mar 6 '18 at 9:41
PeterMPeterM
1162
1162
add a comment |
add a comment |
Use --eval
parameter
mongo --eval 'show dbs'
2
mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier
– eekfonky
Oct 12 '17 at 10:43
add a comment |
Use --eval
parameter
mongo --eval 'show dbs'
2
mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier
– eekfonky
Oct 12 '17 at 10:43
add a comment |
Use --eval
parameter
mongo --eval 'show dbs'
Use --eval
parameter
mongo --eval 'show dbs'
answered Oct 12 '17 at 10:39
SQLadminSQLadmin
801426
801426
2
mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier
– eekfonky
Oct 12 '17 at 10:43
add a comment |
2
mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier
– eekfonky
Oct 12 '17 at 10:43
2
2
mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier
– eekfonky
Oct 12 '17 at 10:43
mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier
– eekfonky
Oct 12 '17 at 10:43
add a comment |
"show dbs" calculate the databases sizes and that may take some time.
You can pass this command to the mongo shell, then clean the response:
echo "db.getMongo().getDBNames()"|mongo --quiet |tr -d [] | tr , "n"|cut -c3-| tr -d "
New contributor
add a comment |
"show dbs" calculate the databases sizes and that may take some time.
You can pass this command to the mongo shell, then clean the response:
echo "db.getMongo().getDBNames()"|mongo --quiet |tr -d [] | tr , "n"|cut -c3-| tr -d "
New contributor
add a comment |
"show dbs" calculate the databases sizes and that may take some time.
You can pass this command to the mongo shell, then clean the response:
echo "db.getMongo().getDBNames()"|mongo --quiet |tr -d [] | tr , "n"|cut -c3-| tr -d "
New contributor
"show dbs" calculate the databases sizes and that may take some time.
You can pass this command to the mongo shell, then clean the response:
echo "db.getMongo().getDBNames()"|mongo --quiet |tr -d [] | tr , "n"|cut -c3-| tr -d "
New contributor
New contributor
answered 17 mins ago
Angel angelbar BarrientosAngel angelbar Barrientos
1
1
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f188305%2flist-all-mongodb-databases-from-linux-bash-terminal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
See this question. stackoverflow.com/questions/25947929/…
– SqlWorldWide
Oct 12 '17 at 10:53