Home  >  Q&A  >  body text

无法连接docker里的mysql服务器

1.宿主机无法连接docker里的mysql
报错如下:
[root@localhost ~]# mysql -h 127.0.0.1 -uroot -p -A
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

如果是localhost。则报错如下:
[root@localhost ~]# mysql -P 3306 -uroot -p -A
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

PHP中文网PHP中文网2712 days ago840

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 15:04:32

    First of all, you need to figure out what your port 3306 is listening to?
    If you have not started mysql on the host machine on 3306, you can use it when starting the mysql program in docker

    docker start  -p 3306:3306 。。。。
    

    Map docker’s 3306 port to the host’s 3306 port.
    This way you can use it on the host machine

    mysql -h localhost -uroot -p 
    

    Connect to mysql in docker.

    reply
    0
  • Cancelreply