Linux命令(十)之使用Windows客户端连接Linux系统中的MySQL时产生的错误已经解决

若在windows使用工具连接MySQL是报如下两种错误:

错误1:

  An error occurred while establishing the connection:

  Long Message:
  Communications link failure

  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

  Details:
     Type: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
     SQL State: 08S01

错误2:

  An error occurred while establishing the connection:

  Long Message:
  null, message from server: "Host '192.168.202.1' is not allowed to connect to this MySQL server"

  Details:
     Type: java.sql.SQLException
     Error Code: 1130
     SQL State: HY000

注:不使用工具的话连接,使用windows的cmd命令远程连接的MySQL的命令为:

    mysql -h 远程主机的地址 -u 用户名 -p 

    如:mysql -h 192.168.202.132 -u root -p

1. 产生错误1的原因被Linux的防火墙拦截了访问

2. 产生错误2的原因:没有权限访问Linux中的mysql

  注:先解决错误1再解决错误2

错误1的解决方法:①在防火墙中直接开放3306的端口;②或者直接关闭防火墙(强烈不推荐)

  具体操作步骤,请参考博客:https://www.cnblogs.com/sun-flower1314/p/11214237.html

错误2的解决方法:

  1)在Linux上,登陆MySQL:

    mysql -u root -p  (用root用户登陆)

  2)查看MySQL的已有用户及权限

    use mysql;

    select host, user, password from user;

  从下图中可以看到,只能本地访问该mysql服务。

  

   3)插入特定数据后,再次查看user表内容

    insert into user(host,user,password) values('%','root','*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B');

  4)为root@%授权,让其具有所有权限

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;

  5)插入成功后,重启MySQL服务就可以了

    service mysql restart

若有遇到其他问题,请留言

原文地址:https://www.cnblogs.com/sun-flower1314/p/11239430.html