[PYTHON] Build a Django environment on Raspberry Pi (MySQL)

Build a django environment on your Raspberry Pi. Because I wanted to. The Raspberry Pi used was Raspberry pi 3B +.

When I checked the construction information of the Django environment, it was all old, and since MySQL is not included in Raspberry Pi, I had a hard time setting it with mariadb, so it is a memo.

Version confirmation

$ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 9.11 (stretch)
Release: 9
Codename: stretch
$ uname -a
Linux raspberry 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv71 GNU/Linux

update

$ sudo apt update
$ sudo apt upgrade -y
$ sudo reboot

Python It should have been installed from the beginning, so you can skip it. If the version is low, you may want to update to 3.7.x.

$ sudo apt install python3 python3-dev
$ python3 -V
Python 3.7.3

Django

Install the main unit

$ sudo apt install python3-django
$ pip3 install Django==3.0.6
$ django-admin --version
3.0.6

You can check if it is the latest version with $ pip3 list --outdated.

MySQL(mariadb) MySQL itself cannot be installed, so install mariadb.

Install the main unit

$ sudo apt install mariadb-server
$ mysql --version
mysql  Ver 15.1 Distrib 10.3.22-MariaDB, for debian-linux-gnu (i686) using readline 5.2

Install client

$ sudo apt install libmariadb-dev
$ pip3 install --no-binary :all: mysqlclient
~ 
Successfully installed mysqlclient-1.4.6

Initial setting

Set the password for the root user. Set '********' to any password.

$ sudo -i
# mysql -u root
mysql> use mysql;
mysql> update user set plugin='' where user='root';
mysql> update user set password=password('********') where user='root';
mysql> flush privileges;

You may put together the update statement.

You can check the setting information with select user, password, plugin from user;. vlcsnap-2020-05-27-16h16m34s284.png

Create DB

mysql> create database mydb;

Create and launch a project

Create a Django project

$ django-admin startproject mysite

Change settings

Change the following two points in settings.py.

$ sudo nano mysite/mysite/settings.py

ʻALLOWED_HOSTS = Enter'*' in [] of []`.

settings.py


# ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['*']

Initially, sqlite3 is specified, so comment it out (or delete it) and describe the MySQL settings. Specify the created DB name in NAME. Set '********' to the set password.

settings.py


DATABASES = {
#    'default': {
#        'ENGINE': 'django.db.backends.sqlite3',
#        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#    }
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': '********',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

Check if the settings are correct

$ python3 mysite/manage.py migrate

vlcsnap-2020-05-27-16h15m56s570.png

Start if there is no problem

$ python3 mysite/manage.py runserver

Let's check it with a browser when it starts. You can connect on the local port 8000. http://127.0.0.1:8000

Now that you have a foundation, you can create an app while looking at the reference site.

image.png

Thank you for your hard work.

trouble shooting

Unable to install mysqlclient

If you cannot install due to the following words such as ʻError Code 1`.

OSError: mysql_config not found

Install the following.

$ sudo apt install python3-dev libmariadb-dev

You can't enter MySQL as the root user.

If you forget the root user password, reset the password.

$ mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

Resetting a password

Start MySQL in safe mode

$ sudo -i
# service mysqld stop
# mysqld_safe --skip-grant-tables &

Set password for root user

# mysql -u root
mysql> use mysql;
mysql> update user set plugin='' where user='root';
mysql> update user set password=password('********') where user = 'root';
mysql> flush privileges;

Quit MySQL started in safe mode

# kill -KILL [mysqld_safe PID]
# kill -KILL [mysqld PID]

* How to check the PID of mysqld

$ ps aux | grep mysqld

Start MySQL

# service mysqld start

Reset permissions

mysql> use mysql;
mysql> select * from user;
mysql> truncate table user;
mysql> flush privileges;
mysql> grant all privileges on *.* to root@localhost identified by '********' with grant option;
mysql> flush privileges;

If it does not boot in safe mode

# mysqld_safe --skip-grant-tables &
$ 2018-08-05T15:34:57.6NZ mysqld_safe A mysqld process already exists

