Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I check database connection to mysql in django

how can I do it?

I thought, I can read something from database, but it looks too much, is there something like?:

settings.DATABASES['default'].check_connection() 
like image 433
doniyor Avatar asked Aug 19 '15 14:08

doniyor


People also ask

How do I test MySQL connection?

To test the connection to your database, run the telnet hostname port on your Looker server. For example, if you are running MySQL on the default port and your database name is mydb, the command would be telnet mydb 3306 . If the connection is working, you will see something similar to this: Trying 10.10.

Can we connect MySQL with Django?

To use MySql as the backend engine for a Django project, we need to follow a simple setup: Install the MySql Server (we can also use a remote one) Install the Mysql Python driver - used by Django to connect and communicate. Create the Mysql database and the user.


2 Answers

All you need to do is start a application and if its not connected it will fail. Other way you can try is on shell try following -

from django.db import connections from django.db.utils import OperationalError db_conn = connections['default'] try:     c = db_conn.cursor() except OperationalError:     connected = False else:     connected = True 
like image 175
Mutant Avatar answered Sep 28 '22 01:09

Mutant


Run the shell

python manage.py shell 

Execute this script

import django print(django.db.connection.ensure_connection()) 

If it print None means everything is okay, otherwise it will throw an error if something wrong happens on your db connection

like image 25
Steven Avatar answered Sep 27 '22 23:09

Steven