一、简介
本文介绍如何在 海光 C86 架构的服务器上安装并配置MySQL 8 。
二、引言
适用于国产化CPU硬件服务器:如 海光 C86-5280等
以及适用于不支持x86-64-v2指令集的CPU:Fatal glibc error: CPU does not support x86-64-v2
替换为不要求 x86-64-v2 的 MySQL 镜像,如:Debian 系列、mysql:8.0.36
详解
1、创建目录
bash
>> mkdir -p /opt/software/mysql8/{conf,init} /data/mysql8/{backup,data}
2、编辑docker-compose.yml
bash
>> cd /opt/software/mysql8/
>> vim docker-compose.yml # 内容如下,复制粘贴即可
yml
---
# 定义密钥
secrets:
passwd_root:
file: ./secrets/passwd_root.txt
# 服务
services:
mysql8:
container_name: mysql-common
image: mysql:8.0.44-debian
user: "999:999"
restart: unless-stopped
privileged: true # 授予容器最高系统权限;获得对宿主机的完全访问控制权
ports:
- 30096:3306 # 宿主机指定网卡映射
volumes:
- /data/mysql8/data:/var/lib/mysql
- ./conf/my.cnf:/etc/mysql/my.cnf:ro
- ./logs:/var/log/mysql
- ./init:/docker-entrypoint-initdb.d
- /etc/localtime:/etc/localtime:ro
secrets: # 挂载secrets; 默认挂载至容器内的 /run/secrets/ 路径
- passwd_root
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD_FILE: "/run/secrets/passwd_root"
healthcheck:
test: ["CMD-SHELL", "MYSQL_PWD=\"$$(cat /run/secrets/passwd_root)\" mysql -h 127.0.0.1 -uroot --protocol=TCP -e 'SELECT 1' >/dev/null 2>&1 || exit 1"]
interval: 10s
timeout: 5s
retries: 5
start_period: 60s
...
3、编辑my.cnf
bash
>> vim ./conf/my.cnf
c
[client]
default-character-set = utf8mb4
socket = /var/lib/mysql/mysql.sock
[mysql]
default-character-set = utf8mb4
[mysqld]
user = mysql
autocommit = 1
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
pid-file = /var/log/mysql/mysqld.pid
log-error = /var/log/mysql/mysqld.log
symbolic-links = 0
character-set-server=utf8mb4
# binlog
log-bin = bin
binlog_cache_size=16M
binlog_format=ROW
binlog_row_image=FULL
max_binlog_cache_size=1GB
# 设置单个 binlog 文件的最大大小
max_binlog_size = 300M
# binlog日志保留时间,单位秒,30天
binlog_expire_logs_seconds=2592000
ft_min_word_len=4
connect_timeout=600
interactive_timeout=1800
wait_timeout=600
# 启用GTID
gtid_mode = ON
enforce_gtid_consistency = ON
group_concat_max_len = 102400
lower-case-table_names=1
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
default-storage-engine=innodb
# 密码认证插件
default_authentication_plugin=mysql_native_password
default-time-zone = '+08:00'
slow_query_log=1
#输出死锁
innodb_print_all_deadlocks=1
long_query_time=10
max_connections=4096
max_connect_errors=1024
max_allowed_packet=64M
back_log=512
interactive_timeout=3600
sort_buffer_size=2M
join_buffer_size=2M
read_buffer_size=2M
read_rnd_buffer_size=2M
thread_cache_size=256
table_open_cache=10000
max_heap_table_size=64M
tmp_table_size=64M
thread_stack=192KB
local_infile=0
skip-name-resolve
skip-external-locking
# innodb
innodb_buffer_pool_size=2048M
#ibdata1的数据设置应该和本地ibdata1大小的一致
innodb_data_file_path=ibdata1:1G;ibdata2:1G:autoextend
innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:5G
innodb_read_io_threads=8
innodb_write_io_threads=8
innodb_file_per_table=1
#将InnoDB的日志刷新方式调整为O_DIRECT,可以减少数据在缓存和磁盘之间的复制
innodb_flush_method=O_DIRECT
#将InnoDB的日志刷新策略调整为每秒刷新,可以提高性能
innodb_flush_log_at_trx_commit=2
innodb_lock_wait_timeout=120
innodb_thread_concurrency=16
innodb_log_buffer_size=8M
#将InnoDB日志文件大小调整为256M,可以减少写入磁盘的次数,提高性能
innodb_log_file_size=256M
innodb_redo_log_capacity=268435456
innodb_buffer_pool_size=4G
innodb_log_files_in_group=3
innodb_max_dirty_pages_pct=90
innodb_open_files=10000
skip_replica_start
log_replica_updates
sql_mode="NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO"
relay-log=relay.log
relay-log-index=slave-relay.index
log_bin_trust_function_creators = 1
4、创建初始化脚本
bash
# 编辑密码文件
>> echo "super@ADMIN_2026" > ./secrets/passwd_root.txt
>> vim ./init/init.sql # 可根据需要自定义
sql
create user 'common'@'%' identified with mysql_native_password by "jiazhi@admin_123";
grant all privileges on *.* to 'common'@'%' WITH GRANT OPTION;
flush privileges;
5、启动MySQL
bash
# 授权
>> chown -R 999:999 /data/mysql8/ /opt/software/mysql8/
>> chmod -R 755 /data/mysql8/ /opt/software/mysql8/
# 启动MySQL
>> docker-compose up -d
# 若后续修改了配置文件后,则重启即可,不会影响现有数据
>> docker-compose restart