51Testing软件测试论坛

标题: django 连接mysql [打印本页]

作者: 测试积点老人    时间: 2019-1-2 15:25
标题: django 连接mysql
替换默认数据库配置
  1. vi sites/settings.py
  2. ''' 
  3. DATABASES = {
  4.     'default': {
  5.         'ENGINE': 'django.db.backends.sqlite3',
  6.         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  7.     }
  8. }
  9. '''

  10. DATABASES = {
  11.     'default': {
  12.         'ENGINE': 'django.db.backends.mysql',
  13.         'NAME':'django',
  14.         'USER':'root',
  15.         'HOST':'localhost',
  16.         'PASSWORD':'',
  17.         'PORT':'3306',

  18.     }
  19. }
复制代码
设置数据库编码
  1. vi /etc/my.cnf

  2. [mysqld]
  3. datadir=/var/lib/mysql
  4. socket=/var/lib/mysql/mysql.sock
  5. user=mysql
  6. # Disabling symbolic-links is recommended to prevent assorted security risks
  7. symbolic-links=0

  8. character_set_server=utf8

  9. [mysqld_safe]
  10. log-error=/var/log/mysqld.log
  11. pid-file=/var/run/mysqld/mysqld.pid


  12. [client]
  13. default-character_set=utf8
复制代码
创建数据库
  1. mysql -uroot

  2. create database django character set utf8

  3. exit

  4. sudo service mysqld restart
复制代码



作者: Miss_love    时间: 2020-12-28 09:24
学习




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2