1.拉取镜像

  1. docker pull mysql:latest

2.挂载运行

  1. 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

  1. mysql -u root -p

默认密码: root

4.检查访问权限

  1. use mysql
  2. select user,host from user;

若权限不存在则设置:update user set host=’%’ where user=’root’;

5.设置密码

  1. ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
  2. alter user 'root'@'%' identified with mysql_native_password BY '新密码';
  3. flush privileges;

6.修改字符编码和时区及MySql5.7以后分组问题

  1. vim /etc/mysql/my.cnf
  2. [mysqld]上添加
  3. [client]
  4. default-character-set=utf8
  5. [mysqld]下添加
  6. default-time_zone = +8:00
  7. sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
  8. default-storage-engine=INNODB
  9. character-set-server=utf8
  10. collation-server=utf8_general_ci
  11. skip-name-resolve
  12. [mysql]
  13. 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连接测试

image.png

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

9.项目启动测试

image.png