I try to connect to use a MongoDB database for the Django project.
So I follow a tutorial for changing the DATABASE from settings.py
# Original
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'testDB',
}
Changed to
# From tutorials
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'testDB',
'USERNAME': 'username',
'PASSWORD': 'password',
'HOST': 'myhostname.example',
'PORT': '27017',
}
}
Trying to run
python manage.py makemigrations
python manage.py migrate
All works, but no data in my database
Apparently django went for the localhost:27017 host and created a database there.
Uninstalling MongoDB, just caused makemigrations to fail
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] No connection could be made because the target machine actively refused it
I found a solution.
https://stackoverflow.com/a/60244703/7637454
To fulfill the answer here this is how you're supposed to configure it now.
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'yourmongodb',
'CLIENT': {
'host': 'some-host.or.ip',
'port': 27017,
'username': 'youruser',
'password': 'yourdbpass',
'authSource': 'yourcollection', # usually admin
}
},
}
The answer provided, does not work for me, instead I adopted following approach.
So, behind the scene, the Djongo uses PyMongo and PyMongo's default configuration are:
class MongoClient(common.BaseObject):
HOST = "localhost" # here HOST has the hardcoded value
PORT = 27017
which are in the following file:
venv/lib/python3.6/site-packages/pymongo/mongo_client.py
replace the harcoded value of HOST to something like
HOST = 'mongodb+srv://<username>:<password>#cluster-name/<dbname>?retryWrites=true&w=majority'
Additionally, we can set the environment variable if we want
HOST = os.getenv('MONGO_DB_URL')
Related
Database settings
DATABASES = {
'default': {
'ENGINE': os.environ.get('DB_ENGINE', "mysql"),
'NAME': os.environ.get('DB_NAME', "django_db"),
'USER': os.environ.get('DB_USER', "root"),
'PASSWORD': os.environ.get('DB_PASS', "123456798"),
'HOST': os.environ.get('DB_HOST', "localhost"),
'PORT': os.environ.get('DB_PORT'),
}
}
error
django.core.exceptions.ImproperlyConfigured: 'mysql' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
Connecting with mysql to generate migration but facing issue
Its my first time and facing following above issue please guide.
Instead of :
mysql in 'ENGINE': os.environ.get('DB_ENGINE', "mysql"),
Try this:
'ENGINE': os.environ.get('DB_ENGINE', "django.db.backends.mysql"),
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'root',
'PASSWORD': '123456798',
}
}
Try this to establish a connection.
I don't usually add HOST or Port but it still connect to database.
I only mention Host or Port if they don't have the default settings.
I'm trying to change the default database used in Django to MySQL, I've adjusted my settings.py file like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'clients',
'HOST': 'localhost',
'PORT': '3307',
'USER': 'root',
'PASSWORD': '*****',
}
}
I'm still getting the django.db.utils.OperationalError: (1049, "Unknown database 'clients'"). When I try to connect to the 'mysql' database that comes default with MySQL I have no issue. I get this error when I try to use manage.py makemigrations,migrations or runserver. I have MySQL-Python installed and everything. Proof that my clients database exists:
I've 2 DB in my one Django app. The two DB's are on the same network, ie on LAN. So, I suppose the HOST IP will be different. Wouldnt it be?
As of right now, to test my code, I've provided same HOST but different PORT.
So, my DB settings are as follow:-
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'vms_db',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '8000',
},
'users': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'aramex_vms_db',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '8080',
}
}
When I syncdb it, it returns an error saying:-
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
But when I use the same port, it works fine but the tables are made in default DB. I know the migrate command by default works on default DB.
1) But what if I want that there should be different tables for default and different for users, how will I do that?
If you want to migrate your database users:
$ ./manage.py migrate --database=users
As you can see here: https://docs.djangoproject.com/en/dev/topics/db/multi-db/#synchronizing-your-databases
If you want to select your database while you retrieve an entry:
#default dabatase
Author.objects.all()
#custom database
Author.objects.using('DATABASE_NAME').all()
Further information: https://docs.djangoproject.com/en/dev/topics/db/multi-db/#manually-selecting-a-database
I've got a django project connected to a MySQL database as follows:
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_db',
'USER': 'username',
'PASSWORD': 'mypassword',
'HOST': '',
'PORT': '',
}
}
on running python manage.py syncdb, it creates the database and on runserver the application runs perfectly and I'm able to input data into the models using the admin panel correctly.
However, on trying to connect to the database using MySQL Workbench, it gives me
Can't connect to MySQL server on '127.0.0.1' (111) error.
I had created the database as follows:
Grant all on my_db.* to 'username'#'localhost' identified by 'mypassword';
flush privileges;
Why would Workbench show me that error even though the server is running correctly? Thanks in advance!
Edit: Used the word pythong.
I had similar issue but it got resolved by updating #port and #host into configuration file.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'vres', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '3306', # Set to empty string for default.
}
}
It could be that your MySQL server is only listening to localhost.
In your my.cnf file, comment out bind-address.
#bind-address = 127.0.0.1
Restart your MySQL server and see if you can connect with Workbench.
Exception Type: OperationalError at /
Exception Value: (1049, "Unknown database 'database'")
At the moment i tried this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'database', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '****', # Not used with sqlite3.
'HOST': '/var/lib/mysql/database/', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '80', # Set to empty string for default. Not used with sqlite3.
}
}
If i don't specify a host i get this error:
OperationalError at /
(2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/database' (13)")
Can it be something with permissions?
thanks in advance :)
First, create the database on mysql.
Second, edit your default conection like this.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'MY_DATABASE_NAME',
'USER': 'root',
'PASSWORD': 'MY_PASSWORD',
}
}
finally run your syncdb.
./manage.py syncdb
PORT is not the web server port, but the database port, which is 3306 for MySQL, and HOST is the database sever's IP or name. You probably want 127.0.0.1.
You should create the database beforehand with create database mydatabase charset utf8; from the mysql prompt.
Login to mysql on Terminal and create a DB
mysql -u root -p
CREATE DATABASE dbname;
USE dbname;
Then on setting.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname',
'USER': 'root', #Mysql username
'PASSWORD': 'password', #mysql password
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
I dont think you can create a database by the name database. So please check the following first...
1) Have installed mysql-server and created the database with proper grant all permissions? If you have done so then check next steps
2)I dont know about Amazon, but this error was common in older versions of Linux distros from Redhat and Fedora. An option is to do this
by setting the SELINUX line in /etc/sysconfig/selinux to Disabled:
SELINUX=Disabled