我想远离PHP一点点学习Python。为了使用Python进行Web开发,我需要一个框架来帮助模板化和其他事情。
我有一个非生产服务器,用于测试所有Web开发的东西。它是Debian 7.1 LAMP堆栈,它运行MariaDB而不是常见的MySQL服务器包。
昨天我安装了Django并创建了我的第一个名为 firstweb 的项目。我还没有改变任何设置。
这是我的第一个大混乱。在教程中,我跟着安装了Django的人,启动了他的第一个项目,重新启动了Apache,Django从此开始工作。他去了他的浏览器,没有问题就去了Django默认页面。
但是,我必须进入我的firstweb文件夹并运行
python manage.py runserver myip:port
它有效。没问题。但是我想知道它是否应该像这样工作,如果这会引起问题呢?
我 第二个问题 是我想要设置它以便它使用我的MySQL数据库。我进入/ firstweb/firstweb下的settings.py,我看到ENGINE和NAME,但我不确定要放在这里。
然后在USER,PASSWORD和Host区域中是我的数据库及其凭据吗?如果我使用 localhost 我可以在主机区域中放置 localhost 吗?
MySQL支持 很容易添加。在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 选项文件 ,从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
参数允许您自己以外的计算机访问您的开发应用程序。准备部署应用程序后,我建议您查看 部署Djangodjangobook上的章节
Mysql默认字符集通常不是utf-8,因此请确保使用此sql创建数据库:
CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_bin
如果你正在使用 Oracle的MySQL连接器 你的ENGINE
行应如下所示:
'ENGINE': 'mysql.connector.Django',
请注意,您首先需要在您的操作系统上安装mysql。
brew install mysql (MacOS)
此外,mysql客户端包已更改为python 3(MySQL-Client
仅适用于python 2)
pip3 install mysqlclient
首先请运行以下命令来安装python依赖项,否则python runserver命令将抛出错误。
Sudo apt-get install libmysqlclient-dev
Sudo pip install MySQL-python
然后配置#Andy定义的settings.py文件,并在最后一次执行时:
python manage.py runserver
玩得开心..!!
如上所述,您可以先从 https://www.apachefriends.org/download.html轻松安装xampp 然后按照说明操作:
http://localhost:80
,数据库位于port 3306
,PhpMyadmin位于http://localhost/phpmyadmin/
编辑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
如果您使用的是python3.x,请运行以下命令
pip install mysqlclient
然后更改setting.py之类的
DATABASES = {
'default': {
'ENGINE': 'Django.db.backends.mysql',
'NAME': 'DB',
'USER': 'username',
'PASSWORD': 'passwd',
}
}
实际上,不同的环境有许多问题,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
你应该好好接受公认的答案。如果这对你很重要,可以删除不必要的包裹。
运行这些命令
Sudo apt-get install python-dev python3-dev
Sudo apt-get install libmysqlclient-dev
pip安装MySQL-python
pip install pymysql
pip install mysqlclient
然后配置settings.py,如
DATABASES = {
'default': {
'ENGINE': 'Django.db.backends.mysql',
'NAME': 'Django_db',
'Host': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': '123456',
}
}
享受mysql连接
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
要在设置中替换“主机”:“127.0.0.1”,只需将其添加到my.cnf:
# my.cnf
[client]
database = NAME
Host = Host NAME or IP
user = USER
password = PASSWORD
default-character-set = utf8
另一个有用的选项是为Django设置存储引擎,你可能需要在你的setting.py中:
'OPTIONS': {
'init_command': 'SET storage_engine=INNODB',
}
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
按照给定的步骤设置它以使用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
您必须首先创建一个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/