项目场景:

最近入手了一台MacBookPro(谨慎尝试,用惯了windos后使用感具别扭,需要大量时间来适应),下载了最新的idea和最新的mysql8.0.23,然后开始了漫长而艰辛的开始,各种没遇到过的问题接踵而来。。。。。接下来说一下这个让我各种方法解决了两天的问题。
提前说一下,mysql8.0.23已安装完成并在后台可以使用。


问题描述:

com.mysql.cj.jdbc.exceptions.CommunicationsException:
Communications link failure

提示一下,这个问题可以出现在很多地方。
1、idea连接mysql数据库

2、idea中写代码访问数据库


原因分析:

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure。 翻译过来就是通信链路失败,很明显是根本就没有连接到数据库。然后通过后台明明可以访问,说明是idea在连接mysql数据库的过程中出现了问题。

由于是第一次使用Mac系统,第一次使用mysql8.0.23,所以开始了漫长的两天百度解决过程。接下来粗暴的总结一下:

问题一、mac版本的mysql是没有配置文件的。windos版本中的mysql安装后会有一个my.ini的配置文件,可以在其中配置mysql的一系列参数,而mac版本中则需要坑人的手动添加才可以(对刚接触的人很不友好)!!!

问题二:mysql的注册驱动问题。之前一直使用的是很稳定的mysql5.7,但查询后得知mysql8.0之后,在驱动这里发生了一些改变。
(com.mysql.cj.jdbc.Driver)

问题三:连接数据库问题。mysql8.0之后新添加了一个时区的概念(serverTimezone)。

具体的原因不是太懂,但经过两天的查询,试过了各种解决方法,又遇到了各种莫名其妙的问题,最终是解决了。接下来不会对问题有具体的分析,直接来简单粗暴的解决方法!!!!!!!


解决方案:

问题一的解决方法:

知道问题了,我们要做的就是给自己的mysql加上配置文件。

这里要注意一下:
1、windows中的配置文件是my.ini,而mac中的配置文件是my.cnf。
2、关于配置文件的位置,查询到的一篇文章中说有四个默认读取位置,有兴趣的小伙伴可以自行查询了解,这里就不一一说明了。

接下来就是在Linux中操作添加配置文件:
1、创建配置文件
首先,进入到 /etc文件夹下 。。。。 cd /etc
然后在 /etc 新建 my.cnf 文件 。。。。sudo vim my.cnf(一定要加上sudo,否则直接打开显示是只读模式,就算你费劲修改了也不能保存)
2、填写配置文件(注意一下,不要有空行,之前搜到了一篇里面好多空行,执行后就报错了)

