查看mysql版本
sudo curl https://registry.hub.docker.com/v1/repositories/mysql/tags | tr -d '[\[\]" ]' | tr '}' '\n' | awk -F: -v image='mysql' '{if(NR!=NF && $3 != ""){printf("%s:%s\n",image,$3)}}'
选择安装mysql:8.0.16
下载镜像
docker pull mysql:8.0.16
创建挂在目录
#存放配置文件
mkdir -p /usr/local/volumes/mysql/conf
mkdir -p /usr/local/volumes/mysql/yshop/{conf,data,logs}
#存放数据
mkdir -p /usr/local/volumes/mysql/data
#存放日志
mkdir -p /usr/local/volumes/mysql/logs
创建配置文件
vim /usr/local/volumes/mysql/conf/my.cnf
#默认的配置文件
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Custom config should go here
!includedir /etc/mysql/conf.d/
设置sql_model
#默认的配置文件
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Custom config should go here
!includedir /etc/mysql/conf.d/
sql_mode =
#去掉了ONLY_FULL_GROUP_BY
STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
启动docker
docker run --restart=always -d --name mysql \
-v /usr/local/volumes/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /usr/local/volumes/mysql/data:/var/lib/mysql \
-e TZ="Asia/Shanghai"
-p 3306:3306 -e MYSQL_ROOT_PASSWORD=ray@9099 mysql:8.0.4
修改密码以及访问主机
#进入容器
$ docker exec -it mysql /bin/bash
#连接mysql,这句会报错 Access denied for user 'root'@'localhost' (using password: YES)
#直接输入mysql 即可
$ mysql -uroot -p
#使用mysql库
$ mysql> use mysql
#修改访问主机以及密码等,设置为所有主机可访问
$ mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'ray123!2021';
#刷新
$ mysql> flush privileges;