Dashboard > ejabberd > ... > Using relational databases > Using ejabberd with MySQL native driver
Using ejabberd with MySQL native driver Log In View a printable version of the current page.

Added by Mickaël Rémond , last edited by Jérôme Sautret on Feb 02, 2007  (view change) show comment
Labels: 

ejabberd can be use with MySQL in native mode instead of the ODBC generic mode. This document describes the necessary steps to get started.

Erlang MySQL native driver

You first need to install the native Erlang MySQL driver. It is available in our ejabberd-modules repository. You can retrieve it with the following command:

 svn co https://svn.process-one.net/ejabberd-modules/mysql/trunk mysql

There is some precompiled Erlang beam files but there's not up-to-date : mysql_beam.tar.gz.

You need to put the MySQL .beam files somewhere in your Erlang path (possibly with your ejabberd .beam files):

  • mysql.beam
  • mysql_auth.beam
  • mysql_conn.beam
  • mysql_recv.beam

MySQL database creation

Before starting ejabberd, you need to create an ejabberd database in MySQL. You probably might already have a MySQL but in case you do not have one instance already running, you can go through the following steps:

Download MySQL archive and uncompress it:

> tar zxvf  mysql-max-4.1.16-pc-linux-gnu-i686-glibc23.tar.gz

Go to the MySQL directory:

> cd mysql-max-4.1.16-pc-linux-gnu-i686-glibc23

Configure the database:

> scripts/mysql_install_db

Start MySQL:

> bin/mysqld_safe &

Create a new user 'ejabberd':

> mysql -h localhost -p -u root -S /tmp/mysql.sock
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 4.1.16-max

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> GRANT ALL ON ejabberd.* TO 'ejabberd'@'localhost' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)

Create a new database 'ejabberd':

> mysql -h localhost -p -u ejabberd -S /tmp/mysql.sock
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4 to server version: 4.1.16-max

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> CREATE DATABASE ejabberd;
Query OK, 1 row affected (0.00 sec)

Download MySQL ejabberd schema:

> wget http://svn.process-one.net/ejabberd/trunk/src/odbc/mysql.sql

Import ejabberd database schema into the ejabberd database:

> mysql -D ejabberd -h localhost -p -u ejabberd -S /tmp/mysql.sock < mysql.sql

Check that the database structure has been correctly created:

> echo "show tables;" | mysql -D ejabberd -h localhost -p -u ejabberd -S /tmp/mysql.sock
Tables_in_ejabberd
last
rostergroups
rosterusers
spool
users
vcard
vcard_search

ejabberd configuration

Get the latest ejabberd version:

> svn co http://svn.process-one.net/ejabberd/trunk ejabberd

Go to ejabberd source directory:

> cd ejabberd/src

Compile ejabberd:

> ./configure --enable-odbc && make

Use the example config file as a basis:

> cp ejabberd.cfg.example ejabberd.cfg

Comment the following line in ejabberd.cfg:

{auth_method, internal}.

Add the following lines in ejabberd.cfg:

{auth_method, odbc}.
{odbc_server, {mysql, "localhost", "ejabberd", "ejabberd", "password"}}.

Note: The MySQL configuration description is of the following form:

{mysql, Server, DB, Username, Password}

When you have done that user accounts are stored in MySQL. You can define extra informations that you might want to store in MySQL. Change the module used in ejabberd.cfg to change the persistance from the Mnesia database to MySQL:

  • Change mod_last to mod_last_odbc to store the last seen date in MySQL.
  • Change mod_offline to mod_offline_odbc to store offline messages in MySQL.
  • Change mod_roster to mod_roster_odbc to store contact lists in MySQL.
  • Change mod_vcard to mod_vcard_odbc to store user description in MySQL.
Anonymous

It works ok for me, but ejabberdstores password in plain text, this way. Isn't there a way of enabling digest encryption for an external database?
Thanks,

Pedro 

Reply To This
Anonymous

I just noticed, that ejabberd seems to need a TCP socket. A MySQL 5 installation on Debian sarge has TCP (port 3306) disabled. It is configured to use the unix socket only. While this makes MySQL safe and works with the "mysql" command line client and phpmyadmin, it does not seem to work with ejabberd. My ejabberd finally started using the MySQL tables after I commented out "skip-networking" in /etc/mysql/my.cnf.

hw

Reply To This
Anonymous

Change the port to something else > 1970 and bind the mySql server to 127.0.0.1 - sockets without WAN access.

Reply To This
Anonymous

