Search code examples
mysqldockerheidisql

Unable to connect to mysl container via HeidiSQL via localhost


Environment:

I've created and started a Docker container on Windows 11 via the following command:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:latest

Then I've created a new HeidiSQL session with the following options:

library: libmysql.dll
host: 127.0.0.1
user: root
password: root
port: 3306

After click on Open it says "Can't connect to MySQL server on '127.0.0.1' (10061)".

I'am able to connect to the container via the terminal.


Solution

  • After some research and some experiment I am able to find a solution.

    First of all the port fordwarding must be forced to be 3306:3306.
    You must create and run the Docker container with the parameter -p 3306:3306.

    docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:latest
    

    Now the error is changed, but at least it can connect to the server.

    "Access denied for user 'root'@'172.17.0.1' (using password: YES)"

    Now most solutions found on the web tells to create a new user 'root'@'172.17.0.1' and grant all privileges to the user.

    But, even after creating the user, HeidiSQL is unable to login again (same "access denied" error).
    The strange thing is that if you login with the new user with a terminal, and then, try to login with HeidiSQL, now it works, this happens everytime you restart the container.

    After searching I found this: https://github.com/HeidiSQL/HeidiSQL/issues/980

    So the problem is that libmysql.dll is outdated and not supported, and it fails everytime you try to access for the first time with HeidiSQL.

    You must switch to libmariadb.dll that is also compatible with mysql, is not needed to create the new user.

    Do not use libmysql-6.1.dll, because crashes and not works for some reasons...