centos7下docker安装mysql8.0

docker安装mysql8.0

  • 一、安装docker
    • 1、检测centos7是否安装docker
    • 2、关闭防火墙
    • 3、安装yum工具
    • 4、设置docker镜像源
    • 5、安装docker
    • 6、查看docker版本
    • 7、启动docker
  • 二、安装mysql8.0.28
    • 1、从docker仓库下载mysql8.0.28
    • 2、查看下载的docker镜像
    • 3、创建挂在目录(方便后续配置)
    • 4、创建并配置my.cnf文件
    • 6、启动镜像
    • 7、查看mysq8.0.28容器是否运行
    • 8、解决图形化界面连接docker中的MySQL 8.0.28报错问题
    • 9、远程测试连接

一、安装docker

1、检测centos7是否安装docker

rpm -qa | grep docker

2、关闭防火墙

(1)、检查防火墙状态
	systemctl status firewalld.service
(2)、临时关闭防火墙
	systemctl stop firewalld.service
(3)、永久关闭防火墙
	systemctl disable firewalld.service

3、安装yum工具

yum install -y yum-utils devi ce-mapper-pers istent-data 1vm2 --ski p- broken

4、设置docker镜像源

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

5、安装docker

yum install -y docker-ce

6、查看docker版本

docker -v

7、启动docker

systemctl start docker

链接: 参考文章.

二、安装mysql8.0.28

1、从docker仓库下载mysql8.0.28

docker pull mysql:8.0.28

2、查看下载的docker镜像

docker images mysql:8.0.28

在这里插入图片描述

3、创建挂在目录(方便后续配置)

mkdir -p /data/docker/mysql8/conf	#配置文件目录
mkdir -p /data/docker/mysql8/data	#数据目录
mkdir -p /data/docker/mysql8/logs	#日志目录

4、创建并配置my.cnf文件

(1)、在/data/docker/mysql8/conf目录下创建my.cnf文件
	touch /data/docker/mysql8/conf/my.cnf
(2)、编辑my.cnf并配置
	vi /data/docker/mysql8/conf/my.cnf
[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqld]
port=3308

init_connect='SET collation_connection = utf8mb4_general_ci'
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
skip-character-set-client-handshake
skip-name-resolve

# 最大连接数
max_connections=1000

6、启动镜像

docker run -p 3308:3308--name mysql8.0.28 -v /data/docker/mysql8/conf:/etc/mysql -v /data/docker/mysql8/data:/var/lib/mysql-files -v /data/docker/mysql8/logs:/var/log -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.28
注意:忽略mysql8.0的大小写,不支持在my.cnf配置。需要在启动镜像时 追加 --lower-case-table-names=1 参数
(docker run -p 3308:3308--name mysql8.0.28 -v /data/docker/mysql8/conf:/etc/mysql -v /data/docker/mysql8/data:/var/lib/mysql-files -v /data/docker/mysql8/logs:/var/log -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.28 --lower-case-table-names=1)

7、查看mysq8.0.28容器是否运行

docker ps

在这里插入图片描述

8、解决图形化界面连接docker中的MySQL 8.0.28报错问题

密码加密方式导致无法连接(如下图)

centos7下docker安装mysql8.0_第1张图片
(1)、查看容器id
docker ps在这里插入图片描述
(2)、登录mysql8.0.28
docker exex -it c11319beeff3 bash
注意: c11319beeff3 表示容器的id
在这里插入图片描述
(3)、输入用户名和密码进入mysql

root@c11319beeff3:/# 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 14
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> 

(4)、查看密码加密规则

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)

mysql> 

(5)、修改密码加密规则并刷新权限使密码生效

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

(6)、查看密码加密规则是否生效

mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | mysql_native_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)

9、远程测试连接

centos7下docker安装mysql8.0_第2张图片

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