Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker 部署数据库连接问题 #346

Open
LeyiW opened this issue Apr 15, 2022 · 3 comments
Open

Docker 部署数据库连接问题 #346

LeyiW opened this issue Apr 15, 2022 · 3 comments

Comments

@LeyiW
Copy link

LeyiW commented Apr 15, 2022

您好, 我在使用docker按照wiki文档(https://github.com/welliamcao/OpsManage/wiki/Docker%E6%9E%84%E5%BB%BAOpsManage)部署的时候 出现了
ERROR 2002 (HY000): Can't connect to MySQL server on 'db' (115)
这个报错。
我的运行环境是windows下运行的centOS7虚拟机
/docker/docker-compose.yml 文件如下

version: "3"
services:
db:
container_name: db
image: mysql:5.6
privileged: true
environment:
- MYSQL_HOST=localhost
- MYSQL_DATABASE=opsmanage
- MYSQL_USER=wly
- MYSQL_PASSWORD=12345
- MYSQL_ROOT_PASSWORD=12345
volumes:
- /data/apps/mysql:/var/lib/mysql
restart: always
#ports:
# - 3306:3306
networks:
- default
redis:
container_name: redis
image: redis:3.2.8
privileged: true
command: redis-server
ports:
- "6379:6379"
volumes:
- /data/apps/redis:/data/redis
networks:
- default
rabbitmq:
container_name: rabbitmq
image: rabbitmq:management
ports:
- "5672:5672"
- "15672:15672"
networks:
- default

ops_web:
image: opsmanage-base:latest
container_name: ops_web
privileged: true
environment:
MYSQL_DATABASE: opsmanage
MYSQL_USER: wly
MYSQL_PASSWORD: 12345
MYSQL_ROOT_PASSWORD: 12345
ports:
- "8000:8000" #vim /mnt/OpsManage/OpsManage/settings.py文件里面的DEBUG设置为DEBUG = True
volumes:
- /mnt/OpsManage:/data/apps/opsmanage
- /mnt/OpsManage/upload:/data/apps/opsmanage/upload
- /mnt/OpsManage/logs:/data/apps/opsmanage/logs
command: bash /data/apps/opsmanage/docker/start.sh
links:
- db
- redis
- rabbitmq
depends_on:
- db
- redis
- rabbitmq
restart: always
networks:
- default
//Nginx没配 就不贴了

/conf/opsmanage.ini
[db]
engine = mysql
host = db
port = 3306
user = wly
password = 12345
database = opsmanage

[redis]
#ansible实时输出数据
host = redis
port = 6379
password =
ansible_db = 3
celery_db = 4
default_db = 0

[amqp]
#celery消息队列中间件
host = rebbitmq
port = 5672
user = guest
password = guest

[deploy]
path = /var/lib/opsmanage/workspaces/

docker compose up -d 之后
docker ps -a 查看所有容器 均可运行, 数据库也能bash 进入, mysql -u -p 用上面的密码也是可以登录的
docker logs ops_web 查看 会发现最开始的错误

如果您有什么解决思路请回复,非常感谢!

@LeyiW
Copy link
Author

LeyiW commented Apr 19, 2022

问题解决了
我下载了一个本地mysql5.6数据库 并按照本地安装教程配置 ,之后使用readme的docker容器启动步骤(不用wiki的docker-compose),只不过在docker run 那一步增加了 --network="host" 让容器使用本地网络作为主机。然后再设置中把host都设置成127.0.0.1, 可成功执行migration操作。
对于 opsmanage使用docker-compose up -d 启动,想把容器内网络变为主机的操作是修改docker-compose.yml 文件,去掉 networks:default,变成 network_mode: host,但是使用docker-compose启动我在执行start.sh脚本 - > init.sql这一步时会报 mysql_exceptions.OperationalError: (1050, "Table 'django_content_type' already exist)这个Table类型已经存在的错误。懒得解决于是删了opsmanage数据库重建,换了readme文档的部署方法。
总之 这次连接数据库失败是因为网络原因 我也不知道为啥我dockercompose起的ip‘’db‘’ 没法让同一个默认网络的 ops_web访问到。但问题总算是解决了:)

@yssky
Copy link

yssky commented Mar 31, 2023

一样的报错

@yssky
Copy link

yssky commented Mar 31, 2023

docker logs ops_web
报错:
Traceback (most recent call last):
File "manage.py", line 23, in
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/init.py", line 381, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/init.py", line 325, in execute
settings.INSTALLED_APPS
File "/usr/local/lib/python3.6/site-packages/django/conf/init.py", line 79, in getattr
self._setup(name)
File "/usr/local/lib/python3.6/site-packages/django/conf/init.py", line 66, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.6/site-packages/django/conf/init.py", line 157, in init
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/lib/python3.6/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "/data/apps/opsmanage/OpsManage/settings.py", line 43, in
DEBUG = true
NameError: name 'true' is not defined
Unlinking stale socket /tmp/supervisor. Sock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants