0
services:
  db:
    image: mariadb
    ports:
      - 3306:3306
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: always
    volumes:
      - db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=pw
      - MYSQL_PASSWORD=pw
      - MYSQL_DATABASE=db
      - MYSQL_USER=user

I used this to create a database, ran docker-compose up, then tried to access the database with:

sudo mysql -u user -h 127.0.0.1:3306 -p

But I get the following error:

ERROR 2005 (HY000): Unknown MySQL server host '127.0.0.1:3306' (-2)

It seems like the port 3306 isn't actually exposed and I don't understand why.

2 Answers 2

1

Your command syntax is invalid. Should be

mysql -u user -h 127.0.0.1 -P 3306 -p

When using default port (3306) you can omit it completely.

mysql -u user -h 127.0.0.1 -p
Sign up to request clarification or add additional context in comments.

Comments

1

Remove port from the DB Host, it should only contain URL

mysql -u user -h 127.0.0.1 -p

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.