Aaron Script
Aaron Script

Reputation: 21

Can't use 127.0.0.1 to connect mysql server docker container, but can use IP?

In my personal PC, there's a docker mysql container binding with port 3306. It works well. I can connect the mysql server in this container over another PC.

sh$ mysql -hxxx.xxx.xxx.110 -uroot -p

In the host PC, I can connect the container by this way:

sh$ mysql -hxxx.xxx.xxx.110 -uroot -p

But when I try to connect the container to 127.0.0.1, it fails:

sh$ mysql -uroot -p

Known:

  1. In the container, I can connect the mysql server by:

       sh$ mysql -uroot -p
    
  2. there's nothing wrong with the password

  3. Meanwhile, I can connect 127.0.0.1:6376 over the host to the redis container....

Upvotes: 1

Views: 1712

Answers (3)

M.K
M.K

Reputation: 398

Please try follow steps: 1.login mysql with ip and use "select host,user,password from mysql.user;" to check 127.0.0.1 exists or not in command result; enter image description here

2.if not please execute grant command and ensure 127.0.0.1 has privileges,then tray login again.

grant all on *.* to root@'127.0.0.1' identified by 'root';
flush privileges;

Upvotes: 0

Mike Mackintosh
Mike Mackintosh

Reputation: 14237

You're going to want to check your my.cnf for the bind-address configuration:

# 
# File: my.cnf
#
[mysqld]
bind-address            = 192.168.1.100

In good practice and depending on your use-case, this should be 127.0.0.1.

Upvotes: -1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385204

I'm only speculating, but the Docker container likely has its own [pseudo] network interface. But you're trying to connect over the host's loopback interface. Those are two separate networks.

Remember, 127.0.0.1 is not just a special IP — it's an IP assigned by a separate network interface.

You can configure Docker to share the host's network stack; perhaps that would be best here.

Upvotes: 2

Related Questions