windows安装mysql 数据持久化

前提:windows 已经安装好docker环境

1 下载mysql镜像

docker pull mysql

2 创建目录

E:\docker\mysql
└─mysql
    ├─conf
    └─data

3 获取默认生成的配置文件

#启动容器
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest --default-authentication-plugin=mysql_native_password
#拷贝默认配置文件
docker cp mysql:/etc/mysql/my.cnf E:/docker/mysql/conf/
# 获取完默认配置 停止 删除容器
docker stop mysql
docker rm mysql

4 修改默认配置文件

[mysqld]
#表名大小写是否敏感
lower_case_table_names=1
#是否开启慢查询日志
slow_query_log=ON
#long_query_time=3
#是否记录未使用索引的查询语句,记录在慢查询日志
log-queries-not-using-indexes=OFF
#错误日志 
log-error=/var/lib/mysql/error.log
#慢查询日志
slow_query_log_file=/var/lib/mysql/slowquery.log
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
# Custom config should go here
!includedir /etc/mysql/conf.d/
​


5 启动容器

docker run --name mysql-test -p 3306:3306 -v /e/docker/mysql/data:/var/lib/mysql/ -v /e/docker/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /e/docker/mysql/mysql-files:/var/lib/mysql-files/ -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest --default-authentication-plugin=mysql_native_password
### 如果启动 报错
docker mysql mysqld: Error on realpath() on '/var/lib/mysql-files' No such file or directory
#将目录加上去
-v /e/docker/mysql/mysql-files:/var/lib/mysql-files/
​

6 创建用户

#创建用户
 CREATE USER 'root'@'172.17.0.1' IDENTIFIED BY '123456';
 grant  ALL privileges on *.* to  'root'@'172.17.0.1' with grant option;
 flush privileges;
 mysql> select user,host from user;
+------------------+--------------+
| user             | host         |
+------------------+--------------+
| root             | 172.17.0.1   |
| mysql.infoschema | localhost    |
| mysql.session    | localhost    |
| mysql.sys        | localhost    |
| root             | localhost    |