远程mysql_java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect

最近在做一个项目,里面要用到远程mysql数据库。

我想把我想要实现的功能说一下吧:

1 /**
2          * 是这样的功能:我的机器是A,我现在先利用我自己写的一个jdbc方法<br>
3          * 调用远程的机器B上安装的数据库<br>
4          * 然后把我想要CRUD操作B机器上的数据库<br>
5          * 或者简单一点,可以这样认为,我现在在机器A上面有一些数据<br>
6          * 我要把这些数据插入到远程的机器B的mysql数据库里面去<br>
7          * 就是这样的操作<br>
8          */

效果图如下:

下面是我在机器A上面写的测试类:

  1 /**
  2  *
  3  */
  4 package edu.gzucm.thms.utils;
  5
  6 import java.sql.Connection;
  7 import java.sql.DriverManager;
  8 import java.sql.PreparedStatement;
  9 import java.sql.SQLException;
 10 import java.util.Calendar;
 11
 12 /**
 13  * @author Hongten</br>
 14  * @date 2012-10-18
 15  *
 16  */
 17 public class SendData {
 18
 19     public static int sendDate2Server(String ip, String port,
 20             String databaseName, String userName, String passwrod,String receiver,String content) {
 21         int flag = 0;
 22         try {
 23             // 加载MySql的驱动类
 24             Class.forName("com.mysql.jdbc.Driver");
 25         } catch (ClassNotFoundException e) {
 26             System.out.println("找不到驱动程序类 ,加载驱动失败!");
 27             e.printStackTrace();
 28         }
 29         // 连接MySql数据库,用户名和密码都是root
 30         String url = "jdbc:mysql://"+ip+":"+port+"/"+databaseName;
 31         System.out.println(url);
 32         String username = "root";
 33         String password = "root";
 34         String sql = getSQL(receiver, content);
 35         Connection conn = null;
 36         PreparedStatement pstmt = null;
 37         try {
 38             conn = DriverManager.getConnection(url, username, password);
 39             pstmt = conn.prepareStatement(sql);
 40             flag = pstmt.executeUpdate(sql);
 41         } catch (SQLException se) {
 42             System.out.println("数据库连接失败!");
 43             se.printStackTrace();
 44         }
 45         if (pstmt != null) { // 关闭声明
 46             try {
 47                 pstmt.close();
 48             } catch (SQLException e) {
 49                 e.printStackTrace();
 50             }
 51         }
 52         if (conn != null) { // 关闭连接对象
 53             try {
 54                 conn.close();
 55             } catch (SQLException e) {
 56                 e.printStackTrace();
 57             }
 58         }
 59         return flag;
 60     }
 61
 62     public static String getSQL(String receiver,String content){
 63         /**
 64          * 下面是一些常量设置: retrytimes=0 pri=1 inpool=0 sendmode=1
 65          * sendtime和inserttime的格式是:2011-05-03 18:55:20
 66          */
 67         String now = getNow();
 68         return "insert into sms_boxsending(sender,receiver,content,sendtime,inserttime,retrytimes,pri,inpool,sendmode)values('system','"+receiver+"','"+content+"','"+now+"','"+now+"','0','1','0','1')";
 69     }
 70
 71     /**
 72      * 获取当前时间,并格式化时间
 73      *
 74      * @return 格式化当前的时间
 75      */
 76     public static String getNow() {
 77         Calendar calendar = Calendar.getInstance();
 78         int year = calendar.get(Calendar.YEAR);
 79         int month = calendar.get(Calendar.MONTH);
 80         int day = calendar.get(Calendar.DAY_OF_MONTH);
 81         int hour = calendar.get(Calendar.HOUR_OF_DAY);
 82         int min = calendar.get(Calendar.MINUTE);
 83         int sec = calendar.get(Calendar.SECOND);
 84
 85         Object monthString = (month < 10) ? "0" + month : month;
 86         Object dayString = (day < 10) ? "0" + day : day;
 87         Object hourString = (hour < 10) ? "0" + hour : hour;
 88         Object minString = (min < 10) ? "0" + min : min;
 89         Object secString = (sec < 10) ? "0" + sec : sec;
 90
 91         return "" + year + "-" + monthString + "-" + dayString + " "
 92                 + hourString + ":" + minString + ":" + secString;
 93     }
 94
 95     public static void main(String[] args) {
 96         //这里是调用远程机器B的mysql数据库
 97         System.out.println(sendDate2Server("210.38.111.34", "3306", "messagecat", null, null,"1342360****","this is a test 短信"));
 98         //这是测试本地的
 99         System.out.println(sendDate2Server("localhost", "3308", "messagecat", null, null,"1342360****","this is a test 短信"));
100     }
101 }

结果是这样的:

那我们应该怎样解决这个问题呢?

我也是查了一下资料,才把这个问题给搞定的。。。。

其实道理很简单,也就是说,远程的机器B不允许机器A访问他的数据库。也就是说,我们要解决这个问题,就是要让机器B的数据库允许

机器A访问,就搞定啦;

操作步骤也是很简单的:

一:打开mysql控制台,输入:

1 use mysql;
2
3 show tables;

二:输入:

1 select host from user;
2
3 update user set host ='%' where user ='root';

