热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

mt5mysql数据库_Django使用MySql数据库

Django默认使用的sqlite3,这在实际的生产环境中是不推荐的;1.创建数据库LinuxVM_0_15_centos3.10.0-693.el7.

Django默认使用的sqlite3,这在实际的生产环境中是不推荐的;

1. 创建数据库

Linux VM_0_15_centos 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

1.1. 使用utf8mb4编码

mysql的utf-8编码最多只支持3个字节,而移动端的一些表情都是以4个字节存储的;utf8mb4是一个替代的方案,建议创建数据库和表都以utf8mb4替代utf-8

1.1.1. 确定mysql的配置文件

# 系统中my.cnf文件的位置

[luizyao@VM_0_15_centos ~]$ locate my.cnf

/etc/my.cnf

/etc/my.cnf.d

/etc/my.cnf.d/client.cnf

/etc/my.cnf.d/mysql-clients.cnf

/etc/my.cnf.d/server.cnf

# mysql启动时,读取配置文件的目录顺序

[luizyao@VM_0_15_centos ~]$ mysql --help | head -n 7

mysql Ver 15.1 Distrib 5.5.56-MariaDB, for Linux (x86_64) using readline 5.1

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Usage: mysql [OPTIONS] [database]

Default options are read from the following files in the given order:

/etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf

1.1.2. 修改配置文件

/etc/my.cnf

[client]

default-character-set = utf8mb4

[mysql]

default-character-set = utf8mb4

[mysqld]

# Settings user and group are ignored when systemd is used.

# If you need to run mysqld under a different user or group,

# customize your systemd unit file for mariadb according to the

# instructions in http://fedoraproject.org/wiki/Systemd

character-set-client-handshake = FALSE

character-set-server = utf8mb4

collation-server = utf8mb4_unicode_ci

init_connect='SET NAMES utf8mb4'

1.1.3. 重启数据库服务,检查相关字段

# 保证character_set_client、character_set_connection、character_set_database、character_set_results和character_set_server的值一定是utf8mb4

MariaDB [(none)]> SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';

+--------------------------+----------------------------+

| Variable_name | Value |

+--------------------------+----------------------------+

| character_set_client | utf8mb4 |

| character_set_connection | utf8mb4 |

| character_set_database | utf8mb4 |

| character_set_filesystem | binary |

| character_set_results | utf8mb4 |

| character_set_server | utf8mb4 |

| character_set_system | utf8 |

| character_sets_dir | /usr/share/mysql/charsets/ |

| collation_connection | utf8mb4_unicode_ci |

| collation_database | utf8mb4_unicode_ci |

| collation_server | utf8mb4_unicode_ci |

+--------------------------+----------------------------+

11 rows in set (0.02 sec)

1.1.4. 新建数据库

MariaDB [(none)]> create database blogproject;

Query OK, 1 row affected (0.01 sec)

--查看blogproject创建时候使用的编码,回显中注释的部分可以看出,使用的是utf8mb4编码

MariaDB [mysql]> show create database blogproject;

+-------------+----------------------------------------------------------------------------------------------------+

| Database | Create Database |

+-------------+----------------------------------------------------------------------------------------------------+

| blogproject | CREATE DATABASE `blogproject` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */ |

+-------------+----------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

1.2. 使用已经存在的数据库

1.2.1. 修改已有数据库的编码

MariaDB [(none)]> alter database blogproject character set utf8mb4;

1.3. 为Django项目新建一个数据库用户

-- 赋予这个新用户增删改查等权限,不授予drop的权限;并且,只允许本地客户端登陆;

MariaDB [mysql]> grant alter,create,delete,index,insert,select,update,trigger on blogproject.* to @localhost identified by '';

Query OK, 0 rows affected (0.04 sec)

MariaDB [mysql]> flush privileges;

Query OK, 0 rows affected (0.03 sec)

-- 检查权限,秘密默认是加密存储

MariaDB [blogproject]> show grants for @localhost;

+----------------------------------------------------------------------------------------------------------------+

| Grants for @localhost |

+----------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO ''@'localhost' IDENTIFIED BY PASSWORD '*5102144CA406FC026831D796EA07645447677551' |

| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, ALTER, TRIGGER ON `blogproject`.* TO ''@'localhost' |

+----------------------------------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)

2. 修改Django的配置

