mysql启动参数和配置文件详解
查看mysql进程
[root@us254-sy-dg-mysql-25-120 innodblog]# ps axu | grep mysql root 5472 0.0 0.0 113320 1524 ? S Feb22 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql_4308/my.cnf mysql 6755 0.0 10.5 870476 807480 ? SLl Feb22 51:00 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql_4308/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql_4308/data --plugin-dir=/usr/local/mysql/lib/mysql/plugin --user=mysql --log-error=/data/mysql_4308/log/mysql.err --open-files-limit=16384 --pid-file=/data/mysql_4308/tmp/mysql.pid --socket=/data/mysql_4308/tmp/mysql.sock --port=4308
root 29473 0.0 0.0 113320 1008 ? S 2023 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql_4307/my.cnf mysql 30756 26.5 13.6 2094212 1040856 ? SLl 2023 75289:40 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql_4307/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql_4307/data --plugin-dir=/usr/local/mysql/lib/mysql/plugin --user=mysql --log-error=/data/mysql_4307/log/mysql.err --open-files-limit=16384 --pid-file=/data/mysql_4307/tmp/mysql.pid --socket=/data/mysql_4307/tmp/mysql.sock --port=4307 30756 26.5 13.6 2094212 1040856 ? SLl 2023 75289:40 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql_4307/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql_4307/data --plugin-dir=/usr/local/mysql/lib/mysql/plugin --user=mysql --log-error=/data/mysql_4307/log/mysql.err --open-files-limit=16384 --pid-file=/data/mysql_4307/tmp/mysql.pid --socket=/data/mysql_4307/tmp/mysql.sock --port=4307
可以看到,起了两个mysql实例。
以 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql_4307/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql_4307/data --plugin-dir=/usr/local/mysql/lib/mysql/plugin --user=mysql --log-error=/data/mysql_4307/log/mysql.err --open-files-limit=16384 --pid-file=/data/mysql_4307/tmp/mysql.pid --socket=/data/mysql_4307/tmp/mysql.sock --port=4307 30756 26.5 13.6 2094212 1040856 ? SLl 2023 75289:40 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql_4307/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql_4307/data --plugin-dir=/usr/local/mysql/lib/mysql/plugin --user=mysql --log-error=/data/mysql_4307/log/mysql.err --open-files-limit=16384 --pid-file=/data/mysql_4307/tmp/mysql.pid --socket=/data/mysql_4307/tmp/mysql.sock --port=4307 这条命令为例,解析每个参数的意思
这个命令是用于启动 MySQL 服务器的,并且指定了多个参数来配置其运行时的环境和行为。以下是对这个命令的详细解读: /usr/local/mysql/bin/mysqld 这是 MySQL 服务器的主执行文件的路径。它告诉系统在哪里可以找到 mysqld 这个程序来启动 MySQL 服务器。 --defaults-file=/data/mysql_4307/my.cnf 这指定了 MySQL 服务器的配置文件的位置。my.cnf 文件包含了服务器启动和运行时的各种设置和参数。 --basedir=/usr/local/mysql 这指定了 MySQL 的基础安装目录。MySQL 的一些文件(如库文件、二进制文件等)可能位于这个目录下。 --datadir=/data/mysql_4307/data 这指定了 MySQL 数据文件的目录。MySQL 的数据库文件(如 .frm、.ibd、.frm 等)通常存储在这里。 --plugin-dir=/usr/local/mysql/lib/mysql/plugin 这指定了 MySQL 插件的目录。MySQL 的插件用于扩展其功能,如存储引擎、身份验证机制等。 --user=mysql 这指定了运行 MySQL 服务器的操作系统用户。在这个例子中,它使用 mysql 用户来运行。 --log-error=/data/mysql_4307/log/mysql.err 这指定了 MySQL 错误日志的位置。如果服务器在运行时遇到错误,它将把错误信息写入这个日志文件。 --open-files-limit=16384 这设置了 MySQL 服务器可以打开的文件描述符的最大数量。在某些系统上,可能需要增加这个值以确保 MySQL 可以正常地打开所有需要的文件。 --pid-file=/data/mysql_4307/tmp/mysql.pid 这指定了 MySQL 服务器进程 ID (PID) 文件的路径。PID 文件包含 MySQL 服务器进程的 ID,这可以用于管理和监控该进程。 --socket=/data/mysql_4307/tmp/mysql.sock 这指定了 MySQL 服务器使用的 Unix 域套接字的路径。客户端(如 mysql 命令行工具)可以通过这个套接字与服务器通信。 --port=4307 这指定了 MySQL 服务器监听的 TCP/IP 端口。默认情况下,MySQL 使用 3306 端口,但在这个例子中,它使用了 4307 端口。 总之,这个命令是用于启动 MySQL 服务器的,并且指定了服务器的各种配置和运行参数。
查看更多参数,使用命令 /usr/sbin/mysqld --verbose --help 可以列出全部的参数
MySQL 的配置文件通常包含多个部分(sections),每个部分由方括号([]
)括起来,并在其中定义与该部分相关的配置选项。这些部分根据它们所配置的内容进行命名。
以下是一些常见的 MySQL 配置文件部分:
- [mysqld]
- 这是 MySQL 服务器的主配置部分。它包含了启动
mysqld
守护进程(服务器进程)时所使用的参数。例如,端口号(port
)、数据目录(datadir
)、套接字文件位置(socket
)等。
- 这是 MySQL 服务器的主配置部分。它包含了启动
- [mysqld_safe]
- 用于配置
mysqld_safe
脚本,该脚本是启动mysqld
服务器的一个包装器(wrapper),它提供了额外的功能和安全性。
- 用于配置
- [client]
- 用于配置客户端工具(如
mysql
、mysqladmin
等)的默认参数。这些参数将作为客户端工具连接到 MySQL 服务器时的默认值。
- 用于配置客户端工具(如
- [mysqldump]
- 用于配置
mysqldump
工具,该工具用于导出 MySQL 数据库的数据和表结构。
- 用于配置
- [mysql]
- 这是客户端工具的另一个配置部分,但通常用于配置非交互式客户端(如通过脚本运行的客户端)。
- [mysqlhotcopy]
- 用于配置
mysqlhotcopy
工具,该工具用于在服务器运行时复制 MySQL 数据库。
- 用于配置
- [mysqlimport]
- 用于配置
mysqlimport
工具,该工具用于导入文本文件中的数据到 MySQL 数据库中。
- 用于配置
- [mysqlshow]
- 用于配置
mysqlshow
工具,该工具用于显示有关 MySQL 数据库、表、列和索引的信息。
- 用于配置
- [server]
- 在某些 MySQL 版本中,此部分与
[mysqld]
部分相同或类似,但可能包含仅适用于特定服务器类型的参数。
- 在某些 MySQL 版本中,此部分与
请注意,不是所有的配置文件都会包含所有这些部分,而且某些部分可能仅在你安装了特定的 MySQL 工具或包时才会出现。
此外,配置文件中的选项和参数可能会因 MySQL 版本和操作系统的不同而有所变化。因此,最好查阅与你正在使用的 MySQL 版本相对应的官方文档以获取准确的信息。
配置文件示例
[client] loose-default-character-set = utf8 [mysqld] ############### General Variables ############### # Connection and Thread Variables user = mysql port = 4308 socket = /data/mysql_4308/tmp/mysql.sock pid-file = /data/mysql_4308/tmp/mysql.pid max_allowed_packet = 64M default-storage-engine = InnoDB max_connections = 650 max_user_connections = 640 max_connect_errors = 1000000 back_log = 200 thread_cache_size = 100 open-files-limit = 16384 #unlimited input and output secure_file_priv='' init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' # Timeout Variables interactive_timeout = 28800 wait_timeout = 28800 # Directory Variables #basedir = /usr/local/Percona_5.7.16 basedir = /usr/local/mysql datadir=/data/mysql_4308/data tmpdir = /data/mysql_4308/tmp # Character Set Variables character-set-server = utf8 # Query Cache query_cache_type = 0 query_cache_size = 0M # Session Variables sort_buffer_size = 2M join_buffer_size = 2M tmp_table_size = 64M thread_stack = 192K key_buffer_size = 1M sort_buffer_size = 2M read_buffer_size = 1M read_rnd_buffer_size = 16M join_buffer_size = 2M # Table Cache table_open_cache = 2048 table_definition_cache = 1400 # MEMORY Variables max_heap_table_size = 64M memlock # MySQL Error Log log_error=/data/mysql_4308/log/mysql.err log-warnings = 2 # Slow Query Log slow_query_log = 1 slow_query_log_file=/data/mysql_4308/log/slowquery.log long_query_time = 1 log_queries_not_using_indexes = 1 # General Query Log general_log = 0 general_log_file=/data/mysql_4308/log/general.log # Binary logging and Replication #-------优化点,可以去除这个复杂逻辑,无需关注eth0还是eth1,根据传入参数来简化 #server-id='{{ansible_eth1.ipv4.address.split(".")[-2]}}{{ansible_eth1.ipv4.address.split(".")[-1]}}' server-id=1930 log-bin=/data/mysql_4308/innodblog/mysqlbin binlog_cache_size = 4M max_binlog_cache_size = 4294967296 max_binlog_size = 1G expire_logs_days = 7 sync_binlog = 1 binlog_format = ROW binlog_rows_query_log_events=on log_bin_trust_function_creators=on # Slave Variables log_slave_updates =1 read_only = 0 relay-log = /data/mysql_4308/log/slave-relay-bin relay-log-index = /data/mysql_4308/log/slave-relay-bin.index master_info_repository=TABLE relay_log_info_repository=TABLE # Security Variables #sql_mode = "TRADITIONAL,ONLY_FULL_GROUP_BY" #sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' #skip-name-resolve = 1 safe-user-create = 1 secure_auth = 1 skip-symbolic-links ############### InnoDB Variables ############### transaction_isolation = READ-COMMITTED # InnoDB Buffer and Cache Variables innodb_buffer_pool_size='200M' innodb_buffer_pool_instances = 8 innodb_max_dirty_pages_pct = 75 # InnoDB I/O Variables innodb_use_native_aio = 1 innodb_flush_method = O_DIRECT innodb_adaptive_flushing = 1 innodb_io_capacity = 200 innodb_read_io_threads = 4 innodb_write_io_threads = 4 innodb_purge_threads = 4 innodb_lru_scan_depth=1000 # InnoDB Directory Variables innodb_data_home_dir=/data/mysql_4308/data innodb_data_file_path = ibdata1:1G:autoextend innodb_file_per_table = 1 # InnoDB Log Variables innodb_log_group_home_dir=/data/mysql_4308/innodblog innodb_log_files_in_group = 2 innodb_log_file_size = 1G innodb_log_buffer_size = 16M innodb_flush_log_at_trx_commit = 1 # InnoDB Lock and Concurrency and Transaction Variables innodb_table_locks = 1 innodb_lock_wait_timeout = 50 innodb_thread_concurrency = 0 innodb_rollback_on_timeout=1 innodb_print_all_deadlocks=on # InnoDB Recovery Variables innodb_fast_shutdown = 1 #innodb_force_recovery=1 #InnoDB Other Variables innodb_stats_on_metadata = 0 #GTID gtid_mode=on enforce_gtid_consistency=on #slave config slave_parallel_type=LOGICAL_CLOCK slave_parallel_workers=8 [mysqldump] quick max_allowed_packet = 64M [mysql] no-auto-rehash max_allowed_packet = 64M prompt=mysql--\\u@\\h:\\d \\R:\\m:\\s>> init-command="set interactive_timeout=28800;set wait_timeout=28800;"