How do I exactly change from SQLite to MySQL ?

56 views
Skip to first unread message

Daniel Cîrstea

unread,
Jan 14, 2018, 10:42:43 AM1/14/18
to Django users
 Hello. I am new to Django and I am trying to use it for my bachelor thesis. Thing is, I want to use MySQL insted of SQLite. How do I exactly do it.. I can't find a step-by-step tutorial on the internet.
Also, I want to deploy project later on Heroku. Can I work on SQLite on local machine and then is possible to change to MySQL on Heroku ? In any case, I want to know how to do both, if possible. Thank you.

sum abiut

unread,
Jan 14, 2018, 5:17:03 PM1/14/18
to django...@googlegroups.com
You can install mysql on your local machine and then later migrate your db to Heroku, will be much easy.

On Mon, Jan 15, 2018 at 1:09 AM, Daniel Cîrstea <daniel.c...@gmail.com> wrote:
 Hello. I am new to Django and I am trying to use it for my bachelor thesis. Thing is, I want to use MySQL insted of SQLite. How do I exactly do it.. I can't find a step-by-step tutorial on the internet.
Also, I want to deploy project later on Heroku. Can I work on SQLite on local machine and then is possible to change to MySQL on Heroku ? In any case, I want to know how to do both, if possible. Thank you.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a7decab1-334e-4f04-a6e9-6ad7bc2cd78b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Julio Biason

unread,
Jan 15, 2018, 6:45:13 AM1/15/18
to django...@googlegroups.com
Hi Daniel,

Well, in simple terms, all you need to do is change the DATABASE setting in your settings (here https://www.coderedcorp.com/blog/django-settings-for-multiple-environments/ is an example of way to keep two different configurations, so you can have a production setting pointing to MySQL while keeping your dev setting using SQLite). You may need to create the user and a database for it, but that's more of a MySQL-specific question than Django.

If you want to migrate your data from SQLite to MySQL, you can use `dumpdata` to "export" the data from your dev enviroment and `loaddata` to load it back to the production data. Both are part of the default manage.py: https://docs.djangoproject.com/en/2.0/ref/django-admin/#dumpdata

On Sun, Jan 14, 2018 at 12:09 PM, Daniel Cîrstea <daniel.c...@gmail.com> wrote:
 Hello. I am new to Django and I am trying to use it for my bachelor thesis. Thing is, I want to use MySQL insted of SQLite. How do I exactly do it.. I can't find a step-by-step tutorial on the internet.
Also, I want to deploy project later on Heroku. Can I work on SQLite on local machine and then is possible to change to MySQL on Heroku ? In any case, I want to know how to do both, if possible. Thank you.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a7decab1-334e-4f04-a6e9-6ad7bc2cd78b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554

Mike Dewhirst

unread,
Jan 15, 2018, 7:11:58 AM1/15/18
to django...@googlegroups.com
Since no-one has suggested avoiding MySQL I should say that I have a few
small websites and every time I check the logs they are full of uglies
trying to access phpmyadmin. Every time I see that I'm glad I went for
PostgreSQL.

From my research it seems Postgres is probably the best option for
Django if you want stability with scalability and atomicity.

YMMV

Mike
> send an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/group/django-users>.
> <https://groups.google.com/d/msgid/django-users/a7decab1-334e-4f04-a6e9-6ad7bc2cd78b%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
>
>
> --
> *Julio Biason*,Sofware Engineer
> *AZION*  | Deliver. Accelerate. Protect.
> Office: +55 51 3083 8101 <callto:+555130838101>  |  Mobile: +55 51
> <callto:+5551996209291>_99907 0554_
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto: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/CAEM7gE32Kg9zKb5t4JzBdoshYA3sCk5Q6xuckwPNtSHt1zuYKA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAEM7gE32Kg9zKb5t4JzBdoshYA3sCk5Q6xuckwPNtSHt1zuYKA%40mail.gmail.com?utm_medium=email&utm_source=footer>.

akash....@tiss.edu

unread,
Jan 15, 2018, 7:23:13 AM1/15/18
to django...@googlegroups.com, Mike Dewhirst

Hi, i haven't used other db's but agree with Mike. It's good enough.

Daniel Hepper

unread,
Jan 15, 2018, 7:34:42 AM1/15/18
to django...@googlegroups.com
Hi Daniel,

you can use SQLite locally and MySQL on Heroku.

On Heroku, your database settings must be loaded from an environment variable. The section on Database Configuration in Heroku's guide for setting up Django apps (https://devcenter.heroku.com/articles/django-app-configuration#database-configuration) explains it

Install dj_database_url:

    pip install dj_database_url

Change your settings to look like this:

    # First, define a default for local development
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

    # Then overwrite the defaults if the environment variable DATABASE_URL is set
    import dj_database_url
    db_from_env = dj_database_url.config()
    DATABASES['default'].update(db_from_env)

One thing to keep in mind is that there are some differences between SQLite and MySQL that might lead to bugs. If you want to use MySQL on Heroku, my suggestion would be to use it locally as well.

To use MySQL locally, you have to install MySQL server and then create a MySQL user and a database.

Your settings file would then look something like this:

    # First, define a default for local development
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'myproject',
            'USER': 'myprojectuser',
            'PASSWORD': 'password',
            'HOST': 'localhost',
            'PORT': '',
        }
    }

    # Then overwrite the defaults if the environment variable DATABASE_URL is set
    import dj_database_url
    db_from_env = dj_database_url.config()
    DATABASES['default'].update(db_from_env)