2.1. 修改settings.py中数据库相关

DATABASES = {

# 'default': {

# 'ENGINE': 'django.db.backends.sqlite3',

# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

# }

'default': {

'ENGINE': 'django.db.backends.mysql',

'NAME': 'blogproject',

'USER': '',

'PASSWORD': '',

'HOST': '',

'PORT': '3306', # 默认的服务端口号

'OPTIONS': {

# 存储引擎启用严格模式,非法数据值被拒绝

'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",

'charset': 'utf8mb4',

},

}

}

Darwin luizyaodeMacBook-Air.local 18.6.0 Darwin Kernel Version 18.6.0: Thu Apr 25 23:16:27 PDT 2019; root:xnu-4903.261.4~2/RELEASE_X86_64 x86_64

2.2.1. 安装mysql-connector-c

luizyaodeMacBook-Air:~ luizyao$ brew install mysql-connector-c

==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/mysql-conne

######################################################################## 100.0%

==> Pouring mysql-connector-c-6.1.11.mojave.bottle.tar.gz

🍺 /usr/local/Cellar/mysql-connector-c/6.1.11: 79 files, 15.3MB

2.2.2. 修复mysql-connector-c在mac os的python3的bug

/usr/local/Cellar/mysql-connector-c/6.1.11/bin/mysql_config

# Create options

libs="-L$pkglibdir"

libs="$libs -l "

修改为

# Create options

libs="-L$pkglibdir"

libs="$libs -lmysqlclient -lssl -lcrypto"

2.2.3. 使用pip安装mysqlclient

[luizyaodeMacBook-Air:django-blog luizyao$ pip3 install mysqlclient

2.2.4. 使用pipenv安装mysqlclient

这个时候会报错,因为:because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

luizyaodeMacBook-Air:django-blog luizyao$ brew info openssl

openssl: stable 1.0.2s (bottled) [keg-only]

SSL/TLS cryptography library

https://openssl.org/

/usr/local/Cellar/openssl/1.0.2s (1,795 files, 12.0MB)

Poured from bottle on 2019-06-22 at 13:16:17

From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/openssl.rb

==> Caveats

A CA file has been bootstrapped using certificates from the SystemRoots

keychain. To add additional certificates (e.g. the certificates added in

the System keychain), place .pem files in

/usr/local/etc/openssl/certs

and run

/usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,

because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:

echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl you may need to set:

export LDFLAGS="-L/usr/local/opt/openssl/lib"

export CPPFLAGS="-I/usr/local/opt/openssl/include"

==> Analytics

install: 490,905 (30 days), 1,748,362 (90 days), 6,591,368 (365 days)

install_on_request: 59,162 (30 days), 234,123 (90 days), 884,807 (365 days)

build_error: 0 (30 days)

根据提示做如下操作

luizyaodeMacBook-Air:django-blog luizyao$ echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

luizyaodeMacBook-Air:django-blog luizyao$ source ~/.bash_profile

luizyaodeMacBook-Air:django-blog luizyao$ export LDFLAGS="-L/usr/local/opt/openssl/lib"

luizyaodeMacBook-Air:django-blog luizyao$ export CPPFLAGS="-I/usr/local/opt/openssl/include"

再安装mysqlclient,就能成功了

luizyaodeMacBook-Air:django-blog luizyao$ pipenv install mysqlclient

Installing mysqlclient…

Adding mysqlclient to Pipfile's [packages]…

✔ Installation Succeeded

Pipfile.lock (cee3a5) out of date, updating to (79d06d)…

Locking [dev-packages] dependencies…

✔ Success!

Locking [packages] dependencies…

✔ Success!

Updated Pipfile.lock (cee3a5)!

Installing dependencies from Pipfile.lock (cee3a5)…

🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 4/4 — 00:00:01

To activate this project's virtualenv, run pipenv shell.

Alternatively, run a command inside the virtualenv with pipenv run.

2.3. 执行migrate操作

[luizyaodeMacBook-Air:django-blog luizyao$ pipenv run python manage.py migrate

Operations to perform:

Apply all migrations: admin, auth, blog, contenttypes, sessions

Running migrations:

Applying contenttypes.0001_initial... OK

Applying auth.0001_initial... OK

Applying admin.0001_initial... OK

Applying admin.0002_logentry_remove_auto_add... OK

Applying admin.0003_logentry_add_action_flag_choices... 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 auth.0008_alter_user_username_max_length... OK

Applying auth.0009_alter_user_last_name_max_length... OK

Applying auth.0010_alter_group_name_max_length... OK

Applying auth.0011_update_proxy_permissions... OK

Applying blog.0001_initial... OK

Applying sessions.0001_initial... OK

只有Applying blog.0001_initial... OK是和我们自己模型相关的,其他的是Django系统自带的一些模型, 我们可以进一步的查看数据库到底做了什么操作;

luizyaodeMacBook-Air:django-blog luizyao$ pipenv run python manage.py sqlmigrate blog 0001

BEGIN;

--

-- Create model Category

--

CREATE TABLE `blog_category` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(100) NOT NULL);

--

-- Create model Tag

--

CREATE TABLE `blog_tag` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(100) NOT NULL);

