WebFaction
Community site: login faq
0
1

How do I configure DATABASES in settings.py if I have a private mysql instance?

I logged in as root and created a database flights.

Then I created a user:

create user fl_user identified by 'my_password';
grant all privileges on flights.* to fl_user;

I can log in from the command line using

mysql -S ~/webapps/<myapp>/var/mysql.sock -D flights -u fl_user -p

But how do I use that user and database from Django?

asked 17 Feb '13, 03:42

tpip
4111321
accept rate: 0%


Hello,

You can set up your private MySQL instance in Django by entering its details in settings.py, as elaborated upon at

http://docs.webfaction.com/software/django/config.html#setting-up-a-database

According to the Django documentation, you can also use the my.cnf options file that is created with each private MySQL instance (in $HOME/webapps/YOURPRIVATEMYSQL/etc/my.cnf):

https://docs.djangoproject.com/en/dev/ref/databases/#connecting-to-the-database

That my.cnf file will contain the port and socket of your instance, so just add the username/password credentials to the [client] section of the file:

[client]
database = NAME
user = USER
password = PASSWORD
port = 33333
socket = /home/SOMEUSER/webapps/YOURPRIVATEMYSQL/var/mysql.sock

and insert the location of the my.cnf file in settings.py.

If you need more information then please do not hesitate to contact us again.

Sincerely,

Valentin I.
WebFaction Support

--
WebFaction - Smarter web hosting
http://webfaction.com
http://twitter.com/webfaction - http://facebook.com/webfaction

permanent link

answered 17 Feb '13, 04:43

valentini
2614
accept rate: 28%

If I want to do it via settings.py, where do I enter the socket information?

'NAME': 'flights',
'USER': 'fl_user', 'PASSWORD': 'my_password',

(17 Feb '13, 04:53) tpip

Hello,

In the HOST variable, as per

https://docs.djangoproject.com/en/dev/ref/settings/#host

"HOST": '/var/run/mysql'

Sincerely,

Valentin I. WebFaction Support

(17 Feb '13, 05:06) valentini

I put EXACTLY that? Or I need to put the address to my directory or app name or something? It's giving "can't connect to local sql server through socket..."

"HOST": '/var/run/mysql'

(17 Feb '13, 07:02) tpip

Ok, got it.

HOST: /home/SOMEUSER/webapps/YOURPRIVATEMYSQL/var/mysql.sock

(I'm sure you get a lot of questions, but this would have been really helpful to have been told in the first answer. A link to a page of directions is not helpful if I don't know that the socket is the same as the host, and I don't know what format the host info should have.)

(17 Feb '13, 07:10) tpip

Please note that an answer cannot cover every single aspect of the actions, involved in reaching your goal. Also I assumed some basic knowledge on your end, since you specifically asked for MySQL and sockets.

The specific value for the socket is specified in your my.cnf file, which you can copy/paste to your settings.py file.

The format of the 'HOST' variable in settings.py is specified in Django's documentation at https://docs.djangoproject.com/en/dev/ref/settings/#host:

If this value starts with a forward slash ('/') and you’re using MySQL, MySQL will connect via a Unix socket to the specified socket...

There are other examples in the "HOST" section of Django's documentation, that pertain to using TCP sockets, instead of Unix ones.

If you need the exact values you must place in your settings.py file, then please submit a ticket - all we can provide without knowing your username is directions and example values.

(17 Feb '13, 07:41) valentini
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×909
×168

question asked: 17 Feb '13, 03:42

question was seen: 4,969 times

last updated: 17 Feb '13, 07:41

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM