一、安装Mysql
1、官网下载mysql的tar包(提示:建议vpn环境下载)
2、解压并安装tar包
# 移动解压后的二进制包到安装目录
sudo mv mysql-5.7.19-osx10.9-x86_64 /usr/local/mysql
# 更改 mysql 安装目录所属用户与用户组
cd /usr/local
sudo chown -R root:wheel mysql
# 初始化数据库cd /usr/local/mysql
sudo bin/mysqld --initialize --user=mysql (提示:初始化数据库时会随机生成root@localhost用户的随机密码,用此密码登陆数据库)
# 开启mysql服务端程序# 启动sudo support-files/mysql.server start# 重启sudo support-files/mysql.server restart
# 停止
sudo support-files/mysql.server stop
# 检查 MySQL服务端 运行状态
sudo support-files/mysql.server status
# 通过自带的Mysql Client 连接数据库cd /usr/local/mysql/bin./mysql -u root -p
# 修改root@localhost用户的密码mysql> alter user 'root'@'localhost' identified by 'new-password';# 将root用户的host改为通配符%,并刷新权限,使root@127.0.0.1等也可以登录
mysql> update user set user.Host = '%' where user.User = 'root';
mysql> flush privileges;
3、将mysql的字符集设置为utf8
# 更改 mysql 的启动配置文件sudo vi /etc/my.cnf
[client]
port = 3306
default-character-set = utf8
[mysqld]
user = mysql
port = 3306
default-storage-engine = Innodb
character-set-server = utf8
collation-server = utf8_general_ci
init_connect = 'SET NAMES utf8'
# 重启mysqld后,进入mysql,查看字符集
mysql> show variables like "%character%";
二、安装Navicat
参考教程:
ps : 一定要下载该教程提供的12.0.22安装包,官网下载地址提供的安装包bug已经被修复,包内容中没有rpk文件,无法修改公钥
三、将远程IP下数据库的内容(包括结构和数据)复制到本地数据库完成数据库环境的搭建
使用mysql-cp-tool工具程序复制数据,这种方法比在Navicat中导出数据再在本地数据库中通过sql脚本导入数据要来得方便,因为导出数据成sql脚本时要将选中数据库中所有数据一起导出,而用程序复制时复制记录的条数可配置,且在复制过程中出错(一般是构造表的时候)能追踪出错原因。
示例:
错误一:
构造表时出现MySQLSyntaxErrorException: Invalid default value for timestamp(3).
原因:
mysql> show variables like "sql_mode";
sql_mode = ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
NO_ZERO_DATE:在非严格模式下,可以插入形如“0000-00-00 00:00:00”的非法日期,MySQL数据库仅抛出一个警告。而启用该选项后,MySQL数据库不允许插入零日期,插入零日期会抛出错误而非警告。
解决方案:
mysql> set global sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
mysql> commit;
错误二:
拷贝数据时出现java.sql.SQLException: Cannot convert value '2018-10-08 22:42:03.973' from column 103 to TIMESTAMP.
Caused by: java.lang.IllegalArgumentException: nanos > 999999999 or < 0
原因:
项目中使用的MySQL JDBC driver版本过低,无法对该类型string串进行处理。
解决方案:
若是低版本的driver,例如mysql-connector-java-5.1.10-bin, 切换为高版本驱动包例如mysql-connector-java-8.0.15.jar之后就能解决问题。
四、运行程序时出现SQLException
错误一:
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'column1' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'column1' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'column1' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
原因:ONLY_FULL_GROUP_BY的意思是:对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中,也就是说查出来的列必须在group by后面出现否则就会报错,或者这个字段出现在聚合函数里面。
解决方案:
将mysql的sql_mode中的ONLY_FULL_GROUP_BY设置去除。
mysql> set global sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
mysql> commit;