--

-- Create model Post

--

CREATE TABLE `blog_post` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `title` varchar(70) NOT NULL, `excerpt` varchar(200) NOT NULL, `body` longtext NOT NULL, `created_at` datetime(6) NOT NULL, `modified_at` datetime(6) NOT NULL, `author_id` integer NOT NULL, `category_id` integer NOT NULL);

CREATE TABLE `blog_post_tag` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `post_id` integer NOT NULL, `tag_id` integer NOT NULL);

ALTER TABLE `blog_post` ADD CONSTRAINT `blog_post_author_id_dd7a8485_fk_auth_user_id` FOREIGN KEY (`author_id`) REFERENCES `auth_user` (`id`);

ALTER TABLE `blog_post` ADD CONSTRAINT `blog_post_category_id_c326dbf8_fk_blog_category_id` FOREIGN KEY (`category_id`) REFERENCES `blog_category` (`id`);

ALTER TABLE `blog_post_tag` ADD CONSTRAINT `blog_post_tag_post_id_a5c00319_fk_blog_post_id` FOREIGN KEY (`post_id`) REFERENCES `blog_post` (`id`);

ALTER TABLE `blog_post_tag` ADD CONSTRAINT `blog_post_tag_tag_id_2bbd31e4_fk_blog_tag_id` FOREIGN KEY (`tag_id`) REFERENCES `blog_tag` (`id`);

ALTER TABLE `blog_post_tag` ADD CONSTRAINT `blog_post_tag_post_id_tag_id_ba2a5f83_uniq` UNIQUE (`post_id`, `tag_id`);

COMMIT;

在数据库中可以看到Django创建的具体表;

MariaDB [blogproject]> show tables;

+----------------------------+

| Tables_in_blogproject |

+----------------------------+

| auth_group |

| auth_group_permissions |

| auth_permission |

| auth_user |

| auth_user_groups |

| auth_user_user_permissions |

| blog_category |

| blog_post |

| blog_post_tag |

| blog_tag |

| django_admin_log |

| django_content_type |

| django_migrations |

| django_session |

+----------------------------+

14 rows in set (0.00 sec)

2.4. 创建一个管理员用户

luizyaodeMacBook-Air:django-blog luizyao$ pipenv run python manage.py createsuperuser

用户名 (leave blank to use 'luizyao'): luizyao

电子邮件地址: luizyao@163.com

Password:

Password (again):

Superuser created successfully.

在数据库中,我们就可以看到这个管理员用户了

MariaDB [blogproject]> select * from auth_user;

+----+--------------------------------------------------------------------------------+------------+--------------+----------+------------+-----------+-----------------+----------+-----------+----------------------------+

| id | password | last_login | is_superuser | username | first_name | last_name | email | is_staff | is_active | date_joined |

+----+--------------------------------------------------------------------------------+------------+--------------+----------+------------+-----------+-----------------+----------+-----------+----------------------------+

| 1 | pbkdf2_sha256$150000$ViP2waofsEQU$3oNPdGxlGPmt5Nbl/lcHJli8V9j7425ZxRfqKF18E0Q= | NULL | 1 | luizyao | | | luizyao@163.com | 1 | 1 | 2019-08-25 03:49:19.667011 |

+----+--------------------------------------------------------------------------------+------------+--------------+----------+------------+-----------+-----------------+----------+-----------+----------------------------+

1 row in set (0.00 sec)



