Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to MySQL in Docker from local host (Docker for Mac beta)

On Mac OS X El Capitan.

Used to use Docker Toolbox. Just removed that and installed Docker for Mac beta. (I think the removal and installation is clean.)

Essentially launching a MySQL server container like this:

docker run -e MYSQL_ROOT_PASSWORD=1234 -d mysql:5.7.13

(or mysql/mysql-server:5.7.13). The launch seems to be fine.Then I tried to connect to this server from Sequel Pro, via "Standard" way, host 127.0.0.1, root, 1234. Got

Unable to connect to host 127.0.0.1 because access was denied.

Double-check your username and password and ensure that access from your current location is permitted.

MySQL said: Access denied for user 'root'@'localhost' (using password: YES)

Using -p 3306:3306 did not help.

It used to work with Docker Toolbox where I launched by

    docker run \
      -e MYSQL_ROOT_PASSWORD=1234 \
      -d \
      --expose 3306 \
      -p 3306:3306 \
      mysql:5.7.12

and connected to host 192.168.99.100. That worked fine.

I suspect the Docker for Mac "migration" during installation (copying existing Docker stuff into the new VM) may keep some old data (from the old MySQL containers) that is causing issues, but don't know how to proceed.

Thanks for your help!

like image 564
zpz Avatar asked Oct 30 '22 01:10

zpz


1 Answers

Looks like you've not configured access for root user from other hosts rather than default localhost.

Passing -p 3306:3306 you just creating a port forwarding from host machine to guest one. So you can simply allow your user to connect to the DB from your host machine which is 172.17.0.1.

E.g. I'm using following dummy container startup script which changes root's password in case it won't changed before:

#!/bin/bash

cd $(dirname $0)

docker-compose up -d

ROOT_PASSWORD=root
CONTAINER=____mysql_container_name___
CHANGE_PASS_SQL="delete from mysql.user;grant all on *.* to 'root'@'%' identified by '${ROOT_PASSWORD}' with grant option;flush privileges;"
CHANGE_PASS="docker exec -it ${CONTAINER} mysql -s -uroot -e \"${CHANGE_PASS_SQL}\" 2>/dev/null"

WITH_PASS='echo "select now();" | docker exec -i ${CONTAINER} mysql -root -proot 1>&2 2>/dev/null'
NO_PASS='echo "select now();" | docker exec -i ${CONTAINER} mysql -root 1>&2 2>/dev/null'

WITH_PASS_EXIT_CODE=1000
NO_PASS_EXIT_CODE=1000

while [ ${WITH_PASS_EXIT_CODE} -gt 0 ];do
    eval "${WITH_PASS}"
    WITH_PASS_EXIT_CODE=$?
    eval "${NO_PASS}"
    NO_PASS_EXIT_CODE=$?

    echo -n .

    if [ ${NO_PASS_EXIT_CODE} -eq 0 ];then
        eval "${CHANGE_PASS}"
    else
        sleep 1
    fi
done

echo

Hope this will help.

like image 122
arhip Avatar answered Nov 15 '22 06:11

arhip