Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
db:mongodb [2014/05/19 14:34] alfred [Getting started] |
db:mongodb [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 9: | Línea 9: | ||
| $ sudo apt-get install mongodb-org | $ sudo apt-get install mongodb-org | ||
| </code> | </code> | ||
| + | |||
| + | Si en /etc/apt/sources.list.d aparece el fichero ''mongod'' sin extensión, entonces tendrás que renombrarlo a ''mongod.list''. \\ | ||
| + | Probablemente tengas que lanzar ''sudo apt-get install mongodb-server'' | ||
| === Run Mongo === | === Run Mongo === | ||
| <code> | <code> | ||
| $ sudo start mongod | $ sudo start mongod | ||
| + | </code> | ||
| + | ... or... | ||
| + | <code> | ||
| + | $ sudo service mongod start | ||
| </code> | </code> | ||
| === Stop Mongo === | === Stop Mongo === | ||
| Línea 22: | Línea 29: | ||
| </code> | </code> | ||
| + | |||
| + | === Mongo principles === | ||
| + | A Mongo server contain multiple data bases. Data bases are collection containers and a collection is a documents container. Documents are equivalents to records. \\ | ||
| + | Documents inside a collection can have different fields, they are stored as BSON objects and accessed as JSON objects as well. \\ | ||
| + | A namespace in Mongo is the concatenation of database and collection names. For an example ''acme.users'' namespace, ''acme'' is the database name and ''users'' is the collection name. We could also have a collection called ''users.history'', then the namespace would be ''acme.users.history''. | ||
| ==== Operations ==== | ==== Operations ==== | ||
| + | By default you access to the ''test'' database. | ||
| + | * You can list your current database with ''db'' command. | ||
| + | * List your databases with ''show dbs'' command. | ||
| + | * Create and switch to a new (newdb) data base with ''use newdb'' command. | ||
| + | * ''help'' to display help. | ||
| + | * ''show collections'' lists available collections. | ||
| + | ==== In a nutshell... ==== | ||
| + | === ... Insert data === | ||
| + | <code> | ||
| + | j = { name : "mongo" } | ||
| + | k = { x : 3 } | ||
| + | db.testData.insert( j ) | ||
| + | db.testData.insert( k ) | ||
| + | </code> | ||
| + | |||
| + | === ... List inserted data === | ||
| + | <code> | ||
| + | db.testData.find() | ||
| + | </code> | ||
| + | ==== Configuration ==== | ||
| + | === Allow external connections === | ||
| + | You should edit ''/etc/mongodb.conf'' file and change the bind_ip and port values. These would be the values: | ||
| + | <code> | ||
| + | bind_ip = 0.0.0.0 | ||
| + | port = 27017 | ||
| + | </code> | ||
| + | |||
| + | ===== Notes ===== | ||
| + | * Logs are found in ''/var/log/mongodb'' | ||
| + | * ''Robomongo'' is a good ubuntu GUI client for MongoDB. | ||
| + | |||
| + | ==== Recover MongoDB after crash ==== | ||
| + | When the system crashes MongoDB remains locked (error is shown in logs as //mongodb exception in initandlisten old lock file//). You chan do two things to unlock it: | ||
| + | * If this is a development machine, you can remove the lock file manually. It's in ''/var/lib/mongodb/mongod.lock'' or it could be named ''mongo.lock''. | ||
| + | * The safer route is to follow [[http://docs.mongodb.org/manual/tutorial/recover-data-following-unexpected-shutdown/|MongoDB's Durability and Repair guide]]. In summary, for a machine with the above configuration, you should execute the following commands: | ||
| + | <code> | ||
| + | $ sudo -u mongodb mongod --repair --dbpath /var/lib/mongodb/ | ||
| + | $ sudo service mongodb start | ||
| + | </code> | ||