1.拉取镜像
docker pull mysql:latest
2.挂载运行
docker run --name mysql-latest --restart=always -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root -v /var/run/docker.sock:/var/run/docker.sock -v /root/mysql/conf.d:/etc/mysql/conf.d -v /root/data:/var/lib/mysql -d mysql:latest
3.在容器中登陆MySql
mysql -u root -p
默认密码: root
4.检查访问权限
use mysql
select user,host from user;
若权限不存在则设置:update user set host=’%’ where user=’root’;
5.设置密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
alter user 'root'@'%' identified with mysql_native_password BY '新密码';
flush privileges;
6.修改字符编码和时区及MySql5.7以后分组问题
vim /etc/mysql/my.cnf
[mysqld]上添加
[client]
default-character-set=utf8
[mysqld]下添加
default-time_zone = +8:00
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
skip-name-resolve
[mysql]
default-character-set=utf8
建议从宿主机拷贝:docker cp ./my.cnf 容器ID:/etc/mysql/my.cnf my.cnf链接:https://pan.baidu.com/s/1tGF9iLAZ2O_tahtc63VYcg 提取码:dong 复制这段内容后打开百度网盘手机App,操作更方便哦 重启登录验证编码:show variables like ‘character%’; 验证时间:select now();
7.Navicat连接测试
8.pom依赖版本
驱动版本:
8.0.11 连接池版本:1.1.10 连接属性设置:url:jdbc:mysql://192.168.10.226:3307/sewage_ls?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT&allowPublicKeyRetrieval=true