zoukankan      html  css  js  c++  java
  • 宿主机连接docker中的mysql

    宿主机连接docker中的mysql

     

    dokcer安装mysql


    docker run 
    --name mysql 
    -v $PWD/mysql:/var/lib/mysql 
    -p 3306:3306 
    -e MYSQL_ROOT_PASSWORD=123456 
    -d mysql:5.7
    

    宿主机连接docker中的mysql


    错误的连接方式

    $ mysql -u root -p
    Enter password: 
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket 
    '/var/run/mysqld/mysqld.sock' (2)
    # 可以看出这样会报错 
    

    正确的连接方式

    $ mysql -h 127.0.0.1 -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 9
    Server version: 5.7.26 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2019, 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> 
    
    

    在docker容器中连接宿主机中的mysql


    1. 查看宿主机和docker之间的桥接ip
    $ ifconfig
    docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
            inet6 fe80::42:8aff:febc:8533  prefixlen 64  scopeid 0x20<link>
            ether 02:42:8a:bc:85:33  txqueuelen 0  (以太网)
            RX packets 4779  bytes 11624681 (11.6 MB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 6006  bytes 441594 (441.5 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    # 宿主机在与容器同一局域网的IP地址一般是docker0对应的IP地址段的首个地址
    即(172.17.0.1 )
    
    1. 在容器中连接宿主机的mysql
    $ mysql -h 172.17.0.1 -u root -p
  • 相关阅读:
    java.lang.NoClassDefFoundError: sun/swing/plaf/synth/SynthIcon
    微信数据库解密--代码报错:未定义标识符 HMAC_CTX_init
    Windows家庭版组策略编辑
    Visual Studio x64 使用 .asm 文件
    某数据库管理软件离线注册分析
    EV剪辑分析
    ciscn_lonelywolf
    arm-pwn
    libc-2.32.so在orw上与其他版本的区别
    更新libc
  • 原文地址:https://www.cnblogs.com/lgj8/p/12425185.html
Copyright © 2011-2022 走看看