mongo check verison
db.version()
create database and collection
use salesupdatedb
db.createCollection("myCollection")
create user for single database
db.createUser({
user: "saleupdate",
pwd: "!XwXmQL4uQ!",
roles: [
{role:"userAdmin", db: "salesupdatedb"}
]
})
db.system.users.find({"user":"saleupdate"})
update User, roles
db.updateUser( "saleupdate",
{
roles : [
{ role : "readWrite", db : "salesupdatedb" }
]
}
)
# storage path
/var/lib/mongodb
Inicia el servidor mongo
mongod
Inicia un shell de mongodb con el comando
mongod
Una vez ya iniciado el shell de mongodb ya podemos usar el comando mongodump
mongodump
backup de una base de datos
mongodump --db=myDatabase --authenticationDatabase admin -uUser -pPassword
restaurar una base de datps
mongorestore --db salesreportdb salesreportdb/
cd /etc/apt/sources.list.d/
- Import the public key used by the package management system. From a terminal, issue the following command to import the MongoDB public GPG Key from https://www.mongodb.org/static/pgp/server-4.0.asc
wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -
The operation should respond with an OK.
- Create a list file for MongoDB.
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
- Reload local package database.
$ sudo apt-get update
- Install the MongoDB packages
$ sudo apt-get install -y mongodb-org
- Open mongo shell
$ mongo
- Star/restart/stoped service
$ sudo service mongod restart
$ sudo service mongod stop
$ sudo service mongod start
sudo service mongod status
or
sudo systemctl stop mongod.service
sudo systemctl status mongod.service
- at ubuntu 16.04
sudo systemctl start mongodb
- Step 1 - Importing the Public Key GPG keys of the software distributor are required by the Ubuntu package manager apt (Advanced Package Tool) to ensure the consistency and authenticity of the package. Execute this command to import MongoDB keys to your server.
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 68818C72E52529D4
- Step 2 - Create source list file MongoDB in /etc/apt/sources.list.d/ with this command:
$ sudo echo "deb http://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
- Step 3 - Update the repository and install MongoDB
$ sudo apt-get update && sudo apt-get install -y mongodb-org
- By default, MongoDB is listening on 127.0.0.1:27017 only:
$ sudo netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1061/mongod
-
MongoDB configuration file
/etc/mongod.conf
-
Switch to the database admin
> use admin
- Check the databases using the following command:
> show dbs
- Check your currently selected database, use the command db:
> db
- Create database
> use markups
ubuntu@ubuntu-xenial:~$ mongo
MongoDB shell version v4.0.21
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2020-11-04T16:09:57.916+0000 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:356:17
@(connect):2:6
exception: connect failed
Solution: Vamos a crear un archivo de unidad para administrar el servicio de MongoDB. Crearemos un archivo de configuración llamado mongodb.service en el directorio /etc/systemd/system utilizando nano o su editor de texto favorito.
sudo nano /etc/systemd/system/mongodb.service
Pega el siguiente contenido
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
Iniciar el servicio
sudo systemctl start mongodb
https://www.digitalocean.com/community/tutorials/como-instalar-mongodb-en-ubuntu-16-04-es
- By default, MongoDB is listening on
127.0.0.1:27017
- Open MongoDB configuration file
/etc/mongod.conf
and change bindIp by adding required LAN interfaces or configure it to bind to all interfaces, for example:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all interfaces
- Restart
mongod
to apply modifications:
sudo service mongod restart
or
sudo systemctl start mongodb
- Now mongod is listening on configured interfaces and can be accessible remotely:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 2392/mongod
- In v4.0.5 the config file is yaml so you need to wrap the list in square brackets:
# network interfaces
net:
port: 27017
bindIp: [0.0.0.0, ::]
- src: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/#install-community-ubuntu-pkg
- src: https://www.howtoforge.com/tutorial/install-mongodb-on-ubuntu-14.04/
- src: https://docs.mongodb.com/manual/tutorial/query-documents/
~/mongo$ mongodump --collection=agent --db=agents
~/mongo$ mongorestore dump/
cd /var/lib/mongo/
user mydatabase
db.bill.copyTo("bill_02MAY25_bak")