MySQL server is one of the most stable relational database system. It has been arround for long time and running behind a lot of website and applications. In this tutorial, i will work you how to setup django to use MySQL server as the data store for the application.
While we will be using MySQL on this tutorial, these steps should be similar to other relational database systems like PostgreSQL.
We will be installing MySQL workbench but it is not required to run Django. MySQL Workbench is only used to manage the database server by providing an easy Graphical User Interface (GUI).
Step 1: Installing MySQL Server, Workbench and some prerequisites
Install MYSQL database, MYSQL workbench, and some prerequisites to connect Django to the database.sudo apt install mysql-server libmysqlclient-dev python-dev mysql-workbench
or use below command if you run an older version of ubuntusudo apt-get install mysql-server libmysqlclient-dev python-dev mysql-workbench
Enter the root password and confirm it when prompted
Step 2: Activating Virtualenv and installing mysqlclient
Navigate into the virtual environment, Activate the virtual environment and install mysqlclient
and occasionally mysql-python
cd venv
(where venv is the name of the virtual environment)source bin/activate
pip install mysqlclient
Step 3: Creating a Database and Username
Open mysql workbench, create a database (schema) and username to be used to connect to the database.
I will use these parameters:
database name = stockmgmt
Username = myusername
Password = myPassword
Step 4: Configuring Django to use the database creating in step 3
In the application settings.py file, configure Django to use MYSQL Database instead of the SQLite file (the default).
in this example we are using the database, username, and password created in the above step:
database name = stockmgmt
Username = myusername
Password = myPassword
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'stockmgmt',
'USER': 'myusername',
'PASSWORD': 'myPassword',
}
}
Step 5: Creating tables in the newly created database
Django allows us to easily create tables in the database with a simple script (python manage.py migrate
)
Do migrate for Django to create the application tables to the newly configured database.
You might want to delete all the files in the migration folder except __init__.py
if you want to are do a clean setup. Watch the video for more explanation../manage.py migrate
./manage.py runserver
hello, i want to ask what function used in django to highlight quantity in a row of a table when it is lower than the reorder level?
Hi Radityo, that video is coming up as number 28 or 29. Before the end of this week inshaAllah
This is the video for reorder level alert: http://www.arbcoms.com/28-how-to-setup-stock-reorder-level-alert/
tried using msql and it failed miserably.
pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
ERROR: MySQL_python-1.2.5-cp27-none-win_amd64.whl is not a supported wheel on this platform.
I mentioned in the video that this might not be required pip install MySQL-python.
Skip it if it gives you error
I finally figured it out and switched databases successfully
That’s great to hear. keep it up
Hello!
Which default password for mysql with root as username?
You set up the password when installing the MySQL server. Do a google search on how to reset if you are not causing issues to any other systems or apps.
when i run python manage.py migrate i am getting this error: “django.db.utils.OperationalError: (1044, “Access denied for user ‘sikutheo’@’%’ to database ‘my_app'””
what should do to fix it
Thx
Your database password seems not the same as what you have in the settings file
I have followed all steps to reset my password but get this error
“[System] [MY-010910] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: Shutdown complete (mysqld 8.0.25) MySQL Community Server – GPL.”
Please help
After changing SQLite to MySql Workbench, am getting an issue to add items in the database. Am getting an error “IntegrityError at /add_items/
(1048, “Column ‘date’ cannot be null”)” on the server-side.
Can you help me with this?