拉取镜像
docker pull mysql:8.0.23
创建挂载文件
mkdir -p /home/docker/mysql/conf
mkdir -p /home/docker/mysql/data
mkdir -p /home/docker/mysql/log
cd /home/docker/mysql/conf
touch my.cnf
编辑my.cnf
vi my.cnf
内容如下:
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
[mysqld]
# 不区分大小写
lower_case_table_names = 1
#开启慢查询
slow_query_log = ON
#修改慢sql的阈值,sql执行超过阈值的时候就会记录到慢日志的log文件中
long_query_time = 1
#由于在MySQL5.7版本以上,默认设置SQL_Mode模式,在模式里有NO_ZERO_DATE,NO_ZERO_IN_DATE,存在表示系统里DATE类型字段不能有0值
sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
#Mysql报错Packet for query is too large问题解决
max_allowed_packet=4M
启动容器
docker run -d \
--name mysql \
-p 3306:3306 \
--restart unless-stopped \
-v /home/docker/mysql/log:/var/log/mysql \
-v /home/docker/mysql/data:/var/lib/mysql \
-v /home/docker/mysql/conf:/etc/mysql/conf.d \
-v /home/docker/mysql/conf/my.cnf:/etc/my.cnf \
-e MYSQL_ROOT_PASSWORD=密码 \
mysql:8.0.23
创建用户和授权
#进入容器内部
docker exec -it 容器id /bin/bash
mysql -u root -p
密码
#创建用户和密码,以及授权
create user 'gxj'@'localhost' identified by '密码';
grant all on *.* to 'gxj'@'localhost';
flush privileges;
#找到mysql数据库
use mysql;
#查看结果是不是root用户仅允许本地 localhost 登录,是的话,就要修改它的host为%,表示任意IP地址都可以登录
select host ,user from user;
#手动修改,把root 的locahost改为%(原作者那是单引号,应该用双引号)
update user set host ="%" where user="gxj";
flush privileges;
标签:set,23,utf8,cnf,mysql8.0,mysql,home,docker
From: https://blog.csdn.net/gxy03/article/details/140280888