1.数据迁移
不同的app连接不同的数据库(例子中blog用默认的数据库,blog4用MySQL数据库)
(1)要连接MySQL数据库,首先下载MySQL数据库和MySQLdb模块
下载MySQL数据库的参考连接:
http://www.cnblogs.com/onlycxue/p/3291889.html
而MySQLdb模块安装参考连接:https://pypi.python.org/pypi/MySQL-
python/1.2.4 (我是下载的.exe)
(2)接下来就是新建django的工程什么的就不详细讲了(我的工程里有blog和blog4两个app
)
(3)修改setting.py配置文件
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.sqlite3‘,
‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘),
},
‘db2‘: {
‘ENGINE‘:‘django.db.backends.mysql‘,
‘NAME‘:‘dbname2‘,
‘USER‘:‘root‘,
‘PASSWORD‘:‘5302613‘,
‘HOST‘:"127.0.0.1",
},
}
DATABASE_ROUTERS = [‘mysite.database_router.DatabaseAppsRouter‘]
DATABASE_APPS_MAPPING = {
‘blog4‘: ‘db2‘,
}
(4)blog4的models.py文件的代码都是如下:
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Blog4(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
def __unicode__(self):
return self.title
(5)在工程的文件夹中的同名文件夹mysite.py中添加一个database_router.py文件
代码在这个网站复制:http://www.ziqiangxuetang.com/django/django-multi-
database.html
(7)进入到工程的目录,输入一下命令:
>>>python manage.py makemigrations
Migrations for ‘blog4‘:
blog4\migrations\0001_initial.py:
- Create model Blog4
>>>mysql -u root -p; (进入MySQL数据库)
>>>create database dbname2;
mysql> create database dbname2;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dbname1 |
| dbname2 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
可以看到我建立的dbname2数据库成功了
>>>python manage.py migrate --database=db2 (为blog3同步到MySQL数据库)
Operations to perform:
Apply all migrations: admin, auth, blog, blog2, blog3, blog4, contenttypes, se
ssions
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 auth.0008_alter_user_username_max_length... OK
Applying blog.0001_initial... OK
Applying blog2.0001_initial... OK
Applying blog3.0001_initial... OK
Applying blog4.0001_initial... OK
Applying blog4.0002_auto_20160323_1429... OK
Applying sessions.0001_initial... OK
看到上面,证明同步到MySQL数据库成功了!!!
进入到MySQL数据库看看是否成功:
mysql> use dbname2;
Database changed
mysql> show tables;
+----------------------------+
| Tables_in_dbname2 |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| blog2_blog2 |
| blog3_blog3 |
| blog4_blog4 |
| blog_blog |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
+----------------------------+
14 rows in set (0.00 sec)
这次同步数据库建立的表是blog4_blog4,可以看到是成功的
(8)操作一下数据库看看能否插入数据
>python manage.py shell
>>> from blog4.models import Blog4
>>> Blog4.objects.create(title="title 1",content="content 1")
<Blog4: title 1>
在去到数据库下看看:
mysql> SELECT * FROM blog4_blog4;
+----+---------+-----------+
| id | title | content |
+----+---------+-----------+
| 1 | title 1 | content 1 |
+----+---------+-----------+
1 row in set (0.00 sec)
呵呵,插入成功,搞定收工.....
有些步骤省略了,将就着看
参考连接:http://www.cnblogs.com/fengri/articles/django5.html