# Example MySQL config file for medium systems.## This is for a system with little memory (32M - 64M) where MySQL plays# an important part, or systems up to 128M where MySQL is used together with# other programs (such as a web server)## MySQL programs look for option files in a set of# locations which depend on the deployment platform.# You can copy this option file to one of those# locations. For information about these locations, see:# http://dev.mysql.com/doc/mysql/en/option-files.html## In this file, you can use all long options that a program supports.# If you want to know which options a program supports, run the program# with the "--help" option.# The following options will be passed to all MySQL clients[client]default-character-set=utf8#password = your_passwordport = 3306socket = /tmp/mysql.sock# Here follows entries for some specific programs# The MySQL server[mysqld]character-set-server=utf8init_connect='SET NAMES utf8port = 3306socket = /tmp/mysql.sockskip-external-lockingkey_buffer_size = 16Mmax_allowed_packet = 1Mtable_open_cache = 64sort_buffer_size = 512Knet_buffer_length = 8Kread_buffer_size = 256Kread_rnd_buffer_size = 512Kmyisam_sort_buffer_size = 8Mcharacter-set-server=utf8init_connect='SET NAMES utf8'
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking# Replication Master Server (default)# binary logging is required for replication log-bin=mysql-bin# binary logging format - mixed recommendedbinlog_format=mixed# required unique id between 1 and 2^32 - 1# defaults to 1 if master-host is not set# but will not function as a master if omittedserver-id = 1# Replication Slave (comment out master section to use this)## To configure this host as a replication slave, you can choose between# two methods :## 1) Use the CHANGE MASTER TO command (fully described in our manual) -# the syntax is:## CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,# MASTER_USER=<user>, MASTER_PASSWORD=<password> ;## where you replace <host>, <user>, <password> by quoted strings and# <port> by the master's port number (3306 by default).## Example:## CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,# MASTER_USER='joe', MASTER_PASSWORD='secret';##OR## 2) Set the variables below. However, in case you choose this method, then# start replication for the first time (even unsuccessfully, for example# if you mistyped the password in master-password and the slave fails to# connect), the slave will create a master.info file, and any later# change in this file to the variables' values below will be ignored and# overridden by the content of the master.info file, unless you shutdown# the slave server, delete master.info and restart the slaver server.# For that reason, you may want to leave the lines below untouched# (commented) and instead use CHANGE MASTER TO (see above)## required unique id between 2 and 2^32 - 1# (and different from the master)# defaults to 2 if master-host is set# but will not function as a slave if omitted# server-id = 2## The replication master for this slave - required#master-host = <hostname>## The username the slave will use for authentication when connecting# to the master - required#master-user = <username>## The password the slave will authenticate with when connecting to# the master - required#master-password = <password>## The port the master is listening on.# optional - defaults to 3306#master-port = <port>## binary logging - not required for slaves, but recommended#log-bin=mysql-bin# Uncomment the following if you are using InnoDB tables#innodb_data_home_dir = /usr/local/mysql/data#innodb_data_file_path = ibdata1:10M:autoextend#innodb_log_group_home_dir = /usr/local/mysql/data# You can set .._buffer_pool_size up to 50 - 80 %# of RAM but beware of setting memory usage too high#innodb_buffer_pool_size = 16M#innodb_additional_mem_pool_size = 2M# Set .._log_file_size to 25 % of buffer pool size#innodb_log_file_size = 5M#innodb_log_buffer_size = 8M#innodb_flush_log_at_trx_commit = 1#innodb_lock_wait_timeout = 50[mysqldump]quickmax_allowed_packet = 16M[mysql]no-auto-rehash# Remove the next comment character if you are not familiar with SQL#safe-updatesdefault-character-set=utf8[myisamchk]key_buffer_size = 20Msort_buffer_size = 20Mread_buffer = 2Mwrite_buffer = 2M[mysqlhotcopy]interactive-timeout
  1. 保存my.cnf文件
    :wq!

  2. 修改my.cnf的文件权限

sudo chmod 664 /etc/my.cnf
//如果是修改为其他的777、666等mysql认为这是不安全的,所以自动忽略. 请将将文件权限改为mysql认可的664
  1. 重启mysql后,配置文件就可以生效了
    啟動MySQL服務 sudo /usr/local/mysql/support-files/mysql.server start
    停止MySQL服務 sudo /usr/local/mysql/support-files/mysql.server stop
    重啟MySQL服務 sudo /usr/local/mysql/support-files/mysql.server restart

重启服务这里要注意一下,可能会出现失败的问题,我就遇到了一个问题:
ERROR! The server quit without updating PID file
这里可以检查一下,mysql的状态(比如mysql已经启动了你还执行启动命令),可以多确认和执行几次,如果还不行就百度查查。

问题二和问题的解决方法:
这个问题是mysql8.0后的驱动和连接变化,可以参考以下代码:

package jdbc;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class JDBCtest01 {public static void main(String[] args) throws ClassNotFoundException, SQLException {Connection con;//jdbc驱动String driver="com.mysql.cj.jdbc.Driver";//这里我的数据库是cxxtString url="jdbc:mysql://localhost:3306/mytestdb1?&useSSL=false&serverTimezone=UTC";String user="root";String password="root";try {//注册JDBC驱动程序Class.forName(driver);//建立连接con = DriverManager.getConnection(url, user, password);if (!con.isClosed()) {System.out.println("数据库连接成功");}con.close();} catch (ClassNotFoundException e) {System.out.println("数据库驱动没有安装");} catch (SQLException e) {e.printStackTrace();System.out.println("数据库连接失败");}}
}

mac之idea连接MySQL数据库报com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure相关推荐

  1. Underlying cause: com.mysql.cj.jdbc.exceptions.CommunicationsException : Communications link failure

    Hive试图schematool -dbType mysql -initSchema时候报错如下: Metastore connection URL:     jdbc:mysql://Desktop ...

  2. com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

    com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure 数据库jdbc连接异常,数据库连接异 ...

  3. Druid 连接池 报错 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

    场景赘述 早晨查看项目前一天的实际运行日志,发现了 一个Springboot项目中的druid 连接池和 mysql 产生了异常信息,重连暂并未对系统产生影响 下面是具体报错信息: com.mysql ...

  4. 【Linux 报错】com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The las

    报错信息如下: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failureThe last pa ...

  5. 连接数据库报错com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure的解决方法

    控制台报错 Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure com. ...

  6. 解决:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure(真实有效)

    数据库连接失败 在数据库连接失败,经常会有蛮多一系列的问题导致的原因,这个时候一定要多去尝试一下各种方法,并且做好自己的梳理! 一.例如我在SpringBoot项目中使用了阿里的数据库连接池Driud ...

  7. 数据库连接失败报错com.mysql.cj.jdbc.exceptions.CommunicationsException

    1.问题: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last pac ...

  8. CommunicationsException: Communications link failure; 项目启动遇到数据库报错

    1. 问题 今天启动项目突然报此错误 2. 原因之一 MySQL服务没有启动 3. 解决办法 启动MySQL服务,本地的启动方法(查看方法),打开任务管理器(ctrl+shift+esc),选择顶部的 ...

  9. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 数据库报错

    -- 查询mysql 数据库链接空闲时间持有最大空闲时间,单位为秒 SHOW VARIABLES WHERE VAriable_name = 'interactive_timeout'; -- 会出现 ...

最新文章

  1. C语言实现上三角蛇形矩阵不用数组,蛇形矩阵c语言实现
  2. springboot entity date_SpringBoot+JWT实战(附源码)
  3. 五、Hashtable与HashMap的区别
  4. 基于JQuery做的一个简单的点击显示和隐藏的小Demo
  5. PCB BOM也能如此好看?图形化交互式BOM工具Interactive Html Bom使用分享
  6. IIS下配置跨域设置Access-Control-Allow-Origin
  7. 英特尔“宠坏”程序员!
  8. Objective-C中的typedef枚举是什么?
  9. matlab中 mcc、mbuild和mex命令详解
  10. touch事件详解【译文】
  11. 豆瓣评分9.4,邱锡鹏教授蒲公英书姊妹篇《神经网络与深度学习:案例与实践》重磅来袭...
  12. 测井数据处理matlab,如何用matlab从文本文件中选择性读取测井曲线
  13. mysql pdo连接不上_通过PDO连不上数据库
  14. Docker升级Wekan
  15. 计算机一直黑屏,win7系统显示器黑屏但电脑一直在运行如何解决
  16. 《Django开发教程》1.2 在ubuntu上安装Django
  17. 棠玥寕近照曝光,淡雅着装,盖不住魅力
  18. 哪些实时翻译的软件好用?分享这三款好用的软件
  19. 如何办理股票开户手续
  20. 模拟生态圈_模拟器之争 夜神构建手游PC化生态圈

热门文章

  1. iOS - 清理缓存
  2. 运筹学 美国人在计算机上实现的四,【天大作业】2019年秋学期考试《计算机软件技术基础(2)》离线作业考核试题{全套100分}...
  3. 193884-53-6,APS-5,9-(4-氯苯硫代磷酰氧亚甲基)-10-甲基二氢吖啶二钠盐作为发光探针被用于基因芯片的研究中
  4. 冶金物理化学复习 --- 熔渣的酸碱性与氧化性
  5. 如何在JSP页面实现Word文件的预览
  6. Python入门:从空瓶换酒聊起
  7. [原创] 海外地图服务Here Map在Android 端的使用介绍
  8. Nature -- 人类首个 “泛基因组”旨在编目人类遗传多样性
  9. jquery取选中的checkbox的值
  10. 还有人不知道Vue路由?想要无痕浏览?一步到位!