Note that the schema as supplied assumes a MySQL installation 4.1 or later, with InnoDB.

For MySQL 4.0.18 I needed to remove each USING HASH qualifier, and change SERIAL on line 41 to INTEGER UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE (phew). 

To make it easier to adapt I would suggest removing all TYPE=InnoDB qualifiers (TYPE= is deprecated in favour of ENGINE= by the way) and put a single SET table_type=InnoDB at the top. This can then be commented out if necessary (assuming ejabberd is compatible with other table types...)

toby@smartgames.ca 

Reply To This

We will make the changes. Thank you !

Reply To This

Hello,

Do you think we should make the following changes:
"For MySQL 4.0.18 I needed to remove each USING HASH qualifier, and change SERIAL on line 41 to INTEGER UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE."

Will this work in all versions from your knowledge ?

Reply To This
Anonymous

It looks like you can't connect using MySQL accounts that do not need a password. Adding a password to the MySQL account I'm using on my test server sorted out MySQL connection problems I was having. Plus, its a good idea from a security perspective as well

Reply To This
Anonymous

I have change ejabberd to use mysql and since then every time I try to register I get error

"The address cannot be register. The username is not avalibale."

Any ideas?

Reply To This

There is probably a problem on installation.
I cannot tell more without more details.

Reply To This
Anonymous

Thanks for yout time. 

Its working fine with the original DB, only after switching to mysql I get this error. I follow the steps above, but for some reason can not get it work. could it be mysql version?

I am running the following version:
mysql  Ver 14.7 Distrib 4.1.11, for pc-linux-gnu (i386)

It could be something to do with the Erlang MySQL native driver. I have download it, but not sure what is that I need to do with it.

Looking forward for any help. 

Reply To This
Anonymous

I believe you must create the admin and users account.

Reply To This
Anonymous

Please try to install a new version of Mysql.

Reply To This
Anonymous

Do you have any solution, to store whole JID on users table, e.g. me@jabber-sample.com insted of just me?

 Thanks,

WS.

Reply To This

Hello,

The database setting are designed to have one database per domain.
What do you want to store the full JID ?

It can however be done very easily by changing a small piece of code.

Reply To This
Anonymous

Thanks for replay Mickael, 

I'm asking, because I would like to have all virtual domains in one database - it's eaysier to manage such configuration for me. When I'm adding new virtual domain, i don't want to edit full configuration file, restart ejabberd etc, configuring new database etc.. 

Maybe there is other posibility to deal with this problem. If not, pls write me how to change code

Regards,

Wojciech

Reply To This

The ejabberd vhost has not been designed for hosting company, if that's your problem.
It would require changes to the way vhost are handle to provide an interface which is adequate for hosting companies.

Reply To This
View the rest of this thread  |  4 more comments by 2 persons
Anonymous


Updated by Mickaël Rémond
Jul 18, 2007 18:03

&pHi. I installed ejabberd1.1.3  on a Linux FC6 server . I want ejabberd use mysql instead of its defautl.  Mysql's version 5.0.27 . But when I start ejabberd it prompt me 'An error  occured' . I check the sasl.log and find

=SUPERVISOR REPORT==== 18-Jul-2007::14:06:51 ===
Supervisor: {local, 'ejabberd_odbc_sup_ns25586.ovh.net'}
 Context: start_error
 Reason: mysql_connection_failed
 Offender: [{pid,undefined},
 {name,1},
 {mfa,{ejabberd_odbc,start_link,["ns25586.ovh.net"]}},
 {restart_type,transient},
 {shutdown,brutal_kill},
 {child_type,worker}]

I tried on another Suse10.2 server with mysql5.0.26 and everything is ok. Does Erlang mysql driver support Mysql5.0.27 ? How can I make ejabberd work ? Thanks . Best regards.

Reply To This

Yes, Erlang MySQL driver supports MySQL 5.

Reply To This
Anonymous

Hello,

I have installed ejabber 1.1.2_2 from ports under FreeBSD 6.0, erlang version is r11b3. I configured authentication with AD and it works now. But when I want to store offline messages and some other info in MySQL database, I had done all things from this article - but ejabberd does not work with MySQL. There are no any errors, or any words about odbc or mysql in sasl.log.

What things need I look or try to?

Regards,

Andrew Parnasov 

Reply To This

Hello,
In ejabberd 1.1.x, you cannot use relationnal DB module if you use LDAP as authentication.
This has been improved in the upcoming ejabberd 2.0.

