小工具知识网

当前位置: 首页 > 行业知识 > k8s 部署mysql 5.7的完整配置

行业知识

k8s 部署mysql 5.7的完整配置

2026-12-05 06:08:34 来源:互联网转载

mysql-configmap.yaml

apiVersion: v1kind: ConfigMapmetadata:  name: mysql-config  namespace: defaultdata:  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/v1kind: Deploymentmetadata:  name: mysql-deployment  namespace: default  labels:    app: mysqlspec:  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: v1kind: Servicemetadata:  name: mysql-servicespec:  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" >文件中包括了许多针对性能优化和其他功能的配置项:

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

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

max_connections:最大连接数。

innodb_buffer_pool_size:InnoDB 缓冲池大小。

innodb_log_file_size:InnoDB 日志文件大小。

其他各种优化参数。

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

上一篇:大学服务器崩了怎么办?

下一篇:服务器主机怎么插耳机?