How can I solve application error on heroku? - python

I am trying to create an app on django and I am trying to host it on heroku. Everything runs perfectly locally. I get an error while trying to migrate the database onto the heroku app by running command -
heroku run python manage.py migrate
This is the error I am getting -
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 211, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
connection = Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/__init__.py", line 121, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/extensions.py", line 167, in make_dsn
parse_dsn(dsn)
psycopg2.ProgrammingError: invalid dsn: invalid connection option "init_command"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/manage.py", line 22, in <module>
main()
File "/app/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 373, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 417, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 90, in wrapped
res = handle_func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 75, in handle
self.check(databases=[database])
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 438, in check
all_issues = checks.run_checks(
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/registry.py", line 77, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/model_checks.py", line 34, in check_all_models
errors.extend(model.check(**kwargs))
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 1307, in check
*cls._check_indexes(databases),
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 1699, in _check_indexes
connection.features.supports_covering_indexes or
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/features.py", line 84, in is_postgresql_11
return self.connection.pg_version >= 110000
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 339, in pg_version
with self.temporary_connection():
File "/app/.heroku/python/lib/python3.9/contextlib.py", line 119, in __enter__
return next(self.gen)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 614, in temporary_connection
with self.cursor() as cursor:
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 270, in cursor
return self._cursor()
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 246, in _cursor
self.ensure_connection()
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 211, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
connection = Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/__init__.py", line 121, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/extensions.py", line 167, in make_dsn
parse_dsn(dsn)
django.db.utils.ProgrammingError: invalid dsn: invalid connection option "init_command"
settings.py -
prod_db = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(prod_db)
settings.py ( database section ) -
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '****',
'USER': '****',
'PASSWORD': '****',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}
How can I solve this issue? Please ask if I need to add any of my files onto the question.

You are using MySQL for local development and PostgreSQL in production. The PostgreSQL connection is coming from the DATABASE_URL environment variable that dj_database_url uses by default.
Since you are updating your default database from the environment (not replacing it), the MySQL-specific init_command option is being left in your PostgreSQL configuration. The psycopg2 PostgreSQL driver doesn't know what to do with it.
A quick and dirty solution would be to completely your default database configuration instead of updating it:
DATABASES['default'] = prod_db
But I strongly urge you to use the same database server for development and production. As you have discovered, database engines aren't drop-in replacements for each other. In this case, it's likely easiest to switch from PostgreSQL to MySQL on Heroku.
First, let's get rid of the Postgres stuff:
Remove psycopg2 from your dependencies. Its presence likely caused Heroku to automatically provision a PostgreSQL database for you, which added to the confusion.
Optionally, de-provision the existing Postgres database. It's probably the free version so it isn't costing you anything, but it's a bit weird to have it hanging around if you are using a completely different database server.
Now, provision a MySQL server. I suggest you pick from the available addons. There are a few MySQL options, including JawsDB and ClearDB. Here is an example using ClearDB:
heroku addons:create cleardb:ignite
Ignite is the free tier for ClearDB. Once provisioned, your app will see a new environment variable CLEARDB_DATABASE_URL. Other addons will use other environment variable names, so make sure to read their documentation.
Finally, update your Django configuration. dj_database_url uses the DATABASE_URL environment variable by default, but we can tell it to use another:
prod_db = dj_database_url.config(env="CLEARDB_DATABASE_URL", conn_max_age=500)
DATABASES['default'].update(prod_db)

Related

Django: unknown database name

