I have deployed my Django app to Google Cloud. It worked fine when I hosted it locally and throughout the steps outlined in this post.
It raises a server Error(500) when I try to view the live link.
When I enable Debug in the settings.py, this is the full traceback. (Torque is the name of my project), and showroom is my app.
The traceback refers to a views attribute (num_manufactureres) which I never had a problem with when hosting it locally.
OperationalError at /showroom/
(2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
Request Method: GET
Request URL: https://torque-256805.appspot.com/showroom/
Django Version: 2.2.5
Exception Type: OperationalError
Exception Value:
(2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
Exception Location: /env/lib/python3.7/site-packages/MySQLdb/connections.py in __init__, line 166
Python Executable: /env/bin/python3.7
Python Version: 3.7.4
Python Path:
['/srv',
'/env/bin',
'/opt/python3.7/lib/python37.zip',
'/opt/python3.7/lib/python3.7',
'/opt/python3.7/lib/python3.7/lib-dynload',
'/env/lib/python3.7/site-packages']
Server time: Thu, 24 Oct 2019 09:45:29 +0300
...
/env/lib/python3.7/site-packages/MySQLdb/__init__.py in Connect
return Connection(*args, **kwargs) …
▶ Local vars
/env/lib/python3.7/site-packages/MySQLdb/connections.py in __init__
super(Connection, self).__init__(*args, **kwargs2) …
▶ Local vars
...
num_manufacturers = Manufacturer.objects.all().count() …
▶ Local vars
I'm new to Google Cloud, so I don't know how to start debugging this.
Here are some possible issues:
The server instance I created on the cloud.google.com uses europe-west3 as a region. But when I was deploying, I thought that it created a completely new server and chose europe-west6 as a better option. (Close proximity, better reliability etc...)
I changed my project settings.py for better security according to the check --deploy Django command.
Otherwise, I can't think of anything else. Can anyone help?
Check out this Django example on App Engine settings.py file:
if os.getenv('GAE_APPLICATION', None):
# Running on production App Engine, so connect to Google Cloud SQL using
# the unix socket at /cloudsql/<your-cloudsql-connection string>
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/[YOUR-CONNECTION-NAME]',
'USER': '[YOUR-USERNAME]',
'PASSWORD': '[YOUR-PASSWORD]',
'NAME': '[YOUR-DATABASE]',
}
}
else:
# Running locally so connect to either a local MySQL instance or connect to
# Cloud SQL via the proxy. To start the proxy via command line:
#
# $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
#
# See https://cloud.google.com/sql/docs/mysql-connect-proxy
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3306',
'NAME': '[YOUR-DATABASE]',
'USER': '[YOUR-USERNAME]',
'PASSWORD': '[YOUR-PASSWORD]',
}
}
Last but not least, if you're running your app locally, make sure you have ALLOWED_HOSTS= ['localhost'] if your database host is HOST='localhost', or else you can just use 'HOST': '127.0.0.1', as shown in the code sample above.
Related
I'm trying to make Django work under Windows 10 but connecting with my (often used) off-site Mysql server I get
django.db.utils.OperationalError: (2006, "Can't connect to MySQL server on 'localhost' (10061)")
While (like I used to do) state in my config file (mysql.conf) to which is pointed to by my settings.py like I used a thousand times before:
[client]
host = 10.8.0.1
database = sitesv
user = niels
password = xxx
port = 3306
default-character-set = utf8
Whether I use the ip within '' or I use a name which is known in my hosts file I still get the same stupid error.... I'm not trying localhost!
In site/settings.py I (still) have:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': BASE_DIR+'\sv\mysql.conf',
},
}
}
With 'still' I mean that I am not frustrated but just halted by some minor mistake which I don't see. ;)
I want to configure my django project in order to connect it with database in MYSQL I created with workbench 8.0,
and then I want to run the server by running
python manage.py runserver
from anaconda command prompt,
so that I can use the Django interface to visualize and alter data.
Please note that I don’t want to downgrade workbench 8.0.
These are the steps I have made:
From anaconda prompt:
pip install mysqlclient
In my project folder, in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'schema_meta',
'USER': 'root',
'PASSWORD': '<mypassword>',
'HOST': '127.0.0.1',
'PORT': '3306',
},
}
Inside the directory of mysql server, I open cnf.ini and insert a the [client] section:
[client]
database=schema_meta
host=127.0.0.1
user=root
password=<mypassword>
port=3306
default-character-set = utf8
Then from anaconda prompt I run
Python manage.py runserver
And I obtain error
django.db.utils.OperationalError: (2059, "Authentication plugin
'caching_sha2_password' cannot be loaded: Impossibile trovare il
modulo specificato.\r\n")
So I try to solve it by following this thread: django.db.utils.operationalError: (2059,"Authentication Plugin 'caching_sha2_password'")
I open mysql workbench and I run this query:
delete from mysql.user
where user='root'
and host = '127.0.0.1';
flush privileges;
CREATE USER 'root'#'127.0.0.1' IDENTIFIED WITH mysql_native_password BY '<mypassword>';
And then, in the my.ini file I change
default-authentication-plugin= caching_sha2_password
With
default-authentication-plugin=mysql_native_password
Finally from anaconda prompt:
python manage.py runserver
But again I get
django.db.utils.OperationalError: (2059, "Authentication plugin
'caching_sha2_password' cannot be loaded: Impossibile trovare il
modulo specificato.\r\n")
Now, what’s wrong? Why it did not get the changes into the authentication method?
In order to check that there are no other errors, from mysql workbench, from first “home” view, I right click on my database, I open “edit connection”, I click on “test connection”, and the software says that the connection is successfull.
mysql workbench saying connection successfully established
Moreover, I wanted to check if the problem was in my Django settings.
So from anaconda prompt I run
pip install pymysql
Then in the project folders I created a “connect_to_mysql.py” script, with the following code inside:
import pymysql.cursors
mydb = pymysql.connect(host='127.0.0.1',
user='root',
password='<mypassword>',
db='schema_meta',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
print(mydb)
and this seems to work fine, since when I run
connect_to_mysql.py
from anaconda, I get
pymysql.connections.Connection object at 0x000002013F2851D0
That I guess it means “connection successfully established”.
And just to be sure that the problem is into mysql (mysql connector I guess), I create a file “connect_to_mysql_2.py” with this code inside:
import mysql.connector
mydb = mysql.connector.connect(user='root', password='<mypassword>',
host='127.0.0.1', database='meta_schema')
print(mydb)
And when I run it from anaconda, again I get
"Authentication plugin '{0}' is not supported".format(plugin_name))
mysql.connector.errors.NotSupportedError: Authentication plugin
'caching_sha2_password' is not supported
That means that I fixed nothing by working on mysql workbench and in my.ini file.
How can I get my Django connected with my mysql database and my server running?
Is there a way to establish the server connector using pymysql connector instead that mysql connector?
This is probably not a problem in your python code, but in the python connector. The caching_sha2_password plugin is now the default auth plugin and clients have to support it in order to connect. So, the best solution is to update your python connector. An alternative way is to disable this plugin, but that's something I don't recommend as it lowers your server's security.
PyMySQL added support for caching_sha2_password in 0.9.0, though there was a Py2 error fixed in 0.9.1.
Also noted in the install instructions, for caching_sha2_password there is the additional requirement:
python3 -m pip install PyMySQL[rsa]
I think I solved it.
By following this thread
https://stackoverflow.com/a/21740692/7658051
In settings.py, at the DATABASES entry, I substituted
'ENGINE': 'django.db.backends.mysql'
with
'ENGINE': 'mysql.connector.django',
and again, as specified here,
https://django-mysql.readthedocs.io/en/latest/checks.html#django-mysql-w003-utf8mb4
i added also
'OPTIONS': {
# Tell MySQLdb to connect with 'utf8mb4' character set
'charset': 'utf8mb4',
},
# Tell Django to build the test database with the 'utf8mb4' character set
'TEST': {
'CHARSET': 'utf8mb4',
'COLLATION': 'utf8mb4_unicode_ci',
}
and then if I run
python manage.py runserver
It seems to work, even if I cannot access http://127.0.0.1:8000/admin , because I am connected to a legacy database, so I still have to inspect db and stuff I still have to learn.
As second proof that the connection is now working, when I run
connect_to_mysql_2.py
(See (2059,“Authentication Plugin 'caching_sha2_password'”) when running server connected with MYSQL database on Django)
I finally get
mysql.connector.connection_cext.CMySQLConnection object at 0x000001DFB7521550
So I really think this time the connection is on.
So, I have a running project that uses a MySQL database with a MAMP local server but for compliance issues I need to change the database name. So I changed it in MAMP and I changed it in settings.py as seen bellow:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'prototipodb', # Or path to database file if using sqlite3.
'USER': 'palapa', # Not used with sqlite3.
'PASSWORD': 'palapa', # Not used with sqlite3.
'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
}
}
I thought that with these changes everything would keep running smoothly, but I got the following error on my server:
Unhandled exception in thread started by >
Traceback (most recent call last):
File "/Users/palapa2/projects/prototipo_tesis/venv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
self.validate(display_num_errors=True)
File "/Users/palapa2/projects/prototipo_tesis/venv/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Users/palapa2/projects/prototipo_tesis/venv/lib/python2.7/site-packages/django/core/management/validation.py", line 103, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "/Users/palapa2/projects/prototipo_tesis/venv/lib/python2.7/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "/Users/palapa2/projects/prototipo_tesis/venv/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 415, in get_server_version
self.cursor().close()
File "/Users/palapa2/projects/prototipo_tesis/venv/lib/python2.7/site-packages/django/db/backends/init.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Users/palapa2/projects/prototipo_tesis/venv/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 387, in _cursor
self.connection = Database.connect(**kwargs)
File "build/bdist.macosx-10.9-intel/egg/MySQLdb/init.py", line 81, in Connect
File "build/bdist.macosx-10.9-intel/egg/MySQLdb/connections.py", line 193, in init
_mysql_exceptions.OperationalError: (1049, "Unknown database 'prototipodb'")
I would appreciate if you can help me with this problem. One of the things I've been thinking about doing is run syncbd command to rebuild the connection, but if there is a better solution to this I would gladly consider it.
I just did this on my mac and it works fine. I copied the DB and changed the name in settings.py. Then I gave my user access to the new DB.`
Did you give user palapa access to the new DB?
Did you flush privileges?
Did you restart Django server?
I know there are tones of questions like that but sadly none of them resolved my problems and as I am new to django I don't know what is wrong.
So I installed django and mysql successfully, then the django-mysql package. When try to import the MySQLdb in python (running in the terminal) it works.
But my question is: how do you start mysql and create a database? And how to "link" it to django?
I tried running mysql but it doesn't work. So I tried /usr/local/mysql/bin/mysql which throws an error ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2).
What do I have to do?
Check your system preferences - there should be a MySQL icon on the bottom. There you can start it.
Also I recommend to install MySQLWorkbench to manage your db
I think that #init3 already answered the question regarding how to start mysql. How do you "link" your database to a django project? Well, in your settings.py file you find something like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'my_database_name', # Or path to database file if using sqlite3.
'USER': 'my_mysql_user', # Not used with sqlite3.
'PASSWORD': 'my_mysql_password', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
So change NAME, USER and PASSWORD accordingly and Django will use that database to create tables and populate fields.
I am trying to configure mysql with my Django project-ecomstore and I get the error given in the title,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', #
'NAME': 'ecomstore', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'password', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
Error:
C:\Python27\Djangoprojects\ecomstore>python manage.py dbshell
'mysql' is not recognized as an internal or external command,
operable program or batch file.
Please provide your inputs.
Just use 'ENGINE': 'mysql',. That's how my current app has it.
This simply means that the ‘mysql’ excutable command file (mysql.exe) was not added to your system path therefore the system was unable to locate this file. To add the ‘mysql’ to system path try to locate the ‘mysql’ resident folder.
Seems you are using windows, here are the steps:
1) go to Control Panel -> System and Security -> System
2)paste "mysql resident folder path" into Variable value: field of Edit System Variable pop up.
python manage.py dbshell is just a shortcut for mysql -d DATABASENAME -u USERNAME -p PASSWORD and you will need to have mysql installed on your system and that mysql command should be able to run from the console.