k8s、docker-compose安装 mysql 8.0.18

一、k8s安装mysql 8.0.18

1、安装服务

vim mysql-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
    name: mysql-deploy
    labels:
      name: mysql-deploy-label
    namespace: paas-db
spec:
    replicas: 1
    selector:
      matchLabels:
        name: mysql-pod
    template:
      metadata:
        labels: 
          name: mysql-pod
      spec:
        nodeSelector: 
          mysql: "true"
        terminationGracePeriodSeconds: 30  #k8s正确、优雅地关闭应用,等待时间30秒
        containers:
        - name: mysql-container
          image: mysql:8.0.18
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: "123456"
          - name: TIME_ZONE
            value: Asia/Shanghai
          volumeMounts:
          - name: mysql-volume
            mountPath: /var/lib/mysql
          #- name: mysql-conf
          #  mountPath: /etc/mysql/conf.d
          - name: date-config
            mountPath: /etc/localtime
        volumes:
        - name: mysql-volume
          hostPath:
            path: /home/k8s-1.19.2/paas-db/mysql/volume
        #- name: mysql-conf
        #  hostPath:
        #    path: /home/k8s-1.19.2/paas-db/mysql/config/conf.d
        - name: date-config
          hostPath:
            path: /etc/localtime

vim mysql-service.yaml

#Service
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
  labels: 
    name: mysql-service-label
  namespace: paas-db
spec:
  type: NodePort
  ports:
  - port: 3306
    protocol: TCP
    targetPort: 3306
    name: http
    nodePort: 30306
  selector:
    name: mysql-pod
2、在数据落地的服务器上把配置文件从容器中拷贝出来
[root@UAT-181-30 config]# docker cp c35f42eb5c0b:/etc/mysql/conf.d .
[root@UAT-181-30 config]# ll
总用量 0
drwxrwxr-x 2 root root 41 12月 29 2019 conf.d
3、去掉deployment的配置文件注释引用外部配置文件
4、mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password, 解决问题方法有两种,一种是升级navicat驱动,一种是把mysql用户登录密码加密规则还原成mysql_native_password

现象:
k8s、docker-compose安装 mysql 8.0.18_第1张图片
处理方式:
1、安装mysql客户端进入mysql
mysql -h 192.168.180.37 -P 30306 -uroot -p123456
或者进入mysql容器,在容器内登录mysql

[root@UAT-181-30 config]# docker exec -it c35f42eb5c0b bash
root@mysql-deploy-69978db9c8-jz6fc:/# mysql -h 127.0.0.1 -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 11
Server version: 8.0.18 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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>

2、对mysql进行如下设置。

MySQL [(none)]>  alter user 'root'@'%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]>

二、docker-compose安装mysql 8.0.18

version: '3'
services:
  db:
    #构建mysql镜像
    image: mysql:8.0.18
    container_name: mysql # 容器名
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root #root管理员用户密码
      MYSQL_ROOT_HOST: '%'       # 访问权限
      MYSQL_USER: test           # 创建新用户,需要手动赋权
      MYSQL_PASSWORD: test       # 新用户的密码
    ports:
    - '3306:3306'  #host物理直接映射端口为6606
    volumes:
    #mysql数据库挂载到host物理机目录
    - "./mysql_data:/var/lib/mysql"
    #容器的配置目录挂载到host物理机目录
    #- "./conf:/etc/mysql/conf.d"
    #容器的时间和操作系统一致
    - /etc/localtime:/etc/localtime

你可能感兴趣的:(mysql,mysql,数据库)