Docker安装mysql8.0
Docker 安装 redis、mysql、mongodb、portainer、activemq、RabbitMQ、kafka、elasticsearch8
建议:当前安装方式仅限于开发、测试等研究使用.
Docker 安装系列
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
命令解析:
docker run
: 启动一个新的Docker容器。
--restart=always
: 设置容器的重启策略。这里指定为always
,意味着无论容器退出的原因是什么(包括手动停止),Docker都会自动重启该容器。
--name mysql3309.8.0.18
: 为新创建的容器指定名称为mysql3309.8.0.18
,便于管理和识别。
-v /data/mysql3309/conf:/etc/mysql
: 使用数据卷(volume)挂载。将主机目录/data/mysql3309/conf
挂载到容器内的/etc/mysql
目录,用于存放MySQL的配置文件。这样,对主机目录的更改将反映在容器内,便于自定义配置。
-v /data/mysql3309/data:/var/lib/mysql
: 将主机目录/data/mysql3309/data
挂载到容器内的/var/lib/mysql
目录,用于存放MySQL的数据文件。这样,即使容器停止或重新创建,数据库数据也能得以保留。
-v /data/mysql3309/log:/var/log
: 将主机目录/data/mysql3309/log
挂载到容器内的/var/log
目录,用于存放MySQL的日志文件。这样,可以在主机上直接查看和管理日志。
-v /data/mysql3309/mysql-files:/var/lib/mysql-files
: 将主机目录/data/mysql3309/mysql-files
挂载到容器内的/var/lib/mysql-files
目录,这是MySQL用来存储上传的文件(如LOAD DATA INFILE操作)的特殊目录。
-p 3309:3306
: 端口映射。将主机的3309端口映射到容器内的3306端口,使得外部可以通过主机的3309端口访问到容器内的MySQL服务。
-e MYSQL_ROOT_PASSWORD='123456'
: 设置环境变量。这里设置了MySQL的root用户密码为123456
。Docker镜像会在初始化时读取这个环境变量来设置MySQL的root密码。
-d mysql:8.0.18
: 以守护(detached)模式运行容器,并指定使用的镜像为mysql:8.0.18
。这意味着容器将在后台运行,并且不会将输出附加到当前终端。
--lower_case_table_names=1
: 传递给容器内MySQL服务的启动参数。这里设置了lower_case_table_names
参数为1,表示在MySQL中将所有表名转换为小写,不论创建时使用何种大小写。这对于在不区分大小写的文件系统(如某些Linux系统)上运行MySQL,并希望在不同大小写敏感的环境中保持表名一致性非常有用。
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
更多推荐
所有评论(0)