docker 安装 mysql8
#docker部署mysql8 步凑;
#1. 拉取镜像
docker pull mysql:8.0
#2. 创建临时镜像,然后将配置文件复制到新建的目录并改名
docker run --name mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0
#3. 创建mysql的镜像映射目录
mkdir -p /data/docker/mysql8/data
mkdir -p /data/docker/mysql8/logs
#4. cd到镜像中复制镜像的mysql配置并改名
cd /data/docker/mysql8
docker cp mysql:/etc/mysql ./
mv mysql conf
#5. 删除临时镜像
docker rm -f mysql
#6. 创建mysql8.0镜像,将数据、日志、配置文件指定到自建的目录
docker run \
-p 3306:3306 \
--name mysql8 \
-v /data/docker/mysql8/conf:/etc/mysql \
-v /data/docker/mysql8/logs:/var/log/mysql \
-v /data/docker/mysql8/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
--privileged=true \
-d mysql:8.0
其他路径 /volume1/docker/mysql8
docker run \
-p 3306:3306 \
--name mysql8 \
-v /volume1/docker/mysql8/conf:/etc/mysql \
-v /volume1/docker/mysql8/logs:/var/log/mysql \
-v /volume1/docker/mysql8/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
--privileged=true \
-d mysql:8.0
#命令解释
-p 端口映射
--privileged=true 挂载文件权限设置
--restart unless-stopped 设置 开机后自动重启容器
-v /mnt/sda1/mysql8.0.20/mysql:/etc/mysql 挂载配置文件
-v /mnt/sda1/mysql8.0.20/logs:/logs \ 挂载日志
-v /mnt/sda1/mysql8.0.20/data:/var/lib/mysql \ 挂载数据文件 持久化到主机,
-v /etc/localtime:/etc/localtime 容器时间与宿主机同步
-e MYSQL_ROOT_PASSWORD=123456 设置密码
-d mysql:8.0.20 后台启动,mysql
#7. 设置mysql8开机重启
docker update mysql8 --restart=always
#8. 进入容器
docker exec -it mysql8 bash
#9. 进入mysql终端
mysql -u root -p
密码 root
#10. 设置root账户外网可访问
use mysql;
select host,user from user;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
flush privileges;
select host,user from user;
#设置mysql的编码
#11. 查看mysql终端编码(进入mysql容器使用该命令)
show variables like 'character%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
#12. 退出
exit
exit
#13. 编辑mysql.cnf
vim /data/docker/mysql8/conf/conf.d/mysql.cnf
#14. mysql.cnf文件插入utf-8编码内容
[client]
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
#15. 退出
esc
:wq!
#16. 再次查看mysql终端编码(进入mysql容器使用该命令)
docker exec -it mysql8 bash
show variables like 'character%';
如果挂载成功,所有的字符都会变为utf-8
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb3 |
| character_set_connection | utf8mb3 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb3 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+