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.
Related
I am using mysql as database for my django project hosted in digitalocean
python3 manage.py makemigrations
python3 manage.py migrate
creating superuser
everything is working. but when i try to open up my site
The error message shows as: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
I checked all those previous questions and solutions but they didn't worked for me..
Below is my database setting.
Hope someone can help me on that. Thanks! It was working perfectly fine on my localhost
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'newdb_py',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", # Strict mode
},
}
}
I have found the error, it seems I haven't added the database configuration that I have made in database.py to my settings.py file. The method I was applying was wrong. I have imported settings.py to database.py which was working for local-machine but it didn't worked for server-hosting.
I solved that issue by just adding a header on settings.py file
from .database import *
P.S: above configuration was done on database.py
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 used to use Postgres for Heroku and you could access it with a command like:
heroku pg:psql
And it would take you to an interface that looked similar to the local MySQL database that you access with:
mysql -u admin -p *********
Now I want to use ClearDB (my tables are for MySQL), but how do I actually insert the tables via terminal, is there a command like heroku cleardb:something that will open the interface, or is that not possible?
Should I just convert them back to Postgres?
Nevermind, I clearly just had a stroke.
This is the CLEARDB_DATABASE_URL you get in heroku config
CLEARDB_DATABASE_URL => mysql://[username]:[password]#[host]/[database name]?reconnect=true
And then you just stick it into a command like you do when accessing your local DB
mysql --host=[host] --user=[user] --password=[password] --reconnect [database]
Or alternatively into your onboard config file
module.exports = {
'connectionLimit': 100,
'host': [host],
'user': [user],
'password': [password],
'database': [database],
}
Easy peasy, don't know why I didn't catch that.
I usually use SQLite but this project requires me to use data from a server so instead of trying to convert the data from SQLite to MySQL, I imported the data into my local MySQL database. I am trying to figure out how to configure my Django project to work with the imported database. Right now the models.py file is empty.
So I have three questions:
Is my settings.py (below) set up correctly?
Do I need to set up the models to match the schema in the imported
database?
Do I need to do anything else?
This is what I'm working with:
Django 1.8.4
Python 2.7
mysql 5.7.14
osx 10.11
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
"""..."""
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'extract',
'USER': 'root',
'PASSWORD': 'xxxxxxxxxx',
'HOST': 'localhost',
'PORT': '22',
}
}
""" ... """
You can try ./manage.py inspectdb after mentioning mysql database in settings.py. have a look at docs here
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.