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
Related
I'm using a database URL string in my settings like:
DATABASES = {
'default': "mysql://root:#localhost:3306/mydb"
}
When I migrate I get this warning:
MySQL Strict Mode is not set for database connection 'default'
Now my question: How can I combine the two things?
I cannot use the "regular" way to set the database settings with a dictionary because my database url comes from an environment variable.
Thx in advance!
You could update your settings afterwards:
DATABASES['default']['OPTIONS'] = {'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"}
I think this one will help you.
you can pass the options as a query string in the URL
DATABASES = {
'default': dj_database_url.config(default="mssql://USER:PASSWORD#HOST:PORT/NAME?init_command=SET sql_mode='STRICT_TRANS_TABLES'&charset=utf8mb4", conn_max_age=500)
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': 'test123',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
'charset': 'utf8mb4',
}
}
}
Try to give init_command of database options in settings.py
If want know more refer the docs django_mysql.W001: Strict Mode
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
I tried setting
DATABASES = {
'default': {
'NAME': 'db',
'ENGINE': 'mysql.connector.django',
'USER': 'dbuser',
'PASSWORD': 'dbpass',
'OPTIONS': {
'autocommit': True,
'init_command' : 'SET storage_engine=INNODB',
},
}
}
(UPD: updated the above code so ppl won't get confused that I am not using django settings the right way)
in Django settings, but this backend doesn't accept such connection option...
http://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
is there any other way of doing this?
First, to use MySQL Connector/Python Django backend, you have to set the ENGINE setting to mysql.connector.django:
DATABASE = {
'default': {
..
'ENGINE': 'mysql.connector.django',
..
}
}
Indeed, MySQL Connector/Python does not have the init_command connection argument. I can see value in adding an option for setting the default storage engine in the Django OPTIONS though. If you really want it, I would suggest opening a feature request on http://bugs.mysql.com.
Small note that MySQL 5.6 (and if you start out, you should use this version) has storage engine set to InnoDB by default.
You need to set the entire setting:
DATABASES = {
'default': {
'NAME': "MyDatabaseName,
'ENGINE': 'django.db.backends.mysql',
'USER': "MyUsername",
'PASSWORD': "MyPassword",
'HOST': "MyHostName",
'OPTIONS': {'init_command': 'SET storage_engine=INNODB'},
}
}
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?
I want to have a single settings.py file that will behave differently when running the application
./manage.py runserver
and when testing
./manage.py test myapp
So, I can change the test db to sqlite for example, with something like:
if IS_TESTING:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'test_db',
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': DATABASE_NAME,
'USER': DATABASE_USER,
'PASSWORD': DATABASE_PASS,
'HOST': 'localhost',
'PORT': '5432',
}
}
I can get this behaviour by changing the manage.py script like this:
if __name__ == "__main__":
os.environ.setdefault('IS_TESTING', 'false')
print 'off'
if sys.argv[1] == 'test':
print 'on'
os.environ['IS_TESTING'] = 'true'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "frespo.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
But I think this is still not good enough, because when I run the tests inside an IDE (PyCharm) it won't use my custom manage.py file. There has to be a variable for this already inside Django. Do you know where it is?
If you need this condition just for Django unit test purposes, the following line in the settings.py file should work:
if 'test' in sys.argv:
DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3'
SOUTH_TESTS_MIGRATE = False # if you're using south
This assumes the other standard DATABASES setting is always declared anyway. The above line simply sets the database to sqlite in case of a unit test run.
Afaik Django does not have such a variable. Assuming you can edit the exact command run by PyCharm, you could just add the test argument there, and then look for it in the settings.py file.
if "test-argument" in sys.argv:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'test_db',
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': DATABASE_NAME,
'USER': DATABASE_USER,
'PASSWORD': DATABASE_PASS,
'HOST': 'localhost',
'PORT': '5432',
}
}