- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 docker 19.03.6、docker-compose 1.25.3 和 ubuntu 18。当我运行 docker-compose up
时Web 容器可以工作,但 mysql 容器无法访问。我能做什么?
问题可能出在mysqlclient包上吗?
我的Python代码:
db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="DBM", port=3306)
注意:我已经尝试过 src_db_1 而不是 localhost!
Dockerfile:
FROM python:3.8
ENV PYTHONUNBUFFERED 1
RUN mkdir /api_rest_mix_1_0
WORKDIR /api_rest_mix_1_0
ADD requirements.txt /api_rest_mix_1_0/
RUN pip install --upgrade pip && pip install -r requirements.txt
ADD . /api_rest_mix_1_0/
Docker 组合:
version: '3'
services:
db:
image: mysql:5.7
ports:
- '3306:3306'
environment:
MYSQL_DATABASE: 'DBM'
MYSQL_USER: 'root'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/my_app_dir
ports:
- "8000:8000"
depends_on:
- db
需求.txt:
Django==3.0.3
mysqlclient==1.4.6
我的终端突击队:docker-compose up
我的命令的输出:
Creating network "src_default" with the default driver
Creating src_db_1 ... done
Creating src_web_1 ... done
Attaching to src_db_1, src_web_1
db_1 | 2020-02-19 11:23:18+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.29-1debian9 started.
db_1 | 2020-02-19 11:23:18+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1 | 2020-02-19 11:23:18+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.29-1debian9 started.
db_1 | 2020-02-19 11:23:18+00:00 [Note] [Entrypoint]: Initializing database files
db_1 | 2020-02-19T11:23:18.929128Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_1 | 2020-02-19T11:23:22.350394Z 0 [Warning] InnoDB: New log files created, LSN=45790
db_1 | 2020-02-19T11:23:23.042973Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
db_1 | 2020-02-19T11:23:23.291068Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 3d632fc3-530a-11ea-9cd8-0242ac140002.
db_1 | 2020-02-19T11:23:23.361130Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
db_1 | 2020-02-19T11:23:24.557385Z 0 [Warning] CA certificate ca.pem is self signed.
web_1 | Watching for file changes with StatReloader
web_1 | Performing system checks...
web_1 |
web_1 | System check identified no issues (0 silenced).
web_1 | February 19, 2020 - 11:23:25
web_1 | Django version 3.0.3, using settings 'api_rest_mix_1_0.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
db_1 | 2020-02-19T11:23:25.254876Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
db_1 | 2020-02-19 11:23:43+00:00 [Note] [Entrypoint]: Database files initialized
db_1 | 2020-02-19 11:23:43+00:00 [Note] [Entrypoint]: Starting temporary server
db_1 | 2020-02-19 11:23:43+00:00 [Note] [Entrypoint]: Waiting for server startup
db_1 | 2020-02-19T11:23:43.925550Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_1 | 2020-02-19T11:23:43.927341Z 0 [Note] mysqld (mysqld 5.7.29) starting as process 81 ...
db_1 | 2020-02-19T11:23:43.932133Z 0 [Note] InnoDB: PUNCH HOLE support available
db_1 | 2020-02-19T11:23:43.932155Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1 | 2020-02-19T11:23:43.932160Z 0 [Note] InnoDB: Uses event mutexes
db_1 | 2020-02-19T11:23:43.932164Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
db_1 | 2020-02-19T11:23:43.932168Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
db_1 | 2020-02-19T11:23:43.932172Z 0 [Note] InnoDB: Using Linux native AIO
db_1 | 2020-02-19T11:23:43.932473Z 0 [Note] InnoDB: Number of pools: 1
db_1 | 2020-02-19T11:23:43.932591Z 0 [Note] InnoDB: Using CPU crc32 instructions
db_1 | 2020-02-19T11:23:43.934281Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
db_1 | 2020-02-19T11:23:43.946320Z 0 [Note] InnoDB: Completed initialization of buffer pool
db_1 | 2020-02-19T11:23:43.948809Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1 | 2020-02-19T11:23:43.960693Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
db_1 | 2020-02-19T11:23:44.063103Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1 | 2020-02-19T11:23:44.063192Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1 | 2020-02-19T11:23:44.347746Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1 | 2020-02-19T11:23:44.348505Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
db_1 | 2020-02-19T11:23:44.348523Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
db_1 | 2020-02-19T11:23:44.348922Z 0 [Note] InnoDB: 5.7.29 started; log sequence number 2630649
db_1 | 2020-02-19T11:23:44.349095Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1 | 2020-02-19T11:23:44.349223Z 0 [Note] Plugin 'FEDERATED' is disabled.
db_1 | 2020-02-19T11:23:44.350675Z 0 [Note] InnoDB: Buffer pool(s) load completed at 200219 11:23:44
db_1 | 2020-02-19T11:23:44.354928Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
db_1 | 2020-02-19T11:23:44.354950Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
db_1 | 2020-02-19T11:23:44.355643Z 0 [Warning] CA certificate ca.pem is self signed.
db_1 | 2020-02-19T11:23:44.355680Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
db_1 | 2020-02-19T11:23:44.393563Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db_1 | 2020-02-19T11:23:44.420138Z 0 [Note] Event Scheduler: Loaded 0 events
db_1 | 2020-02-19T11:23:44.420413Z 0 [Note] mysqld: ready for connections.
db_1 | Version: '5.7.29' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server (GPL)
db_1 | 2020-02-19 11:23:44+00:00 [Note] [Entrypoint]: Temporary server started.
web_1 | Internal Server Error: /api/statistics/4
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
web_1 | response = get_response(request)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
web_1 | response = self.process_exception_by_middleware(e, request)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
web_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1 | File "/api_rest_mix_1_0/api/views.py", line 171, in statistics
web_1 | json_response = stt_sex_status_total()
web_1 | File "/api_rest_mix_1_0/api/views.py", line 99, in stt_sex_status_total
web_1 | db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="DBMix_hml", charset='utf8', use_unicode=True)
web_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/__init__.py", line 84, in Connect
web_1 | return Connection(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/connections.py", line 179, in __init__
web_1 | super(Connection, self).__init__(*args, **kwargs2)
web_1 | MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
web_1 | [19/Feb/2020 11:23:45] "GET /api/statistics/4 HTTP/1.1" 500 82444
web_1 | Internal Server Error: /api/statistics/4
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
web_1 | response = get_response(request)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
web_1 | response = self.process_exception_by_middleware(e, request)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
web_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1 | File "/api_rest_mix_1_0/api/views.py", line 171, in statistics
web_1 | json_response = stt_sex_status_total()
web_1 | File "/api_rest_mix_1_0/api/views.py", line 99, in stt_sex_status_total
web_1 | db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="DBMix_hml", charset='utf8', use_unicode=True)
web_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/__init__.py", line 84, in Connect
web_1 | return Connection(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/connections.py", line 179, in __init__
web_1 | super(Connection, self).__init__(*args, **kwargs2)
web_1 | MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
web_1 | [19/Feb/2020 11:23:46] "GET /api/statistics/4 HTTP/1.1" 500 82444
web_1 | Internal Server Error: /api/statistics/4
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
web_1 | response = get_response(request)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
web_1 | response = self.process_exception_by_middleware(e, request)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
web_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1 | File "/api_rest_mix_1_0/api/views.py", line 171, in statistics
web_1 | json_response = stt_sex_status_total()
web_1 | File "/api_rest_mix_1_0/api/views.py", line 99, in stt_sex_status_total
web_1 | db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="DBMix_hml", charset='utf8', use_unicode=True)
web_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/__init__.py", line 84, in Connect
web_1 | return Connection(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/connections.py", line 179, in __init__
web_1 | super(Connection, self).__init__(*args, **kwargs2)
web_1 | MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
web_1 | [19/Feb/2020 11:23:47] "GET /api/statistics/4 HTTP/1.1" 500 82444
web_1 | Internal Server Error: /api/statistics/4
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
web_1 | response = get_response(request)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
web_1 | response = self.process_exception_by_middleware(e, request)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
web_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1 | File "/api_rest_mix_1_0/api/views.py", line 171, in statistics
web_1 | json_response = stt_sex_status_total()
web_1 | File "/api_rest_mix_1_0/api/views.py", line 99, in stt_sex_status_total
web_1 | db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="DBMix_hml", charset='utf8', use_unicode=True)
web_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/__init__.py", line 84, in Connect
web_1 | return Connection(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/connections.py", line 179, in __init__
web_1 | super(Connection, self).__init__(*args, **kwargs2)
web_1 | MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
web_1 | [19/Feb/2020 11:23:47] "GET /api/statistics/4 HTTP/1.1" 500 82444
web_1 | Internal Server Error: /api/statistics/4
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
web_1 | response = get_response(request)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
web_1 | response = self.process_exception_by_middleware(e, request)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
web_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1 | File "/api_rest_mix_1_0/api/views.py", line 171, in statistics
web_1 | json_response = stt_sex_status_total()
web_1 | File "/api_rest_mix_1_0/api/views.py", line 99, in stt_sex_status_total
web_1 | db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="DBMix_hml", charset='utf8', use_unicode=True)
web_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/__init__.py", line 84, in Connect
web_1 | return Connection(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/connections.py", line 179, in __init__
web_1 | super(Connection, self).__init__(*args, **kwargs2)
web_1 | MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
web_1 | [19/Feb/2020 11:23:47] "GET /api/statistics/4 HTTP/1.1" 500 82444
db_1 | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
db_1 | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
web_1 | Internal Server Error: /api/statistics/4
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
web_1 | response = get_response(request)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
web_1 | response = self.process_exception_by_middleware(e, request)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
web_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1 | File "/api_rest_mix_1_0/api/views.py", line 171, in statistics
web_1 | json_response = stt_sex_status_total()
web_1 | File "/api_rest_mix_1_0/api/views.py", line 99, in stt_sex_status_total
web_1 | db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="DBMix_hml", charset='utf8', use_unicode=True)
web_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/__init__.py", line 84, in Connect
web_1 | return Connection(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/connections.py", line 179, in __init__
web_1 | super(Connection, self).__init__(*args, **kwargs2)
web_1 | MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
web_1 | [19/Feb/2020 11:23:48] "GET /api/statistics/4 HTTP/1.1" 500 82444
db_1 | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
db_1 | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
db_1 | 2020-02-19 11:23:52+00:00 [Note] [Entrypoint]: Creating database DBMix_hml
db_1 | 2020-02-19 11:23:52+00:00 [Note] [Entrypoint]: Creating user root
db_1 | ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'
src_db_1 exited with code 1
最佳答案
将host="localhost"更改为host="db",例如
db = MySQLdb.connect(host="db", user="root", passwd="password", db="DBM", port=3306)
docker-compose.yml
services:
db:
image: mysql:5.7
ports:
- '3306:3306'
environment:
MYSQL_DATABASE: 'DBM'
MYSQL_USER: 'root'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/my_app_dir
ports:
- "8000:8000"
depends_on:
- db
links: # <-- add this
- db # <-- add this
关于python - (2002, "Can' t 通过套接字 '/var/run/mysqld/mysqld.sock' (2) 连接到本地 MySQL 服务器”),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60299450/
我对这个错误很困惑: Cannot implicitly convert type 'System.Func [c:\Program Files (x86)\Reference Assemblies\
考虑这段代码: pub trait Hello { fn hello(&self); } impl Hello for Any { fn hello(&self) {
问题很简单。是否可以构造这样一个类型 T,对于它下面的两个变量声明会产生不同的结果? T t1 = {}; T t2{}; 我已经研究 cppreference 和标准一个多小时了,我了解以下内容:
Intellij idea 给我这个错误:“Compare (T, T) in Comparator cannot be applied to (T, T)” 对于以下代码: public class
任何人都可以告诉我 : n\t\t\t\t\n\t\t\t 在以下来自和 dwr 服务的响应中的含义和用途是什么. \r\n\t\t\t \r\n\t\t\t
让 T 成为一个 C++ 类。 下面三个指令在行为上有什么区别吗? T a; T a(); T a = T(); T 为不带参数的构造函数提供了显式定义这一事实是否对问题有任何改变? 后续问题:如果
Rust中的智能指针是什么 智能指针(smart pointers)是一类数据结构,是拥有数据所有权和额外功能的指针。是指针的进一步发展 指针(pointer)是一个包含内存地
比如我有一个 vector vector > v={{true,1},{true,2},{false,3},{false,4},{false,5},{true,6},{false,7},{true,8
我有一个来自 .xls 电子表格的数据框,我打印了 print(df.columns.values) 列,输出包含一个名为:Poll Responses\n\t\t\t\t\t。 我查看了 Excel
This question already has answers here: What are good reasons for choosing invariance in an API like
指针类型作为类型前缀与在类型前加斜杠作为后缀有什么区别。斜线到底是什么意思? 最佳答案 语法 T/~ 和 T/& 基本上已被弃用(我什至不确定编译器是否仍然接受它)。在向新向量方案过渡的初始阶段,[T
我正在尝试找到一种方法来获取模板参数的基类。 考虑以下类: template class Foo { public: Foo(){}; ~Foo(){};
这是一个让我感到困惑的小问题。我不知道如何描述它,所以只看下面的代码: struct B { B() {} B(B&) { std::cout ::value #include
为什么有 T::T(T&) 而 T::T(const T&) 更适合 copy ? (大概是用来实现move语义的???) 原始描述(被melpomene证明是错误的): 在C++11中,支持了一种新
在 Java 7 中使用 eclipse 4.2 并尝试实现 List 接口(interface)的以下方法时,我收到了警告。 public T[] toArray(T[] a) { ret
假设有三个函数: def foo[T](a:T, b:T): T = a def test1 = foo(1, "2") def test2 = foo(List(), ListBuffer()) 虽
我对柯里化(Currying)和非柯里化(Currying)泛型函数之间类型检查的差异有点困惑: scala> def x[T](a: T, b: T) = (a == b) x: [T](a: T,
考虑一个类A,我如何编写一个具有与相同行为的模板 A& pretty(A& x) { /* make x pretty */ return x; } A pretty(A&& x) {
Eclipse 表示由于泛型类型橡皮擦,类型参数不允许使用 instanceof 操作。 我同意在运行时不会保留任何类型信息。但是请考虑以下类的通用声明: class SomeClass{ T
在 C++14 中: 对于任何整数或枚举类型 T 以及对于任何表达式 expr: 有没有区别: struct S { T t { expr }; }; 和 struct S { T t = { exp
我是一名优秀的程序员,十分优秀!