I am trying to change the database in django from sqlite3 to mysql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER':'myusername',
'PASSWORD':'mypassword',
'HOST':'127.0.0.1',
'PORT':'3306',
}
}
and after trying to run my server i get this error
System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
self.connect()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\mysql\base.py", line 244, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\MySQLdb\__init__.py", line 123, in Connect
return Connection(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\MySQLdb\connections.py", line 185, in __init__
super().__init__(*args, **kwargs2)
MySQLdb.OperationalError: (1049, "Unknown database 'django'")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\commands\runserver.py", line 137, in inner_run
self.check_migrations()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 576, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\loader.py", line 58, in __init__
self.build_graph()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\loader.py", line 235, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\recorder.py", line 81, in applied_migrations
if self.has_table():
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\recorder.py", line 57, in has_table
with self.connection.cursor() as cursor:
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 284, in cursor
return self._cursor()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 260, in _cursor
self.ensure_connection()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
self.connect()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
self.connect()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\mysql\base.py", line 244, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\MySQLdb\__init__.py", line 123, in Connect
return Connection(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\MySQLdb\connections.py", line 185, in __init__
super().__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1049, "Unknown database 'django'")
i dont know why django can't see my db, maybe you guys have some ideas.
Mysql didn't get me any problems before this case,
i haven't found a way to fix an error, so I counting on your help.
thanks in advance
The shortest answer to that is that django database ist't crated in MySQL. Django will not create the database for you like it do on sqlite3, you have to had this database created before.
You can ensure that you created the database by running a command like that:
$ mysql -h 127.0.0.1 -pmypassword -u myusername # to connect on your mysql
mysql> show databases; # list all databases on your MySQL server
To create a new database my recomendation is the following:
mysql> create database django charset utf8mb4; -- charset is optional
mysql> use django;

makemigrations isn't working- how to fix it?

I have a dockerized django project. I'm trying to make migrations, but I keep getting this error. What does this mean, and how can I fix it? I am really new to this, so please be as detailed as possible! Thank you
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/core/management/commands/makemigrations.py", line 101, in handle
loader.check_consistent_history(connection)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/migrations/loader.py", line 283, in check_consistent_history
applied = recorder.applied_migrations()
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 76, in applied_migrations
if self.has_table():
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 260, in cursor
return self._cursor()
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 236, in _cursor
self.ensure_connection()
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
self.connect()
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
self.connect()
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
connection = Database.connect(**conn_params)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Your Django application is not connecting to the database.
Couple of things to check.
What port is your database running on and what is the hostname? If you're using docker-compose, the host will be the same as the database service name.
Next, check your settings.py insuring that your database is using the correct host and port. By default, on Postgres, Django will use port 5432. If this is not the case, update it.
Similarly, if you have not explicitly set a host, Django will use localhost. You will need to change this value to whatever your database service name is.

Error while running python manage py migrate

I am new to django, I was running the command
python manage.py migrate
and got this error.
(py1) G:\django\djangoproject1>python manage.py migrate Traceback
(most recent call last): File "manage.py", line 22, in
execute_from_command_line(sys.argv) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\management__init__.py",
line 364, in execute_from_command_line
utility.execute() File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\management__init__.py",
line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\management\base.py",
line 283, in run_from_argv
self.execute(*args, **cmd_options) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\management\base.py",
line 327, in execute
self.check() File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\management\base.py",
line 359, in check
include_deployment_checks=include_deployment_checks, File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\management\commands\migrate.py",
line 61, in _run_checks
issues = run_checks(tags=[Tags.database]) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\checks\registry.py",
line 81, in run_checks
new_errors = check(app_configs=app_configs) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\checks\database.py",
line 10, in check_database_backends
issues.extend(conn.validation.check(**kwargs)) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\mysql\validation.py",
line 9, in check
issues.extend(self._check_sql_mode(**kwargs)) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\mysql\validation.py",
line 13, in _check_sql_mode
with self.connection.cursor() as cursor: File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\base\base.py",
line 254, in cursor
return self._cursor() File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\base\base.py",
line 229, in _cursor
self.ensure_connection() File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\base\base.py",
line 213, in ensure_connection
self.connect() File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\utils.py", line
94, in exit
six.reraise(dj_exc_type, dj_exc_value, traceback) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\base\base.py",
line 213, in ensure_connection
self.connect() File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\base\base.py",
line 189, in connect
self.connection = self.get_new_connection(conn_params) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\mysql\base.py",
line 274, in get_new_connection
conn = Database.connect(**conn_params) File "C:\Users\Mahin\Envs\py1\lib\site-packages\MySQLdb__init__.py", line
84, in Connect
return Connection(*args, **kwargs) File "C:\Users\Mahin\Envs\py1\lib\site-packages\MySQLdb\connections.py",
line 164, in init
super(Connection, self).init(*args, **kwargs2) django.db.utils.OperationalError: (1045, "Access denied for user
'root'#'localhost' (using password: YES)")
Since I should have magically known your settings.py DB config and my post was deleted for not answering the question without "requiring clarification from the asker" I'm reposting to try and help. The default password for root user in a mysql db is "", that is its left blank. Right now there is a mismatch between your db and the configuration you provided in your settings.py file. Go into your dbms and make sure that the setting for the database match the ones you provided in your settings.py.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB NAME',
'USER': 'USER NAME',
'PASSWORD':'USER PW',
'HOST' : 'localhost',
}
}
If these match to your db you should be able to now migrate.

django.db.utils.operationalError: (2059,"Authentication Plugin 'caching_sha2_password'")