If you get angry when you are told that it is already running, stop the process that is already running. Check the PID above and kill it.

When you get angry without a MySQL client

The one who tends to get up when looking at old information.

Error loading MySQLdb module: No module named 'MySQLdb'.
Did you install mysqlclient or MySQL-python?

As a solution in the previous version, it seems to write the following program in $ pip install mysqlclient or setting.py, and if you check the error as it is, only this information will come out, but this solution itself Is old.

settings.py


#This solution is old and should be removed
import pymysql
pymysql.install_as_MySQLdb()

Since django 2.0, pymysql has been deprecated, so if you have it installed, please uninstall it. Currently, mysqlclient is used, so please install it with the following contents.

$ pip3 install --force-reinstall --ignore-installed --no-binary :all: mysqlclient

If you just install it normally ($ pip3 install mysqlclient), django doesn't seem to refer to it and you get the same error.

reference

-Introduction to Django3: Even beginners can create web services in 10 minutes! How to use Python framework Django and PaizaCloud -Be careful of SQL users! How to set a MySQL root password that you do not know unexpectedly -Corrective action for "Access denied for user'root'@'localhost'" encountered when installing MySQL -What to do when mysqld_safe A mysqld process already exists -Install MySQL 8.0 with yum & configuration memo -# 25 Install MySQL on Raspberry Pi

Recommended Posts

Build a Django environment on Raspberry Pi (MySQL)
Build a Python development environment on Raspberry Pi
Build a Tensorflow environment with Raspberry Pi [2020]
Build OpenCV-Python environment on Raspberry Pi B +
Build an Arch Linux environment on Raspberry Pi
Build an OpenCV4 environment on Raspberry Pi using Poetry
Build a Django development environment using pyenv-virtualenv on Mac
How to build a Django (python) environment on docker
Build a Python virtual environment using venv (Django + MySQL ①)
Build a python3 environment on CentOS7
Build wxPython on Ubuntu 20.04 on raspberry pi 4
Build a python environment on MacOS (Catallina)
Build a Python + OpenCV environment on Cloud9
# 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2
Build Python + django + nginx + MySQL environment using docekr
Build a LAMP environment on your local Docker
Build a WardPress environment on AWS with pulumi
Simply build a Python 3 execution environment on Windows
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
Build a python environment with ansible on centos6
Try using a QR code on a Raspberry Pi
pigpio on Raspberry pi
Build a Python environment on Mac (Mountain Lion)
[Python] Build a Django development environment with Docker
Deploy Django apps on Ubuntu + Nginx + MySQL (Build)
Build a Django environment with Vagrant in 5 minutes
Build a Python development environment on your Mac
Build a Django development environment with Doker Toolbox
Build a Kubernetes environment for development on Ubuntu
Quickly build a Python Django environment with IntelliJ
Cython on Raspberry Pi
Connect to MySQL with Python on Raspberry Pi
I made a webAPI! Build environment from Django Rest Framework 1 on EC2
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Build a distributed environment with Raspberry PI series (Part 3: Install and configure dnsmasq)
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
# 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)
Build a local development environment for Laravel6.X on Mac
Build a machine learning Python environment on Mac OS
Build a GVim-based Python development environment on Windows 10 (1) Installation
Build a Python development environment on Mac OS X
Build a Python environment on your Mac using pyenv
Build a Python development environment using pyenv on MacOS
Build a development environment with Poetry Django Docker Pycharm
How to build a Python environment on amazon linux 2
Build a Django environment for Win10 (with virtual space)
Build a machine learning environment natively on Windows 10 (x64)
Build a LAMP environment [CentOS 7]
Build Python environment on Windows
Django environment development on Windows 10
Build python environment on windows
Build a machine learning environment
Build a Python environment offline
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Build a server on Linux and local network with Raspberry Pi NextCloud and desktop sharing
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a web server --3. Use MySQL
Build a python machine learning study environment on macOS sierra
Install LAMP on Amazon Linux 2 and build a WordPress environment.