设置Django使用MySQL [英] Setting Django up to use MySQL

查看:142
本文介绍了设置Django使用MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想离开PHP一点,学习Python。为了使用Python进行Web开发,我需要一个框架来帮助模板和其他事情。



我有一个非生产性的服务器,我用来测试所有的Web开发的东西。它是一个Debian 7.1 LAMP堆栈,运行MariaDB而不是常见的MySQL服务器软件包。



昨天我安装了Django,并创建了我的第一个项目,称为 firstweb 。我还没有改变任何设置。



这是我的第一大混乱。在教程中,我跟着安装了Django的人,开始了他的第一个项目,重新启动了Apache,Django刚从那时开始工作。我去了他的浏览器,去了没有问题的Django默认页面。



然而,我必须cd进入我的firstweb文件夹并运行

  python manage.py runserver myip:port 

它的作品。没问题。但是我想知道是否应该这样工作,如果这会导致问题出现?



我的第二个问题是我想设置它,所以它使用我的MySQL数据库。我进入我的settings.py / firstweb / firstweb,我看到ENGINE和NAME,但我不知道该怎么放在这里。



然后在USER中, PASSWORD和HOST区域是我的数据库和凭据?如果我正在使用 localhost ,可以在HOST区域放置 localhost

解决方案>

MySQL支持很容易添加。在您的 DATABASES 字典中,您将有一个这样的条目:

  DATABASES = {
'default':{
'ENGINE':'django.db.backends.mysql',
'NAME':'DB_NAME',
'USER' 'DB_USER',
'PASSWORD':'DB_PASSWORD',
'HOST':'localhost',#或您的数据库托管在
'PORT'上的IP地址:'3306' ,
}
}

您还可以选择使用MySQL 选项文件。您可以通过如下设置 DATABASES 数组来实现此目的:

  DATABASES = {
'default':{
'ENGINE':'django.db.backends.mysql',
'OPTIONS':{
'read_default_file':'/ path /到/ my.cnf',
},
}
}

您还需要创建具有上述类似设置的 /path/to/my.cnf 文件

  [client] 
database = DB_NAME
host = localhost
user = DB_USER
password = DB_PASSWORD
default-character-set = utf8

使用Django 1.7连接的这种新方法,重要的是要知道顺序连接已建立:

  1。选项。 
2. NAME,USER,PASSWORD,HOST,PORT
3. MySQL选项文件。




换句话说,如果您在OPTIONS中设置数据库的名称,这将优先于NAME,这将覆盖MySQL选项文件中的任何内容。







如果您只是在本地机器上测试您的应用程序,可以使用

  python manage.py runserver 

添加 ip:port 参数允许除自己以外的机器访问您的开发应用程序。一旦准备好部署应用程序,我建议您查看部署Django djangobook



Mysql默认字符集通常不是utf-8,因此请确保使用此sql创建数据库:

  CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_bin 

如果您使用 Oracle的MySQL连接器您的 ENGINE 行应如下所示:

 'ENGINE':'mysql.connector.django',


I want to move away from PHP a little and learn Python. In order to do web development with Python I'll need a framework to help with templating and other things.

I have a non-production server that I use to test all of web development stuff on. It is a Debian 7.1 LAMP stack that runs MariaDB instead of the common MySQL-server package.

Yesterday I installed Django and created my first project called firstweb. I have not changed any settings yet.

Here is my first big piece of confusion. In the tutorial I followed the guy installed Django, started his first project, restarted Apache, and Django just worked from then on. He went to his browser and went to the Django default page with no problems.

Me however, I have to cd into my firstweb folder and run

python manage.py runserver myip:port

And it works. No problem. But I'm wondering if it is supposed to work like this, and if this will cause problems down the line?

My second question is that I want to set it up so it uses my MySQL database. I go into my settings.py under /firstweb/firstweb and I see ENGINE and NAME but I'm not sure what to put here.

And then in the USER, PASSWORD, and HOST areas is this my database and its credentials? If I am using localhost can I just put localhost in the HOST area?

解决方案

MySQL support is simple to add. In your DATABASES dictionary, you will have an entry like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'DB_NAME',
        'USER': 'DB_USER',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

You also have the option of utilizing MySQL option files, as of Django 1.7. You can accomplish this by setting your DATABASES array like so:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'read_default_file': '/path/to/my.cnf',
        },
    }
}

You also need to create the /path/to/my.cnf file with similar settings from above

[client]
database = DB_NAME
host = localhost
user = DB_USER
password = DB_PASSWORD
default-character-set = utf8

With this new method of connecting in Django 1.7, it is important to know the order connections are established:

1. OPTIONS.
2. NAME, USER, PASSWORD, HOST, PORT
3. MySQL option files.

In other words, if you set the name of the database in OPTIONS, this will take precedence over NAME, which would override anything in a MySQL option file.


If you are just testing your application on your local machine, you can use

python manage.py runserver

Adding the ip:port argument allows machines other than your own to access your development application. Once you are ready to deploy your application, I recommend taking a look at the chapter on Deploying Django on the djangobook

Mysql default character set is often not utf-8, therefore make sure to create your database using this sql:

CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_bin

If you are using Oracle's MySQL connector your ENGINE line should look like this:

'ENGINE': 'mysql.connector.django',

这篇关于设置Django使用MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