k8s 部署mysql 5.7的完整配置

2024-06-22 4760阅读

k8s 部署mysql 5.7的完整配置 第1张

mysql-configmap.yaml

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

docker pull registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql:5.7
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

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

验证部署

kubectl get pods -l app=mysql
kubectl exec -it  -- cat /etc/mysql/conf.d/my.cnf

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

上述 my.cnf 配置index.php/tags-522.html" class="superseo">文件中包括了许多针对性能优化和其他功能的配置项:

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

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

max_connections:最大连接数。

innodb_buffer_pool_size:InnoDB 缓冲池大小。

innodb_log_file_size:InnoDB 日志文件大小。

其他各种优化参数。

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


    免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

    目录[+]