xKxAxKx
xKxAxKx

Reputation: 1074

How to connect to mysql created by docker-compose

I can not connect to Mysql created with Docker in local environment (Mac OS X).
I have created the following configuration file.

version: '2'
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: mysqldatabase
      MYSQL_USER: mysql
      MYSQL_PASSWORD: mysql
      MYSQL_ROOT_PASSWORD: password
    ports:
      - "33333:3306"
    container_name: mysql-db
    volumes:
      - db-data:/var/lib/mysql

volumes:
  db-data:
    driver: local

Then, I started the docker container of mysql with the following command.

$ docker-compose up -d
$ docker start mysql-db

Up to this point there was no problem, but an error occurred when trying to connect to mysql.

$ mysql -p 33333 -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Please tell me how to deal with it.

Upvotes: 2

Views: 5259

Answers (2)

harshavmb
harshavmb

Reputation: 3872

Try mysql -h 0.0.0.0 -P 33333 -u root -p.

Note port here has upper case P and password has lower case p.

Hope this helps!

Upvotes: 5

reachlin
reachlin

Reputation: 4782

I think if you connect from the host to the container. mysql will see the connection from non-local host, so disconnect it. You can change mysql config to allow other hosts, or connect from inside that container using docker exec -it mysql ...

Upvotes: 0

Related Questions