Detailed explanation of Mysql configuration parameters and optimized configuration (reproduced)

MySQL has the following types of logs:
  Error log: log-err
  query log: log
  slow query log: log-slow-queries
  Update log: log-update
  Binary log: log-bin


should generate the log in the /var/log directory ( It is the place where the system log is stored, only the root account has write permissions), and the MySQL process needs to have read and write permissions to this directory, which is generally not done. It also takes into account security issues, including the data security of MySQL itself, because of the data security of MySQL. All operations are logged to the general query log. Do not use the /var/log/ directory for MySQL logs.

-------------------------------------------------- ------------------- 2my.cnf

default-character-set = utf8
port = 3306
socket = /tmp/mysql.sock

no-auto-rehash

#default-character- set=utf8
user=mysql
port=3306
socket=/tmp/mysql.sock
basedir=/usr/local/webserver/mysql
datadir=/data0/mysql/3306/data
open_files_limit = 10240
back_log = 600
max_connections = 3000 The maximum number of connections allowed by the actual MySQL server is 16384;
max_connect_errors = 6000 How many error connections can be allowed
table_cache = 1024 The size of the cache. The value of table_cache is the default value in machines with less than 2G memory 256 to 512, if the machine has 4G memory, the default value is 2048, but this definitely means that the larger the machine memory, the larger the value should be, because the increase of table_cache makes mysql respond faster to SQL, Inevitably, more deadlocks will occur, which will slow down the entire set of database operations and seriously affect performance.
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 2M is a connection-level parameter. When each connection needs to use this buffer for the first time, the set memory is allocated at one time. It is not that the bigger the better, because it is a connection-level parameter , too large settings + high concurrency may exhaust system memory resources.
join_buffer_size = 2M
thread_cache_size = 64 Reuse the number of threads saved in the cache. When disconnecting, if there is space in the cache, the client's thread will be placed in the cache. If the thread is re-requested, the request will be from the cache. During reading, if the cache is empty or there is a new request, then this thread will be recreated. If there are many new threads, increasing this value can improve system performance.
  According to the physical memory setting rules are as follows:
    1G ---> 8
    2G ---> 16
    3G ---> 32
    >3G ---> 64


thread_concurrency = 16 has a great impact on the performance of mysql, in the case of multiple cpu (or multi-core), the wrong setting If the value of thread_concurrency is changed, MySQL will not be able to make full use of multiple CPUs (or multiple cores), and only one CPU (or core) can work at the same time.
  It should be set to 2 times the number of CPU cores. For example, if there is a dual-core CPU, the thread_concurrency should be 4; if there are 2 dual-core CPUs, the thread_concurrency value should be 8.

query_cache_size = 32M The cache mechanism is simply to cache sql text and For query results, if the same sql is run, the server directly fetches the results from the cache without parsing and executing the sql. If a table is changed, then all buffered queries using this table will no longer be valid, and the relevant entries for the query cache value are
emptied
The smallest unit of space, the default is 4K. Check the status value Qcache_free_blocks. If the value is very large, it indicates that there are many fragments in the buffer, which indicates that the query results are relatively small. In this case, you need to reduce query_cache_min_res_unit
default-storage-engine = MyISAM
default_table_type = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED Set the default transaction isolation level for all connections
tmp_table_size = 256M Maximum length of temporary HEAP data table
max_heap_table_size = 256M Maximum length of HEAP data table (memory table) (default setting is 16M);
long_query_time = 1
log_long_format
log-bin = /data0/mysql/3306/binlog
binlog_cache_size = 8M specifies the cache size used by SQL query statements for binary log during query request processing
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 512M
expire_logs_days = 7
key_buffer_size = 256M The size of the index buffer, Strictly speaking, it determines the speed of database index processing, especially the speed of index reading.
read_buffer_size = 1M The buffer size that can be used by read query operations, and the allocated memory corresponding to this parameter is also exclusive to each connection.
read_rnd_buffer_size = 16M The query results (default 256K) output in a specific order (such as the ORDER BY clause) speed up the read data after the sorting operation and improve the speed of reading the sorted rows.
bulk_insert_buffer_size = 64M specifies that MyISAM type data table tables use a special tree-structured cache
myisam_sort_buffer_size = 128M sort_buffer_size of myisam engine
myisam_max_sort_file_size = 10G
#myisam_max_extra_sort_file_size = 10G this parameter is no longer in MySQL
myisam_repair_threads = 1
myisam_recover automatic check and repair failed to close MyISAM table properly

skip-name-resolve
master-connect-retry=10
slave-skip-errors= 1032, 1062, 126, 1114, 1146, 1048, 1396

server-id = 1 set to master

innodb_additional_mem_pool_size = 16M InnoDB is used to store the data dictionary and other internal data structure memory pool size.
The more tables in the application, the more memory should be allocated. If innodb runs out of this memory, it will ask for the system memory.
  And write to the warning log. According to the MySQL manual, for a machine with 2G memory, the recommended value is 20M.
  The default value is 1M. Usually it is not too big, as long as it is enough, it is related to the complexity of the table structure.


