MongoDB is a persistent data storage solution where users/applications store data. It falls under NoSQL category i.e. instead of traditional table format used by SQL databases it follows JSON notation to store data. It provides more felxibility than SQL based databases.
So be it user data, images, binaries, one can store almost anything is MongoDB.
Mostly MongoDB is installed on the local system in the form of binaries. There are various sources via which users can install MongoDB on their machines. For Ubuntu follow the guide.
Databases in general should be accessed using credentials and not the open access that everyone who knows your IP and port number can get access. Best practise is to add a user and start MongoDB with authentication mode on.
This tutorial is a quick guide to add a user so that the MongoDB can be accessed using credentials.
# first go inside mongodb
$ mongo
# create a user and password along with a role, e.g. username as "superuser" and password as "password"
# However it is adviced to not only use a stronger password but a complex username as well.
$ db.createUser( { user: "superuser", pwd: "password", roles: ["readWriteAnyDatabase" ] } )
Upon successful exectuion of above commands, now makes changes in the MongoDB config files to let only authenticated users use MongoDB. Usually, the config file is present in a standard location in Ubuntu(linux based machines) i.e.
/etc/mongodb.conf
Open this file in editor and find the tag with the name auth and set it to true:
$ vim /etc/mongodb.conf
It should look like the following:
# Turn on/off security. Off is currently the default
auth = true
Now, restart the MongoDB instance:
$ systemctl restart mongodb.service
Also please check if the restart has been successful:
$ systemctl status mongodb.service
If it shows Active: active (running) in the output then proceed to the final step.
Now use the following command from the terminal to make sure that the credentials are working:
$ mongo --port 27017 --host 127.0.0.1 -u "superuser" -p "password" --authenticationDatabase "admin"
If everything goes perfect then following will be displayed:
MongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017/
Implicit session: session { "id" : UUID("e949c438-d914-4067-8762-a28b3bc88d3d") }
MongoDB server version: 3.6.8
happy hacking!
]]>MongoDB is a persistent data storage solution where users/applications store data. It falls under NoSQL category i.e. instead of traditional table format used by SQL databases it follows JSON notation to store data. It provides more felxibility than SQL based databases.
So be it user data, images, binaries, one can store almost anything is MongoDB.
This tutorial is a quick guide to setup a MongoDB instance.
Either one can install the MongoDB binaries locally, which obviously takes a little more time and might get conflict with any existing MongoDB installation, or, one can avoid all that mess by starting MongoDB instance inside a docker container which takes less time and easier to maintain. So, let’s learn how to deploy a MongoDB container in less than 2 min.
It is highly recommended to install docker from the official Docker website.
Also, docker-compose is needed in order to make sure you are doing things neat and earier to debug in case of an error. Install docker-compose.
It is a good practise to create a directory where everything related to the MongoDB you are about to use should be put.
$ mkdir mongo
# create a volume to store the MongoDB data from container
$ mkdir mongodb_data
# now enter the created directory
$ cd mongo
To quickly setup a MongoDB instance inside a container there are a few things you need to take under consideration:
Code to start the MongoDB container should be put inside a docker-compose.yml file.
Assume you want authentication based MongoDB access running on port number 27017 with a volume on the host machine to get the MongoDB data, you can use the following template:
version: '3.8'
volumes:
mongodb_data:
services:
mongodb:
image: mongo
container_name: mongodb
restart: always
volumes:
- 'mongodb_data:/data/db'
environment:
MONGO_INITDB_ROOT_USERNAME: 'superuser'
MONGO_INITDB_ROOT_PASSWORD: 'password'
MONGO_INITDB_DATABASE: mongodb
ports:
- 27017:27017
In the snippet above you can choose replace the environment variables such as username, password, database etc. with the credentials of your choice.
If you want a MongoDB instance without autentication requirements, simple remove the environment variable and 3 following lines under that. Now new code will look like this:
version: '3.8'
volumes:
mongodb_data:
services:
mongodb:
image: mongo
container_name: mongodb
restart: always
volumes:
- 'mongodb_data:/data/db'
ports:
- 27017:27017
To deploy the container containing the MongoDb instance, use the following command:
$ docker-compose up -d --build
Creating network "mongo_default" with the default driver
Creating mongodb ... done
This will spawn a docker container running MongoDB service which can be accessed by the user. To verify if the container is up and running user the following command:
$ docker ps
If you see output something like the follwoing then it means the MongoDB container is up and running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4bb7211b38b1 mongo "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:27017->27017/tcp, :::27017->27017/tcp mongodb
If you don’t see such output then something went wrong while deploying the container. If you see any error then, Well in that case probably google it or ask me, I will be happy to help!
To stop the container use the following command from the same directory where the original docker-compose.yml file is present.
$ docker-compose down -v
Note using the command above will not only stop the MongoDB container but will also remove it from the system so MongoDB specific logs which shows what was going inside the MongoDB container will be inaccessible. BUT It will not remove any data generated by MongoDB from your mongodb_data directory.
If you want to see what is going inside the container then you can use the docker logs feature. To use it following command will help:
# after logs it is the container id, you will get from the `docker ps` command as mentioned above
$ docker logs 4bb7211b38b1
docker logs command can also be used on stopped containers, you just need to get its container id, by using docker ps -a command if the container was not removed manually or using the docker-compose down command as mentioned in the previous section.
Now you can use any application that require a MongoDB instance running on port 27017 like it was installed on your local machine. The experience will be very smooth and easier to manage.
happy hacking!
]]>| Tool Name | version Number | Pros | Cons |
|---|---|---|---|
| Barman | 2.8 May 2019 | demo Text | demo Text |
| Amanda | demo Text | demo Text | |
| Bacula | |||
| Handy Backup | |||
| Iperius Backup | |||
| Backup and Recovery Manager for PostgreSQL | |||
| NetVault Backup | |||
| pgBackRest | |||
| Simpana | |||
| Spectrum Protect | |||
| Manuals | demo Text | demo Text |
In the world of SQL or RDBMS based databases, PostgreSQL offers great services which can be used by an individual user to large scale enterprises. Similar to most of the SQL based databases, setting it up is fairly simple, and it provides users a great experience and flexibility to meet your goals.
To understand the PostgreSQL and how it suffice your use case, setting it up is an essential step. This post demonstrates how to install PostgreSQL on an Ubuntu 16.04 machine along with basic database administration with a running use case(small app).
The most recommended method is to install it via the apt packing manager.
user@foo:~$ sudo apt update
user@foo:~$ sudo apt install postgresql postgresql-contrib
After the above installation, automatically a user and a database named as postgres have been added to the machine and PostgreSQL service, it is an administration account. Now, you can use this administration user account to access the PostgreSQL related functions and roles. Roles are nothing but the different types of functions you can perform in the PostgreSQL database.
There are a few ways to use postgres account to access PostgreSQL shell.
To switch over to the postgres account run the following command:
user@foo:~$ sudo -i -u postgres
postgres@foo:~$
Now you have successfully entered in the postgres account, to access the PostgreSQL process shell run the following command:
postgres@foo:~$ psql
postgres=#
user@foo:~$ sudo -i -u postgres psql
postgres=#
Now you are free to interact with the database management services.
Exit out of the PostgreSQL shell by:
postgres=# \q
user@foo:~$
In PostgreSQL, a role/user is required so that it can interact with the PostgreSQL database service via as 3rd party standalone apps, PHP web application, Python scripts, etc.
A very simple way of creating a user is from postgres account:
postgres@foo:~$ createuser -P -s -e demopy
Enter password for new role:
Enter it again:
SELECT pg_catalog.set_config('search_path', '', false)
CREATE ROLE demopy PASSWORD 'md516a97ee203992bfc3ab84a77b340de86' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;
The above script has automatically made the role/user you created with superuser privileges. Select a password and remember, and it will be feed to your application that interacts with the PostgreSQL database on your behalf.
Now, very quickly, you can create a database to play around with an application. From your postgres account type the following command:
postgres@foo:~$ createdb demopy
Access of the newly created PostgreSQL database demopy can be done in two ways:
user@foo:~$ sudo adduser demopy
Note: the password of the unix user can be different of the password of the role, as during the access of the database through a 3rd party app or plugin the role password will be used not the unix user local account password.
Now to access the database using the following command:
user@foo:~$ sudo -i -u demopy
demopy@foo:~$ psql
demopy=#
pydemo=# is the shell through which the database named demopy can be accessed.
Once you enter the PostgreSQL using the postgres account, by default it selects the postgres database:
postgres@foo:~$ psql
postgres=#
You can switch to any database present inside the PostgreSQL service using the command
\c <name_of_the_database>
postgres=# \c demopy
You are now connected to database "demopy" as user "postgres".
demopy=#
Database admins mostly advise step 1.
In the database, Tables are of a defined structure according to which the data is stored. After selecting a database to work on creating a table:
demopy=# CREATE TABLE test (
id_number varchar(50) PRIMARY KEY,
name varchar(100) NOT NULL,
city varchar(50)
);
After this fill database entries for demonstration purpose
demopy=# INSERT INTO test (id_number, name, city) VALUES ('1', 'abc', 'JFK');
demopy=# INSERT INTO test (name, id_number) VALUES ('xyz', '2');
Run a query against the database to check if the data exists inside the database.
demopy=# SELECT * FROM test;
id_number | name | city
-----------+------+------
1 | abc | JFK
2 | xyz |
(2 rows)
You can perform many queries/operations over the database, more queries can be found here
We can query the database via 3rd party applications, any web plugin or script, etc. In this example, a simple python script will be used to query the database.
#!/usr/bin/python
import pg
conn = pg.DB(host="localhost", user="demopy", passwd="PASSWORD", dbname="demopy")
result = conn.query("SELECT * FROM test")
print result.getresult()
conn.close()
Output:
[('1', 'abc', 'JFK'), ('2', 'xyz', None)]