docker-library / mysql

Docker Official Image packaging for MySQL Community Server
https://dev.mysql.com/
GNU General Public License v2.0
2.47k stars 2.2k forks source link

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111) #732

Closed Certseeds closed 3 years ago

Certseeds commented 3 years ago

hello, I found that i can not connect with database inside of docker. enviorment is newest docker mysql image, mysql Ver 8.0.22.,python3 3.8.3 anaconda,docker-py first, I build and start a docker container by

    import docker
    clientAPI = docker.from_env().api
    docker_id = clientAPI.create_container(
        image="mysql",
        detach=True,
        name=dockerContainerName,
        environment={"MYSQL_ROOT_PASSWORD": "123456"},
        ports=[3306],  # inside ports
        volumes=['/tmp/folder'],
        host_config=clientAPI.create_host_config(
           port_bindings={3306: 3310},
            binds={'/home/nanoseeds/docker_dir/mysql': {
                'bind': '/tmp/folder',
                'mode': 'rw'
            }}
        )
    )
    clientAPI.start(docker_id['Id'])

then, i exec a command by

output1 = clientAPI.exec_start(clientAPI.exec_create(
        container=name, cmd="/bin/bash /tmp/folder/script.sh"))
for i in output1.splitlines():
    print(i)

inside the script.sh there are

echo "[client]
protocol=tcp
host=127.0.0.1
user=root
password=123456
port=3306
" > "${HOME}"/.mysql
echo "CREATE DATABASE tempTable DEFAULT CHARACTER SET utf8mb4;" > buildDataBase.sql  
  mysql --defaults-file="${HOME}"/.mysql < buildDataBase.sql

but the output of output1 is always b"ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)"

is there exists some tricks can Eliminate this error?

PS:link1 this one is useless after test.

wglambert commented 3 years ago

What's the full docker logs output? Was the server ready to accept connections at the time?

$ docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.22
991278a4537db7b8fa38076017ee1d4a3f1e90d1fb7e9bded4bf1ca74fec4816

$ docker exec -it mysql bash

root@991278a4537d:/# mysql -uroot -p123456 -h 127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
Certseeds commented 3 years ago

I found that the service in docker need nearly 9 second to be ready for connections, that is the reason why it cannot connect, thanks.

DagimAsnake commented 10 months ago

I found that the service in docker need nearly 9 second to be ready for connections, that is the reason why it cannot connect, thanks.

Am getting this error and if you have solved it how did you solved it........