Replace 'myproject', 'myprojectuser' and 'password' with your values. If you are using version control (e.g. Git), you have to decide if it is acceptable to check in your MySQL password.

You also have to install the Python package mysqlclient.

    pip install mysqlclient

Don't forget to add dj_database_url and mysqlclient to your requirements.txt file, otherwise Heroku won't install it.

I would also suggest you consider using PostgreSQL instead of MySQL. While Django is database-agnostic, using PostgreSQL gives you some additional features, see https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/

Hope that helps,
Daniel

On Sun, Jan 14, 2018 at 3:09 PM, Daniel Cîrstea <daniel.c...@gmail.com> wrote:
 Hello. I am new to Django and I am trying to use it for my bachelor thesis. Thing is, I want to use MySQL insted of SQLite. How do I exactly do it.. I can't find a step-by-step tutorial on the internet.
Also, I want to deploy project later on Heroku. Can I work on SQLite on local machine and then is possible to change to MySQL on Heroku ? In any case, I want to know how to do both, if possible. Thank you.

--
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.

graeme

unread,
Jan 15, 2018, 8:49:20 AM1/15/18
to Django users


On Monday, January 15, 2018 at 12:11:58 PM UTC, Mike Dewhirst wrote:
snip 

 From my research it seems Postgres is probably the best option for
Django if you want stability with scalability and atomicity.

In addition Django how comes with postgres specific functionality in django.contrib postgres which makes it a lot more flexible.

Another big advantage is that Postgres has transactional schema changes so if a migration crashes any schema changes that have been done at the time of the crash are rolled back so you do not have to fix the database manually - you just fix and apply the migration.

>     To post to this group, send email to django...@googlegroups.com
>     <mailto:django...@googlegroups.com>.
>     Visit this group at https://groups.google.com/group/django-users
>     <https://groups.google.com/group/django-users>.
>     To view this discussion on the web visit
>     https://groups.google.com/d/msgid/django-users/a7decab1-334e-4f04-a6e9-6ad7bc2cd78b%40googlegroups.com
>     <https://groups.google.com/d/msgid/django-users/a7decab1-334e-4f04-a6e9-6ad7bc2cd78b%40googlegroups.com?utm_medium=email&utm_source=footer>.
>     For more options, visit https://groups.google.com/d/optout
>     <https://groups.google.com/d/optout>.
>
>
>
>
> --
> *Julio Biason*,Sofware Engineer
> *AZION*  | Deliver. Accelerate. Protect.
> Office: +55 51 3083 8101 <callto:+555130838101>  |  Mobile: +55 51
> <callto:+5551996209291>_99907 0554_
> --
> 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
Reply all
Reply to author
Forward
0 new messages