当前位置:网站首页>Docker篇 (五) MySQL的安装
Docker篇 (五) MySQL的安装
2022-04-23 14:11:00 【anron】
一、MySQL 8.0的安装
1.1 拉取镜像
docker pull mysql:latest
该镜像是MySQL 8.0.20 Community Server - GPL
1.2 创建容器
docker run -d --restart=always --name mysql \
-v /etc/localtime:/etc/localtime:ro \
-v /ljh/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3306:3306 \
mysql:latest --lower_case_table_names=1
注意: --lower_case_table_names=1要加在镜像名后面,不然会报错,MySQL8版本只能在初始化时配置是否表名大小写敏感,不能在初始化之后再修改该参数
MYSQL_ROOT_PASSWORD参数指定MySQL的密码为123456
/etc/localtime设置容器里的时区和宿主机一致
1.3 修改root账号的密码
docker exec -it mysql /bin/bash
mysql -uroot -p123456
mysql> use mysql;
mysql> show variables like 'lower_case_table_names';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_table_names | 1 |
+------------------------+-------+
1 row in set (0.00 sec)
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> alter user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '654321';
Query OK, 0 rows affected (0.00 sec)
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 | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
这样数据库本机登录(host=localhost)的密码是123456,远程登录(host=%)的密码是654321
注意:MySQL 8.0默认是caching_sha2_password加密方式,MySQL 5.X是mysql_native_password
1.4 配置my.cnf
这是修改my.cnf之前的配置
mysql> show variables like 'default_authentication_plugin';
+-------------------------------+-----------------------+
| Variable_name | Value |
+-------------------------------+-----------------------+
| default_authentication_plugin | caching_sha2_password |
+-------------------------------+-----------------------+
1 row in set (0.00 sec)
#先拷贝出容器中的my.cnf文件
docker cp mysql:/etc/mysql/my.cnf /ljh/mysql/my.cnf
#修改拷贝出的文件,在[mysqld]下加入配置
[mysqld]
default_authentication_plugin=mysql_native_password
#把修改好后的文件拷贝到容器中
docker cp /ljh/mysql/my.cnf mysql:/etc/mysql/my.cnf
#重启容器
docker restart mysql
mysql> show variables like 'default_authentication_plugin';
+-------------------------------+-----------------------+
| Variable_name | Value |
+-------------------------------+-----------------------+
| default_authentication_plugin | mysql_native_password |
+-------------------------------+-----------------------+
1 row in set (0.00 sec)
1.5 修改时区
如果没有用【-v /etc/localtime:/etc/localtime:ro】创建的容器,会存在时区不一致,需要手动修改。Mysql的镜像默认是UTC时区,SQL语句中的now()得到的时间比北京时间晚8个小时,需要进入镜像调整时区为CST
[root@localhost ~]# mv /etc/localtime /etc/localtime.bak
[root@localhost ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
二、MySQL 5.7的安装
2.1 拉取镜像
docker pull mysql:5.7
2.2 创建容器
docker run -d --restart=always --name mysql \
-v /etc/localtime:/etc/localtime:ro \
-v /ljh/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3306:3306 \
mysql:5.7
2.3 修改root账号的密码
和1.3中一样
2.4 配置mysqld.cnf
#先拷贝出容器中的mysqld.cnf文件
docker cp mysql:/etc/mysql/mysql.conf.d/mysqld.cnf /ljh/mysql/mysqld.cnf
#修改拷贝出的文件,在[mysqld]下加入配置
[mysqld]
lower_case_table_names=1
#把修改好后的文件拷贝到容器中
docker cp /ljh/mysql/mysqld.cnf mysql:/etc/mysql/mysql.conf.d/mysqld.cnf
#重启容器
docker restart mysql
三、Docker-compose下安装
3.1 docker-compose.yml文件如下
version: "3"
networks:
anron-net:
driver: bridge
services:
anron-database-1:
image: mysql:5.7
container_name: mysql5.7
restart: always
networks:
- anron-net
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
command:
--lower_case_table_names=1
volumes:
- /ljh/mysql5.7/data:/var/lib/mysql
- /etc/localtime:/etc/localtime:ro
anron-database-2:
image: mysql:latest
container_name: mysql8.0
restart: always
networks:
- anron-net
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
command:
--default-authentication-plugin=mysql_native_password
--lower_case_table_names=1
volumes:
- /ljh/mysql8.0/data:/var/lib/mysql
- /etc/localtime:/etc/localtime:ro
版权声明
本文为[anron]所创,转载请带上原文链接,感谢
https://blog.csdn.net/anron/article/details/107013830
边栏推荐
- redis数据库讲解(三)redis数据类型
- 01-nio basic ByteBuffer and filechannel
- js 格式化时间
- 统信UOS卸载php7.2.24,安装php7.4.27 ;卸载再安装为PHP 7.2.34
- Flop effect
- Use the executors class to quickly create a thread pool
- 容灾有疑问?点这里
- 字节面试编程题:最小的K个数
- 使用DialogFragment的一些感受及防踩坑经验(getActivity、getDialog为空,cancelable无效等)
- FBS (fman build system) packaging
猜你喜欢
星界边境文本自动翻译机(高级版)使用说明
星界边境文本自动翻译机使用说明
Installation and use of postman pit
Nacos作为配置中心(四) 使用Demo
x509证书cer格式转pem格式
A table splitting implementation scheme of MySQL and InnoDB, MyISAM and MRG_ Introduction to MyISAM and other engine application scenarios
man man随记和crontab的@reboot用法
krpano全景之vtour文件夹和tour
HyperMotion云迁移完成阿里云专有云产品生态集成认证
回顾2021:如何帮助客户扫清上云最后一公里的障碍?
随机推荐
金融行业云迁移实践 平安金融云整合HyperMotion云迁移解决方案,为金融行业客户提供迁移服务
xx项目架构随记
sar命令详解
Five ways of using synchronized to remove clouds and fog are introduced
Preview CSV file
js 键值判断
文字组合,不重复,做搜索或查询关键字匹配
返回数组排序后下标
MySQL数据库讲解(八)
HyperMotion云迁移完成阿里云专有云产品生态集成认证
困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
DP energy Necklace
错误:无法远程查找到密钥 “428F7ECC7117F726“
Introduction to the use of semaphore for inter thread control
操作系统常见面试题目:
解决ssh配置文件优化以及连接慢的问题
星界边境Starbound创意工坊订阅的mod的存放路径
回顾2021:如何帮助客户扫清上云最后一公里的障碍?
统信UOS卸载php7.2.24,安装php7.4.27 ;卸载再安装为PHP 7.2.34
Use the executors class to quickly create a thread pool