美文网首页CentOS
Dokcer mysql 无法连接

Dokcer mysql 无法连接

作者: Rinaloving | 来源:发表于2023-03-04 23:36 被阅读0次

    mysql 无法连接

    1. 解决

    • 进入容器
    docker exec -it mysql1 /bin/bash
    
    • 登录mysql
    mysql -u root -p
    
    • 修改用户信息(让外面可以访问)
     ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
    
    • 查看修改结果 (root 用户的 host 已经变为 % ,表示非本地(localhost)也可访问)
    select host,user,plugin,authentication_string from mysql.user;
    
    • 操作明细
    root@Pomelo:~# docker exec -it mysql1 /bin/bash
    bash-4.4# ls
    bin  boot  dev  docker-entrypoint-initdb.d  entrypoint.sh  etc  home  lib  lib64  media  mnt  mysql  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    bash-4.4# whereis mysql
    bash: whereis: command not found
    bash-4.4# cd mysql/
    bash-4.4# ls
    bash-4.4# cd ..
    bash-4.4# mysql -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 14
    Server version: 8.0.32 MySQL Community Server - GPL
    
    Copyright (c) 2000, 2023, Oracle and/or its affiliates.
    
    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> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
    Query OK, 0 rows affected (0.21 sec)
    
    mysql> select host,user,plugin,authentication_string from mysql.user;
    +-----------+------------------+-----------------------+------------------------------------------------------------------------+
    | host      | user             | plugin                | authentication_string                                                  |
    +-----------+------------------+-----------------------+------------------------------------------------------------------------+
    | %         | root             | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9                              |
    | localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
    | localhost | mysql.session    | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
    | localhost | mysql.sys        | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
    | localhost | root             | caching_sha2_password | $A$005$*Fe\Uv}X-@?%L7vmwodbeCTCMRNJuRlISFbP/2D6FDyjPkzYqrltE5Pi7 |
    +-----------+------------------+-----------------------+------------------------------------------------------------------------+
    5 rows in set (0.00 sec)
    
    mysql> 
    

    2. 再次连接

    QQ截图20230305233341.png

    3. 补充

    • 如果要正常退出不关闭容器,可以通过按Ctrl+P+Q进行退出容器

    • 从mysql中退出容器:Ctrl+D,按两次


      QQ截图20230305233630.png

    4. 参考文档

    相关文章

      网友评论

        本文标题:Dokcer mysql 无法连接

        本文链接:https://www.haomeiwen.com/subject/akhuldtx.html