运维开发笔记整理-数据库同步
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.修改Django默认的数据库类型并将表信息同步至修改后的数据库中
1>.修改mysql数据库
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # 连接的数据库类型 'HOST': '127.0.0.1', # 连接数据库的地址 'PORT': 3306, # 端口 'NAME': "devops", # 数据库名称 'USER': 'root', # 用户 'PASSWORD': 'yinzhengjie' # 密码 } }
2>.查看数据库的同步情况
C:UsersyinzhengjiesoftwaresPycharmProjectDevOps>python manage.py showmigrations admin [ ] 0001_initial [ ] 0002_logentry_remove_auto_add auth [ ] 0001_initial [ ] 0002_alter_permission_name_max_length [ ] 0003_alter_user_email_max_length [ ] 0004_alter_user_username_opts [ ] 0005_alter_user_last_login_null [ ] 0006_require_contenttypes_0002 [ ] 0007_alter_validators_add_error_messages [ ] 0008_alter_user_username_max_length contenttypes [ ] 0001_initial [ ] 0002_remove_content_type_name dashboard (no migrations) sessions [ ] 0001_initial C:UsersyinzhengjiesoftwaresPycharmProjectDevOps> C:UsersyinzhengjiesoftwaresPycharmProjectDevOps>python manage.py sqlmigrate sessions 0001 BEGIN; -- -- Create model Session -- CREATE TABLE `django_session` (`session_key` varchar(40) NOT NULL PRIMARY KEY, `session_data` longtext NOT NULL, `expire_date` datetime NOT NULL); CREATE INDEX `django_session_expire_date_a5c62663` ON `django_session` (`expire_date`); COMMIT; C:UsersyinzhengjiesoftwaresPycharmProjectDevOps>
3>.同步数据库信息
4>.再次查看数据库的同步信息
二.使用Pycharm链接数据库
1>.如图所示,选择Database
2>.选择需要链接数据库的类型(我这里使用的是mysql)
3>.配置数据库的链接信息
4>.查看数据库中表的信息
三.使用Django自带的脚本登录数据库(python manage.py dbshell)
其实我们直接执行一条命令就登录到我们之前在settting.py中配置的数据库信息中指定的库啦!
C:UsersyinzhengjiesoftwaresPycharmProjectDevOps> C:UsersyinzhengjiesoftwaresPycharmProjectDevOps>python manage.py dbshell Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 137 Server version: 5.5.62 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show tables; +----------------------------+ | Tables_in_devops | +----------------------------+ | 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) mysql>