解决:连接MySQL的10060错误:Can‘t connect to MySQL server on ‘*.*.*.*‘(10060)

文章目录

问题描述

在这里插入图片描述

  1. 第一步:检查网络配置呗,能不能ping上客户机。(跳过)

  2. 第二步:客户机上的mysql没启动:systemctl status mysqld

    [root@localhost mysql]# systemctl status mysqld
    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since 四 2022-10-13 06:01:00 UTC; 41min ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
      Process: 10500 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
      Process: 10479 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
     Main PID: 10503 (mysqld)
        Tasks: 28
       CGroup: /system.slice/mysqld.service
               └─10503 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
    
    1013 06:01:00 localhost.localdomain systemd[1]: Stopped MySQL Server.
    1013 06:01:00 localhost.localdomain systemd[1]: Starting MySQL Server...
    1013 06:01:00 localhost.localdomain systemd[1]: Started MySQL Server.
    
  3. 第三步:防火墙上的3306端口没开放,查一下:ss -lnt

    [root@localhost mysql]# ss -lnt
    State      Recv-Q Send-Q                                                             Local Address:Port                                                                            Peer Address:Port              
    LISTEN     0      128                                                                            *:111                                                                                        *:*                  
    LISTEN     0      128                                                                            *:6000                                                                                       *:*                  
    LISTEN     0      5                                                                  192.168.122.1:53                                                                                         *:*                  
    LISTEN     0      128                                                                            *:22                                                                                         *:*                  
    LISTEN     0      128                                                                    127.0.0.1:631                                                                                        *:*                  
    LISTEN     0      100                                                                    127.0.0.1:25                                                                                         *:*                  
    LISTEN     0      128                                                                    127.0.0.1:6010                                                                                       *:*                  
    LISTEN     0      80                                                                            :::3306                                                                                      :::*                  
    LISTEN     0      128                                                                           :::111                                                                                       :::*                  
    LISTEN     0      128                                                                           :::6000                                                                                      :::*                  
    LISTEN     0      128                                                                           :::22                                                                                        :::*                  
    LISTEN     0      128                                                                          ::1:631                                                                                       :::*                  
    LISTEN     0      100                                                                          ::1:25                                                                                        :::*                  
    LISTEN     0      128                                                                          ::1:6010                                                                                      :::*                  
    
  4. 3306端口开了,但是3306端口没有被监听!(我的问题就是这),所以,先添加需要监听的端口:

    [root@localhost mysql]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
    Warning: ALREADY_ENABLED: 3306:tcp
    success
    [root@localhost mysql]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
    Warning: ALREADY_ENABLED: 3306:tcp
    success
    
    
  5. 加了3306端口之后可以测一下了,如果显示不允许连接,那么开启mysql ,使用root登录,进入mysql数据库,查询mysql.user这张表,可以看到HOST,是localhost,表示只能本地登录mysql,

    mysql> use mysql
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> select User,HOST from mysql.user;
    +---------------+-----------+
    | User          | HOST      |
    +---------------+-----------+
    | mysql.session | localhost |
    | mysql.sys     | localhost |
    | root          | localhost |
    +---------------+-----------+
    3 rows in set (0.00 sec)
    
    
  6. 增加远程访问权限,%百分号表示允许任何IP访问数据库,并刷新:flush privileges;,再重启下mysql数据库,接下来就可以正常登录了

    mysql> grant all privileges on *.* to root@"%" identified by "1234566";
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select User,HOST from mysql.user;
    +---------------+-----------+
    | User          | HOST      |
    +---------------+-----------+
    | root          | %         |
    | mysql.session | localhost |
    | mysql.sys     | localhost |
    | root          | localhost |
    +---------------+-----------+
    4 rows in set (0.00 sec)
    
    mysql> systemctl restart mysqld 
    

猜你喜欢

转载自blog.csdn.net/qq_43408367/article/details/127301332
今日推荐