springboot+mybaits+redis + mysql连接问题 解决方法
最近在学习springboot+mybaits+redis + mysql并使用时遇到的一堆问题
// An highlighted block
2021-01-22 18:50:25.760 ERROR 13780 --- [io-8080-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379] with root cause
java.net.ConnectException: Connection refused: no further information
sport-4.1.55.Final.jar:4.1.55.Final]
现在找一下原因
Unable to connect to Redis;
nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379] with root cause
发现时redis的连接出现了问题这时候我去看了我的redis配置文件
#redis配置
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=123456
spring.redis.timeout=3000
spring.redis.jedis.pool.max-idle=200
spring.redis.jedis.pool.max-active=2000
spring.redis.jedis.pool.max-wait=1000
并没有发现什么不妥之处,我决定试一试用cmd连接redis看看是否是信息输入错误,结果突然发现没有启动redis(我真是个大聪明🙃)
使用cmd连接发现可以连接,这时候我开始怀疑是否是引号的问题决定尝试一下
2021-01-22 19:27:19.334 ERROR 14832 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379] with root cause
io.lettuce.core.RedisCommandExecutionException: ERR Client sent AUTH, but no password is set
at io.lettuce.core.internal.ExceptionFactory.createExecutionException(ExceptionFactory.java:137) ~[lettuce-core-6.0.1.RELEASE.jar:6.0.1.RELEASE]
at io.lettuce.core.internal.ExceptionFactory.createExecutionException(ExceptionFactory.java:110) ~[lettuce-core-6.0.1.RELEASE.jar:6.0.1.RELEASE]
at io.lettuce.core.protocol.AsyncCommand.completeResult(AsyncCommand.java:120) ~[lettuce-core-6.0.1.RELEASE.jar:6.0.1.RELEASE]
at io.lettuce.core.protocol.AsyncCommand.complete(AsyncCommand.java:111) ~[lettuce-core-6.0.1.RELEASE.jar:6.0.1.RELEASE]
发现报错内容发生改变
这个意思是默认的redis是没有密码的你需要设置一下
然后设置了一下密码看看是否能够解决问题
redis 的问题解决了 ,但是又出来一个mysql的报错,/(ㄒoㄒ)/~~
果然是写bug的一天
2021-01-22 19:34:20.666 ERROR 14832 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLSyntaxErrorException: Unknown database 'zhang123'
### The error may exist in com/example/shlm/controller/mappers/UserMapper.java (best guess)
### The error may involve com.example.shlm.controller.mappers.UserMapper.selectAll
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLSyntaxErrorException: Unknown database 'zhang123'] with root cause
java.sql.SQLSyntaxErrorException: Unknown database 'zhang123'
意思是说我获取JDBC连接失败,查询时获取失败,根本原因很可能是这个‘zhang123’的数据库
然后我就去查看我的mysql的配置文件
结果发现根本没有‘zhang123’的任何信息,这时我突然发现我的yml(本来是没有的想用yml看起来更简洁一点,自己创了一个因为懒得再写,就没有使用)文件没有删除,也就是现在我有properties 和yml后缀的两个配置文件
可是我记得只要有properties在,这个yml的后缀文件就会无效的 ,但我还是打开了yml后缀的文件查看究竟
果然发现了zhang123的相关信息,我决定删除yml看看是否能够连接 成功
2021-01-22 19:59:00.384 ERROR 14720 --- [nio-8080-exec-2] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specific time zone value if you want to utilize time zone support.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.22.jar:8.0.22]
果然人生和写代码一样不可能是一帆风顺的
这次告诉我这个叫HikariPool-1-池初始化期间出现异常
当然数据库连接池出现问题归根结底就是连接的问题,我看了看旁边正在运行的Navicat for MySQL客户端,显然不是数据库的问题,又是我配置文件的问题
#mysql配置
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shlm?useUnicode=true&characterEncoding=UTF-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
看了看配置好像没啥子问题,唉,继续看错误
The server time zone value,服务器时区值的问题刚才竟然没注意到,😥,url后面+&serverTimezone=UTC
在配置语句中加上时区试一试
终于成功了