springboot连接mysql问题解决

学习的项目路径:https://www.bysocket.com/?p=1627
即:https://github.com/JeffLi1993/springboot-learning-example 下面的springboot-restfull

准备工作:下载了Postman工具做http请求接口验证。使用2018-idea IDE。
工程放在了自己的MAC本上。

1. mac上新安装了8.0.13的mysql
   工程项目是使用的mysql-connector-java:5.1.39
   直接修改成8.0.13之后会报错,再接着修改其它的依赖,也不行,无奈,项目中依赖的包太多。
   
 => 准备使用自己台式机的上mysql,台式机上的是5.5.47版本,应该可以。
 2. 在工程中配置使用自己的台式机的mysql。
    依然连接不上。
    报错:Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

=> 按照朋友的推荐下载了一个mysqlworkbench。MAC mysql可以连接,但是台式机上的mysql连接不上。
    中间报错can't connect to mysql server***(61)
    最后的解决方法是:
    a. 创建了一个mysql账户,使其有在所有IP上的连接的权限。
    CREATE USER 'spring'@'%' IDENTIFIED WITH mysql_native_password BY 'springboot';  v8.0.13
    CREATE USER 'spring'@'%' IDENTIFIED BY 'springboot'; v5.5.47
    grant all privileges on *.* to 'spring'@'%';
    b. 在/etc/mysql/my.cnf里的 bind-address           = 127.0.0.1 为 bind-address           = 0.0.0.0
    即设置为所有的地址可以连接该mysql服务。
    c. 另外,注意/etc/mysql/my.cnf 里面配置的端口是你在项目中配置的端口。
    也可以使用命令行mysql -uroot -p, 进入mysql后使用show global variables like 'port'; 来查看mysql使用的端口。
    d. 通过lsof -i :3306 或者 netstat -tulpen 检查该端口是否被你的mysql而非其它的应用占用。
    e. 另外,依然连接不上话,检查是否该端口被墙了。
    sudo ufw status
    查看防火墙的状态。如果是inactive就不用管了。
    或者 直接 关闭防火墙sudo ufw disable

该问题前后折腾了快一天,我的苍天啊~~
    

发布了8 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/xnfreedom/article/details/86593975