k8s 部署mysql 5.7的完整配置

mysql-configmap.yaml

bash 复制代码
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: default
data:
  my.cnf: |
    [client]
    port = 3306
    socket = /var/run/mysqld/mysqld.sock

    [mysqld_safe]
    pid-file = /var/run/mysqld/mysqld.pid
    socket = /var/run/mysqld/mysqld.sock
    nice = 0

    [mysqld]
    skip-host-cache
    skip-name-resolve
    user = mysql
    pid-file = /var/run/mysqld/mysqld.pid
    socket = /var/run/mysqld/mysqld.sock
    port = 3306
    basedir = /usr
    datadir = /var/lib/mysql
    tmpdir = /tmp
    lc-messages-dir = /usr/share/mysql
    explicit_defaults_for_timestamp = true
    symbolic-links = 0
    !includedir /etc/mysql/conf.d/

    # Custom settings
    lower_case_table_names = 1
    wait_timeout = 2147483
    interactive_timeout = 2147483
    max_connections = 20000
    max_connect_errors = 6000
    open_files_limit = 65535
    table_open_cache = 512
    max_allowed_packet = 500M
    binlog_cache_size = 1M
    max_heap_table_size = 8M
    tmp_table_size = 64M
    read_buffer_size = 2M
    read_rnd_buffer_size = 8M
    sort_buffer_size = 8M
    join_buffer_size = 8M
    key_buffer_size = 512M
    thread_cache_size = 32
    query_cache_size = 128M
    default-storage-engine = INNODB
    innodb_file_per_table = 1
    innodb_open_files = 500
    innodb_buffer_pool_size = 4G
    innodb_write_io_threads = 4
    innodb_read_io_threads = 4
    innodb_thread_concurrency = 0
    innodb_purge_threads = 1
    innodb_flush_log_at_trx_commit = 2
    innodb_log_buffer_size = 2M
    innodb_log_file_size = 1G
    innodb_log_files_in_group = 3
    innodb_max_dirty_pages_pct = 90
    innodb_lock_wait_timeout = 120
    bulk_insert_buffer_size = 8M
    myisam_sort_buffer_size = 32M
    myisam_max_sort_file_size = 10G
    sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

mysql-deployment.yaml

bash 复制代码
docker pull registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql:5.7
bash 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  namespace: default
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql:5.7   #由于国内镜像问题,需要自己的外部镜像地址
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"     #设置自己打用户密码
        volumeMounts:
        - name: mysql-config-volume
          mountPath: /etc/mysql/my.cnf
          subPath: my.cnf
      volumes:
      - name: mysql-config-volume
        configMap:
          name: mysql-config
          items:
          - key: my.cnf
            path: my.cnf

mysql-service.yaml

bash 复制代码
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql
  ports:
    - port: 3306
      targetPort: 3306
      nodePort: 30306  # 这里指定暴露的NodePort
  type: NodePort
bash 复制代码
kubectl apply -f mysql-configmap.yaml
bash 复制代码
kubectl apply -f mysql-deployment.yaml
bash 复制代码
kubectl apply -f mysql-service.yaml

验证部署

bash 复制代码
kubectl get pods -l app=mysql
bash 复制代码
kubectl exec -it <mysql-pod-name> -- cat /etc/mysql/conf.d/my.cnf

将 替换为实际的 MySQL Pod 名称。

上述 my.cnf 配置文件中包括了许多针对性能优化和其他功能的配置项:

lower_case_table_names=1:使表名不区分大小写。

wait_timeout 和 interactive_timeout:设置连接超时时间。

max_connections:最大连接数。

innodb_buffer_pool_size:InnoDB 缓冲池大小。

innodb_log_file_size:InnoDB 日志文件大小。

其他各种优化参数。

通过这些配置,可以优化 MySQL 的性能并确保其在 Kubernetes 环境中稳定运行。

相关推荐
vx_dmxq21121 小时前
【微信小程序学习交流平台】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·python·mysql·微信小程序·小程序·idea
m***92381 天前
【SQL】MySQL中的字符串处理函数:concat 函数拼接字符串,COALESCE函数处理NULL字符串
数据库·sql·mysql
TracyCoder1231 天前
MySQL 实战宝典(八):Java后端MySQL分库分表工具解析与选型秘籍
java·开发语言·mysql
敲上瘾1 天前
【探索实战】:Kurator分布式统一应用分发平台的全面解析与实践指南
分布式·容器·kubernetes·serverless
q***81641 天前
MySQL:数据查询-limit
数据库·mysql
p***92481 天前
DBeaver连接本地MySQL、创建数据库表的基础操作
数据库·mysql
I***t7161 天前
一条sql 在MySQL中是如何执行的
数据库·sql·mysql
vx_dmxq2111 天前
【PHP考研互助系统】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·mysql·考研·微信小程序·小程序·php
Connie14511 天前
记一次K8s故障告警排查(Grafna告警排查)
云原生·容器·kubernetes·grafana
vx_vxbs662 天前
【SSM电影网站】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·python·mysql·小程序·php·idea