推荐阅读
  • 安装mysqlclient失败解决办法
    本文介绍了在MAC系统中,使用django使用mysql数据库报错的解决办法。通过源码安装mysqlclient或将mysql_config添加到系统环境变量中,可以解决安装mysqlclient失败的问题。同时,还介绍了查看mysql安装路径和使配置文件生效的方法。 ... [详细]
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • Linux如何安装Mongodb的详细步骤和注意事项
    本文介绍了Linux如何安装Mongodb的详细步骤和注意事项,同时介绍了Mongodb的特点和优势。Mongodb是一个开源的数据库,适用于各种规模的企业和各类应用程序。它具有灵活的数据模式和高性能的数据读写操作,能够提高企业的敏捷性和可扩展性。文章还提供了Mongodb的下载安装包地址。 ... [详细]
  • PeopleSoft安装镜像版本及导入语言包的方法
    本文介绍了PeopleSoft安装镜像的两个版本,分别是VirtualBox虚拟机版本和NativeOS版本,并详细说明了导入语言包的方法。对于Windows版本,可以通过psdmt.exe登录进入,并使用datamover脚本导入语言包。对于Linux版本,同样可以使用命令行方式执行datamover脚本导入语言包。导入语言包后,可以实现多种语言的登录。参考文献提供了相关链接以供深入了解。 ... [详细]
  • PatchODAX8: ... [详细]
  • 本文介绍了一种轻巧方便的工具——集算器,通过使用集算器可以将文本日志变成结构化数据,然后可以使用SQL式查询。集算器利用集算语言的优点,将日志内容结构化为数据表结构,SPL支持直接对结构化的文件进行SQL查询,不再需要安装配置第三方数据库软件。本文还详细介绍了具体的实施过程。 ... [详细]
  • centos安装Mysql的方法及步骤详解
    本文介绍了centos安装Mysql的两种方式:rpm方式和绿色方式安装,详细介绍了安装所需的软件包以及安装过程中的注意事项,包括检查是否安装成功的方法。通过本文,读者可以了解到在centos系统上如何正确安装Mysql。 ... [详细]
  • 代理模式的详细介绍及应用场景
    代理模式是一种在软件开发中常用的设计模式,通过在客户端和目标对象之间增加一层中间层,让代理对象代替目标对象进行访问,从而简化系统的复杂性。代理模式可以根据不同的使用目的分为远程代理、虚拟代理、Copy-on-Write代理、保护代理、防火墙代理、智能引用代理和Cache代理等几种。本文将详细介绍代理模式的原理和应用场景。 ... [详细]
  • 本文介绍了关系型数据库和NoSQL数据库的概念和特点,列举了主流的关系型数据库和NoSQL数据库,同时描述了它们在新闻、电商抢购信息和微博热点信息等场景中的应用。此外,还提供了MySQL配置文件的相关内容。 ... [详细]
  • 在Windows10系统上使用VMware创建CentOS虚拟机的详细步骤教程
    本文详细介绍了在Windows10系统上使用VMware创建CentOS虚拟机的步骤,包括准备条件、安装VMware、下载CentOS ISO文件、创建虚拟机并进行自定义配置、设置虚拟机的ISO与网络、进行安装和配置等。通过本文的指导,读者可以轻松地创建自己的CentOS虚拟机并进行相应的配置和操作。 ... [详细]
  • DataGrip 初探:如何离线安装数据库驱动
    一、引言在日常的工作中,难免会与多个数据库打交道。此时,一个能够帮助我们管理多个数据库连接的软件就非常必要了,在我从事程序员的日子里,用过了NavicatforMySQL、DbVi ... [详细]
  • 本文内容皆为作者原创,如需转载,请注明出处:https:www.cnblogs.comxuexianqip13045462.html1.自定义分页器的拷贝及使用当我们需要使用 ... [详细]
  • 新注册腾讯云最长可免费使用CVM服务器半年
    腾讯旗下的云计算业务目前开始推出面向新用户的免费套餐活动,最长每位用户可免费使用CVM服务器半 ... [详细]
  • 【BUUCTF】[极客大挑战 2019]LoveSQL 详细题解总结笔记 Writeup
    【BUUCTF】[极客大挑战2019]LoveSQL一.SQL注入考点二.解题过程0.存在SQL注入1.万能密码adminor112.爆字段3.看回显4.爆数据库5.爆数据库的表6 ... [详细]
author-avatar
zr8744814
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有