Django Getting Started: 4_MySQL Integration

table of contents

Django Getting Started: 1_Environment Building Django Getting Started: 2_Project Creation Beginning with Django: 3_Apache integration Beginning with Django: 4_MySQL integration

Last time, I was ready to publish a project created with Django in collaboration with Apache. We will continue to build the environment without writing the actual program. This time, change the DB settings.

Django uses SQLite by default. While it has the advantage of being able to start a project very easily (no settings required), When it comes to migrating to MySQL later, it will take more time and effort. Let's do the painful things first.

What I was addicted to when migrating Django's database from sqlite3 to MySQL

Especially since I plan to do text analysis related, I read somewhere that SQLite is not suitable for handling a large amount of data, so I decided to change the setting. Also, I was used to MySQL.

I couldn't find a good article about the difference between SQLite and MySQL, so I would appreciate it if you could introduce a good article or book. It's light, but here's the difference. List of database management systems that can be used on rental servers

environment

MariaDB installation

yum install mariadb mariadb-server mariadb-devel

Edit configuration file

Open the mysql configuration file. When handling Japanese, specify the character code.

vi /etc/my.cnf

Add the following under [mysqld]

character-set-server = utf8
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

In addition, the whole postscript below

[client]
default-character-set = utf8

Let's reboot for the settings to take effect.

systemctl start mariadb
systemctl enable mariadb

Initial setting

I will be asked various questions, so I will answer.

mysql_secure_installation
Enter current password for root (enter for none):  #The default setting is blank. Just press enter
Set root password? [Y/n] y
New password: #Any password
Re-enter new password:: #Password confirmation

All subsequent questions are OK with "y".

Run mysql with a password.

$ mysql -u root -p
Enter password: #Enter the password you set earlier
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 789
Server version: 5.5.47-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

Now let's create a database for Django.

MariaDB [(none)]> create database xxx;
MariaDB [(none)]> exit;

This completes the settings on the DB side. After that, let Django recognize this DB.

Django settings

Open the configuration file.

vi settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'xxx',  #DB name created earlier
        'USER': 'root',  #mysql username (root is NG for security, change accordingly)
        'PASSWORD': 'pass',  #mysql password
        'HOST': '',  #Host IP
        'PORT': '',  #port
    }
}

...

TIME_ZONE = 'Asia/Tokyo'

HOST and PORT can be empty if you are using the default port in the localhost DB.

Add module

pip install PyMySQL

Instructs to use the module added to manage.py.

vi manage.py 
import pymysql
pymysql.install_as_MySQLdb()

Let's create and run a migration.

python manage.py makemigrations
python manage.py migrate

reference

Connect to MySQL with Python3 + Django

Recommended Posts

Django Getting Started: 4_MySQL Integration
Django Getting Started: 3_Apache integration
Getting Started with Django 1
Getting Started with Django 2
Django Getting Started Part 3 about Python3 & MySQL Connector
Django Getting Started: 2_ Project Creation
Django Getting Started: 1_Environment Building
Getting Started with Python Django (1)
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Getting Started with Django with PyCharm
Getting Started with Python Django (5)
Django + MySQL settings
Grails getting started
Django Getting Started Part 2 with eclipse Plugin (PyDev)
Getting started with Android!
Getting Started with Golang 2
Getting started with apache2
Getting Started with Golang 1
Getting Started with Python
Getting Started with Optimization
Getting Started with Golang 3
Getting Started with Numpy
Getting started with Spark
Getting Started with Python
Use MySQL with Django
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Getting Started with Python Functions
Getting Started with Tkinter 2: Buttons
Get started with Django! ~ Tutorial ⑤ ~
Getting Started with Go Assembly
[Linux] [Initial Settings] Getting Started
Getting Started with PKI with Golang ―― 4
Get started with Django! ~ Tutorial ④ ~
Getting started and using sample class-based generic views in Django
Get started with Django! ~ Tutorial ⑥ ~
Python3 | Getting Started with numpy
Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Getting Started with Python responder v2
Getting Started with Git (1) History Storage
Getting started with Sphinx. Generate docstring with Sphinx
Getting Started with Python Web Applications
Getting Started with Python for PHPer-Classes
Getting started on Docker Apache Hadoop
Getting Started with Sparse Matrix with scipy.sparse
Getting Started with Julia for Pythonista
Getting Started with Python Basics of Python
How to get started with Django
Getting Started with Cisco Spark REST-API
Getting started with USD on Windows
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Use Unicode 6.0 emoji with django / MySQL
Getting Started with CPU Steal Time