当前位置:网站首页>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
边栏推荐
猜你喜欢
VMware installation 64 bit XP Chinese tutorial
krpano全景之vtour文件夹和tour
Pass in external parameters to the main function in clion
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
RecyclerView高级使用(一)-侧滑删除的简单实现
OpenStack命令操作
进入新公司,运维工程师从下面这几项了解系统的部署
使用Executors类快速创建线程池
金融行业云迁移实践 平安金融云整合HyperMotion云迁移解决方案,为金融行业客户提供迁移服务
win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
随机推荐
关于NodeJS中JSON5的相关配置和使用
VMware installation 64 bit XP Chinese tutorial
x509证书cer格式转pem格式
Request module
FBS (fman build system) packaging
JDBC details
xx项目架构随记
Essential difference between restful WebService and gSOAP webservice
在Clion中给主函数传入外部参数
MySQL数据库讲解(十)
Recyclerview advanced use (I) - simple implementation of sideslip deletion
MySQL数据库讲解(九)
Five ways of using synchronized to remove clouds and fog are introduced
On the problem of cliff growth of loss function in the process of training
JS recursion (1)
Visio installation error 1:1935 2: {XXXXXXXX
OpenStack命令操作
HyperMotion云迁移完成阿里云专有云产品生态集成认证
JS format time
倒计时1天~2022云容灾产品线上发布会即将开始