Docker安装mysql8.0

建议:当前安装方式仅限于开发、测试等研究使用.   

Docker 常用命令

Docker 安装系列

  1.    Docker mongo:5.0 安装 

  2.    Docker portainer(Docker管理工具) 

  3.    Docker 安装Reids 

  4.    Docker 安装 Redis的完全体版本RedisMod

  5.    Docker安装 activemq   

  6.    Docker 安装kafka

  7.    Docker安装 RabbitMQ

  8.    Docker  elasticsearch:8

1、准备工作

        已安装Docker、如阿里云服务器注意开通服务器安全组访问规则端口号

2、下载mysql8.0docker镜像

docker pull mysql:8.0

3、查看下载的docker镜像

[root@tseng ~]# docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
docker.io/mysql                    8.0                 c60d96bd2b77        7 days ago          514 MB

 4、创建挂载目录

mkdir -p /data/mysql/conf
mkdir -p /data/mysql/data
mkdir -p /data/mysql/logs

5、创建my.cnf文件,放在 /data/mysql/conf 目录中,注意配置文件中的端口号、字符集、时区

vi /data/mysql/conf/my.cnf
[client]
port = 3308
default-character-set = utf8mb4

[mysql]
port = 3308
default-character-set = utf8mb4

[mysqld]
# bind-address = 0.0.0.0
# port = 3306

max_connections=10000

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

# 设置时区和字符集
# default-time-zone='+8:00'
character-set-client-handshake=FALSE
init_connect='SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci'

gtid-mode=ON
enforce-gtid-consistency = ON

6、启动镜像

docker run  --restart=always  --name mysql8.0  -v /data/mysql/conf:/etc/mysql  -v /data/mysql/data:/var/lib/mysql  -v /data/mysql/log:/var/log  -v /data/mysql/mysql-files:/var/lib/mysql-files -p 3308:3306  -e MYSQL_ROOT_PASSWORD='123456'   -d mysql:8.0

 6.1  --lower_case_table_names=1 :忽略大小写 

docker run  --restart=always  --name mysql3309.8.0.18  -v /data/mysql3309/conf:/etc/mysql  -v /data/mysql3309/data:/var/lib/mysql  -v /data/mysql3309/log:/var/log  -v /data/mysql3309/mysql-files:/var/lib/mysql-files -p 3309:3306  -e MYSQL_ROOT_PASSWORD='123456'   -d mysql:8.0.18 --lower_case_table_names=1

6.2  mysql 5.7启动

docker pull mysql:5.7

mkdir -p /data/mysql/conf
mkdir -p /data/mysql/data
mkdir -p /data/mysql/logs

docker run -d -p 3308:3306 -v /data/mysql/conf:/etc/mysql/conf.d -v /data/mysql/data:/var/lib/mysql -v /data/mysql/log:/var/log -e MYSQL_ROOT_PASSWORD=Qcd@2022 --name mysql5.7 mysql:5.7

6.3  数据库记录时间晚8小时解决办法

6.3.1 更改容器时间-01,亲测可用

[root@tseng ~]# docker exec -it mysql8.0 bash
root@a498923c0bd3:/# date
Tue Aug  3 06:44:32 UTC 2021
root@a498923c0bd3:/# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
root@a498923c0bd3:/# exit
[root@tseng /]# docker restart mysql8.0        -- 重启镜像
[root@tseng /]# docker exec -it mysql8.0 bash    
root@a498923c0bd3:/# date
Tue Aug  3 14:45:35 CST 2021

 6.3.2 更改容器时间-02

root@a498923c0bd3:/# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
或者
root@a498923c0bd3:/# systemctl enable ntpd
root@a498923c0bd3:/# systemctl start ntpd
或者
root@a498923c0bd3:/# timedatactl set-timezone Asia/Shanghai

若想改回UTC时间 删除/etc/localtime便可

  6.3.2 临时更改mysql 时间-亲测可用,重启后失效

// 查看当前时间
select now();

// 设置全局
set global time_zone = '+8:00';

// 立即生效
flush privileges;

7、解决无法登录问题;登录mysql

        注:本人Mac navicat for mysql 不修改密码规则可连接成功,Win10无法连接成功,根据需要修改配置。

1251 Client does not support authentication protocol requested by server

7.1 输入用户名密码进入mysql

docker exec -it mysql8.0 bash
root@8e06ede28f4b:/# 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 22
Server version: 8.0.26 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> 

7.2 查看密码加密规则

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> 

7.3 修改密码加密规则

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

7.4 刷新权限,使修改的密码生效

FLUSH PRIVILEGES;

7.5 查看密码加密规则是否生效

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)

mysql> 

8、出现:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 解决

        【不重启mysql的解决】使用 select @@global.sql_mode; 查询功能依赖检测功能。如果启用了ONLY_FULL_GROUP_BY SQL模式(默认情况下),MySQL将拒绝选择列表,HAVING条件或ORDER BY列表的查询引用在GROUP BY子句中既未命名的非集合列,也不在功能上依赖于它们。(5.7.5之前,MySQL没有检测到功能依赖关系,默认情况下不启用。

mysql> select @@global.sql_mode;
+----------------------------------------------------------------------------------------------------+
| @@global.sql_mode                                                                                  |
+----------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

        去掉ONLY_FULL_GROUP_BY,重新设置值

mysql> set @@global.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)
mysql> select @@global.sql_mode;
+----------------------------------------------------------------------------------------------------+
| @@global.sql_mode                                                                                  |
+----------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

        【重启mysql的解决方案】修改my.cnf文件,在[mysqld] 下方添加以下规则,并重启mysql容器。同时重启mysql连接工具、连接程序重启。

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

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