三:进入计算机的服务界面,重新启动mysql服务就搞定啦。。

四:下面是运行效果图:

返回值都为1,说明,程序运行正常....同时也祝你好运....

还有跟多方法:http://hi.baidu.com/soutnila/item/5356f2c0002781bf0d0a7bae

来源:http://www.cnblogs.com/hongten/archive/2012/10/25/mysql.html

while opening read connection: jdbc:mysql message from server: Host is not allowed to connect to t相关推荐

  1. java.sql.SQLException : null, message from server: “Host ‘‘ is not allowed to connect to this Maria

    文章目录 错误详情 错误原因 解决方案 最后 错误详情 今天在初始化云主机中Hive时报错如下: Underlying cause: java.sql.SQLException : null, mes ...

  2. 解决 javasql.null, message from server: “Host ‘‘ is not allowed to connect to this MySQL server“

    解决办法: 1.打开cmd,进入mysql mysql -u root -p 2.查看root用户的信息 use mysql; select user,host from user; 3.修改root ...

  3. 把mysql部署在局域网的服务器上,远程连接mysql时报错误代码1130 Host ‘***.***.***.***’is not allowed to connect to this MySQL

    ==刚学习mysql的时候,知道DBMS分为两类: ①基于文件共享的dbms(access) ②基于客户机–服务器的DBMS(Mysql.Oracle),在使用客户端时需要安装服务端,数据也存储在服务 ...

  4. MySQL(MariaDB):解决“ Host ‘*‘ is not allowed to connect to this MariaDB server”

    简单的权限问题,原因: MySQL没有设置远程(或局域网)的访问权限 可以先查询当前mysql的用户都有什么,顺手把没用的清一清.  如果和图中一样有一条 host=% 的用户信息,那么它其实就是一个 ...

  5. message from server: “Host ‘DESKTOP-FAJUM7V‘ is not allowed to connect to this MySQL server“

    一.现象(非本地(localhost)无法登录数据库) 后台无法连接数据库,并提示如下错误 message from server: "Host 'DESKTOP-FAJUM7V' is n ...

  6. SQLException:null, message from server: “Host ‘xx‘ is not allowed to connect to this MySQL server“

    java.sql.SQLException: null,  message from server: "Host 'PC-20220124VASY' is not allowed to co ...

  7. 服务器连接mysql数据库,报错message from server: “Host xxx is not allowed to connect to this MySQL server“

    操作系统 Windows 开发工具:IDEA 数据库:MySQL 使用MyBatis访问数据库的表提示错误信息: message from server: "Host xxx is not ...

  8. Hive连接Spark报错java.sql.SQLException: null, message from server: Host 'datanode03' is blocked becaus

    背景: 线上一些任务大部分使用Spark Sql来处理Hive的数据:今天任务由于数据量增大,任务耗时也增加,因此导致多个任务同时运行.但是后来任务迟迟运行不完,因此去查看任务日志: 16-08-20 ...

  9. java.sql.SQLException: null, message from server: Host 'XXXXX' is not allowed to connect

    本人问题出现在,在linux的spark shell上,使用JDBC从其他数据库读取数据时,已导入mysql-connector-java-5.1.47-bin.jar包,运行代码时报错. //以下x ...

最新文章

  1. 第十五届全国大学生智能车竞赛安徽赛区比赛报名信息
  2. 网络营销推广介绍网站该如何布置关键词?
  3. hibernate.hbm2ddl.auto配置详解
  4. Android端的短视频开发技术
  5. java注解和python装饰器_Java 的注解 和 Python 的装饰器 是一回事吗?
  6. oracle复制sequence,oracle sequence语句重置方介绍
  7. 自定义图片验证码认证过滤器
  8. 提高c 语言的方法,提高C程序效率的方法
  9. ARM Neon 列子 - Vector Add
  10. 关于C语言逻辑值的说法错误的是( ),2017年计算机二级c语言题库及答案
  11. VMware 克隆多个centos 7 操作系统
  12. Cocoa异常编程 NSException
  13. maven的setting.xml文件中只配置本地仓库路径的方法
  14. 1.程序员的自我修养---简介
  15. Flash cs6 学习(一) 制作一个包含几个按钮控件的影片剪辑
  16. 使用Unity粒子特效的基本步骤
  17. linux 安装Python3 并安装Python Blog Wagtail
  18. oracle 根据出生日期计算年龄
  19. 三维全景融合拼接技术
  20. 卡耐基梅隆大学计算机科学,卡耐基梅隆大学之计算机科学系

热门文章

  1. Kerberos简介,概述,协议内容,3次通信
  2. python数据类型详解(转自:http://www.cnblogs.com/linjiqin/p/3608541.html)
  3. 产品打包工具的制作,ant,编译源码,打jar包,打tag,打war包,备份release版本等
  4. Oracle中PLSQL中一个例外的写法
  5. pytorch中的pre-train函数模型引用及修改(增减网络层,修改某层参数等)
  6. 从GitHub中整理出来的15个最受欢迎的Python开源框架,你喜欢哪个
  7. Struts2的配置文件——web.xml
  8. cmake中的变量和命令的大小写
  9. IQ推理:红眼睛和蓝眼睛
  10. [Selenium] 基本使用