Setting up Django with MySQL, syncdb giving segmentation fault? - python

I'm currently working on building a Django app. I'm following the "tangowithdjango" tutorial, which uses Django 1.54. In their tutorial, they use Sql-lite, but I'm planning on building this app for most robust purpose, which is why I'm attempting to connect MySQL instead.
Needless to say, it's been a nightmare. I can't get MySQL to connect for the life of me.
Here's what my settings.py looks like:
DATABASE_PATH = os.path.join(PROJECT_PATH, 'app.db')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'rideb',
'USER': 'root',
'PASSWORD': 'nantucket',
#'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
#'PORT': '', # Set to empty string for default.
}
}
And here's my output I'm getting...
(rideb)grantmcgovern#gMAC:~/Dropbox/Developer/Projects/RideB/master$ python manage.py syncdb
Segmentation fault: 11
I've installed python-mysqldb, and now I'm simply getting this, and I'm very perplexed to say the least. Is it some Django compatibility issue?
Everything works fine as the tutorial suggests with SQL-lite, but not looking to use that.
OS:
Mac OSX 10.10 Yosemite
MySQL (installed via .dmg on Oracle's site):
Server version: 5.6.19 MySQL Community Server (GPL)

I had a similar problem and it turns out it was related to incorrect mysql implementation on my Mac (OS X 10.10 Yosemite). I had "mysql-connector-c" installed instead of "mysql". Uninstalling "mysql-connector-c" (brew uninstall mysql-connector-c) and installing "mysql" (brew install mysql) resolved the problem right away.
To find out if you are facing this exact problem as I did, open the Console app on your Mac, go to User Diagnostic Reports and look for the report for your crash (should start with Python_). Under queue, if it shows you "0 libmysqlclient.18.dylib", then you have the same problem as I had.

As already mentioned you do not need DATABASE_PATH.
My working configuration looks like:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'db_pass',
'HOST': '127.0.0.1',
}
}
I am using a different engine, because it is required for Python3. → see documentation
After that you have to create the database and the user. Do not forget to give all needed rights to the user.
With python manage.py migrate your database is populated with your models.
syncdb the predecessor to migrate will be removed in Django 1.9

Related

Following Django tutorial, "python manage.py migrate" doesn't create any django tables (MySQL, Pycharm)

In mysite.settings.py I have:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.path.join(BASE_DIR, 'test'),
#'NAME': 'test',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
}}
When I run 'python manage.py migrate' in the PyCharm terminal, nothing happens, no error message etc. No new tables are created in the database specified. I tried two things for NAME as you can see.
MySQL Server version 8.0.19
MySQL Connector/Python 8.0.19
Python version 3.8
In PyCharm I have package mysqlclient 1.4.6 installed.
Any advice on how to find the problem? How to get an error message?
Thank you
DATABASES property specifies your DB configurations. Now NAME is the property which specifies your database name.
Currently here 'NAME': os.path.join(BASE_DIR, 'test'), won't work as it will give you something like C:/YourUser/YourPath... so this string is not a valid db name. basically (os.path.join() returns a path to your project directory)
First create a db manually on MySql, note the name and put it in property as 'Name': DBNAME in SETTINGS.py.
(Make sure you have your environment set while running following commands)
Then try python manage.py makemigrations for checking migration changes, if everything goes fine up till here then fire python manage.py migrate command.

Django+MySQL - "Authentication plugin 'caching_sha2_password' cannot be loaded"

When running the command: python manage.py runserver I'm getting the following error:
django.db.utils.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: The specified module could not be found.\r\n")
These are the relevant settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'polls',
'USER': 'root',
'PASSWORD': '<my-password>',
'HOST': 'localhost'
}
}
I've seen solutions that went around the problem, such as in this thread. They suggest changing the engine to mysql.connector.django, however the engine recommended by django is mysqlclient and therefore I would like to use it.
Other suggestion were to use naive password instead of sha2. I'd rather not go with this solution for security reasons.
Versions:
mysql 8.0.17 for Win64 on x86_64 (MySQL Community Server - GPL)
python 3.7.4
Django 2.2.4
mysqlclient 1.4.4
Try
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY '<my-password>';

Connect to remote mysql with django on Windows 10

I've got a Django settings file which works on mac and linux which will let me use my. my.cnf file to connect to a remote MySQL database on AWS. However, on Windows 10 this doesn't seem to be the case. It keeps on saying it can't connect to the local database as if the my.cnf file doesn't exist.
I've installed mysql connector and python mysql connector on windows 10, along with pip install mysqlclient as well like I would need to on linux however the problem still persists.
db_conf = Path("Project/my.cnf")
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '{}'.format(db_conf),
}
}
}
With the same settings file I can run makemigrations, migrate and run the server. On windows this same thing crashes it. my.cnf is the same on Windows/Mac/Linux any help would be appreciated.
I suspect it's a hangup with the way Windows does paths but I'm unsure how to resolve this as I usually code in Linux.
I've resolved the issue but..not in the way I wanted. I don't know why but django and windows don't seem to like using os.path or Path when trying to use it to connect to mysql database.
As such I've used a longer winded route using configparser and no longer having django read a config file instead give it the details in the settings file.
import configparser
db_conf = Path("Project/my.cnf")
conf = configparser.ConfigParser()
conf.read(db_conf)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': conf.get("client", "database"),
'USER': conf.get("client", "user"),
'PASSWORD': conf.get("client", "password"),
'HOST': conf.get("client", "host"),
'PORT': '3306',
}
}
What I find hilarious about this whole situation is that Path works normally except when trying to read it with django on Windows. It's not the situation I wanted but it does resolve the issue. I hope this helps others who may be struggling doing the same thing when connecting to a remote mysql server on Windows.

Django MySQL: SSL Error using mysqlclient and Python 2.7

I'm a Django newby (I started yesterday) and I'm trying to configure the DB. But since I think I'll always be using Django with MySQL I want to set it up. I read the doc and I saw that in order to use MySQL you have to install a module, and so I did. I installed mysqlclient since installing MySQLdb with pip return:
$ pip2 install mysqldb
Could not find a version that satisfies the requirement mysqldb (from versions: )
No matching distribution found for mysqldb
and the Oracle solution isn't working at all (django don't recognize it).
So I entered my credentials, and it look like this in project's settings.py:
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname_entered_and_tested',
'USER': 'username_entered_and_tested',
'PASSWORD': 'correct_password',
'HOST': 'externale.domain_name_to_db.com',
'PORT': '3306',
}
}
So it returned me
$ python manage.py runserver
... 30 lines of Exceptions ...
django.db.utils.OperationalError: (2026, 'SSL connection error: error:00000001:lib(0):func(0):reason(1)')
So I googled and I found Django AWS RDS MySQL Error: (2026, 'SSL connection error: error:00000001:lib(0):func(0):reason(1)')
So I set my settings to:
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname_entered_and_tested',
'USER': 'username_entered_and_tested',
'PASSWORD': 'correct_password',
'HOST': 'externale.domain_name_to_db.com',
'PORT': '3306',
'OPTIONS': {
'skip-ssl',
},
}
}
And it returned me
$ python manager.py runserver
... 30 lines of Exceptions ...
ValueError: dictionary update sequence element #0 has length 8; 2 is required
So I managed to generate my own certificats after reading another stackoverflow post, using OpenSSL and the MySQL doc (https://dev.mysql.com/doc/refman/5.6/en/creating-ssl-files-using-openssl.html) and I succeded, but django threw the same:
django.db.utils.OperationalError: (2026, 'SSL connection error: error:00000001:lib(0):func(0):reason(1)')
I already tried connecting with multiple other clients to my db using the same credentials, disabling SSL, enabling SSL, another user account.
I found a solution but it involve downgrading the MySQL version to 5.6.27 (Django server not starting correctly seems to be a mysql issue.) and it won't be a problem... if I only could :C . The MySQL database since to be a pain for a lot of Django users (so much post talking about that out there ...) and I'm just a newbie but I don't want to be discouraged by a simple bug and stop Django :S So, does someone have an answer ?
EDIT: I'm using the latest version of both django, pip and mysqlclient

Set up a Django Project with Mamp?

I just downloaded a newer version of MAMP (3.2.1) and i noticed that this Version has Python installed and also seems to handle SQLite Databases.
Shouldn't I be able to manage Django Projects with it?
Where and how would i install it?
I found some Posts in the Web (before my new MAMP release) where People already trying to get MAMP + Django to work with MySQL but those seemed more complicated to me then the usual setup with Virtualenv + SQLite/Postgres.
I'm pretty new to django but starting a project at the time seems quite simple to me.
If Django would work with MAMP together what would be the advantages?
Anyone has already experiences or useful links?
OK i gues working with MAMP MySQL has the advantage that i can easy import/export Database with php MyAdmin tool.
Anyway based on tanorix answer here how for me Django worked with MAMP MySQL Database:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'projectdb',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
'PORT': '8888',
}
}
Then
python manage.py migrate
I don't have knowledge about MAMP but I can give you some elements to put Django Database with WAMP, so I think it can be the same manipulation:
First, in MAMP, you need to create a database, call it : projectdb.
Then, at your settings.py, update your variable DATABASES like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'projectdb', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
Then, if you are using South, at your shell write this:
python manage.py schemamigration <name of your app> --init
python manage.py syncdb # => create your tables at your MAMP
python manage.py migrate

Categories

Resources