innodb_status_file
InnoDB sends diagnostic output to stderr or a file, rather than to stdout or a fixed-size memory buffer, to avoid underlying buffer overflows. As a side effect, the output of SHOWINNODB STATUS is written to a status file every 15 seconds. The name of this file is innodb_status.pid, where pid is the server process ID. This file is created in the MySQL data directory. During a graceful shutdown, InnoDB deletes this file. In the event of an abnormal shutdown, instances of these state files may be displayed and must be deleted manually. Before removing them, you may want to check them to see if they contain useful information about the cause of the erratic shutdown. The innodb_status.pid file is created only when the configuration option innodb_status_file=1 is set.



innodb_buffer_pool_size = 2048M Specifies the size of memory to buffer data and indexes.
  For a single MySQL database server, this value can be set to a maximum of 80% of physical memory.
  According to the MySQL manual, for a machine with 2G memory, the recommended value is 1G (50%)

innodb_data_file_path = ibdata1:1024M:autoextend
innodb_file_io_threads = 4 The maximum number of threads for IO operations (hard disk write operations) (the default setting is 4).
innodb_thread_concurrency = 16 The maximum number of threads that the InnoDB driver can use at the same time (the default setting is 8).
innodb_flush_log_at_trx_commit = 2 If the logging method of InnoDB
  is set to 1, MySQL will write the transaction log to disk when each transaction commits.
  If set to 0 or 2, logs are written to disk approximately once per second.
  The actual test found that this value has a great impact on the speed of inserting data. When it is
   set to 2, it only takes 2 seconds to insert 10,000 records, when it is set to 0, it only takes 1 second, and when it is set to 1, it takes 229 seconds.
  It is recommended to combine insert operations into a single transaction as much as possible, which can greatly improve the speed.
  This value can be set to 0 under the premise that there is a risk of losing the most recent part of the transaction.

innodb_log_buffer_size = 16M The
  default size of the log buffer can generally meet the performance requirements of the server in the case of medium-intensity write loads and short transactions.
  This value should be increased if the update operation peaks or the load is heavy. 8-16M can be.

innodb_log_file_size = 128M The size of each log file in the log group is important in case of high write load especially with large datasets.
The higher the value, the higher the performance, but the recovery time will be longer. The default is 5M. Javaeye recommends innodb_log_file_size = 64M

innodb_log_files_in_group = 3 The number of log files in the log group, it is recommended to use 3
innodb_max_dirty_pages_pct = 90 The percentage of the largest dirty pages
innodb_lock_wait_timeout = 120 The transaction gets resource timeout setting, default 50s
innodb_file_per_table = 0 Create a table for each new data table Space files instead of centrally storing all data tables in the central tablespace

Quick do not buffer queries, export directly to stdout
max_allowed_packet = 32M
Disable the strict verification of mysql STRICT_TRANS_TABLES
#Remove "STRICT_TRANS_TABLES," in sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION", restart the MySQL service, the problem is solved... Shame~~~
After this battle, summarize a note in database design Point, that is, two ways to avoid this phenomenon:
1. In the design process of the table, set default values ​​for all fields that cannot be empty
2. Configure the MySQL service, remove STRICT_TRANS_TABLES as described above #Open


event
#event_scheduler = on
#myisam auto repair
myisam-recover=BACKUP,FORCE


# Enter
log-bin=/var/lib/mysql/mysql-bin
in #expire_logs_days = 10
#max_binlog_size = 100M
log=/var/lib/mysql/mysql.log
log -error=/var/lib/mysql/error.log
#log-update=/var/lib/mysql/update.log failed to generate
long_query_time=1
log-slow-queries
log-queries-not-using-indexes


Whether the log is enabled
mysql>show variables like 'log_%';
how to know the current log
mysql>show master status;
display the number of binary logsmysql
>show master logs;
to see the binary log file use mysqlbinlog
shell>mysqlbinlog mail-bin.
shell>mysqlbinlog mail-bin.000001 | tail #Details


log
-bin=/var/lib/mysql/mysql-bin#Binary LOG, mysqlbinlog directly reads the binary log file. The binary log contains all the statements that update the data, and its purpose is to use it to restore the data to the last possible state when restoring the database. In addition, if you do synchronous replication (Replication), you also need to use the binary log to transfer the modification. log_bin specifies the log file. If no file name is provided, MySQL will generate the default file name by itself. MySQL automatically appends a numerical index to the filename, and every time the service is started, a new binary is regenerated.

