baomidou / mybatis-plus

An powerful enhanced toolkit of MyBatis for simplify development
https://baomidou.com
Apache License 2.0
16.42k stars 4.31k forks source link

本地springboot应用使用MP如何才能连接远程ECS服务器中docker的mysql? #2363

Closed flypangzhi closed 4 years ago

flypangzhi commented 4 years ago

版本信息

springboot 2.1.9.RELEASE
mybatis-plus-generator  3.1.2
mybatis-plus-boot-starter  3.1.2 
mybatis-ehcache 1.0.0
org.apache.velocity  2.1 
druid-spring-boot-starter  1.1.18

重要信息:

在本地vavicat使用SSH通道可以连接上数据库,应该排除网络和端口的错误问题

该问题是怎么引起的?**

1、本地使用代码生成器连接远程ECS服务器中docker的mysql失败。
2、还有springboot的yml如何配置连接信息也不得而知

报错信息

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:827)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:447)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.baomidou.mybatisplus.generator.config.DataSourceConfig.getConn(DataSourceConfig.java:191)
    at com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder.handlerDataSource(ConfigBuilder.java:269)
    at com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder.<init>(ConfigBuilder.java:132)
    at com.baomidou.mybatisplus.generator.AutoGenerator.execute(AutoGenerator.java:92)
    at com.project.generator.MybatisGenerator.main(MybatisGenerator.java:106)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
    at com.mysql.cj.NativeSession.connect(NativeSession.java:150)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:947)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:817)
    ... 10 more
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
    ... 13 more
Exception in thread "main" java.lang.NullPointerException
    at com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder.getTablesInfo(ConfigBuilder.java:457)
    at com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder.handlerStrategy(ConfigBuilder.java:281)
    at com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder.<init>(ConfigBuilder.java:142)
    at com.baomidou.mybatisplus.generator.AutoGenerator.execute(AutoGenerator.java:92)
    at com.project.generator.MybatisGenerator.main(MybatisGenerator.java:106)

生成器代码

public class MybatisGenerator {

    private static String baseOutPutDir = "/src/main/java";

    private static String author = "huangjg";

    private static DbType dbType = DbType.MYSQL;

    private static String dataSourceDriverName = "com.mysql.cj.jdbc.Driver";

    private static String dataSourceUrl = "jdbc:mysql://111.11.11.111:3309/t_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8";

    private static String dataSourceUserName = "root";

    private static String dataSourcePassword = "root";

    private static String basePackageName = "com.project.modular.user";

    private static String[] includeTable = { "user" };

    private static String[] excludeTable = {};

    public static void main(String[] args) throws InterruptedException {
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + baseOutPutDir);
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setIdType(IdType.AUTO);
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(false);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        gc.setOpen(false);
        gc.setAuthor(author);
        // 自定义文件命名,注意 %s 会自动填充表实体属性!
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("I%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(dbType);
        dsc.setDriverName(dataSourceDriverName);
        dsc.setUrl(dataSourceUrl);
        dsc.setUsername(dataSourceUserName);
        dsc.setPassword(dataSourcePassword);
        mpg.setDataSource(dsc);
        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(basePackageName);
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setXml("mapper.mapping");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setController("controller");
        mpg.setPackageInfo(pc);
        // 模板配置
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml("/templates/mapper.xml.vm");
        mpg.setTemplate(templateConfig);
        InjectionConfig injectionConfig = new InjectionConfig() {

            // 使用map进行自定义属性设置
            @Override
            public void initMap() {
                // Map<String, Object> map = new HashMap<>();
                // map.put("abc", this.getConfig().getGlobalConfig().getAuthor()
                // + "-mp");
                // this.setMap(map);
            }
        };
        // 配置自定义属性注入
        mpg.setCfg(injectionConfig);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // 此处可以修改为您的表前缀
        // strategy.setTablePrefix(new String[] { "tb_" });
        // 表名生成策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        // 需要生成的表
        strategy.setInclude(includeTable);
        // 排除生成的表
        strategy.setExclude(excludeTable);
        // 使用lombok
        strategy.setEntityLombokModel(true);
        mpg.setStrategy(strategy);
        // 执行生成
        mpg.execute();
    }
}
miemieYaho commented 4 years ago

数据库连接和我们无关,自己百度