4

Ive been using Django with sqlite3 on my localhost for awhile, and I understand how to use sqlite decently well (I went through as much "learn SQL the hard way" as Zed Shaw has available online. So I understand how to create a database, manipulate it with SQL code etc.

I'm trying to set up mysql to run on my localhost and then run it with django. I've tried to read as much documentation as possible but the documentations or "How to" appear different everywhere I look.

_1. I downloaded the MySQL dmg

_2. I installed both of the packages that come with it (Mac Mavericks)

_3. ...followed some different command line instructions in the documentation...

_4. Did the following on my command line:

Nicholass-MacBook-Air:~ NickStefan$ cd /usr/local/mysql
Nicholass-MacBook-Air:mysql NickStefan$ sudo ./bin/mysqld_safe
140311 18:40:06 mysqld_safe Logging to '/usr/local/mysql-5.6.16-osx10.7-x86_64/data/Nicholass-MacBook-Air.local.err'.
140311 18:40:06 mysqld_safe A mysqld process already exists

_5. So I guess that means I already got the mysql server running. However, I still don't understand how I actually use the mysql in a similar local fashion that I did with sqlite3.

_6. next I looked for _mysql:

Nicholass-MacBook-Air:mysql NickStefan$ dscl .list/Users|grep mysql
Nicholass-MacBook-Air:mysql NickStefan$ sudo dscl .
 > cd Groups
/Groups > list . PrimaryGroupID
...
_mysql                           74

_7. How do I create a mysql database file that I can plug into my django settings on localhost? This is what I have so far in my settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': ????????????????????,
        'USER': '_mysql',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '3360',
    }
}

FULL ANSWER: (so far)

edit my .bash_profile (hidden system file that affects command line start up) to have these additional lines:

alias mysql=/usr/local/mysql/bin/mysql
alias mysqladmin=/usr/local/mysql/bin/mysqladmin

enter into mysql as the root user with (enter blank at the prompting for a password):

mysql -u root -p

create a new user, create the database, grant access:

CREATE USER 'nick'@'localhost' IDENTIFIED BY 'mypass';
CREATE DATABASE mydb;
GRANT ALL ON mydb.* TO 'nick'@'localhost';

settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb'),
        'USER': 'nick',
        'PASSWORD': 'mypass',
        'HOST': 'localhost',
        'PORT': '3360',
    }
}

Add this to .bash_profile

export PATH=$PATH:/usr/local/mysql/bin
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

Re-launch command line and:

pip install mysql-python

finally it worked to run:

python manage.py schemamigration myapp --initial
python manage.py syncdb
./manage.py migrate
1
  • 1
    Just pointing this out, MySQL's default port is 3306, not 3360. Looks like this wasn't holding you back though. Commented May 28, 2015 at 3:43

1 Answer 1

4

As database setup part of the django tutorial says:

If you’re using PostgreSQL or MySQL, make sure you’ve created a database by this point. Do that with “CREATE DATABASE database_name;” within your database’s interactive prompt.

If you’re using SQLite, you don’t need to create anything beforehand - the database file will be created automatically when it is needed.

So, what you need to do now is to create a database via a mysql console. Then, put that name under the NAME key in your default DATABASES setting.

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.