1, mybatis-docker安装mysql

docker安装mysql

docker还没有安装的可以查看博客历史文章docker入门

# 拉取mysql镜像
docker search mysql
-->
[root@centos-master ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9718                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3542                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   708                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   77                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   72                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      44                                      [OK]
....
<--

# 获取mysql镜像, 等待安装完成
docker pull mysql

# 安装完成后查看mysql信息
docker history mysql

[root@centos-master ~]# docker history mysql
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
be0dbf01a0f3        4 weeks ago         /bin/sh -c #(nop)  CMD ["mysqld"]               0B                  
<missing>           4 weeks ago         /bin/sh -c #(nop)  EXPOSE 3306 33060            0B                  
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENTRYPOINT ["docker-entry…   0B                  
<missing>           4 weeks ago         /bin/sh -c ln -s usr/local/bin/docker-entryp…   34B                 
<missing>           4 weeks ago         /bin/sh -c #(nop) COPY file:7cbb26bbdb8e71b3…   13.2kB              
<missing>           4 weeks ago         /bin/sh -c #(nop) COPY dir:478f098f3681084f7…   1.22kB              
<missing>           4 weeks ago         /bin/sh -c #(nop)  VOLUME [/var/lib/mysql]      0B         
...

# 运行
docker run -itd --name mysql-test -p 3306:3306  mysql

# 查看容器运行
docker container ps

# 发现mysql没有运行起来, 查看日志
docker logs mysql-test
-->
[root@centos-master ~]# docker logs mysql-test
2020-07-10 06:57:44+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
2020-07-10 06:57:45+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-07-10 06:57:45+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
2020-07-10 06:57:45+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
        You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
<--

# 密码参数没有设置
# MYSQL_ROOT_PASSWORD设置root密码
# MYSQL_ALLOW_EMPTY_PASSWORD允许密码为空
# MYSQL_RANDOM_ROOT_PASSWORD 随机生成密码

docker container rm mysql-test
docker run -itd --name mysql-test   -v /mydocker/mysql/conf:/etc/mysql/conf.d -v /mydocker/mysql/logs:/var/log/mysql -v /mydocker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -p 3306:3306  mysql

# 进入到容器中
docker exec -it mysql-test /bin/bash

# 链接到mysql
mysql -uroot -proot

-->
root@cb046d255ab7:/# mysql -uroot -proot 
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.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> 
<--

# 链接成功, 安装完成

如果想安装msyql5.6的版本, 可以docker pull mysql:5.6, docker查看镜像历史版本的命令好像没有找到, 只找到了一个查询的docker镜像查看

配置远程登录

docker的mysql已经配置了root用户远程登录, 但是使用的是cacheing_sha2_password加密, navicat,sqlyog登录不上, 修改加密方式

use mysql;

# 查看用户及访问权限
mysql> select user,host from user;
-->
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)
<--


# docker中的mysql已经开启了远程访问权限, 

# 修改加密方式, by 后面的root为的密码
alter user 'root'@'%' identified with mysql_native_password by 'root';

安装msyql5.7

docker run --name mysql5.7 -v /dk/mysql5:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7