Docker安装Mysql 8.0.26数据持久化到宿主主机保姆级教程

Docker安装mysql 8.0.26并配置保姆级教程

前言:Docker安装Mysql 8.0.26并配置,本地选取的版本是Mysql 8.0.26,注意防火墙,因为mysql默认端口是3306,要在防火墙放行3306端口不然访问不通,所有操作都是基于docker环境,言简意赅,不喜勿喷!

1.宿主机上创建目录

mkdir /usr/local/src/mysql/conf
mkdir /usr/local/src/mysql/data
mkdir /usr/local/src/mysql/log

2.拉取Mysql镜像

docker pull mysql:8.0.26

3.在mysql目录中创建my.cnf文件,把下面的复制到文件里面保存一下

[mysqld]
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/
[mysqld]
user=mysql
#服务端口号 默认3306
port=3306
#忽略表名大小写
lower_case_table_names=1
character-set-server=utf8
#密码加密
default_authentication_plugin=mysql_native_password
secure_file_priv=/var/lib/mysql
expire_logs_days=7
#mysql8会有group by 报错这样可以避免
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=1000
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8mb4
 
[mysql]
default-character-set=utf8mb4

3.创建Mysql服务

#启动mysql ,挂载配置文件,数据持久化到宿主主机
docker run \
-p 3306:3306 \
--name mysql \
--restart=always \
--privileged=true \
--restart unless-stopped \
-v /usr/local/src/mysql/log:/var/log/mysql \
-v /usr/local/src/mysql/data:/var/lib/mysql \
-v /usr/local/src/mysql/my.cnf:/etc/mysql/my.cnf \
-v /usr/local/src/mysql/conf/conf.d:/etc/mysql/conf.d \
-v /etc/localtime:/etc/localtime \
-e MYSQL_ROOT_PASSWORD=123456\
-d mysql:8.0.26

Docker安装Mysql 8.0.26数据持久化到宿主主机保姆级教程_第1张图片
这样就运行起来了

4 进入mysql容器

1.进入mysql容器内部

docker exec -it mysql /bin/bash

2.执行命令输入密码:123456

mysql -u root -p

Docker安装Mysql 8.0.26数据持久化到宿主主机保姆级教程_第2张图片
3.修改访问主机以及密码

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'admin666';

4.刷新一下权限

FLUSH PRIVILEGES;

5 测试连接

Docker安装Mysql 8.0.26数据持久化到宿主主机保姆级教程_第3张图片

你可能感兴趣的:(mysql,docker,java)