Reply To This
Anonymous

Hello all,

I have install ejabberd 1.1.3 with MySQL 5, with MySQL all is ok, but with ejabberd I could not see the Users and the connecting Users. Moreover Mnesia has not got the off_line table.

The command tv:start(). don t work... 

And moreover the environment variable DISPLAY don t exist now ...

Have you an idea ??? 

Paul 

Reply To This
Anonymous

Hi,

is there a way to encrypt the passwords which are stored in the database?

I am using odbc to get to my mysql server and ejabberd1.1.4 installer 64bit

Thanks.

Florian

Reply To This
Anonymous

Hi,

yes i modified the odbc_queries.erl file to store encrypt passwords in the database.

Marcel

Reply To This
Anonymous

 Would you mind sharing, I'd like that behaviour too and of course I'm not familiar with erlang at all.

Reply To This
Anonymous

I've just tried this and it works great--will solve a number of problems for me.

That said, I'd like to know if there is anyway to make the odbc module authenticate against other tables (besides the external script).  Is it possible to use external authentication and still use odbc for roster, last, etc.?  

Finally, when I was setting all this up, I noticed what I think might be a bug.  I accidentally mistyped

Unknown macro: {auth_method, odbc}
as
Unknown macro: {auth_mehtod, odbc}
.  Instead of throwing a config file parsing error, ejabberd just fell back to internal silently.  This was surprising given that, in my experience, it is normally quite sensitive to syntax errors. 

 Thanks,

Todd 

Reply To This
Anonymous

We have an existing MySQL database table with usernames and encrypted passwords - how can make ejabberd work with it?

Reply To This
Anonymous

program it... either let your app create/delete the ejabberd-users or change ejabberd so, that it will use your tables..

Reply To This
Anonymous

Just one question..this might be a real dumb one ...I have done the steps and restarted ejabberd..now authentication happens through the MySQL database. I assumed that I can not logon any more as admin to the web interface or messenger because the users do not exist in the newly created db.

So I opened phpmyadmin and entered admin and admin@myjaber.mydmonain.net each seperately as a user with password 123456 but it did not work any comments please ?

Reply To This

Creating the admin in MySQL should work.

Reply To This
Anonymous

Just confirming here that when everything is working correctly, creating accounts should be no problem:

I was able to create an account manually via MySQL just by adding a row to the users table with a username and password.

I was also able to register a new account via my IM client, same way as usual.

If this isn't working, you are probably misconfigured. The gotcha that got me at first was the fact that in the ejabberd.cfg file the odbc_server line for the MSSQL server is uncommented by defaut; you of course have to comment that if you plan to use MySQL.

Reply To This
Anonymous

Dear Sir,

