MySQL-5.x与MySQL-MySQL-6.X及以上
- driver
- 连接MySQL-6.X及以上忘记加'`cj`'
- url
- 连接MySQL-6.X及以上 url未加`useSSL=false`
- 连接MySQL-6.X及以上 url未加`serverTimezone=UTC`
- url重要参数意义
- 初始密码
大家好我是EverdayForCode,你今天学习了吗?
今天我们一起回忆那些连接MySQL-5.X和MySQL-MySQL-6.X及以上填过的坑
driver
#MySQL-5.x驱动:
driver = com.mysql.jdbc.Driver
#MySQL-6.X及以上驱动:
driver = com.mysql.cj.jdbc.Driver
连接MySQL-6.X及以上忘记加’cj
’
踩坑:
Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and
manual loading of the driver class is generally unnecessary.
url
#MySQL-5.x url:
url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8"
#MySQL-6.X及以上驱动:
url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC"
连接MySQL-6.X及以上 url未加useSSL=false
踩坑:
WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements
SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate
property is set to ‘false’.You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate
verification.
连接MySQL-6.X及以上 url未加serverTimezone=UTC
踩坑:
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 specifc time zone value if you want to utilize time zone support.
url重要参数意义
参数名 | 参数作用 |
useUnicode | 是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为true |
characterEncoding | 当useUnicode设置为true时,指定字符编码。比如可设置为gb2312或gbk,utf8 |
autoReconnect | 当数据库连接异常中断时,是否自动重新连接? |
autoReconnectForPools | 是否使用针对数据库连接池的重连策略 |
failOverReadOnly | 自动重连成功后,连接是否设置为只读? |
maxReconnectsautoReconnect | 设置为true时,重试连接的次数 |
initialTimeoutautoReconnect | 置为true时,两次重连之间的时间间隔,单位:秒 |
connectTimeout | 和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK 1.4以上 |
socketTimeoutsocket | 操作(读写)超时,单位:毫秒。 0表示永不超时 |
verifyServerCertificate | 如果强制要求设置SSL模式,将verifyServerCertificate设置为false,将不需要设置SSL |
useSSL | 是否开启SSL模式连接 |
useJDBCCompliantTimezoneShift | 是否开启JDBC时区设置 |
serverTimezone | 设置时区:如:HongKong、ShangHai、UTC |
初始密码
MySQL5.7一下需要自己设置密码
初始密码从MySQL 5.7以上版本就开始改变了,有一个默认初始密码
在MySQL的这篇《Changes in MySQL 5.7.4 (2014-03-31, Milestone 14)》的文档里,有这么一段:
Incompatible Change: MySQL deployments installed using mysql_install_db now are secure by default. The following changes have been implemented as the default deployment characteristics:
The installation process creates only a single root account, ‘root’@‘localhost’, automatically generates a random password for this account, and marks the password expired. The MySQL administrator must connect as root using the random password and use SET PASSWORD to select a new password. (The random password is found in the .mysql_secret file in the home directory of the effective user running the script.)
Installation creates no anonymous-user accounts.
Installation creates no test database.
翻译:
不兼容的变化:用mysql_install_db的现在默认的安全安装了MySQL部署。以下更改已实现为默认的部署特点:
在安装过程中只创建一个根帐户,“根” @“本地主机”,自动为这个帐户生成随机密码,和标记的密码已过期。 MySQL的管理员必须使用随机密码,并使用SET PASSWORD选择一个新的密码作为根连接。 (随机密码,在运行脚本的有效用户的主目录中的.mysql_secret文件中找到。)
安装不产生匿名的用户帐户。
安装不会创建测试数据库。