my-优化后配置5.7
用法:将代码复制下来,后缀名为:ini,后面有关于my.ini的详细说明,请坚持看完。
代码如下:
# Other default tuning values# MySQL Server Instance Configuration File# ----------------------------------------------------------------------# Generated by the MySQL Server Instance Configuration Wizard### Installation Instructions# ----------------------------------------------------------------------## On Linux you can copy this file to /etc/my.cnf to set global options,# mysql-data-dir/my.cnf to set server-specific options# (@localstatedir@ for this installation) or to# ~/.my.cnf to set user-specific options.## On Windows you should keep this file in the installation directory # of your server (e.g. C:\Program Files\MySQL\MySQL Server X.Y). To# make sure the server reads the config file use the startup option # "--defaults-file". ## To run run the server from the command line, execute this in a # command line shell, e.g.# mysqld --defaults-file="C:\Program Files\MySQL\MySQL Server X.Y\my.ini"## To install the server as a Windows service manually, execute this in a # command line shell, e.g.# mysqld --install MySQLXY --defaults-file="C:\Program Files\MySQL\MySQL Server X.Y\my.ini"## And then execute this in a command line shell to start the server, e.g.# net start MySQLXY### Guildlines for editing this file# ----------------------------------------------------------------------## In this file, you can use all long options that the program supports.# If you want to know the options a program supports, start the program# with the "--help" option.## More detailed information about the individual options can also be# found in the manual.## For advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html### CLIENT SECTION# ----------------------------------------------------------------------## The following options will be read by MySQL client applications.# Note that only client applications shipped by MySQL are guaranteed# to read this section. If you want your own MySQL client program to# honor these values, you need to specify it as an option during the# MySQL client library initialization.#[client]no-beep# pipe# socket=0.0port=3306[mysql]default-character-set=utf8# SERVER SECTION# ----------------------------------------------------------------------## The following options will be read by the MySQL Server. Make sure that# you have installed the server correctly (see above) so it reads this # file.## server_type=3[mysqld]# The next three options are mutually exclusive to SERVER_PORT below.# skip-networking# enable-named-pipe# shared-memory# shared-memory-base-name=MYSQL# The Pipe the MySQL Server will use# socket=MYSQL# The TCP/IP Port the MySQL Server will listen onport=3306# Path to installation directory. All paths are usually resolved relative to this.# basedir="C:/Program Files/MySQL/MySQL Server 5.7/"# Path to the database rootdatadir=C:/ProgramData/MySQL/MySQL Server 5.7\Data# The default character set that will be used when a new schema or table is# created and no character set is definedcharacter-set-server=utf8# The default storage engine that will be used when create new tables whendefault-storage-engine=INNODB# Set the SQL mode to strictsql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"# Enable Windows Authentication# plugin-load=authentication_windows.dll# General and Slow logging.log-output=FILEgeneral-log=0general_log_file="JSDS-WORK2-PC.log"slow-query-log=1slow_query_log_file="JSDS-WORK2-PC-slow.log"long_query_time=10# Binary Logging.# log-bin# Error Logging.log-error="JSDS-WORK2-PC.err"# Server Id.server-id=1# Secure File Priv.secure-file-priv="C:/ProgramData/MySQL/MySQL Server 5.7/Uploads"# The maximum amount of concurrent sessions the MySQL server will# allow. One of these connections will be reserved for a user with# SUPER privileges to allow the administrator to login even if the# connection limit has been reached.max_connections=1500# Query cache is used to cache SELECT results and later return them# without actual executing the same query once again. Having the query# cache enabled may result in significant speed improvements, if your# have a lot of identical queries and rarely changing tables. See the# "Qcache_lowmem_prunes" status variable to check if the current value# is high enough for your load.# Note: In case your tables change very often or if your queries are# textually different every time, the query cache may result in a# slowdown instead of a performance improvement.query_cache_size=0# The number of open tables for all threads. Increasing this value# increases the number of file descriptors that mysqld requires.# Therefore you have to make sure to set the amount of open files# allowed to at least 4096 in the variable "open-files-limit" in# section [mysqld_safe]table_open_cache=4096# Maximum size for internal (in-memory) temporary tables. If a table# grows larger than this value, it is automatically converted to disk# based table This limitation is for a single table. There can be many# of them.tmp_table_size=1024M# How many threads we should keep in a cache for reuse. When a client# disconnects, the client's threads are put in the cache if there aren't# more than thread_cache_size threads from before. This greatly reduces# the amount of thread creations needed if you have a lot of new# connections. (Normally this doesn't give a notable performance# improvement if you have a good thread implementation.)thread_cache_size=20#*** MyISAM Specific options# The maximum size of the temporary file MySQL is allowed to use while# recreating the index (during REPAIR, ALTER TABLE or LOAD DATA INFILE.# If the file-size would be bigger than this, the index will be created# through the key cache (which is slower).myisam_max_sort_file_size=100G# If the temporary file used for fast index creation would be bigger# than using the key cache by the amount specified here, then prefer the# key cache method. This is mainly used to force long character keys in# large tables to use the slower key cache method to create the index.myisam_sort_buffer_size=1G# Size of the Key Buffer, used to cache index blocks for MyISAM tables.# Do not set it larger than 30% of your available memory, as some memory# is also required by the OS to cache rows. Even if you're not using# MyISAM tables, you should still set it to 8-64M as it will also be# used for internal temporary disk tables.key_buffer_size=64M# Size of the buffer used for doing full table scans of MyISAM tables.# Allocated per thread, if a full scan is needed.read_buffer_size=128Kread_rnd_buffer_size=256K#*** INNODB Specific options ***# innodb_data_home_dir=0.0# Use this option if you have a MySQL server with InnoDB support enabled# but you do not plan to use it. This will save memory and disk space# and speed up some things.# skip-innodb# If set to 1, InnoDB will flush (fsync) the transaction logs to the# disk at each commit, which offers full ACID behavior. If you are# willing to compromise this safety, and you are running small# transactions, you may set this to 0 or 2 to reduce disk I/O to the# logs. Value 0 means that the log is only written to the log file and# the log file flushed to disk approximately once per second. Value 2# means the log is written to the log file at each commit, but the log# file is only flushed to disk approximately once per second.innodb_flush_log_at_trx_commit=0# The size of the buffer InnoDB uses for buffering log data. As soon as# it is full, InnoDB will have to flush it to disk. As it is flushed# once per second anyway, it does not make sense to have it very large# (even with long transactions).innodb_log_buffer_size=16M# InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and# row data. The bigger you set this the less disk I/O is needed to# access data in tables. On a dedicated database server you may set this# parameter up to 80% of the machine physical memory size. Do not set it# too large, though, because competition of the physical memory may# cause paging in the operating system. Note that on 32bit systems you# might be limited to 2-3.5G of user level memory per process, so do not# set it too high.innodb_buffer_pool_size=3072M# Size of each log file in a log group. You should set the combined size# of log files to about 25%-100% of your buffer pool size to avoid# unneeded buffer pool flush activity on log file overwrite. However,# note that a larger logfile size will increase the time needed for the# recovery process.innodb_log_file_size=128M# Number of threads allowed inside the InnoDB kernel. The optimal value# depends highly on the application, hardware as well as the OS# scheduler properties. A too high value may lead to thread thrashing.innodb_thread_concurrency=9# The increment size (in MB) for extending the size of an auto-extend InnoDB system tablespace file when it becomes full.innodb_autoextend_increment=64# The number of regions that the InnoDB buffer pool is divided into.# For systems with buffer pools in the multi-gigabyte range, dividing the buffer pool into separate instances can improve concurrency,# by reducing contention as different threads read and write to cached pages.innodb_buffer_pool_instances=8# Determines the number of threads that can enter InnoDB concurrently.innodb_concurrency_tickets=5000# Specifies how long in milliseconds (ms) a block inserted into the old sublist must stay there after its first access before# it can be moved to the new sublist.innodb_old_blocks_time=1000# It specifies the maximum number of .ibd files that MySQL can keep open at one time. The minimum value is 10.innodb_open_files=300# When this variable is enabled, InnoDB updates statistics during metadata statements.innodb_stats_on_metadata=0# When innodb_file_per_table is enabled (the default in 5.6.6 and higher), InnoDB stores the data and indexes for each newly created table# in a separate .ibd file, rather than in the system tablespace.innodb_file_per_table=1# Use the following list of values: 0 for crc32, 1 for strict_crc32, 2 for innodb, 3 for strict_innodb, 4 for none, 5 for strict_none.innodb_checksum_algorithm=0# The number of outstanding connection requests MySQL can have.# This option is useful when the main MySQL thread gets many connection requests in a very short time.# It then takes some time (although very little) for the main thread to check the connection and start a new thread.# The back_log value indicates how many requests can be stacked during this short time before MySQL momentarily# stops answering new requests.# You need to increase this only if you expect a large number of connections in a short period of time.back_log=80# If this is set to a nonzero value, all tables are closed every flush_time seconds to free up resources and# synchronize unflushed data to disk.# This option is best used only on systems with minimal resources.flush_time=0# The minimum size of the buffer that is used for plain index scans, range index scans, and joins that do not use# indexes and thus perform full table scans.join_buffer_size=256K# The maximum size of one packet or any generated or intermediate string, or any parameter sent by the# mysql_stmt_send_long_data() C API function.max_allowed_packet=4M# If more than this many successive connection requests from a host are interrupted without a successful connection,# the server blocks that host from performing further connections.max_connect_errors=100# Changes the number of file descriptors available to mysqld.# You should try increasing the value of this option if mysqld gives you the error "Too many open files".open_files_limit=4161# Set the query cache type. 0 for OFF, 1 for ON and 2 for DEMAND.query_cache_type=0# If you see many sort_merge_passes per second in SHOW GLOBAL STATUS output, you can consider increasing the# sort_buffer_size value to speed up ORDER BY or GROUP BY operations that cannot be improved with query optimization# or improved indexing.sort_buffer_size=256K# The number of table definitions (from .frm files) that can be stored in the definition cache.# If you use a large number of tables, you can create a large table definition cache to speed up opening of tables.# The table definition cache takes less space and does not use file descriptors, unlike the normal table cache.# The minimum and default values are both 400.table_definition_cache=1400# Specify the maximum size of a row-based binary log event, in bytes.# Rows are grouped into events smaller than this size if possible. The value should be a multiple of 256.binlog_row_event_max_size=8K# If the value of this variable is greater than 0, a replication slave synchronizes its master.info file to disk.# (using fdatasync()) after every sync_master_info events.sync_master_info=10000# If the value of this variable is greater than 0, the MySQL server synchronizes its relay log to disk.# (using fdatasync()) after every sync_relay_log writes to the relay log.sync_relay_log=10000# If the value of this variable is greater than 0, a replication slave synchronizes its relay-log.info file to disk.# (using fdatasync()) after every sync_relay_log_info transactions.sync_relay_log_info=10000# Load mysql plugins at start."plugin_x ; plugin_y".# plugin_load# MySQL server's plugin configuration.# loose_mysqlx_port=33060
配置详解
max_connections
最大并发连接数。当MySQL的并发连接达到这个设定值时,新的连接将会被拒绝。当发现MySQL有能力处理更多的并发的时候, 建议调大这个值,相应给服务器带来更高的负载(CPU/IO/内存)。默认值:100 参考设置:900
back_log
TCP/IP连接队列的最大值。当MySQL同时有大量连接请求的时候,MySQL会尝试用当前现有的线程处理这些请求,如果处理不过来MySQL会把连接先放到一个队列里面,然后起新的线程处理。这个过程会很快,但是并发请求很多的话,需要调高这个值,否则新来的连接请求会被拒绝。在一次压测的时候发现客户端返回大量的“Lost connection to MySQL”, 就是因为back_log的默认值太小导致的。增大这个值会增大CPU负载并消耗更多的内存。默认值50, 参考设置200
skip-name-resolve
关闭反向域名解析。MySQL默认会对每个客户端连接作反向域名解析,强烈建议关闭反向域名解析。关闭的方法是在my.cnf里面加一行skip-name-resolve
innodb_file_per_table
独立表空间。共享表空间指某一个数据库的所有表数据、索引全部放在一个文件中,默认这个共享表空间的文件路径在data目录下,默认的文件名为ibdata1初始化为10M。独立表空间指每一个表都会生成独立的.frm表描述文件来存储,还有一个.ibd文件。其中这个文件包括了单独一个表的数据内容以及索引内容,默认情况下它的存储位置也是在表的位置之中。
如果使用innodb,强烈建议打开这个设置,否则所有的innodb表共享一个文件,并且这个文件的大小不会因为表数据的减少而减小,时间长了会把磁盘搞爆。
max_connect_errors
当客户端连接服务端超时(超过connect_timeout),服务端就会给这个客户端记录一次error,当出错的次数达到max_connect_errors的时候,这个客户端就会被锁定。除非执行FLUSH HOSTS命令。默认值10,参考设置1844674407370954751(能设多大,设多大)
connect_timeout
连接超时的秒数。默认值5,参考设置15
slave_net_timeout
MySQL主从复制的时候,当Master和Slave之间的网络中断,但是Master和Slave无法察觉的情况下(比如防火墙或者路由问题)。Slave会等待slave_net_timeout设置的秒数后,才能认为网络出现故障,然后才会重连并且追赶这段时间主库的数据。默认是3600秒,相信一个小时之后,黄花菜都凉了。默认值3600,参考设置30
lower_case_table_names
忽略表名大小写,否则使用mycat时会报错 lower_case_table_names=1
wait_timeout
连接过期秒数。当一个连接sleep超过wait_timeout秒后,MySQL服务端会中断这个连接。这个值设置的过长有可能会导致大量的sleep链接占用系统资源,过小会导致“MySQL has gone away”的错误。默认值28800 不变
key_buffer
主键缓存。如果发现有大量的slow log,可以尝试调高这个值,相应会带来更高的内存开销。
table_cache
给经常访问的表分配的内存。调大这个值,一般情况下可以降低磁盘IO, 但是相应会占用更多的内存。
注意事项:
(1)my-default.ini文件存不存在不重要,关键需要自己在根目录下新建一个my.ini文件,针对JSDS的my.ini文件在文章最开始的地方
这里举例是常用的配置,具体内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
可以直接复制粘贴,主要修改下basedir后的路径,注意路径中为\\而非\
(2)请不要自己创建data文件夹
网上很多教程建议自己新建一个空data文件夹,再在my.ini文件中增加一句:
1 2 |
|
这样会导致服务无法启动,不要添加这一句,也不要自己新建data文件夹,由mysql自动生成data就好,具体来说:
1)以管理员的方式打开cmd命令窗口(直接打开cmd运行,可能会报错),并且进入到mysql安装目录的bin目录下。然后输入命令mysqld install 显示成功
2)再输入:mysqld –initialize 此时应该没有任何提示
3)再输入:net start mysql 显示
最后data文件夹里面的内容应该显示为:
另注:第一次登陆MYSQL时,会提示要求输入初始密码,这是考虑安全因素,命令:mysqld –initialize会随机生成密码。
初始密码在上图data文件夹下的xxx.err文件中,可以用记事本打开,用ctrl+f 查找功能找到如下一行记录:
[Note] A temporary password is generated for root@localhost: NZ+uhXPq1zN.
其中NZ+uhXPq1zN.即为初始密码(注意.号不要漏了)
进入后可以用如下命令修改,这里密码改为root:
1 |
|
这里是5.7.22版本,显示基本如下:
以上就是关于mysql的my.ini配置文件的详细说明,需要的小朋友请详看,不需要的只需要将最开始的代码复制粘贴,替换原来的my.ini,
那么查看my.ini路径这里提供两种方法,请看下面:
在DOS命令行窗口登录MySQL,输入如下命令查看MySQL的安装目录和数据存放目录,MySQL的配置文件就在数据存放目录下:
另外一种方法:
在“开始 → 所有程序 → MySQL”下面找到MySQL的命令行客户端工具,右键选择该命令行工具查看“属性”,在“目标”里面也可以看到MySQL使用的配置文件位置。
注意:ProgramData目录可能是“隐藏”的,如果在显示的分区中看不到的话就修改一下系统设置,让隐藏文件/文件夹可见。或者直接将ProgramData/MySQL/MySQL Server 5.6目录复制下来直接粘贴到文件夹的目录窗口中也可以。
--------------------------------------------------- 我 是 分 隔 线 ----------------------------------------------------
第一步在我们的电脑上面找到mysql的安装路径,点击进去。
第二步进去之后可以看到my.ini在mysql安装路径的根目录,这是其中一种情况。
第三步另外一种情况是我们在mysql安装目录找不到,看到了my-default.ini文件,这时my.ini在“C:\ProgramData\MySQL\MySQL Server 5.6”目录下面,我们首先需要找到ProgramData文件夹,这是一个隐藏文件,需要隐藏文件可见才行,或者可以直接检索。
第四步我们可以看到在“C:\ProgramData\MySQL\MySQL Server 5.6”目录下面找到了my.ini文件。