Tutorial: Mnesia

2 Getting Started

2.1 Download the Tutorial Project

To help you get started more quickly, we've provided a repo with some helper tools in them. The rest of the tutorial will refer to this project. Here it is:

    $ git clone https://github.com/lfe/mnesia-tutorial.git
    $ cd mnesia-tutorial

Next, let's get all the dependencies downloaded and built:

    $ make compile

Okay, we're ready to go :-) You can start up an mnesia-ready LFE REPL by using a make target we provided in the Makefile for you. You need to pass it the path of the database you want to create or connect to, stored in an environment variable:

    $ DB=/tmp/funky make mnesia-shell

This will dump you into the shell, ready to start dataing:

    Erlang R16B (erts-5.10.1) [source] [smp:8:8] [async-threads:10] ...

    LFE Shell V5.10.1 (abort with ^G)
>

If you did want to execute a manual command analogous to the make target, you could do this, with the assumption that you're using the LFE from your project's deps directory:

    $ ERL_LIBS=./deps/lfe:. ./deps/lfe/bin/lfe -mnesia dir '"/tmp/funky"'

2.1 First Run

You're now in the LFE REPL, so let's create a new database on disk, start it up, and then create a table:

    > (mnesia:create_schema (list (node)))
    ok
    > (mnesia:start)
    ok
    > (mnesia:create_table 'funky '())
    #(atomic ok)
    >

We can take a look at our creation with this command:

    (mnesia:info)

Which should give you something like this:

    ---> Processes holding locks <---
    ---> Processes waiting for locks <---
    ---> Participant transactions <---
    ---> Coordinator transactions <---
    ---> Uncertain transactions <---
    ---> Active tables <---
    funky          : with 0        records occupying 317      words of mem
    schema         : with 2        records occupying 541      words of mem
    ===> System info in version "4.8", debug level = none <===
    opt_disc. Directory "/tmp/funky" is used.
    use fallback at restart = false
    running db nodes   = [nonode@nohost]
    stopped db nodes   = []
    master node tables = []
    remote             = []
    ram_copies         = [funky]
    disc_copies        = [schema]
    disc_only_copies   = []
    [{nonode@nohost,disc_copies}] = [schema]
    [{nonode@nohost,ram_copies}] = [funky]
    3 transactions committed, 0 aborted, 0 restarted, 2 logged to disc
    0 held locks, 0 in queue; 0 local transactions, 0 remote
    0 transactions waits for other nodes: []
    ok

You can quit the REPL now, as we'll restart it in the next section.

Next

We've had a little taste, next we're going to spend some time getting to know Mnesia a little better.