Django with MySQL DB for multitenancy

471 views
Skip to first unread message

Peter Edache

unread,
Dec 9, 2016, 10:21:44 AM12/9/16
to Django users
Hello guys, we are currently looking for a way to migrate our previous django application to a full multi-tenant  application, currently the way we are handling our client is by giving each client a docker container and using nginx to do the routing, through sub-domain mapping.  But after researching i found, lot of article on multitenant using postgres sql but could not find a better approach for MySQL. Can anyone share a solution with me.
Thanks

Antonis Christofides

unread,
Dec 10, 2016, 10:33:13 AM12/10/16
to django...@googlegroups.com

Hi,

I don't understand what the problem is. You have a setup that works. What exactly do you want? You want to run many distinct instances of the application on the same server? And you want to get rid of docker?

Antonis Christofides
http://djangodeployment.com
On 2016-12-09 17:13, Peter Edache wrote:
Hello guys, we are currently looking for a way to migrate our previous django application to a full multi-tenant  application, currently the way we are handling our client is by giving each client a docker container and using nginx to do the routing, through sub-domain mapping.  But after researching i found, lot of article on multitenant using postgres sql but could not find a better approach for MySQL. Can anyone share a solution with me.
Thanks
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ff7515e9-5de5-46ef-9490-6a8e49c3c289%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Edache

unread,
Dec 16, 2016, 9:53:02 AM12/16/16
to Django users
I basically want to get rid of all the docker containers for each client, and just have a single application with many client (as the tenant) and mysql databases for each tenant. Just like the way django_tenant_schemas is working on postgres sql server. 
Thanks @Antonis Christofides

Antonis Christofides

unread,
Dec 19, 2016, 2:50:19 AM12/19/16
to django...@googlegroups.com

Hi,

I'm not the expert on that, but since no-one else is replying I'll throw my ideas anyway.

One solution that does get rid of the docker containers but doesn't otherwise achieve very much is to continue routing from nginx. For one client nginx can be connecting to the :8001 backend, for another client with :8002, and so on. Now all backends can be running on the same machine and use the same virtualenv and project directory, just different settings. For example, you might have /etc/mydjangoproject/client1/settings.py, /etc/mydjangoproject/client2/settings.py, and likewise have many uwsgi/gunicorn instances, each one of them setting PYTHONPATH to a different one of these directories so that Django reads the settings from there.

The problem with this setup is that it seems like an unnecessary use of resources. You will still be running X Django instances when, in theory, one should be enough. The benefits are that it will be somewhat simpler (since you want be using Docker), and, if you were running many MySQL instances (one in each docker), you also save some resources because you will be running only one MySQL server (with many databases).

Otherwise, to run a single Django instance, you could define many DATABASES and create a database router that selects the appropriate database each time. I believe that this is doable with minimal or no changes to the application code (only in settings and in a file that defines the database router and is imported by settings). But you may also need to take care of media files. In FileField, ImageFields, you need to specify a callable in upload_to.

Regards,

Antonis Christofides
http://djangodeployment.com

Avraham Serour

unread,
Dec 19, 2016, 5:15:41 AM12/19/16
to django-users
You are right, you may do the routing with nginx but will you have many isntances of django this way.

You are also right that you should need to create a database router yourself taking in account whatever is needed to decide which database to use.
You could for example use the domain from the request to decide which database to use, but take into account that the database is not the only thing different from each deploy, the private key for example should be different, but it would also work if it is the same for everyone.

But if you are using caching for example you should add a preffix, there are probably more things to take into account but I'm too hungry right now to think straight.

Good luck
Avraham

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages