gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:22:25 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com