目录

  • 前言
  • 测试环境
  • my.cnf配置文件参数说明

前言

本文主要介绍MySQL配置文件中参数(my.cnf)

测试环境

虚拟机环境:VirtualBox 6.0.24
操作系统:Oracle Linux Server release 6.5 x86_64
MySQL版本:5.7.33

my.cnf配置文件参数说明

mysql、client部分参数说明,如下所示:

[client]
default-character-set = utf8mb4
port = 3306
socket = /usr/local/mysql/mysql-files/mysql.sock

[mysql]
prompt = "\\U \\R:\\m:\\s [\d]> "
no_auto_rehash
show-warnings
default-character-set = utf8mb4
socket = /usr/local/mysql/mysql-files/mysql.sock
1. default-character-set 
 默认字符集设置。
 通常来说这个非必选项,当客户端字符集设置为与系统字符集不匹配时,可以进行按需设置。
 2. port 
 侦听TCP/IP连接时使用的端口号。
 命令行方式指定为--port=portnum或-P portnum。
 数据库中变量名称为port。
 3. socket 
 在Unix上,这个选项指定在监听本地连接时使用的Unix套接字文件。
 默认值为“/tmp/mysql.sock”。
 命令行方式指定为--socket=filename。
 数据库中变量名称为socket。 
 4. prompt
 连接提示符设置格式
 5. no_auto_rehash
 自动补全功能,默认开启,禁用可以提高mysql启动速度
 6. show-warnings
 在每个语句之后(如果有的话)显示警告。

查看数据库中以上相关变量,如下所示:

[root@rac02 ~]# mysql --print-defaults
mysql would have been started with the following arguments:
--socket=/usr/local/mysql/mysql-files/mysql.sock --prompt=\U \R:\m:\s [\d]>  --no_auto_rehash --show-warnings --default-character-set=utf8mb4 


root@localhost 21:42:41 [(none)]> show variables like '%chara%';
+--------------------------+----------------------------------------------------------------+
| Variable_name            | Value                                                          |
+--------------------------+----------------------------------------------------------------+
| character_set_client     | utf8mb4                                                        |
| character_set_connection | utf8mb4                                                        |
| character_set_database   | utf8mb4                                                        |
| character_set_filesystem | binary                                                         |
| character_set_results    | utf8mb4                                                        |
| character_set_server     | utf8mb4                                                        |
| character_set_system     | utf8                                                           |
| character_sets_dir       | /usr/local/mysql-5.7.33-linux-glibc2.12-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+
8 rows in set (0.01 sec)

root@localhost 21:41:56 [(none)]> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)


 root@localhost 21:41:38 [(none)]> show variables like 'socket';
+---------------+-----------------------------------------+
| Variable_name | Value                                   |
+---------------+-----------------------------------------+
| socket        | /usr/local/mysql/mysql-files/mysql.sock |
+---------------+-----------------------------------------+
1 row in set (0.00 sec)

mysqld部分参数说明如下所示:

[mysqld]
user = mysql
port = 3306
symbolic-links = 0
server_id = 56102
basedir = /usr/local/mysql
datadir = /usr/local/mysql/mysql-files
socket = /usr/local/mysql/mysql-files/mysql.sock
pid_file = /usr/local/mysql/mysql-files/rac02.pid
character-set-server = utf8mb4
skip_name_resolve = 1
1. server_id
 Server id,唯一标识,为复制做准备,开启binlog必须设置。
 注释server_id后启动mysql会提示报错,启动失败
 [root@rac02 ~]# service mysql start
 Starting MySQL.The server quit without updating PID file (/[FAILED]l/mysql/mysql- files/rac02.pid).
 2. character-set-server
 服务器的默认字符集。
 3. skip_name_resolve
 检查客户端连接时是否解析主机名。
 默认值是OFF。
#metadata lock time,单位秒
lock_wait_timeout = 3600

#mysqld从操作系统中可用的文件描述符的数量
#在Unix系统中,该值不能设置大于ulimit -n命令显示的值
open_files_limit    = 65535

#back_log值表示在MySQL暂时停止响应新请求之前的短时间内,可以堆叠多少个请求。
#只有当您期望在短时间内进行大量连接时,才需要增加此值。Back_log不能设置得高于操作系统限制。
#操作系统的值参考net.ipv4.tcp_max_syn_backlog。
back_log = 1024

#允许的最大同时客户端连接数
max_connections = 512
max_connect_errors = 1000000

table_open_cache = 1024
table_definition_cache = 1024


#每个线程的堆栈大小
thread_stack = 512K

#每个连接需要的内存
sort_buffer_size = 4M
join_buffer_size = 4M
read_buffer_size = 8M
read_rnd_buffer_size = 4M


bulk_insert_buffer_size = 64M
thread_cache_size = 768

#参数同时修改,要不取两个值最大值
#服务器在关闭交互式连接之前等待活动的秒数
interactive_timeout = 600
#服务器在关闭非交互式连接之前等待活动的秒数
wait_timeout = 600

#内部内存临时表的最大大小。这个变量不适用于用户创建的MEMORY表。
tmp_table_size = 32M
max_heap_table_size = 32M

#error日志
log_error = /usr/local/mysql/mysql-files/error.log
log_error_verbosity = 3

#慢日志
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/mysql-files/slow.log

long_query_time = 0.1
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1

#binlog,format有statement,row,mixed
log_bin = /usr/local/mysql/mysql-files/mysql_binlog
binlog_format = ROW

#控制MySQL服务器将二进制日志同步到磁盘的频率
sync_binlog = 1   #双1设置
binlog_cache_size = 4M
max_binlog_cache_size = 2G     
max_binlog_size = 1G
binlog_rows_query_log_events = 1
binlog_checksum = CRC32
gtid_mode = ON
enforce_gtid_consistency = TRUE


#myisam的配置
key_buffer_size = 32M
myisam_sort_buffer_size = 128M


#innodb相关的配置
transaction_isolation = REPEATABLE-READ
#缓冲池的字节大小,InnoDB缓存表和索引数据的内存区域
innodb_buffer_pool_size = 500M
#当缓冲池的大小大于1GB时,将innodb_buffer_pool_instances的值设置为大于1可以提高繁忙服务器的可伸缩性
#非windows系统下,innodb_buffer_pool_size 小于1G时,该项不生效,默认为1;
#非windows系统下,innodb_buffer_pool_size 大于1G时,默认值为8
innodb_buffer_pool_instances = 4  #该设置不生效,在本配置文件中,显示默认值1

innodb_data_file_path = ibdata1:12M:autoextend
#在每个事务提交时,将日志写入和刷新到磁盘,刷新频率由innodb_flush_log_at_timeout控制,默认1秒
innodb_flush_log_at_trx_commit = 1   #双1设置

innodb_log_buffer_size = 32M
innodb_log_file_size = 200M
innodb_log_files_in_group = 3
innodb_max_undo_log_size = 1G

innodb_io_capacity = 400
innodb_io_capacity_max = 800
innodb_open_files = 65535
#
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_online_alter_log_max_size = 4G
innodb_status_file = 1

innodb_status_output = 0
innodb_status_output_locks = 1
innodb_sort_buffer_size = 67108864
innodb_adaptive_hash_index = OFF



[mysqldump]
quick

以上说明来源于官方文档及相关书籍阅读整理的个人理解。