373๐
MySQL support is simple to add. In your DATABASES
dictionary, you will have an entry like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
You also have the option of utilizing MySQL option files, as of Django 1.7. You can accomplish this by setting your DATABASES
array like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/path/to/my.cnf',
},
}
}
You also need to create the /path/to/my.cnf
file with similar settings from above
[client]
database = DB_NAME
host = localhost
user = DB_USER
password = DB_PASSWORD
default-character-set = utf8
With this new method of connecting in Django 1.7, it is important to know the order connections are established:
1. OPTIONS.
2. NAME, USER, PASSWORD, HOST, PORT
3. MySQL option files.
In other words, if you set the name of the database in OPTIONS, this will take precedence over NAME, which would override anything in a MySQL option file.
If you are just testing your application on your local machine, you can use
python manage.py runserver
Adding the ip:port
argument allows machines other than your own to access your development application. Once you are ready to deploy your application, I recommend taking a look at the chapter on Deploying Django on the djangobook
Mysql default character set is often not utf-8, therefore make sure to create your database using this sql:
CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_bin
If you are using Oracleโs MySQL connector your ENGINE
line should look like this:
'ENGINE': 'mysql.connector.django',
Note that you will first need to install mysql on your OS.
brew install mysql (MacOS)
Also, the mysql client package has changed for python 3 (MySQL-Client
works only for python 2)
pip3 install mysqlclient
30๐
To the very first please run the below commands to install python dependencies otherwise python runserver command will throw error.
sudo apt-get install libmysqlclient-dev
sudo pip install MySQL-python
Then configure the settings.py file as defined by #Andy and at the last execute :
python manage.py runserver
Have fun..!!
- [Django]-Django return file over HttpResponse โ file is not served correctly
- [Django]-Detect mobile, tablet or Desktop on Django
- [Django]-Best practices for adding .gitignore file for Python projects?
25๐
If you are using python3.x then Run below command
pip install mysqlclient
Then change setting.py like
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB',
'USER': 'username',
'PASSWORD': 'passwd',
}
}
- [Django]-How do I match the question mark character in a Django URL?
- [Django]-Strings won't be translated in Django using format function available in Python 2.7
- [Django]-Django: For Loop to Iterate Form Fields
17๐
As all said above, you can easily install xampp first from https://www.apachefriends.org/download.html
Then follow the instructions as:
- Install and run xampp from http://www.unixmen.com/install-xampp-stack-ubuntu-14-04/, then start Apache Web Server and MySQL Database from the GUI.
- You can configure your web server as you want but by default web server is at
http://localhost:80
and database atport 3306
, and PhpMyadmin athttp://localhost/phpmyadmin/
- From here you can see your databases and access them using very friendly GUI.
- Create any database which you want to use on your Django Project.
-
Edit your
settings.py
file like:DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'DB_NAME', 'HOST': '127.0.0.1', 'PORT': '3306', 'USER': 'root', 'PASSWORD': '', }}
-
Install the following packages in the virtualenv (if youโre using django on virtualenv, which is more preferred):
sudo apt-get install libmysqlclient-dev
pip install MySQL-python
-
Thatโs it!! you have configured Django with MySQL in a very easy way.
-
Now run your Django project:
python manage.py migrate
python manage.py runserver
- [Django]-How to select a record and update it, with a single queryset in Django?
- [Django]-Reload django object from database
- [Django]-Django Cannot set values on a ManyToManyField which specifies an intermediary model. Use Manager instead
7๐
Actually, there are many issues with different environments, python versions, so on. You might also need to install python dev files, so to โbrute-forceโ the installation I would run all of these:
sudo apt-get install python-dev python3-dev
sudo apt-get install libmysqlclient-dev
pip install MySQL-python
pip install pymysql
pip install mysqlclient
You should be good to go with the accepted answer. And can remove the unnecessary packages if thatโs important to you.
- [Django]-How to set the timezone in Django
- [Django]-How can I get the full/absolute URL (with domain) in Django?
- [Django]-You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application
7๐
Run these commands
sudo apt-get install python-dev python3-dev
sudo apt-get install libmysqlclient-dev
pip install MySQL-python
pip install pymysql
pip install mysqlclient
Then configure settings.py like
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': '123456',
}
}
Enjoy mysql connection
- [Django]-Django model CharField: max_length does not work?
- [Django]-Django model constraint for related objects
- [Django]-What's the best solution for OpenID with Django?
5๐
- Install
mysqlclient
sudo pip3 install mysqlclient
if you get error:
Command โpython setup.py egg_infoโ failed with error code 1 in
/tmp/pip-install-dbljg4tx/mysqlclient/
then:
1. sudo apt install libmysqlclient-dev python-mysqldb
2. sudo pip3 install mysqlclient
-
Modify settings.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'website', 'USER': 'root', 'PASSWORD': '', 'HOST': '127.0.0.1', 'PORT': '3306', 'OPTION': {'init_command':"SET sql_mode='STRICT_TRANS_TABLE',"}, } }
- [Django]-When saving, how can you check if a field has changed?
- [Django]-How to get Request.User in Django-Rest-Framework serializer?
- [Django]-Django order_by query set, ascending and descending
4๐
Andyโs answer helps but if you have concern on exposing your database password in your django setting, I suggest to follow django official configuration on mysql connection: https://docs.djangoproject.com/en/1.7/ref/databases/
Quoted here as:
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/path/to/my.cnf',
},
}
}
# my.cnf
[client]
database = NAME
user = USER
password = PASSWORD
default-character-set = utf8
To replace โHOSTโ: โ127.0.0.1โ in setting, simply add it in my.cnf:
# my.cnf
[client]
database = NAME
host = HOST NAME or IP
user = USER
password = PASSWORD
default-character-set = utf8
Another OPTION that is useful, is to set your storage engine for django, you might want it in your setting.py:
'OPTIONS': {
'init_command': 'SET storage_engine=INNODB',
}
- [Django]-How to delete a record in Django models?
- [Django]-How to test auto_now_add in django
- [Django]-Django โ Rotating File Handler stuck when file is equal to maxBytes
4๐
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER': 'root',
'PASSWORD': '*****',
'HOST': '***.***.***.***',
'PORT': '3306',
'OPTIONS': {
'autocommit': True,
},
}
}
then:
python manage.py migrate
if success will generate theses tables:
auth_group
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_content_type
django_migrations
django_session
and u will can use mysql.
this is a showcase example ,test on Django version 1.11.5:
Django-pool-showcase
- [Django]-Django-reversion and related model
- [Django]-Duplicate column name
- [Django]-Stack trace from manage.py runserver not appearing
1๐
Follow the given steps in order to setup it up to use MySQL database:
1) Install MySQL Database Connector :
sudo apt-get install libmysqlclient-dev
2) Install the mysqlclient library :
pip install mysqlclient
3) Install MySQL server, with the following command :
sudo apt-get install mysql-server
4) Create the Database :
i) Verify that the MySQL service is running:
systemctl status mysql.service
ii) Log in with your MySQL credentials using the following command where -u is the flag for declaring your username and -p is the flag that tells MySQL that this user requires a password :
mysql -u db_user -p
iii) CREATE DATABASE db_name;
iv) Exit MySQL server, press CTRL + D.
5) Add the MySQL Database Connection to your Application:
i) Navigate to the settings.py file and replace the current DATABASES lines with the following:
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/etc/mysql/my.cnf',
},
}
}
...
ii) Next, letโs edit the config file so that it has your MySQL credentials. Use vi as sudo to edit the file and add the following information:
sudo vi /etc/mysql/my.cnf
database = db_name
user = db_user
password = db_password
default-character-set = utf8
6) Once the file has been edited, we need to restart MySQL for the changes to take effect :
systemctl daemon-reload
systemctl restart mysql
7) Test MySQL Connection to Application:
python manage.py runserver your-server-ip:8000
- [Django]-Django model blank=False does not work?
- [Django]-How do I remove Label text in Django generated form?
- [Django]-Django: Arbitrary number of unnamed urls.py parameters
1๐
This is a considerably old question but if anyone is working on any latest versions of python and django you can follow the following steps
Note - Versions
Python version - 3.9.5
Django version - 3.2.4
MySQL server version - 5.7
After installing django, run the following command
pip install mysqlclient
In my IDE if I do pip list
this is the list of Packages and Versions
Package Version
----------- -------
asgiref 3.3.4
Django 3.2.4
mysqlclient 2.0.3
pip 21.1.2
pytz 2021.1
setuptools 57.0.0
sqlparse 0.4.1
Now, make sure you already have created the Database schema you are going to use.
In the settings.py
file of your django project under DATABASES change
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
to
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
You should be able to run both
python manage.py makemigrations
and
python manage.py migrate
- [Django]-How to merge consecutive database migrations in django 1.9+?
- [Django]-Django manage.py runserver invalid syntax
- [Django]-Django 2.0 โ Not a valid view function or pattern name (Customizing Auth views)
0๐
You must create a MySQL database first. Then go to settings.py
file and edit the 'DATABASES'
dictionary with your MySQL credentials:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'YOUR_DATABASE_NAME',
'USER': 'YOUR_MYSQL_USER',
'PASSWORD': 'YOUR_MYSQL_PASS',
'HOST': 'localhost', # Or an IP that your DB is hosted on
'PORT': '3306',
}
}
Here is a complete installation guide for setting up Django to use MySQL on a virtualenv:
http://codex.themedelta.com/how-to-install-django-with-mysql-in-a-virtualenv-on-linux/
- [Django]-How to loop over form field choices and display associated model instance fields
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
- [Django]-What are the limitations of Django's ORM?
-1๐
python3 -m pip install mysql-connector
pip install mysqlclient
These commands helpful to settingup the mysql db in django without errors
- [Django]-Django models.py Circular Foreign Key
- [Django]-How to get the domain name of my site within a Django template?
- [Django]-How to access request body when using Django Rest Framework and avoid getting RawPostDataException