먼저 mysql을 설치합니다. 윈도우, 리눅스, 맥 각자 버전에 맞는 mysql을 설치하면 됩니다.
참고로 실제 서비스에서는 웹 서버와 디비 서버는 반드시 별도의 물리서버로 분리 운영되어야 하고, 디비의 2중화와 백업도 거의 필수적입니다.
초보분들은 유튜브의 '이것이 MySQL이다' 를 보시는 것도 좋습니다.
- 다운로드 및 설치
mac의 경우 설치 중간에 root 패스워드를 알려주는데 잃어버릴 경우 리셋이 매우 어렵습니다. 반드시 저장을 해 놓는 게 좋습니다.
- 루트 사용자 로그인
$ mysql -u root -p
- 데이터베이스 생성
- 사용자 생성: 아이디 honux 이고 password가 cs1234인 사용자 생성
- 데이터베이스에 권한 부여
> create database djangodb character set utf8;
> create user 'honux'@'%' identified by 'cs1234';
> grant all on djangodb.* to 'honux'@'%';
> flush privileges;
pip install mysqlclient
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/Users/honux/my.cnf',
},
}
}
# my.cnf
[client]
database = djangodb
user = honux
password = cs1234
default-character-set = utf8
$ python manage.py makemigrations
$ python manage.py migrate
$ mysql -u honbux -p
> use djangodb
> show tables;
$ python manage.py createsuperuser
https://docs.djangoproject.com/en/1.10/ref/databases/#mysql-db-api-drivers