当前位置:网站首页>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
边栏推荐
- 线程间控制之CountDownLatch和CyclicBarrier使用介绍
- Web page, adaptive, proportional scaling
- 使用Executors类快速创建线程池
- Essential difference between restful WebService and gSOAP webservice
- FBS (fman build system) packaging
- RecyclerView高级使用(一)-侧滑删除的简单实现
- gif转为静态图片处理
- STD:: map and STD:: vector memory free
- js 抛物线运动方法封装
- 贷款市场报价利率(LPR)与贷款基准利率介绍
猜你喜欢
Uni app message push
RecyclerView进阶使用-实现仿支付宝菜单编辑页面拖拽功能
KVM学习资源
MySQL-InnoDB-事务
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
字节面试编程题:最小的K个数
困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
差分隐私(背景介绍)
xx项目架构随记
Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface
随机推荐
PySide2
Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting
ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
容灾有疑问?点这里
redis数据库讲解(四)主从复制、哨兵、Cluster群集
VMware installation 64 bit XP Chinese tutorial
Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
关于云容灾,你需要知道这些
Request module
正则表达式
MySQL数据库讲解(九)
ActiveMQ Basics
Uni app message push
KVM学习资源
JDBC和servlet写CRUD的接口总结
云迁移的六大场景
在电视屏幕上进行debug调试
Arrays类的使用案例
js 抛物线运动方法封装
redis数据库讲解二(redis高可用、持久化、性能管理)