Erlang is one of the oldest programming language, first appeared in 1986. The language was designed by Joe Armstrong and developed by Ericsson.  OTP (Open Telecom Protocol) comes with an embedded database named Mnesia which is written in Erlang. Currently we at Pathao is working with a Erlang based MQTT broker named EMQ. To match it with our requirements we need to do few customization within the broker through plugin support. While customizing the broker we used Mnesia database to store data. Lets see how Mnesia works. Assume we have a record (Record is similar to object in OOP languages), %% er\_user is the name of record \-record(er\_user, { username :: binary(), % username is a binary field password :: binary() % password is a binary field })**. % Erlang statement ends with dot** **Opening Database Connection** : mnesia:create\_schema(\[node()\]). % create database for this node mnesia:start(). % starts database connection mnesia:create\_table(er\_user, \[{disc\_copies, \[node()\]}, {attributes, record\_info(fields, er\_user)}\]). % creates table for er\_user record **Data insertion** : % insert\_user is a function having two parameter Username, Password insert\_user(Username, Password) -> Insert = **fun**() -> mnesia:write( #er\_user{ username = Username, password = Password }) **end**, {atomic, Results} = mnesia:transaction(Insert). % Transaction writes data to database. Results will return ok if success **Search Data** : Lets say we are finding user by their username find\_user(Username) -> Query = **fun**() -> mnesia:match\_object({er\_user, Username, '\_'}) **end**, {atomic, Results} = mnesia:transaction(Query). mnesia:match\_object matches data in database with given pattern. Like in above example, we are matching Username and Password could be anything as we have put ‘\_’. The first value is the name of record, in above example which is er\_user. The method will return array of records matching the pattern. %%% Result from mnesia:match\_object function \[{er\_user,<<"123">>,<<"Abcd">>}\] If you want to find all users into database, the query will look like, % Ignore username match as well mnesia:match\_object({er\_user, '\_', '\_'}) **Delete Data** : Delete query is similar to find, the specific function is delete() % Delete user by username Query = **fun**() -> mnesia:delete({er\_user, Username}) **end**, {atomic, Results} = mnesia:transaction(Query). For more complex search query use, mnesia:select() Example source code : [https://github.com/s4kibs4mi/er\_mnesia\_test](https://github.com/s4kibs4mi/er_mnesia_test)