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.
$ 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
$ 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
$ 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.
$ 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
$ sudo apt install libmariadb-dev
$ pip3 install --no-binary :all: mysqlclient
~
Successfully installed mysqlclient-1.4.6
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;
.
mysql> create database mydb;
$ django-admin startproject mysite
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',
}
}
$ python3 mysite/manage.py migrate
$ 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.
Thank you for your hard work.
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
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)
$ sudo -i
# service mysqld stop
# mysqld_safe --skip-grant-tables &
# 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;
# kill -KILL [mysqld_safe PID]
# kill -KILL [mysqld PID]
$ ps aux | grep mysqld
# service mysqld start
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;
# 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.
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.
-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