pycharm 如何连接我用docker compose创建的数据库示例?

gtlvzcf8  于 2022-11-08  发布在  PyCharm
关注(0)|答案(2)|浏览(195)

我正在尝试编写一个简单的python应用程序(遵循this tutorial),我想使用一个用docker创建的MySQL数据库,但我遇到了一些问题。
这是我的docker-compose.yml

version: '3.7'
services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    command: 'python manage.py runserver 0.0.0.0:8000'
    ports:
      - '8000:8000'
    volumes:
      - .:/app
    depends_on:
      - db
  db:
    image: mysql:5.7.22
    restart: always
    environment:
      MYSQL_DATABASE: admin
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - .dbdata:/var/lib/mysql
    ports:
      - '33077:3306'

我可以运行docker-compose up,它看起来不错。docker-compose ps输出:

Name                    Command               State                     Ports                   
-----------------------------------------------------------------------------------------------------
admin_backend_1   python manage.py runserver ...   Up      0.0.0.0:8000->8000/tcp,:::8000->8000/tcp  
admin_db_1        docker-entrypoint.sh mysqld      Up      0.0.0.0:33077->3306/tcp,:::33077->3306/tcp

然后,我尝试连接用户:root和密码:rootlocalhost:33077,我收到this error。你能给予我一些帮助吗?
当我执行docker-compose up时,我会得到:

Starting admin_db_1 ... done
Starting admin_backend_1 ... done
Attaching to admin_db_1, admin_backend_1
db_1       | 2022-03-02T12:26:20.204261Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_1       | 2022-03-02T12:26:20.205273Z 0 [Note] mysqld (mysqld 5.7.22) starting as process 1 ...
db_1       | 2022-03-02T12:26:20.207089Z 0 [Note] InnoDB: PUNCH HOLE support available
db_1       | 2022-03-02T12:26:20.207104Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1       | 2022-03-02T12:26:20.207105Z 0 [Note] InnoDB: Uses event mutexes
db_1       | 2022-03-02T12:26:20.207107Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
db_1       | 2022-03-02T12:26:20.207108Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
db_1       | 2022-03-02T12:26:20.207109Z 0 [Note] InnoDB: Using Linux native AIO
db_1       | 2022-03-02T12:26:20.207250Z 0 [Note] InnoDB: Number of pools: 1
db_1       | 2022-03-02T12:26:20.207316Z 0 [Note] InnoDB: Using CPU crc32 instructions
db_1       | 2022-03-02T12:26:20.208157Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
db_1       | 2022-03-02T12:26:20.212058Z 0 [Note] InnoDB: Completed initialization of buffer pool
db_1       | 2022-03-02T12:26:20.213177Z 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       | 2022-03-02T12:26:20.224331Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
db_1       | 2022-03-02T12:26:20.232279Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1       | 2022-03-02T12:26:20.232321Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1       | 2022-03-02T12:26:20.264177Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1       | 2022-03-02T12:26:20.264599Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
db_1       | 2022-03-02T12:26:20.264604Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
db_1       | 2022-03-02T12:26:20.264790Z 0 [Note] InnoDB: Waiting for purge to start
db_1       | 2022-03-02T12:26:20.315127Z 0 [Note] InnoDB: 5.7.22 started; log sequence number 12359913
db_1       | 2022-03-02T12:26:20.315447Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1       | 2022-03-02T12:26:20.315686Z 0 [Note] Plugin 'FEDERATED' is disabled.
db_1       | 2022-03-02T12:26:20.317675Z 0 [Note] InnoDB: Buffer pool(s) load completed at 220302 12:26:20
db_1       | 2022-03-02T12:26:20.322304Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
db_1       | 2022-03-02T12:26:20.322594Z 0 [Warning] CA certificate ca.pem is self signed.
db_1       | 2022-03-02T12:26:20.326271Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
db_1       | 2022-03-02T12:26:20.326318Z 0 [Note] IPv6 is available.
db_1       | 2022-03-02T12:26:20.326330Z 0 [Note]   - '::' resolves to '::';
db_1       | 2022-03-02T12:26:20.326346Z 0 [Note] Server socket created on IP: '::'.
db_1       | 2022-03-02T12:26:20.331099Z 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       | 2022-03-02T12:26:20.332360Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
db_1       | 2022-03-02T12:26:20.332405Z 0 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1       | 2022-03-02T12:26:20.332422Z 0 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1       | 2022-03-02T12:26:20.332457Z 0 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1       | 2022-03-02T12:26:20.332466Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1       | 2022-03-02T12:26:20.332482Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
db_1       | 2022-03-02T12:26:20.335750Z 0 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1       | 2022-03-02T12:26:20.335772Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1       | 2022-03-02T12:26:20.342036Z 0 [Note] Event Scheduler: Loaded 0 events
db_1       | 2022-03-02T12:26:20.342204Z 0 [Note] mysqld: ready for connections.
db_1       | Version: '5.7.22'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
backend_1  | Watching for file changes with StatReloader
backend_1  | Performing system checks...
backend_1  | 
backend_1  | System check identified no issues (0 silenced).
backend_1  | 
backend_1  | You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
backend_1  | Run 'python manage.py migrate' to apply them.
backend_1  | March 02, 2022 - 12:26:20
backend_1  | Django version 4.0.2, using settings 'admin.settings'
backend_1  | Starting development server at http://0.0.0.0:8000/
backend_1  | Quit the server with CONTROL-C.
vbkedwbf

vbkedwbf2#

对我来说,在你的Docker-compose中添加一个虚拟网络的创建会更好。也许这能解决你的问题。就像这个enter image description here
再见

相关问题