Skip to content

Instantly share code, notes, and snippets.

@marinsagovac
Created December 15, 2017 10:49
Show Gist options
  • Save marinsagovac/49245cfe197803f769a1304cc7c21ed8 to your computer and use it in GitHub Desktop.
Save marinsagovac/49245cfe197803f769a1304cc7c21ed8 to your computer and use it in GitHub Desktop.
Ubuntu MongoDB install
### Ubuntu Mongodb ###
### Installing Mongodb ###
sudo apt-get install mongodb-server
sudo service mongodb start
Check log in "/var/log/mongodb/mongodb.log"
Configuration:
/etc/mongodb.conf
Default db path:
dbpath=/var/lib/mongodb
Administration: Check init mongodb + status:
http://localhost:28017/
Listener app: MongoDB listening when on logs show "waiting for connections on port 27017":
This is localhost:27017
-- or change in config "/etc/mongodb.conf" to:
#port = 27017
If daemon fails, on critical when locking database or sometimes when need remove ".lock" file to get an service run:
mongod --repair
### PHP INSTALL ###
Install:
sudo apt-get install php5-mongo
If is automatically installed check extension using phpinfo():
Find "MongoDB Support"
My version: Version 1.4.5
If not found, install MongoDB and add extension:
extension=mongo.so
echo 'extension=mongo.so' >> /etc/php5/apache2/php.ini
Sample of configuration:
php -i |grep "mongo"
/etc/php5/cli/conf.d/20-mongo.ini,
mongo
mongo.allow_empty_keys => 0 => 0
mongo.chunk_size => 262144 => 262144
mongo.cmd => $ => $
mongo.default_host => localhost => localhost
mongo.default_port => 27017 => 27017
mongo.is_master_interval => 15 => 15
mongo.long_as_object => 0 => 0
mongo.native_long => 0 => 0
mongo.ping_interval => 5 => 5
### PREPARE MONGODB ###
-- Mongo DB
Shell
-----
Type "mongo"
Console mongo db:
user@host:~$ mongo
MongoDB shell version: 2.4.9
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
>
show dbs - list all databases
--------
db - display database
--
use test - use "test" database
--------
> use test;
switched to db test
use somedatabase - create "somedatabase" database
> db.getCollection("test") - get Collection
Response: test.test
Example fetch and save data:
---------------------------
> show dbs
local 0.078125GB
test (empty)
> db.getCollection("test")
test.test
> db.test // alternative way as "db.getCollection("test")"
test.test
> db.test.save( { username: "test" } )
> db.test.find() // find all
{ "_id" : ObjectId("56279b3d108bc16d50eb8e6d"), "username" : "test" }
> db.test.remove()
TRUNCATE COLLECTION
> db.lokd_lokacija.stats()
Get status
-- MongoDB in Node.js
sudo npm install mongoskin
var db = require('mongoskin').db('mongodb://127.0.0.1:27017/test');
db.collection('test').find().toArray(function(err, result) {
//if (err) throw err;
//console.log(result);
});
### MORE INFO ###
http://www.w3resource.com/mongodb/introduction-mongodb.php
https://docs.mongodb.org/master/tutorial/getting-started-with-the-mongo-shell/
http://mongoosejs.com/
http://theholmesoffice.com/how-to-create-a-mongodb-database/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment