Configuring Django and MySQL is not hard as it seems, just with the right commands its few steps works. Django is a web framework for python and MySQL is relational database management system. Together they are perfect for each other.
MySQL Connector/ODBC Developer Guide / Connector/ODBC Installation / Installing Connector/ODBC on macOS 4.3 Installing Connector/ODBC on macOS macOS is based on the FreeBSD operating system, and you can normally use the MySQL network port for connecting to MySQL servers on other hosts. In addition to the core installation, the Package Installer also includes Chapter 3, Installing a MySQL Launch Daemon and Chapter 4, Installing and Using the MySQL Preference Pane, both of which simplify the management of your installation.
MySQL can easily handle around 1 million records. In one of my web application developed in Django, had 1.5 million records and it is working perfectly fine (I had to do partitioning in MySQL when the number of records increased to 1 million).
Lets see towards configuration, but there are few prerequisites:
- Install Django
- Install MySQL
- Python connector for MySQL
Now I hope you already have Django and MySQL installed in your Mac OS X, so you only need to install python connector (you can skip this part if you already have MySQLdb working in your mac).
Install MySQL-python
While configuring Django and MySQL setup, sometimes people get errors in mac os x while installing “MySQL-python” using pip or easy_install, so there is a workaround, you should first install “Homebrew”(its a missing package manager for mac os x) and then run these commands
Now lets move to configuration of Django and MySQL, its same across all versions of Mac OS X( 10.10 Yosemite, 10.9 Mavericks, 10.8 Mountain Lion etc)
Configure Django with MySQL
Step 1: Open MySQL command line by typing this command, you might prompted for password so you’ll have to enter the password of user “root” which you would have set during installation of MySQL.
Step 2: Create a database for your django project and grant access to this database.
Step 3: Open settings.py of your django project and under database section add these lines
Step 4: Open terminal and navigate to your project root directory and run this command
Hope this helps. If you still have any issues of suggestions please post them in comments
Long story short, when I write the following:
I get the error
EnvironmentError: mysql_config not found
All right, so there are plenty of threads and the like on how to fix that, so I run this code:
Then I rerun my sudo code:
Then I get the following error.
Setup script exited with error: command ‘llvm-gcc-4.2’ failed with exit status 1
Google/Stack Overflow that, and I am told to download a GCC package which I did the other day, 200 MB’s or there-abouts and still no fix.
At this point I am lost, they say insanity is doing the same thing over and over while expecting a different result. Well, I’ve continually run the aforementioned code expecting a different result, so I’m not to far away from going insane.
At this point in my Python career, I am new to this, but I am willing to try pretty much anything to get this up and running.
If it helps I am officially running, Mac OS X 10.7.5, and I do have MAMP installed (is that an issue?)
Also, the other day when I was trying all of this for the first time I installed (reinstalled?) MySQL, so I’m really in a tough spot at this point.
Is there a fix?
I’ve racked my brain, searched Google, read Stack Overflow, and spent hours trying to figure this out to no avail.
Another option is to use pymysql it is a pure Python client connection to MySQL so you don’t have to mess around with compiling, a good exercise, but it can be frustrating if you are just trying to get something done. pymysql follows the same API as MySQLdb, it can essentially be used as a drop in replacement.
Also, it used to be that MySQLdb, did not work with Python 3, but this may have changed, pymysql didn’t have that problem which also induced me to switch, this may have changed though. pymysql can be slower than MySQLdb but you’ll have to see if you notice that, it is also under a different license (MIT for pymysql, GPL for MySQLdb)
Here’s what I would install, especially if you want to use homebrew:
- XCode and the command line tools (as suggested by @7stud, @kjti)
- Install homebrew
brew install mysql-connector-c
pip install mysql-python
Install mysql via homebrew, then you can install mysql python via pip.
It works for me.
I am using OSX -v 10.10.4. The solution above is a quick & easy.
Happening OSX does not have the connection library by default.
First you should install the connector:
Then install with pip mysql
For Python 3+ the mysql-python
library is broken. Instead, use the mysqlclient library. Install with: pip install mysqlclient
It is a fork of mysql-python (also known as MySQLdb) that supports Python 3+
This library talks to the MySQL client’s C-interface, and is faster than the pure-python pymysql libray.
Note: you will need the mysql-developer tools installed. An easy way to do this on a Mac is to run
to delegate this task to homebrew. If you are on linux, you can install these via the instructions at the mysqlclient github page.
As others mentioned before me….getting Python to work with MySQL on a Mac is a [email protected]#[email protected]&%^!! nightmare.
Installed Django framework on Mac OS 10.7.5 initially from the original Django website and when the MySQLdb didn’t work, and after many hours googling and trying solutions from SO, I have installed the Django stack from BitNami http://bitnami.com/stack/django
Still, got the issues mentioned above and then some more…
What helped me eventually is what Josh recommends on his blog: http://joshbranchaud.com/blog/2013/02/10/Errors-While-Setting-Up-Django.html
Now Python 2.7 is finally connected to MySQL 5.5
The issue you are having is that the gcc
compiler is not installed on your Mac. It will be installed if you have installed XCode. You will have to download gcc
complier and install it manually. Follow the below link and download it –
I once had this problem installing Ruby 1.9 and I had to compile ruby for myself because Mountain Lion wasn’t supported at that time. After installing the package, verify the install by the command gcc
.
It’s time to be a big boy and install from source. Try this:
1) Download the MySQL-python-1.X.X.tar.gz file(by default will go to your Downloads directory)
2) Open a Terminal window and cd to the Downloads directory.
3) Unzip the file you downloaded:
That will create a directory inside your Downloads directory called MySQL-python
4) cd into the newly created directory.
5) Typically, you just open the file called README or INSTALL and follow the instructions–but generally to install a python module all you do is:
If you care to look, there should be a file called setup.py inside your newly created MySQL-python directory, and you are invoking that program to install the module.
Also note that this:
is not permanent if you did that on the command line. You need to put that line in a file called .bashrc in your home directory (~/ or equivalently /Users/YOUR_USER_NAME). To see if .bashrc already exists(it’s a hidden file), issue the command:
and look for .bashrc. If .bashrc doesn’t exist, then create it.
I am using Python 2.7.11 :: Anaconda 2.3.0 (x86_64) on Mac OS X 10.11.4 15E65.
You may want to follow the steps below:
- Install homebrew
- Open a terminal and run: brew install mysql-connector-c
- pip install mysql-python
Then the Anaconda will have the mysql-python installed and you can start with MySQLdb then.
Good luck. Thanks.
Tags: mysql, python, sql