UU Blog

Django配置MySQL数据库

##更改配置

<project_name>/settings.py 一开始是使用sqlite3的,改成MySQL的就好

1
2
3
4
5
6
7
8
9
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'opencourse',
'USER': 'opencourse',
'PASSWORD': 'opencourse',
'HOST': '172.25.254.10'
}
}

安装驱动

*如果没有安装MySQL驱动的,就pip install MySQL-python 安装一个.如果出现错误,提示没有找到mysql_config的。
Ubuntu类的安装apt install libmysqlclient-dev,Redhat类yum install -y mysql-devel

在确保安装了libmysqlclient-dev或者mysql-devel的同学,如果使用pycharm的话,直接File->Settins->Project Interpreter点加号+ 搜索mysql,然后点击Install Package即可。

生成数据库表

python manage.py makemigrations,或者pycharm中,Tools->Run manage.py Task 直接输入makemigrations,然后migrate。

如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
manage.py@opencourse > makemigrations
No changes detected
manage.py@opencourse > migrate
Operations to perform:
Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying sessions.0001_initial... OK

给作者打一针鸡血