How to run a MySQL command terminal in Docker, with a docker-compose server
NickName:souyahia Ask DateTime:2020-05-12T17:01:23

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. Now what do I need to do to access the mysql terminal prompt from "outside" the container ? 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.

I tried running a new Docker container like this :

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).

I guess the "localhost" or 3306 port are wrong ? What should I put in these parameters ?

Thanks in advance for your answers :)

Copyright Notice:Content Author:「souyahia」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/61747708/how-to-run-a-mysql-command-terminal-in-docker-with-a-docker-compose-server

More about “How to run a MySQL command terminal in Docker, with a docker-compose server” related questions

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

Show Detail

MySql docker container, via terminal works, via docker-compose no

the following file is a docker-compose file. If I execute it via docker-compose up the container create itselfs but is impossible to connect to server, via terminal as via database visual editor. A...

Show Detail

Hot to run HTTP server with docker-compose?

I want to create isolated environment to run modified python interpreter in it. I have docker-compose.yaml: version: '3.5' services: cpython: image: ubuntu ports: - ...

Show Detail

I get port in use error when I try to run the docker container for the nodejs and mysql application using the docker-compose

How can I use the docker-compose to create container and pass the parameter to my Node.js application during the start? I am trying to write a program where I can connect MYSQL using the NODEJS and

Show Detail

Docker-compose link external/ local mysql

I have problem that unable to connect with my localhost mysql. DockerFile FROM python:2.7 WORKDIR /app ADD . /app RUN pip install -r requirements.txt EXPOSE 80 ENV NAME HelloWorld CMD ["pytho...

Show Detail

docker-compose: MySQL service not running

I want to create a docker-compose file combining a custom maven environment, a specific runtime environment and a mysql server runtime. Compose file looks like this: version: '3.7' services: mys...

Show Detail

docker-compose / mysql / increasing max connections

I have a docker-compose file that creates a mysql server and then runs python script which pushes data into that docker mysql server. However, when the mysql server is created it takes the default ...

Show Detail

How Can Run Phpmyadmin, Mysql and Apache Tomcat with Docker-Compose File?

I am trying to run java web app. Thus, I must setup MySQL, phpmyadmin and Apache Tomcat for accessing website. I want to use the docker and its docker-compose application to make installation easie...

Show Detail

How to capture logs of a command run through `docker-compose exec -d` in background?

Consider a docker-compose.yml file like this: version: '3' services: test: image: ubuntu:18.04 tty: true I can bring up the services in background by running docker-compose up -d Now ...

Show Detail

docker-compose: command not found on jenkins

I have a docker compose file version: "3" services: mysql: image: mysql:latest container_name: locations-service-mysql environment: MYSQL_ROOT_PASSWORD: root MYSQL_USERNA...

Show Detail