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
yum install mariadb mariadb-server mariadb-devel
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
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.
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.
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
Connect to MySQL with Python3 + Django
Recommended Posts