当前位置:网站首页>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