DEV Community

Madhav Jha
Madhav Jha

Posted on

Docker MySQL setup on Mac OS X

Kill all running docker containers

docker ps -aq | xargs -r docker stop; docker ps -aq | xargs -r docker rm
Enter fullscreen mode Exit fullscreen mode

Start a MySQL server

Very Important is the flag MYSQL_ROOT_HOST is set to %.

docker run -d --name=local-mysql -p 54321:3306 -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=password mysql/mysql-server
Enter fullscreen mode Exit fullscreen mode

Connect to MySQL using mac's mysql

export PATH="$PATH:/usr/local/mysql/bin"
# restart shell if required
mysql -h 127.0.0.1 -P 54321 -u root -ppassword
Enter fullscreen mode Exit fullscreen mode

Docker Compose Setup

version: '3'

services:
  db:
    image: mysql:5.7
    container_name: db
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_ROOT_HOST: '%'
    ports:
      - "54321:3306"
    volumes:
      - dbdata:/var/lib/mysql
volumes:
  dbdata:

Enter fullscreen mode Exit fullscreen mode

Save the above file as docker-compose.yaml. Then run the following commands to start/stop the container. The container will persist data across restarts. If you want to clean up the volume use docker-compose down -v command.

docker-compose up
docker-compose up -d # runs in the background
docker-compose down
docker-compose down -v
Enter fullscreen mode Exit fullscreen mode

Top comments (0)