I am getting error when try to run
python manage.py migrate
django.db.utils.OperationalError: (1045, "Access denied for user 'someuser'#'localhost' (using password: NO)")
This is my database setting inside setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/config/mysql.cnf',
},
}
}
config/mysql.cnf
[client] database = dbname user =
root password = passwt host = localhost
default-character-set = utf8
The MySql configs can be provided in settings.py file, as like below
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',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
'charset': 'utf8mb4',
}
} }
If need to configure with external file, below may be helpful
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': os.path.join(BASE_DIR, 'mysql.cnf'),
}
} }
The mysql.cnf file should be in project dir
Related
I have created a mysql database with Cpanel . And I have some settings for database in the settings.py :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '*****_db',
'USER': '******',
'PASSWORD': '********',
'HOST': 'localhost',
'PORT': '3306',
'OPTIONS': { 'init_command': 'SET storage_engine=INNODB,character_set_connection=utf8,collation_connection=utf8_unicode_ci'
}
}
}
but the problem is when I try to add a new record in django-admin with some arabic chars , I get this error :
OperationalError at /admin/courses/arguments/add/
(1366, "Incorrect string value: '\\xD8\\xB3\\xDA\\xAF' for column `asiatrad_db`.`courses_arguments`.`name` at row 1")
What is the problem ? Do I need to create a new database with charset on utf-8 ?
The “utf8” encoding only supports three bytes per character. The real UTF-8 encoding, which everybody uses, needs up to four bytes per character. See this article.
So use “utf8mb4” charset instead of “utf8”.
The settings.py should look as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '*****_db',
'USER': '******',
'PASSWORD': '********',
'HOST': 'localhost',
'PORT': '3306',
'OPTIONS': { 'init_command': 'SET storage_engine=INNODB','charset': 'utf8mb4'}
}
I am getting the error OperationalError: fe_sendauth: no password supplied on my production server but I cannot see why...
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'dbuser',
'PASSWORD': PROD_DB_PASSWORD,
'HOST': 'localhost',
'PORT': '5432',
}
}
pg_hba.conf:
host dbname dbuser localhost md5
If I do psql -d dbname -U dbuser -h localhost and then enter the password at the prompt I can see that it works so IDK why django is not sending the password and IDK where to look from here.
I suspect you're not passing the password correctly. Here's how you debug. After the DATABASES line in settings.py, can you try printing out the dict.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'dbuser',
'PASSWORD': PROD_DB_PASSWORD,
'HOST': 'localhost',
'PORT': '5432',
}
}
print DATABASES
Then manage.py runserver as you would.
See if the password is properly passed. Apologies my rep's not enough to comment yet.
For future readers also check the spelling and later cases. All database keys must be in UPPER CASE e.g ENGINE, NAME, USER, PASSWORD, HOST and PORT.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.getenv("DB_NAME", "db_name"),
"USER": os.getenv("DB_USERNAME", "db_user"),
"PASSWORD": os.getenv("DB_PASSWORD", "db#password"),
"HOST": os.getenv("DB_HOST", "localhost"),
"PORT": os.getenv("DB_PORT", "5432")
}
}
I try to use read_defaul_file option but it doesn't work.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/vagrant/project/my.cnf'
}
}}
my.cnf
[client]
database=db_2
user=root
password=root
default-character-set=utf8
[mysqld]
character_set_client=utf8
character_set_server=utf8
collation_server=utf8_unicode_ci
When I pass settings as usual It works normally. But It doesn't read my.cnf file. File has 777 permissions.
Settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': os.path.join(BASE_DIR, 'my.cnf'),
}
}
}
my.cnf
[client]
database = db_2
user = root
password = root
default-character-set = utf8
Have a look on it https://code.djangoproject.com/ticket/24653
I'm using Heroku with Django.
The database is configured in settings.py this way, using the dj-database-url module:
DATABASES = {'default': dj_database_url.config()}
How can I do to add additional parameters, for example ATOMIC_REQUESTS? In a "normal" case I would do like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
'ATOMIC_REQUESTS': True,
}
}
DATABASES is just a dictionary: you can add the relevant settings after the initial configuration.
DATABASES = {'default': dj_database_url.config()}
DATABASES['default']['ATOMIC_REQUESTS'] = True
I have created new project in django and write mysql connection in it's settings file like :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'optdb',
'User': 'root',
'Password':'975',
'Host':'127.0.0.1',
'Port':'8080',
}
}
Now i write command : python manage.py syncdb and that generates following error:
How to solve this error mentioned above?
Note: I have currently one user in my MYSQL WAMP SERVER that is root with host 127.0.0.1.
All of those keys should be in capitals.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'optdb',
'USER': 'root',
'PASSWORD':'975',
'HOST':'127.0.0.1',
'PORT':'8080',
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'optdb',
'USER': 'root',
'PASSWORD':'975',
'HOST':'127.0.0.1',
'PORT':'8080',
}
}
It's case sensitive...
Also, are you sure your mysql server is at port 8080? That's unusual. If unsure, just leave out that line and default mysql port will be used. See How can I connect to MySQL on a WAMP server?