问题:
settings.py中添加单独的设置:默认设置(用于测试)和开发设置(用于开发);并认为.travis.xml在运行迁移任务时将使用“默认”设置。但特拉维斯CI错误与error : django.db.utils.OperationalError: (1045, "Access denied for user 'sajay'@'localhost' (using password: YES)")
我不知道它为什么要访问我的开发数据库设置?我查看了django1.7的文档,搜索了一下,但没有运气。
感谢你的帮助,谢谢
我的settings.py数据库部分如下所示:
DATABASES = {
'default': {
'ENGINE':'django.db.backends.mysql',
'NAME':'expenses_db',
'USER':'root',
'PASSWORD':'',
'HOST':'127.0.0.1',
'PORT':'3306',
},
# 'development': {
# 'ENGINE':'django.db.backends.mysql',
# 'NAME':'myapp_db',
# 'USER':'sajay',
# 'PASSWORD':'secret',
# 'HOST':'127.0.0.1',
# 'PORT':'3306',
# },
}备注:当评论“开发”部分时,Travis构建是绿色的
我的.travis.yml粘贴在下面:
language: python
services:
- mysql
python:
- "2.7"
env:
- DJANGO_VERSION=1.7 DB=mysql
install:
- pip install -r requirements.txt
- pip install mysql-python
before_script:
- mysql -e 'create database IF NOT EXISTS myapp_db;' -uroot
- mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';" -uroot
- python manage.py migrate
script:
- python manage.py test发布于 2014-10-31 21:28:36
遵循http://www.slideshare.net/jacobian/the-best-and-worst-of-django中建议的方法,将不同环境的设置分开
我查了一下How to manage local vs production settings in Django?,但没有得到一个明确的答案。谢谢
发布于 2014-12-22 03:31:38
您所遇到的问题是因为您没有为Travis CI获得正确的数据库名称和设置。首先,您需要在Travis和您的项目之间分离您的设置。为此,我使用了一个名为BUILD_ON_TRAVIS的环境变量(或者,如果您愿意,可以使用不同的设置文件)。
settings.py
import os
#Use the following live settings to build on Travis CI
if os.getenv('BUILD_ON_TRAVIS', None):
SECRET_KEY = "SecretKeyForUseOnTravis"
DEBUG = False
TEMPLATE_DEBUG = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'travis_ci_db',
'USER': 'travis',
'PASSWORD': '',
'HOST': '127.0.0.1',
}
}
else:
#Non-travis DB configuration goes here然后,在.travis.yml文件中的before_script部分中,您需要使用与DATABASES设置相同的数据库名。然后,我们只需在.travis.yml文件中设置环境变量,如下所示:
env:
global:
- BUILD_ON_TRAVIS=true
matrix:
- DJANGO_VERSION=1.7 DB=mysql编辑:
在构建Travis时,现在默认设置了一个环境变量。使用这个环境变量,我们可以更简单地解决问题:
settings.py
import os
#Use the following live settings to build on Travis CI
if os.getenv('TRAVIS', None):
#Travis DB configuration goes here
else:
#Non-Travis DB configuration goes here这样做更好,因为我们不再需要自己在.travis.yml文件中定义环境变量。
https://stackoverflow.com/questions/26467853
复制相似问题