After a long time, the log bin will also occupy a lot of hard disk space, so add a limit to it in my.ini.
#expire_logs_days = 10
#max_binlog_size = 100M
---------------------
# show master status;
#mysqlbinlog mysql-bin.000052
-------------格式如下:
flush privileges
;
# at 173
#090814 10:09:22 server id 1  end_log_pos448  Query thread_id=51   exec_tim                                     e=0   error_code=0
use sq_gllutf8;
SET TIMESTAMP=1250215762;
;
SET@@session.character_set_client=33,@@session.collation_connection=33,@@sessio                                    n.collation_server=8;
update gll_products set sn_name='休闲裤'  ,ch_name='H957-3' ,color='卡其'  ,co                                    mposition='60%马棉44'  ,huoqi='' ,p_remarks=''  ,utime='2009-08-1410:09:22'  ,                                    uip='192.168.200.96'  WHERE id = '339'
;
# at 448
#090814 10:11:14 server id 1 end_log_pos556 Query thread_id=59 exec_time=0 error_code=0
SET TIMESTAMP=1250215874;
delete from gll_member WHERE id = '26'
;
DELIMITER ;
# End of log file
ROLLBACK ;
;
--- --------------------------------

log=/var/lib/mysql/mysql.log #For all executed statements Record
---------------------------- in the following format:
090814 10:11:14 59 Connect sq_szczkj@localhost on
                 59 Init DB sq_gllutf8
                 59 Query SET NAMES 'UTF8'
                 59 Query delete from gll_member WHEREid = '26'
                 59 Quit
090814 10:11:15     60 Connect   sq_szczkj@localhost on
                 60 Init DB   sq_gllutf8
                 60 Query     SET NAMES 'UTF8'
                 60 Query     select * from gll_member  ORDERBY id DESC
                 60 Query     select * from gll_member  ORDERBY id DESC  limit  0,50
                 60 Quit
                 61 Connect   sq_szczkj@localhost on
                 61 Init DB   sq_gllutf8
                 61 Query     SET NAMES 'UTF8'
                 61 Query     select * from gll_zone  ORDERBY id DESC
                 61 Query     select * from gll_zone  ORDERBY id DESC  limit  0,99
                 61 Quit
---------------------------------

log-error=/var/lib/mysql/error.log
#log- update=/var/lib/mysql/update.log failed to generate

long_query_time=1
log-slow-queries
#--log-slow-queries[=file_name



] The file_name parameter is optional, the default value is host_name



-slow. log, if the file_name parameter is specified, mysql will record the log of the slow query to the file set by file_name. If the file_name provides a relative path, mysql will record the log to the data directory of mysql.
log-queries-not-using-indexes#Add the --log-queries-not-using-indexes parameter to the mysql startup configuration file or command line parameters to enable unused index query statements. The log recorded file is log The file corresponding to -slow-queries.


----------------Mysql's log_slow_queries log file is not properly set Error
1, error-prone settings:
log_slow_queries = ON
log_slow_queries=/var/log/slow-queries.log
long_query_time=5
This can easily lead to an error that slow-queries.log cannot be read (permission issue).
Common error message:
Could not use /var/log/slow-queries.log for
logging (error 13). Turning logging off for the whole duration of the
MySQL server process. To turn it on again: fix the cause, shutdown the
MySQL server and restart it.

2, a more appropriate configuration:
log_slow_queries = ON
log_slow_queries=slow-log
long_query_time=5
Generate slow-log files directly under the directory /var/lib/mysql/



------------- --------------- Test the slow log
mysql>select sleep(1);
mysql>select sleep(3);
mysql>select * from gll_products;

open mysql.log and slow log files can see
select sleep(3);
select * from gll_products;








------------------------------------- --------------------------------3 Command Description



show variables like 'long%'; 查询 long_query_time 的值

mysql>show variables like 'log_%'; 是否启用了日志
mysql> show variables like 'log_%';
+---------------------------------+--------------------------+
| Variable_name               | Value                 |
+---------------------------------+--------------------------+
| log_bin                     |ON                   |
| log_bin_trust_function_creators | OFF                  |
| log_error                   | /var/lib/mysql/error.log|
| log_queries_not_using_indexes  |ON                   |
| log_slave_updates            | OFF                  |
| log_slow_queries              |ON                   |
| log_warnings | 1 |
+---------------------------------+---------- ----------------+




mysql> show master logs; display the number of binary logs

mysql>show variables like 'log_bin'; confirm whether your log is enabled

mysql> show master status; how to know the current

View from a certain period of time to a certain period of time
mysqlbinlog --start-datetime='2008-01-19 00:00:00'--stop-datetime='2008-01-30 00:00: 00' /var/lib/mysql/mysql-bin.000006 > mysqllog1.log

shell>mysqlbinlog mail-bin.000001 Look at the binary log file with mysqlbinlog
or shell>mysqlbinlog mail-bin.000001 | tail

flush privileges; refresh the database

mysql> SHOW STATUS; Run the following command to get the value of the status variable:

mysql> SHOW STATUS LIKE '[match pattern]'; (%, ?, etc. can be used) If you only want to check a few status variables, you can use the following command:

select version( ); Check mysql version



http://blog.sina.com.cn/s/blog_417b97470100mvnj.html Mysql performance tuning

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326580859&siteId=291194637