I follow all the steps above ( The only difference is: I used ejabberd installer so I didn't "./configure --enable-odbc && make" )

And, I believe that my ejabberd configuration, and MySQL work correctly because :
I can start ejabberd, create a new account via IM client, and see the new user record "test" on "ejabberd.users" table.
However, I cannot login to ejabberd server from either my IM client or ejabberd web admin 

( I have both "admin" and "test" usear on "ejabberd.users" table now ).

The feedback from IM client is "Not Authorized".
======== info ========
ejabberd 2.0.0
MySQL 5.0.45
try on: WinXP and RedHat
try on: localhost and DNS
====================

I have read and tried all the discussions here, but cannot make ejabber serve clients yet.
Please give me some hint. Let me know if you need more information. THANK YOU.
- FAYE -

Reply To This
Anonymous

I too have a problem getting the mysql-options to run properly.

When trying to add the admin, I will get an error telling me that the User already exists:

>ejabberdctl register admin myhostname.com password
User "admin@myhostname.com" already registered at node ejabberd@server

I had a look at sasl.log and found:

=CRASH REPORT==== 21-Feb-2008::17:41:02 ===
  crasher:
    pid: <0.233.0>
    registered_name: []
    error_info: {{case_clause,{mysql,"localhost",
                                      "ejabberd",
                                      "ejabberd",
                                      "password"}},
                  [{ejabberd_odbc,init,1},
                   {gen_server,init_it,6},
                   {proc_lib,init_p,5}]}
    initial_call: {gen,init_it,
                      [gen_server,
                       <0.232.0>,
                       <0.232.0>,
                       ejabberd_odbc,
                       ["myhostname.com"],
                       []]}
    ancestors: ['ejabberd_odbc_sup_myhostname.com',<0.36.0>]
    messages: []
    links: [<0.232.0>]
    dictionary: []
    trap_exit: false
    status: running
    heap_size: 377
    stack_size: 21
    reductions: 103
  neighbours:

=SUPERVISOR REPORT==== 21-Feb-2008::17:41:02 ===
     Supervisor: {local,
                                              'ejabberd_odbc_sup_myhostname.com'}
     Context:    start_error
     Reason:     {{case_clause,{mysql,"localhost",
                                      "ejabberd",
                                      "ejabberd",
                                      "password"}},
                  [{ejabberd_odbc,init,1},
                   {gen_server,init_it,6},
                   {proc_lib,init_p,5}]}
     Offender:   [{pid,undefined},
                  {name,1},
                  {mfa,{ejabberd_odbc,start_link,["myhostname.com"]}},
                  {restart_type,transient},
                  {shutdown,brutal_kill},
                  {child_type,worker}]

Log in to my mysql-Server using the supplied username/password works, "ejabberctl registered-users" does not show any user being registered.

My ejabberd.cfg follows:

{acl, local, {user_regexp, ""}}.
{access, configure, [{allow, admin}]}.
{access, register, [{allow, all}]}.
{welcome_message,
 {"Welcome!",
  "Welcome to Debian Jabber Service.  "
  "For information about Jabber visit http://jabber.org"}}.
{registration_watchers, ["webmaster@myhostname.com"]}.
{access, announce, [{allow, admin}]}.
{access, c2s, [{deny, blocked},
               {allow, all}]}.
{shaper, normal, {maxrate, 1000}}.
{shaper, fast, {maxrate, 50000}}.
{access, c2s_shaper, [{none, admin},
                      {normal, all}]}.
{access, s2s_shaper, [{fast, all}]}.
{access, muc_admin, [{allow, admin}]}.
{access, muc, [{allow, all}]}.
{access, local, [{allow, local}]}.
{auth_method, odbc}.
{odbc_server, {mysql, "localhost", "ejabberd", "ejabberd", "password"}}.
{hosts, ["myhostname.com"]}.
{language, "en"}.
{listen,
 [{5222, ejabberd_c2s,     [{access, c2s},
                            starttls, {certfile, "/etc/ssl/certs/ejabberd.pem"},
                            {shaper, c2s_shaper}]},
  {5223, ejabberd_c2s,     [{access, c2s},
                            tls, {certfile, "/etc/ssl/certs/ejabberd.pem"},
                            {shaper, c2s_shaper}]},
  {5269, ejabberd_s2s_in,  [{shaper, s2s_shaper}]},
  {5280, ejabberd_http,    [http_poll, web_admin]}
 ]}.
{outgoing_s2s_port, 5269}.
{modules,
 [
  {mod_announce,   [{access, announce}]},
  {mod_register,   [{access, register}]},
  {mod_roster_odbc,     []},
  {mod_shared_roster, []},
  {mod_privacy,    []},
  {mod_configure,  []},
  {mod_configure2, []},
  {mod_disco,      [{extra_domains, ["users.jabber.org"]}]},
  {mod_stats,      []},
  {mod_vcard_odbc,      []},
  {mod_offline_odbc,    []},
  {mod_echo,       []},
  {mod_private,    []},
  {mod_irc,        []},
  {mod_muc,        [{access, muc},
                    {access_create, muc},
                    {access_admin, muc_admin}]},
  {mod_pubsub,     []},
  {mod_time,       []},
  {mod_last_odbc,       []},
  {mod_version,    []}
 ]}.

Does anyone hva an idea what could be wrong?

Best regards,

Tobias

Reply To This
Anonymous

Tobias,

   The same problem was driving me crazy for a while.  For me this problem was solved by following the first item on the list above: copy the mysql beam files to the ejabberd folder with the rest of the beam files (I had assumed that aptitude had included the files for me - oops).   After that just reboot ejabbered and you should be good.

 
-Chris

Reply To This
Anonymous

Chris,
I'm solving the same problem, but your advice didn't help me

-Victor

Reply To This
Anonymous


Updated by Mickaël Rémond
Feb 27, 2008 12:40

we have configured module of ejabber in ejabbard.cfg file of ejabbard-1.1.4 as follows

[snip]

in last table of seconds field is changing in our mysql database. how ever in that same last table state field is not changing when ever we changed presence info of user.

can any one help on this

Thanks in advance.

Satya. 

Reply To This

mod_last is not intended to store the current status (live update), but only the status of the person when he is leaving.

Reply To This