python - (2002, "Can' t 通过套接字 '/var/run/mysqld/mysqld.sock' (2) 连接到本地 MySQL 服务器”)

标签 python mysql docker docker-compose ubuntu-18.04

我正在使用 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

最佳答案

  1. host="localhost"更改为host="db",例如

    db = MySQLdb.connect(host="db", user="root", passwd="password", db="DBM", port=3306)

    <
  2. 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/

相关文章:

python - 在 Django 模板中访问模型数据

python - 如何创建新行字符

python - 在所有错误消息中打印 python 版本

php - MySQL基于两列的插入/更新

java - 将java应用程序部署到azure容器应用程序

bash - 使用docker-compose run -rm时的前缀

docker - 如何在Dockerfile内部直接复制到主机

python - 如何在 Matplotlib 的子图中单独绘制相同的图形?

php - 查询未在 SQL 中执行(WHERE 子句)

MySQL。同一字段的 UNIQUE 和 PRIMARY KEY 约束