首页 文章

将Django设置为使用MySQL

提问于
浏览
140

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

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

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

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

但是,我必须进入我的firstweb文件夹并运行

python manage.py runserver myip:port

它有效 . 没问题 . 但是我想知道它是否应该像这样工作,如果这会引起问题呢?

我的 second question 是我想要设置它所以它使用我的MySQL数据库 . 我进入/ firstweb / firstweb下的settings.py,我看到ENGINE和NAME,但我不确定要放在这里 .

然后在USER,PASSWORD和HOST区域是我的数据库及其凭据?如果我使用localhost,我可以将localhost放在HOST区域吗?

10 回答

  • 243

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

    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',
        }
    }
    

    您也可以选择使用MySQL option files,从Django 1.7开始 . 您可以通过设置 DATABASES 数组来完成此操作:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'OPTIONS': {
                'read_default_file': '/path/to/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. OPTIONS.
    2. NAME, USER, PASSWORD, HOST, PORT
    3. MySQL option files.
    

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


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

    python manage.py runserver
    

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

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

    CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_bin
    

    如果您使用Oracle's MySQL connector,您的 ENGINE 行应如下所示:

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

    首先请运行以下命令来安装python依赖项,否则python runserver命令将抛出错误 .

    sudo apt-get install libmysqlclient-dev
    sudo pip install MySQL-python
    

    然后配置#Andy定义的settings.py文件,并在最后一次执行时:

    python manage.py runserver
    

    玩得开心..!!

  • 13

    如上所述,您可以从https://www.apachefriends.org/download.html轻松安装xampp然后按照说明操作:

    • http://www.unixmen.com/install-xampp-stack-ubuntu-14-04/安装并运行xampp,然后从GUI启动Apache Web Server和MySQL数据库 .

    • 您可以根据需要配置Web服务器,但默认情况下,Web服务器位于 http://localhost:80 ,数据库位于 port 3306 ,PhpMyadmin位于 http://localhost/phpmyadmin/

    • 从这里您可以看到您的数据库并使用非常友好的GUI访问它们 .

    • 创建要在Django项目中使用的任何数据库 .

    • 编辑您的 settings.py 文件,如:

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'DB_NAME',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'USER': 'root',
        'PASSWORD': '',
    }}
    
    • 在virtualenv中安装以下软件包(如果你在virtualenv上使用django,这是更优选的):

    sudo apt-get install libmysqlclient-dev

    pip安装MySQL-python

    • 就是这样!!您已经以非常简单的方式使用MySQL配置了Django .

    • 现在运行你的Django项目:

    python manage.py migrate

    python manage.py runserver

  • 23

    如果您使用的是python3.x,请运行以下命令

    pip install mysqlclient
    

    然后更改setting.py之类的

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'DB',
         'USER': 'username',
        'PASSWORD': 'passwd',
      }
      }
    
  • 3

    实际上,不同的环境有许多问题,python版本等等 . 您可能还需要安装python dev文件,因此要“强制”安装我将运行所有这些:

    sudo apt-get install python-dev python3-dev
    sudo apt-get install libmysqlclient-dev
    pip install MySQL-python
    pip install pymysql
    pip install mysqlclient
    

    你应该好好接受公认的答案 . 如果这对你很重要,可以删除不必要的包裹 .

  • 9

    Andy的回答有帮助,但是如果你担心在django设置中暴露你的数据库密码,我建议在mysql连接上关注django官方配置:https://docs.djangoproject.com/en/1.7/ref/databases/

    在这里引用为:

    # settings.py
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'OPTIONS': {
                'read_default_file': '/path/to/my.cnf',
            },
        }
    }
    
    
    # my.cnf
    [client]
    database = NAME
    user = USER
    password = PASSWORD
    default-character-set = utf8
    

    要在设置中替换'HOST':'127.0.0.1',只需将其添加到my.cnf:

    # my.cnf
    [client]
    database = NAME
    host = HOST NAME or IP
    user = USER
    password = PASSWORD
    default-character-set = utf8
    

    另一个有用的OPTION是设置您的存储引擎对于django,您可能需要在您的setting.py中:

    'OPTIONS': {
       'init_command': 'SET storage_engine=INNODB',
    }
    
  • 3

    Run these commands

    sudo apt-get install python-dev python3-dev

    sudo apt-get install libmysqlclient-dev

    pip安装MySQL-python

    pip install pymysql

    pip install mysqlclient

    Then configure settings.py like

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'django_db',
            'HOST': '127.0.0.1',
            'PORT': '3306',
            'USER': 'root',
            'PASSWORD': '123456',
        }
    }
    

    Enjoy mysql connection

  • 1

    settings.py

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django',
        'USER': 'root',
        'PASSWORD': '*****',
        'HOST': '***.***.***.***',
        'PORT': '3306',
        'OPTIONS': {
            'autocommit': True,
        },
    }
    

    }

    然后:

    python manage.py migrate
    

    如果成功将生成这些表:

    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
    

    你将可以使用mysql .

    这是一个展示示例,测试Django版本1.11.5:Django-pool-showcase

  • 8

    按照给定的步骤设置它以使用MySQL数据库:

    1) Install MySQL Database Connector :
    
        sudo apt-get install libmysqlclient-dev
    
    2) Install the mysqlclient library :
    
        pip install mysqlclient
    
    3) Install MySQL server, with the following command :
    
        sudo apt-get install mysql-server
    
    4) Create the Database :
    
        i) Verify that the MySQL service is running:
    
            systemctl status mysql.service
    
        ii) Log in with your MySQL credentials using the following command where -u is the flag for declaring your username and -p is the flag that tells MySQL that this user requires a password :  
    
            mysql -u db_user -p
    
    
        iii) CREATE DATABASE db_name;
    
        iv) Exit MySQL server, press CTRL + D.
    
    5) Add the MySQL Database Connection to your Application:
    
        i) Navigate to the settings.py file and replace the current DATABASES lines with the following:
    
            # Database
            # https://docs.djangoproject.com/en/2.0/ref/settings/#databases
    
            DATABASES = {
                'default': {
                    'ENGINE': 'django.db.backends.mysql',
                    'OPTIONS': {
                        'read_default_file': '/etc/mysql/my.cnf',
                    },
                }
            }
            ...
    
        ii) Next, let’s edit the config file so that it has your MySQL credentials. Use vi as sudo to edit the file and add the following information:
    
            sudo vi /etc/mysql/my.cnf
    
            database = db_name
            user = db_user
            password = db_password
            default-character-set = utf8
    
    6) Once the file has been edited, we need to restart MySQL for the changes to take effect :
    
        systemctl daemon-reload
    
        systemctl restart mysql
    
    7) Test MySQL Connection to Application:
    
        python manage.py runserver your-server-ip:8000
    
  • 3

    您必须首先创建一个MySQL数据库 . 然后转到 settings.py 文件并使用您的MySQL凭据编辑 'DATABASES' 字典:

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

    这是一个完整的安装指南,用于设置Django在virtualenv上使用MySQL:

    http://codex.themedelta.com/how-to-install-django-with-mysql-in-a-virtualenv-on-linux/

相关问题