【问题标题】:Java - connecting to a database on another pcJava - 连接到另一台电脑上的数据库
【发布时间】:2016-12-21 01:22:06
【问题描述】:

我有一台运行 wamp 服务器和 mysql 数据库的 PC A。

和想要连接到该数据库的 PC B,我正在使用 Spring jpa、JDBC,试图获得直接访问。

到目前为止我做了什么?

PC A 可以很好地连接到数据库,因为它是本地的。

PC B 没有连接权限,抛出“拒绝访问”异常,所以我做了以下操作:

将 myDB 上的所有权限授予由“root”标识的“root”@“myip”;

然而,第二次尝试,抛出另一个异常,用户 ''@'myip' 没有权限!

无论如何,只是为了确保,我给并清空了用户对该 IP 的所有权限。

但仍然抛出异常“选择命令被拒绝”,,(很确定它看不到数据库)

这些是我的 database.properties:

javax.persistence.jdbc.url=jdbc:mysql://myIp:3306/myDB?useUnicode=yes&characterEncoding=UTF-8&characterSetResults=UTF-8
javax.persistence.jdbc.user=root
javax.persistence.jdbc.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect

弹簧配置:

@Configuration
@EnableJpaRepositories("chechecn.elections.organizer.repository")
@EnableTransactionManagement
public class SpringConfig {

    @Bean
    public DataSource dataSource() {

        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.HSQL).build();
    }

    @Bean(name = "entityManagerFactory")
    public EntityManagerFactory entityManagerFactory() {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("chechecn.elections.organizer");

        factory.setJpaProperties(additionalJpaProperties());

        // factory.setDataSource(dataSource());

        factory.afterPropertiesSet();

        return factory.getObject();
    }

    private Properties additionalJpaProperties() {
        Properties properties =     PropertiesReader.instance.getPropValues(PropertiesConstants.DATABASE_PROPERTIES);

        return properties;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {

        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }


    @Bean
    public ServiceConnector serviceConnector() {
        return new ServiceConnector();
    }
}

【问题讨论】:

  • mysql设置支持联网?
  • 我真的不知道,你能帮忙吗?提示?
  • @PauChorro 反正不使用那个 bean
  • @NeilStockton 解决了,我所要做的就是为该 IP 启用 root 访问权限

标签: java mysql spring jpa


【解决方案1】:

您确定您对该地址的另一台计算机具有 root 访问权限吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 2013-08-24
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多