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.
我想从PHP中移开一点,学习Python。为了使用Python进行web开发,我需要一个框架来帮助进行模板和其他事情。
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.
我有一个非生产服务器,用来测试所有web开发的东西。它是一个Debian 7.1 LAMP堆栈,运行MariaDB而不是普通的MySQL-server包。
Yesterday I installed Django and created my first project called firstweb. I have not changed any settings yet.
昨天我安装了Django并创建了我的第一个项目firstweb。我还没有改变任何设置。
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.
这是我的第一大困惑。在本教程中,我跟踪了安装Django的人,启动了他的第一个项目,重新启动了Apache,而Django从那时起就开始工作了。他去了他的浏览器,去了Django默认页面没有问题。
Me however, I have to cd into my firstweb folder and run
但是,我必须将cd放入我的firstweb文件夹并运行。
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.
我的第二个问题是,我想设置它,它使用我的MySQL数据库。我进入我的设置。py在/firstweb/firstweb上,我看到了引擎和名字,但我不知道在这里放什么。
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?
然后在用户,密码和主机区域是我的数据库和它的凭证?如果我使用localhost,我可以把localhost放在主机区域吗?
9 个解决方案
#1
224
MySQL support is simple to add. In your DATABASES
dictionary, you will have an entry like this:
MySQL支持很容易添加。在数据库字典中,将有这样的条目:
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:
您还可以选择使用MySQL选项文件,如Django 1.7。您可以通过设置这样的数据库数组来实现这一点:
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
您还需要从上面创建/path/to/my.cnf文件。
[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:
有了这种连接Django 1.7的新方法,很重要的一点是要知道订单连接的建立:
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.
换句话说,如果您在选项中设置数据库的名称,这将优先于名称,它将覆盖MySQL选项文件中的任何内容。
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
添加ip:端口参数允许除您自己之外的机器访问您的开发应用程序。一旦准备好部署应用程序,我建议您看一下关于在djangobook上部署Django的章节。
Mysql default character set is often not utf-8, therefore make sure to create your database using this sql:
Mysql默认字符集通常不是utf-8,因此请确保使用此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:
如果你使用Oracle的MySQL连接器,你的引擎线应该是这样的:
'ENGINE': 'mysql.connector.django',
#2
20
To the very first please run the below commands to install python dependencies otherwise python runserver command will throw error.
首先,请运行下面的命令来安装python依赖项,否则python runserver命令将抛出错误。
sudo apt-get install libmysqlclient-dev
sudo pip install MySQL-python
Then configure the settings.py file as defined by #Andy and at the last execute :
然后配置设置。由#Andy定义的py文件和最后一次执行:
python manage.py runserver
Have fun..!!
玩得开心! !
#3
12
As all said above, you can easily install xampp first from https://www.apachefriends.org/download.html Then follow the instructions as:
如上所述,您可以很容易地从https://www.apachefriends.org/download.html安装xampp,然后按照以下说明执行:
- Install and run xampp from http://www.unixmen.com/install-xampp-stack-ubuntu-14-04/, then start Apache Web Server and MySQL Database from the GUI.
- 安装并运行xampp,从http://www.unixmen.com/install-xampp-stack-ubuntu-14-04/,然后从GUI启动Apache Web服务器和MySQL数据库。
- You can configure your web server as you want but by default web server is at
http://localhost:80
and database atport 3306
, and PhpMyadmin athttp://localhost/phpmyadmin/
- 您可以根据需要配置您的web服务器,但是默认的web服务器位于http://localhost:80和port3306的数据库,而PhpMyadmin位于http://localhost/phpmyadmin/。
- From here you can see your databases and access them using very friendly GUI.
- 从这里您可以看到您的数据库并使用非常友好的GUI访问它们。
- Create any database which you want to use on your Django Project.
- 创建您想在Django项目上使用的任何数据库。
-
Edit your
settings.py
file like:编辑您的设置。py文件:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'DB_NAME', 'HOST': '127.0.0.1', 'PORT': '3306', 'USER': 'root', 'PASSWORD': '', }}
-
Install the following packages in the virtualenv (if you're using django on virtualenv, which is more preferred):
在virtualenv中安装以下包(如果您使用的是virtualenv的django,则更可取):
sudo apt-get install libmysqlclient-dev
sudo apt-get安装libmysqlclient-dev
pip install MySQL-python
pip安装MySQL-python
-
That's it!! you have configured Django with MySQL in a very easy way.
就是这样! !您已经用一种非常简单的方式配置了Django和MySQL。
-
Now run your Django project:
现在运行Django项目:
python manage.py migrate
python管理。py迁移
python manage.py runserver
python管理。py runserver
#4
9
If you are using python3.x then Run below command
如果你使用的是python3。然后运行以下命令。
pip install mysqlclient
Then change setting.py like
然后改变设置。py像
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB',
'USER': 'username',
'PASSWORD': 'passwd',
}
}
#5
8
Actually, there are many issues with different environments, python versions, so on. You might also need to install python dev files, so to 'brute-force' the installation I would run all of these:
实际上,在不同的环境、python版本等方面存在许多问题。您可能还需要安装python dev文件,因此对于“brute-force”的安装,我将运行所有这些:
sudo apt-get install python-dev python3-dev
sudo apt-get install libmysqlclient-dev
pip install MySQL-python
pip install pymysql
pip install mysqlclient
You should be good to go with the accepted answer. And can remove the unnecessary packages if that's important to you.
你应该接受这个公认的答案。如果这对你很重要的话,可以删除不必要的包。
#6
3
Andy's answer helps but if you have concern on exposing your database password in your django setting, I suggest to follow django official configuration on mysql connection: https://docs.djangoproject.com/en/1.7/ref/databases/
Andy的回答很有帮助,但是如果您担心在django设置中暴露您的数据库密码,我建议在mysql连接上使用django官方配置:https://docs.djangoproject.com/en/1.7/ref/databases/。
Quoted here as:
引用在这里:
# 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
To replace 'HOST': '127.0.0.1' in setting, simply add it in my.cnf:
在设置中替换“主机”:“127.0.0.1”,只需将其添加到my.cnf:
# my.cnf
[client]
database = NAME
host = HOST NAME or IP
user = USER
password = PASSWORD
default-character-set = utf8
Another OPTION that is useful, is to set your storage engine for django, you might want it in your setting.py:
另一个有用的选项是为django设置存储引擎,您可能希望它在您的设置中。
'OPTIONS': {
'init_command': 'SET storage_engine=INNODB',
}
#7
3
Run these commands
运行这些命令
sudo apt-get install python-dev python3-dev
sudo apt-get安装python-dev python3-dev。
sudo apt-get install libmysqlclient-dev
sudo apt-get安装libmysqlclient-dev
pip install MySQL-python
pip安装MySQL-python
pip install pymysql
pip安装pymysql
pip install mysqlclient
pip安装mysqlclient
Then configure settings.py like
然后配置设置。py像
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': '123456',
}
}
Enjoy mysql connection
享受mysql连接
#8
1
settings.py
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER': 'root',
'PASSWORD': '*****',
'HOST': '***.***.***.***',
'PORT': '3306',
'OPTIONS': {
'autocommit': True,
},
}
}
}
then:
然后:
python manage.py migrate
if success will generate theses tables:
如果成功将产生这些表格:
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
and u will can use mysql.
你可以使用mysql。
this is a showcase example ,test on Django version 1.11.5: Django-pool-showcase
这是一个展示示例,测试Django版本1.11.5:Django- poolt -橱窗。
#9
0
You must create a MySQL database first. Then go to settings.py
file and edit the 'DATABASES'
dictionary with your MySQL credentials:
首先必须创建一个MySQL数据库。然后去设置。py文件并使用MySQL证书编辑“数据库”字典:
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',
}
}
Here is a complete installation guide for setting up Django to use MySQL on a virtualenv:
这里有一个完整的安装指南,用于设置Django,以便在virtualenv上使用MySQL:
http://codex.themedelta.com/how-to-install-django-with-mysql-in-a-virtualenv-on-linux/
http://codex.themedelta.com/how-to-install-django-with-mysql-in-a-virtualenv-on-linux/
#1
224
MySQL support is simple to add. In your DATABASES
dictionary, you will have an entry like this:
MySQL支持很容易添加。在数据库字典中,将有这样的条目:
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:
您还可以选择使用MySQL选项文件,如Django 1.7。您可以通过设置这样的数据库数组来实现这一点:
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
您还需要从上面创建/path/to/my.cnf文件。
[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:
有了这种连接Django 1.7的新方法,很重要的一点是要知道订单连接的建立:
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.
换句话说,如果您在选项中设置数据库的名称,这将优先于名称,它将覆盖MySQL选项文件中的任何内容。
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
添加ip:端口参数允许除您自己之外的机器访问您的开发应用程序。一旦准备好部署应用程序,我建议您看一下关于在djangobook上部署Django的章节。
Mysql default character set is often not utf-8, therefore make sure to create your database using this sql:
Mysql默认字符集通常不是utf-8,因此请确保使用此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:
如果你使用Oracle的MySQL连接器,你的引擎线应该是这样的:
'ENGINE': 'mysql.connector.django',
#2
20
To the very first please run the below commands to install python dependencies otherwise python runserver command will throw error.
首先,请运行下面的命令来安装python依赖项,否则python runserver命令将抛出错误。
sudo apt-get install libmysqlclient-dev
sudo pip install MySQL-python
Then configure the settings.py file as defined by #Andy and at the last execute :
然后配置设置。由#Andy定义的py文件和最后一次执行:
python manage.py runserver
Have fun..!!
玩得开心! !
#3
12
As all said above, you can easily install xampp first from https://www.apachefriends.org/download.html Then follow the instructions as:
如上所述,您可以很容易地从https://www.apachefriends.org/download.html安装xampp,然后按照以下说明执行:
- Install and run xampp from http://www.unixmen.com/install-xampp-stack-ubuntu-14-04/, then start Apache Web Server and MySQL Database from the GUI.
- 安装并运行xampp,从http://www.unixmen.com/install-xampp-stack-ubuntu-14-04/,然后从GUI启动Apache Web服务器和MySQL数据库。
- You can configure your web server as you want but by default web server is at
http://localhost:80
and database atport 3306
, and PhpMyadmin athttp://localhost/phpmyadmin/
- 您可以根据需要配置您的web服务器,但是默认的web服务器位于http://localhost:80和port3306的数据库,而PhpMyadmin位于http://localhost/phpmyadmin/。
- From here you can see your databases and access them using very friendly GUI.
- 从这里您可以看到您的数据库并使用非常友好的GUI访问它们。
- Create any database which you want to use on your Django Project.
- 创建您想在Django项目上使用的任何数据库。
-
Edit your
settings.py
file like:编辑您的设置。py文件:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'DB_NAME', 'HOST': '127.0.0.1', 'PORT': '3306', 'USER': 'root', 'PASSWORD': '', }}
-
Install the following packages in the virtualenv (if you're using django on virtualenv, which is more preferred):
在virtualenv中安装以下包(如果您使用的是virtualenv的django,则更可取):
sudo apt-get install libmysqlclient-dev
sudo apt-get安装libmysqlclient-dev
pip install MySQL-python
pip安装MySQL-python
-
That's it!! you have configured Django with MySQL in a very easy way.
就是这样! !您已经用一种非常简单的方式配置了Django和MySQL。
-
Now run your Django project:
现在运行Django项目:
python manage.py migrate
python管理。py迁移
python manage.py runserver
python管理。py runserver
#4
9
If you are using python3.x then Run below command
如果你使用的是python3。然后运行以下命令。
pip install mysqlclient
Then change setting.py like
然后改变设置。py像
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB',
'USER': 'username',
'PASSWORD': 'passwd',
}
}
#5
8
Actually, there are many issues with different environments, python versions, so on. You might also need to install python dev files, so to 'brute-force' the installation I would run all of these:
实际上,在不同的环境、python版本等方面存在许多问题。您可能还需要安装python dev文件,因此对于“brute-force”的安装,我将运行所有这些:
sudo apt-get install python-dev python3-dev
sudo apt-get install libmysqlclient-dev
pip install MySQL-python
pip install pymysql
pip install mysqlclient
You should be good to go with the accepted answer. And can remove the unnecessary packages if that's important to you.
你应该接受这个公认的答案。如果这对你很重要的话,可以删除不必要的包。
#6
3
Andy's answer helps but if you have concern on exposing your database password in your django setting, I suggest to follow django official configuration on mysql connection: https://docs.djangoproject.com/en/1.7/ref/databases/
Andy的回答很有帮助,但是如果您担心在django设置中暴露您的数据库密码,我建议在mysql连接上使用django官方配置:https://docs.djangoproject.com/en/1.7/ref/databases/。
Quoted here as:
引用在这里:
# 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
To replace 'HOST': '127.0.0.1' in setting, simply add it in my.cnf:
在设置中替换“主机”:“127.0.0.1”,只需将其添加到my.cnf:
# my.cnf
[client]
database = NAME
host = HOST NAME or IP
user = USER
password = PASSWORD
default-character-set = utf8
Another OPTION that is useful, is to set your storage engine for django, you might want it in your setting.py:
另一个有用的选项是为django设置存储引擎,您可能希望它在您的设置中。
'OPTIONS': {
'init_command': 'SET storage_engine=INNODB',
}
#7
3
Run these commands
运行这些命令
sudo apt-get install python-dev python3-dev
sudo apt-get安装python-dev python3-dev。
sudo apt-get install libmysqlclient-dev
sudo apt-get安装libmysqlclient-dev
pip install MySQL-python
pip安装MySQL-python
pip install pymysql
pip安装pymysql
pip install mysqlclient
pip安装mysqlclient
Then configure settings.py like
然后配置设置。py像
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': '123456',
}
}
Enjoy mysql connection
享受mysql连接
#8
1
settings.py
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER': 'root',
'PASSWORD': '*****',
'HOST': '***.***.***.***',
'PORT': '3306',
'OPTIONS': {
'autocommit': True,
},
}
}
}
then:
然后:
python manage.py migrate
if success will generate theses tables:
如果成功将产生这些表格:
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
and u will can use mysql.
你可以使用mysql。
this is a showcase example ,test on Django version 1.11.5: Django-pool-showcase
这是一个展示示例,测试Django版本1.11.5:Django- poolt -橱窗。
#9
0
You must create a MySQL database first. Then go to settings.py
file and edit the 'DATABASES'
dictionary with your MySQL credentials:
首先必须创建一个MySQL数据库。然后去设置。py文件并使用MySQL证书编辑“数据库”字典:
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',
}
}
Here is a complete installation guide for setting up Django to use MySQL on a virtualenv:
这里有一个完整的安装指南,用于设置Django,以便在virtualenv上使用MySQL:
http://codex.themedelta.com/how-to-install-django-with-mysql-in-a-virtualenv-on-linux/
http://codex.themedelta.com/how-to-install-django-with-mysql-in-a-virtualenv-on-linux/