当前位置: 代码迷 >> 综合 >> nacos-server连接本地mysql数据库遇到的问题
  详细解决方案

nacos-server连接本地mysql数据库遇到的问题

热度:36   发布时间:2023-10-23 13:22:41.0

本地mysql环境是8.0的版本

准备条件大家看过nacos的也肯定知道,本地新建一个nacos_config的数据库,在nacos的conf文件夹下导入sql脚本执行(nacos-mysql.sql)

然后在application.properties配置文件中添加本地数据库连接参数

第一次使用nacos-server-1.1.3

application.properties

spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://localhost:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=123456

点击start.cmd启动

 used by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)
        at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
        at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
        at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
        at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:151)
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115)
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78)
        ... 10 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
        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.jdbc.Util.handleNewInstance(Util.java:377)
        at com.mysql.jdbc.Util.getInstance(Util.java:360)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:870)
        at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2144)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069)
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
        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.jdbc.Util.handleNewInstance(Util.java:377)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
        at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
        at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
        at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
        at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
        ... 15 more
Caused by: java.lang.NullPointerException
        at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:2983)
        at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1873)
        at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1802)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1206)
        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2234)
        at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2085)
        ... 29 more

报数据库连接异常 ,异常也显示了,由于驱动版本不匹配导致的,所以修改驱动版本

参考:https://github.com/alibaba/nacos/pull/855 解决办法是说将mysql8.0的驱动放在 :xx / nacos / plugins / mysql 文件夹下(文件夹不存在要新建

nacos-server连接本地mysql数据库遇到的问题

重新启动,不出意外,错误跟上面一致,不起效果

有人回答是因为没有指向 plugins / mysql下去加载驱动,所以还需要修改startup.cmd 

nacos-server连接本地mysql数据库遇到的问题

发现还是不行, 又百度说需要下载源码更改驱动后重新打包(github好烦,一致打不开,擦)

nacos-server1.1.3使用的5.1.34驱动包,更改成8.0.22之后重新打包

nacos-server连接本地mysql数据库遇到的问题

 完成后重新启动(配置别忘了添加)即可

随后又使用了nacos-server1.3.2

官网上介绍从1.3之后就支持了8.0,此时不需要导入驱动包了,

更改配置本地库

nacos-server连接本地mysql数据库遇到的问题

启动过程中发现另一个问题,启动闪退

查看nacos.log发现报异常了

Caused by: com.alibaba.nacos.api.exception.NacosException: java.net.UnknownHostException: jmenv.tbsite.net
    at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.run(AddressServerMemberLookup.java:118)
    at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.start(AddressServerMemberLookup.java:75)
    at com.alibaba.nacos.core.cluster.ServerMemberManager.initAndStartLookup(ServerMemberManager.java:154)
    at com.alibaba.nacos.core.cluster.ServerMemberManager.init(ServerMemberManager.java:143)
    at com.alibaba.nacos.core.cluster.ServerMemberManager.<init>(ServerMemberManager.java:128)
    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 org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172)
    ... 114 common frames omitted
Caused by: java.net.UnknownHostException: jmenv.tbsite.net
    at java.net.InetAddress.getAllByName0(InetAddress.java:1281)
    at java.net.InetAddress.getAllByName(InetAddress.java:1193)
    at java.net.InetAddress.getAllByName(InetAddress.java:1127)
    at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:111)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
    at com.alibaba.nacos.common.http.BaseHttpClient.execute(BaseHttpClient.java:47)
    at com.alibaba.nacos.common.http.NacosSyncHttpClient.get(NacosSyncHttpClient.java:46)
    at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.syncFromAddressUrl(AddressServerMemberLookup.java:141)
    at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.run(AddressServerMemberLookup.java:109)
    ... 123 common frames omitted

这个异常什么意思呢,是说找不到主机(启动的时候默认是集群模式启动cluster

此时只需要更改startup.cmd的启动模式即可

nacos-server连接本地mysql数据库遇到的问题

测试添加一项配置,查看数据库中有没有

nacos-server连接本地mysql数据库遇到的问题

nacos-server连接本地mysql数据库遇到的问题 

第三次使用了最新版的nacos-server2.0.0

跟1.3.2一样的配置,startip.cmd改成单机模式,添加本地数据库配置,本以为能正常启动,但是...

Caused by: java.lang.IllegalStateException: Fail to init node, please see the logs to find the reason.
        at com.alipay.sofa.jraft.RaftServiceFactory.createAndInitRaftNode(RaftServiceFactory.java:48)
        at com.alipay.sofa.jraft.RaftGroupService.start(RaftGroupService.java:129)
        at com.alibaba.nacos.core.distributed.raft.JRaftServer.createMultiRaftGroup(JRaftServer.java:268)
        at com.alibaba.nacos.core.distributed.raft.JRaftProtocol.addRequestProcessors(JRaftProtocol.java:163)
        at com.alibaba.nacos.naming.core.v2.service.impl.PersistentClientOperationServiceImpl.<init>(PersistentClientOperationServiceImpl.java:92)
        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 org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:175)
        ... 57 common frames omitted
tted

这个异常大致意思就是响应已经结束,导致的情况特别多,因为之前部署的时候也遇到过,我首选想到的是,部署路径还有非法字符(比如中文...)

果然,我把架包直接放在d盘下重新启动,成功

nacos-server连接本地mysql数据库遇到的问题

nacos-server连接本地mysql数据库遇到的问题 

测试数据插入就不测试了,2.0能读到刚刚插入的数据,那肯定也能正常插入,可能2.0启动更为严格吧。。。

  相关解决方案