1 创建数据库设置访问授权
$ mysql -uroot -p1234@abcd.com -h127.0.0.1
mysql> CREATE DATABASE sandboxOA;
mysql> GRANT ALL ON sandboxOA.* to 'ddadmin'@'%' IDENTIFIED BY '1234@abcd.com';
2 安装mysqlclient
mysqlclient是python3下连接mysql数据库的模块,首先打开CMD窗口进入项目运行的python虚拟环境使用pip install mysqlclient来安装模块
C:\Users\RobbieHan>workon sandbox-env
(sandbox-env) C:\Users\RobbieHan>pip install mysqlclient
windows下安装mysqlclient经常会碰到安装错误,解决办法如下:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python (这个网站还是挺重要的)
访问上面的网址找到和你系统版本相对应的mysqlclient版本,下载到本地然后在本次项目使用的python虚拟环境中安装文件.
我下载的版本是:mysqlclient-1.3.13-cp36-cp36m-win_amd64.whl ,对应的是python3.6 windows 64位
C:\Users\RobbieHan>cd Downloads # 文件现在存放目录,请进入你自己的文件存放目录
C:\Users\RobbieHan\Downloads>workon sandbox-env
(sandbox-env) C:\Users\RobbieHan\Downloads>pip install mysqlclient-1.3.13-cp36-cp36m-win_amd64.whl
# 以下是安装输出的日志
Processing c:\users\robbiehan\downloads\mysqlclient-1.3.13-cp36-cp36m-win_amd64.whl
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.13
3 修改settings.py文件中的数据库配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # 使用mysql数据库
'NAME': 'sandboxOA', # 数据库名称,前面已经创建好的数据库
'HOST': '172.168.100.12', # 数据库连接地址
'USER': 'ddadmin', # 数据库用户名
'PASSWORD': '1234@abcd.com', # 数据库密码
'PORT': '3306' # 数据库端口
}
}
4 运行项目
完成了以上配置,我们就可以将项目运行起来了,打开CMD命令窗口,进入项目目录
C:\Users\RobbieHan>workon sandbox-env
(sandbox-env) C:\Users\RobbieHan>d:
(sandbox-env) D:\>cd PycharmProject/sandboxOA
(sandbox-env) D:\PycharmProject\sandboxOA>python manage.py runserver
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
July 14, 2018 - 20:10:09
Django version 1.11.6, using settings 'sandboxOA.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
这时候我们的项目已经可以正常运行了,提示信息看到,系统访问地址:http://127.0.0.1:8000
注意提示消息中的例外一段话:
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
我们需要运行 python manage.py migrate 将django的基础数据库表更新到数据库中去,回到我们刚才的cmd命令窗口,ctrl+c 来停止项目,输入下面命令
python manage.py migrate
命令执行完成后登录数据库后台,可以看到数据库中已经更细了djang基础数据表:
$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2262
Server version: 5.6.40 MySQL Community Server (GPL)
mysql> USE sandboxOA
Database changed
mysql> show tables;
+----------------------------+
| Tables_in_sandboxOA |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
+----------------------------+
10 rows in set (0.00 sec)
知乎专栏:
Django 学习小组知乎专栏:
SandBox轻量级办公管理系统项目开源地址:
RobbieHan/gistandard