I am trying to connect my django project 'mysite' to mysql. I made a user in mysql and granted it all privileges to access the project. These are the changes I made to settings.py :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysitedb',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
Now when I try to migrate the database using python3 manage.py makemigrations, I get the following error:
django.db.utils.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory")</code></pre>
The complete stack trace is as follows:
Traceback (most recent call last):
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 236, in get_new_connection
return Database.connect(**conn_params)
File "/usr/lib/python3/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python3/dist-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/management/base.py", line 332, in execute
self.check()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/management/base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/core/checks/model_checks.py", line 27, in check_all_models
errors.extend(model.check(**kwargs))
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/models/base.py", line 1200, in check
errors.extend(cls._check_fields(**kwargs))
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/models/base.py", line 1272, in _check_fields
errors.extend(field.check(**kwargs))
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 894, in check
errors = super().check(**kwargs)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 206, in check
errors.extend(self._check_backend_specific_checks(**kwargs))
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 303, in _check_backend_specific_checks
return connections[db].validation.check_field(self, **kwargs)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/validation.py", line 21, in check_field
field_type = field.db_type(self.connection)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 648, in db_type
return connection.data_types[self.get_internal_type()] % data
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 133, in data_types
if self.features.supports_microsecond_precision:
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/mysql/features.py", line 65, in supports_microsecond_precision
return self.connection.mysql_version >= (5, 6, 4)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 345, in mysql_version
with self.temporary_connection() as cursor:
File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 576, in temporary_connection
cursor = self.cursor()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 255, in cursor
return self._cursor()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 232, in _cursor
self.ensure_connection()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/prabhjeet/.local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 236, in get_new_connection
return Database.connect(**conn_params)
File "/usr/lib/python3/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python3/dist-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory")
How can I resolve this error? I am using python 3 on ubuntu 16.04 platform.
Actually, there is no need to downgrade the MYSQL Server. Follow these two steps and it should work:
Step 1: Change MYSQL configuration to use mysql_native_password. Edit one of ini files that mysqld is using. You can see which one my using the command
mysqld --verbose --help
and make sure this line is added -
default-authentication-plugin=mysql_native_password
Step 2: Create an new or alter the existing user. To create a new with mysql_native_password password:
CREATE USER 'username'#'ip_address' IDENTIFIED WITH mysql_native_password BY 'password';
and to alter user
ALTER USER 'username'#'ip_address' IDENTIFIED WITH mysql_native_password BY 'password';
By the way, this step is also pointed out by #Kol_ya
The issue is (probably) your (new) version of MySQL.
Starting with version 8.04 MySQL uses caching_sha2_password as default authentication plugin where previously mysql_native_password has been used (which causes compatibility issues with older services that expect mysql_native_password authentication).
Possible solutions:
Downgrade the MySQL Server to a version below that change or change the authentication plugin (on a user basis)
eg when creating the user:
CREATE USER 'username'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
#Kol_ya created new user using CREATE USER 'username'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; where username = creation of new username and password = current password to login for root(username created with caching_sha2). This step allowed me to connect to MySql Server 8.0.13 without having caching_sha2 authentication error.
Looked for solution several threads but this one helped just like that. Thanks #kol_ya
Suggestion to people who are having caching_sha2 issue. In server side make sure your user authentication type is standard not caching_sha2_password.
In MySql 8.0 the default authentication plugin is 'caching_sha2_password', which causes authentication plugin issues(error 2059), one can create a new user in the MySql workbench
and setting there Authentication to be standard.
This error is typically gotten when the DB user don't have all access to the database
Run this command:
ALTER USER 'username'#'ip_address' IDENTIFIED WITH mysql_native_password BY 'password';
*Make sure to have your MySQL server running.

Django installation picking up wrong path names for migrate after project was copied

I have tried to create a new Django project on my Mac by making a copy of an existing working Django project.
I have tried to remove references to the old project paths from the new project but when doing a migrate or createsuperuser I get an error:
psycopg2.OperationalError: invalid connection option "init_command"
Both projects use a virtual environment called myvenv. The original project used the MySQL database. The new one uses Postgresql which has been installed in the virtual environment.
The trace history for the error shows that some of the paths show the correct path name of yhistory-server and some show the path name for the old project from which the new project was copied: veeuserver. I have been through all my code but cannot see where it is picking up the veeuserver path name from.
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/runpy.py", line 182, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/billnoble/Documents/YHistory-Server/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 52, in execute
return super(Command, self).execute(*args, **options)
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 86, in handle
default_username = get_default_username()
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/contrib/auth/management/__init__.py", line 189, in get_default_username
auth_app.User._default_manager.get(username=default_username)
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/models/query.py", line 381, in get
num = len(clone)
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/models/query.py", line 240, in __len__
self._fetch_all()
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 846, in execute_sql
cursor = self.connection.cursor()
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/backends/base/base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/backends/base/base.py", line 204, in _cursor
self.ensure_connection()
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
connection = Database.connect(**conn_params)
File "/Users/billnoble/Documents/VeeUServer/myvenv/lib/python3.4/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: invalid connection option "init_command"
I experienced the same issue when moving a site from MySQL to Postgres. The key lies in the settings.py file, particularly where you specify the database option. In MySQL, it looked like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/etc/mysql/my.cnf',
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
'NAME': 'mydatabase',
'USER': 'username',
'PASSWORD': password,
'HOST': '127.0.0.1',
'POST': '',
}
}
In order to make it work with Postgres, I had to change the engine to 'django.db.backends.postgresql', delete the OPTIONS and POST, and add PORT: '5432'. It now looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'username',
'PASSWORD': password,
'HOST': '127.0.0.1',
'PORT': '5432'
}
}
It's still trying to use a postgres database. You should update the DATABASES setting to use django's mysql backend, and you may need to remove all of your migrations as well. After you've removed the migrations and change the DATABASES settings, you should be able to python manage.py migrate and python manage.py makemigrations then finally createsupersuser.

Categories