简体   繁体   English

如何使用 docker-compose 服务器在 Docker 中运行 MySQL 命令终端

[英]How to run a MySQL command terminal in Docker, with a docker-compose server

Let's say I have the following compose file:假设我有以下撰写文件:

networks:
  my_network:

services:

  ...

  mysql:
    container_name: "mysql"
    image: "mysql:5.7"
    volumes:
      - ./mysql.cnf:/etc/mysql/conf.d/mysql.cnf
    environment: 
      MYSQL_ROOT_PASSWORD: "password"
      MYSQL_USER: "user"
      MYSQL_PASSWORD: "password"
      MYSQL_DATABASE: "test-db"
    ports:
      - "3306:3306"
    restart: always
    networks: 
      - my_network

Once I run docker-compose up , my services get started and I can see that my MySQL server is ready to accept connections.一旦我运行docker-compose up ,我的服务就会启动,我可以看到我的 MySQL 服务器已准备好接受连接。 Now what do I need to do to access the mysql terminal prompt from "outside" the container?现在我需要做什么才能从容器“外部”访问 mysql 终端提示符? I remember seeing a teacher run another docker container (from a new terminal), and access the MySQL command prompt, enabling him to manage the database by hand, from another terminal window, but I can't remember the command exactly.我记得看到一位老师运行另一个 docker 容器(从新终端),并访问 MySQL 命令提示符,使他能够从另一个终端 window 手动管理数据库,但我完全记得命令。

I tried running a new Docker container like this:我尝试像这样运行一个新的 Docker 容器:

docker run -it --rm --name mysqlterm \
  --network="compose_project_my_network" \
  --link mysql mysql:5.7 \
  sh -c 'exec mysql \
  -h "localhost" -P 3306" \
  -uroot \
  -ppassword'

But unfortunatly, the container can't connect to the MySQL server, giving me the following error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) .但不幸的是,容器无法连接到 MySQL 服务器,给我以下错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) .

I guess the "localhost" or 3306 port are wrong?我猜“localhost”或3306端口错了? What should I put in these parameters?我应该在这些参数中输入什么?

Thanks in advance for your answers:)提前感谢您的回答:)

You need an ordinary MySQL client;你需要一个普通的MySQL客户端; if your host's package manager (Debian/Ubuntu APT, MacOS Homebrew, ...) has a packaged named something like mysql-client , that's it.如果您的主机的 package 管理器(Debian/Ubuntu APT,MacOS Homebrew,...)有一个名为mysql-client的打包,就是这样。

When you run mysql , that tool does not interpret localhost as a host name but rather as a signal to use a Unix socket, so you need to use the corresponding IP address 127.0.0.1 instead.当您运行mysql时,该工具不会将localhost解释为主机名,而是将其解释为使用 Unix 套接字的信号,因此您需要使用相应的 ZA12A3079E14CED46E69BA52B8A90B211AZ 来代替。地址。1。 (If the database is running on a different system, use its DNS name or IP address: this is indistinguishable from running the database directly on the host outside of Docker.) (如果数据库在不同的系统上运行,请使用其 DNS 名称或 IP 地址:这与直接在 Docker 之外的主机上运行数据库没有区别。)

So from the host run:所以从主机运行:

mysql -h 127.0.0.1 -u root -p

The default port 3306 matches the published ports: , and you'll be prompted to interactively enter the password.默认端口 3306 与发布的ports: ,系统将提示您以交互方式输入密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM