Navicat连接docker启动的mysql8容器

@

环境准备

1,centos7中安装docker
2,使用docker拉取mysql8镜像
3,关闭防火墙,selinux

[root@huhy ~]# docker -v
Docker version 20.10.17, build 100c701
[root@huhy ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Image is up to date for mysql:latest
docker.io/library/mysql:latest
[root@huhy ~]# 

#拉取的最新的MySQL8镜像,我已经拉取过了,配置阿里云镜像加速,[可参考](https://blog.csdn.net/m0_56363537/article/details/116894284)

启动mysql容器

1,做mysql文件的持久化存储

如果不做持久化挂载,msyql容器重启后把原来的数据删除了

#opt目录是Linux提供我们扩展的目录,可以存放用户需求的文件
[root@huhy ~]# mkdir -p /opt/datas/docker/mysql/conf
[root@huhy ~]# mkdir -p /opt/datas/docker/mysql/data

2,配置myslq的conf文件

在上面创建的配置文件目录/opt/datas/docker/mysql/conf下创建MySQL的配置文件my.cnf

[root@huhy ~]# vi /opt/datas/docker/mysql/conf/my.cnf

#添加一下内容

[client]

#socket = /usr/mysql/mysqld.sock

default-character-set = utf8mb4

[mysqld]

#pid-file = /var/run/mysqld/mysqld.pid
#socket = /var/run/mysqld/mysqld.sock
#datadir = /var/lib/mysql
#socket = /usr/mysql/mysqld.sock
#pid-file = /usr/mysql/mysqld.pid

datadir = /opt/datas/docker/mysql/data
character_set_server = utf8mb4
collation_server = utf8mb4_bin
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/

3,运行容器

参数说明:
--network host ,设置网卡的模式为主机模式,后续才能访问到MySQL
–restart=unless-stopped,在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器
-p 3306:3306:映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
-e 环境参数,MYSQL_ROOT_PASSWORD设置root用户的密码—可以自行更改
-d 后台运行

docker run \
--network host
--name mysql8 \
--restart=unless-stopped \
-it -p 3306:3306 \
-v /opt/datas/docker/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /opt/datas/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql

运行如下

[root@huhy ~]# docker run --network host --name mysql-huhy --restart=unless-stopped -it -p 3306:3306 -v /opt/datas/docker/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /opt/datas/docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
WARNING: Published ports are discarded when using host network mode
a0e84fcfada9237a84b2c0a1ce5f19f5b127aa5b5881137946cdd68bf0446a2e
[root@huhy ~]# 

MySQL 8修改密码

MySQL8版本的修改密码方式和前面版本有些差异算是分水岭了都

1,进入数据库操作

[root@huhy ~]# docker container list
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS     NAMES
a0e84fcfada9   mysql:latest   "docker-entrypoint.s…"   About a minute ago   Up About a minute             mysql-huhy
[root@huhy ~]# docker exec -it mysql-huhy bin/bash
root@huhy:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

MySQL 8版本更改密码的格式为 alter user ‘root’@‘%’ identified by ‘mima’; 注:如果密码设置为mima则为免密登录

mysql> alter user root@'%' identified by '000000';
Query OK, 0 rows affected (0.01 sec)

mysql> 

测试

在这里插入图片描述

posted @ 2022-09-05 15:56  huhy  阅读(440)  评论(0编辑  收藏  举报