Mnesia Quickstart

5 01 2010

Basic introduction on Mnesia

Mnesia is a RDBMS. But it belongs to the nosql category of RDBMS. Reason being the query language is not sql but Erlang. That makes it very easy to store and retrieve without having to go through Object Relational Mapping. So we can actually call Mnesia an object relational database.

Why and where would one want to Mnesia?

Erlang in general is used to program highly distributed and fault tolerant systems. Even though it has its roots in the telecommunication industry, it has proven useful in several other sites like ejabbered, Facebook chat etc. Mnesia is just a part of Erlang and is built with Erlang.

Hence it gives you configurable degree of Fault-tolerance (by means of replication).

Another important feature of mnesia is the Dirty read interface. It is possible to read, write and search Mnesia tables without protecting the operation inside a transaction.


The below steps should get you started on mnesia, if you are using it for the first time.

Required Software – Erlang

I use ubuntu as my OS. But that should not make it much different on any other OS.

Now lets start with some code. It is useful to install the table-viewer (erlang-tv).

The goal of this exercise is to create table called person, insert few records and read them.

create a file called Person.hrl

-record(person, {name,      %% atomic, unique key
age,        %% age
married_to, %% name of partner or undefined
children }).%% list of children

create a file called Person.erl

init() ->
insert() ->
T = fun() ->
X = #person{name=john,
read(Name) ->
R = fun() ->

Start command line erlang. Type in the below command from the directory which contains the above two files

erl mnesia dir .

The above command conveys that the current directory will be used to store Mnesia files.

Compile the person.erl



Start mnesia


Create person table.


Insert a record.


Use table viewer to check if the record has been inserted.


This will launch table view application. By default the table viewer shows the ETS tables. To look at the table we just created go to view menu and select Mnesia tables.

Read the record using Mnesia:read()


In my next post I will cover Mnesia queries as List Comprehension.




6 responses

12 04 2010
Peter veentjer

Interesting post. When can we expect the next entry about list comprehension?

12 04 2010

Thanks for the comment. List comprehension will appear soon. Trying to come up with good examples.

4 10 2011

Can i ask, is there a way to switch directories , like to choose the directory containning the 2 files?

5 10 2011

I am not an expert in this area. Maybe you can run below command in a different directory to generate the beam file.
erlc person.erl

Person.erl includes Person.hrl file. You can copy the beam file to any directory you need and use it there. Hope this helps.

19 12 2011
anders pearson

Just a couple notes on things in this post that might trip up newbies:

1) The filenames should be all lowercase. ‘person.erl’ and ‘person.hrl’ not ‘Person.erl’ and ‘Person.hrl’ as written.

2) the source listing for person.erl is missing a “.” at the end of the read function so it gives a compile error as-is.

3) the source listing for person.erl inserts a ‘john’ user but the read example retrieves a ‘klacke’ user. That should be “person:read(john).”

None of these are terribly hard to figure out if you’re familiar with Erlang, but I remember how frustrating it can be to read a tutorial on something new, try to follow along and have things not work.

19 12 2011

Thanks a lot. Appreciate the comments.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: