Home Database Mysql Tutorial mysql配置_MySQL

mysql配置_MySQL

May 30, 2016 pm 05:10 PM
mysql配置

前言:《mysql配置》,在centOS上安装完成mysql后,自然是要对mysql进行配置,对于mysql来说,my.cnf可是很重要的,相当于画龙点睛的作用呢。

经常不经意间就发现文章被扣到各式各样的网站上面,甚是可恶!
那么人生何处不爬虫,爬虫请标http://blog.csdn.net/qing_gee
见贤思齐焉,见不贤而内自省也!

特此说明,我这个配置文件内容是结合我项目实战经验多次总结出来的王道,对提升mysql性能有着关键性的作用,当然了,这要看你的项目是否需要这样做喽。

这个文件里面的配置项目很多,我就一一说明了(主要是俺其实有一些也不懂,千万要笑啊,我是真不懂,不过我有问过度娘!),

<code class="shell hljs makefile">[client]
#no-beep
port=3306

[mysql]
default-character-set=utf8
socket          = /var/lib/mysql/mysql.sock

[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
socket          = /var/lib/mysql/mysql.sock

character-set-server=utf8

#默认引擎设置为INNODB,这要看你的数据库是做什么用的
default-storage-engine=INNODB
#最大连接数,这个说实话,我没有测出来最合理的数值
max_connections = 500
#下面这两个参数就是禁用缓存查询,主要是因为我的数据库大量的写操作,所以设置了cache,反而会影响性能,也是基于理论上的,所以你大可不必相信。
query_cache_size=0
query_cache_type=0

#这几个数值,你千万要找度娘理论一下啊,我是说不清楚了
table_open_cache=2000
tmp_table_size=19M
thread_cache_size = 18
myisam_max_sort_file_size = 1G
myisam_sort_buffer_size=30M
key_buffer_size=8M
read_buffer_size = 512K
read_rnd_buffer_size = 1M
sort_buffer_size = 512k

#这个很重要了,对性能有着很大的影响,我会告诉你的。
innodb_flush_log_at_trx_commit=2

innodb_log_buffer_size=1M

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size=2G
innodb_buffer_pool_instances=1
#上面这两个参数对性能的作用我会论证给你的。

#这一块参数的作用我也忘的差不多了,所以度娘吧
innodb_log_file_size=48M
innodb_thread_concurrency=9
innodb_autoextend_increment=64
innodb_buffer_pool_instances=8
innodb_concurrency_tickets=5000
innodb_old_blocks_time=1000
innodb_open_files=300
innodb_stats_on_metadata=0
innodb_file_per_table=1
innodb_checksum_algorithm=0
flush_time=0
join_buffer_size=256K
max_connect_errors=100
max_allowed_packet = 16M
open_files_limit=4161
table_definition_cache=1400
binlog_row_event_max_size=8K

#二进制的类型,这个有很大学问,稍候我也会告诉你的。
binlog-format = MIXED

#事务锁时间,这个同样学问很大。
innodb_lock_wait_timeout = 20

#事务锁级别,这个学问同样很大很大啊
transaction-isolation = REPEATABLE-READ


binlog_cache_size = 1M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
#这个参数就是设置二进制文件的路径的,注意啊,注意啊!
log_bin=mysql-bin

server_id = 1

[mysqldump]
max_allowed_packet = 16M
</code>
Copy after login


重点来了,下面这些内容,如果你没有看到,我觉得你错过了精彩,精彩啊,如果你错过了,我强烈抗议的,虽然抗议无效!

1.innodb_flush_log_at_trx_commit=2

Controls the balance between strict ACID compliance for commit operations, and higher performance<br /> that is possible when commit-related I/O operations are rearranged and done in batches. You can<br /> achieve better performance by changing the default value, but then you can lose up to a second of<br /> transactions in a crash.<br /> ? The default value of 1 is required for full ACID compliance. With this value, the contents of the InnoDB<br /> log buffer are written out to the log file at each transaction commit and the log file is flushed to disk.<br /> ? With a value of 0, the contents of the InnoDB log buffer are written to the log file approximately once<br /> per second and the log file is flushed to disk. No writes from the log buffer to the log file are performed<br /> at transaction commit. Once-per-second flushing is not 100% guaranteed to happen every second,<br /> due to process scheduling issues. Because the flush to disk operation only occurs approximately once<br /> per second, you can lose up to a second of transactions with any mysqld process crash.<br /> ? With a value of 2, the contents of the InnoDB log buffer are written to the log file after each transaction<br /> commit and the log file is flushed to disk approximately once per second. Once-per-second flushing<br /> is not 100% guaranteed to happen every second, due to process scheduling issues. Because the<br /> flush to disk operation only occurs approximately once per second.

大致的意思是将该属性主要是为数据库的ACID原则进行服务的,并且默认为1,但是实际情况下(我们项目是结合spring和mybatis,可能是某一方面设置不当),设置为2会提高很多的事务性能,从文档中可以看得出来,&ldquo;1的时候,innodb的缓存会在事务提交或者每秒钟时都会进行磁盘的刷新操作,2的时候,innodb缓存会在提交事务时写入到事务日志但不会刷新磁盘,然后在每秒钟时进行磁盘刷新操作&rdquo;,2要比1提高很多性能,但是对于隐患来说,我没有太好的理解,按照文档中给出的结果好像是&ldquo;在操作系统崩溃的时候,2的情况下,会丢失1秒的数据&rdquo;,但是仔细想想发生的时间节点,1.事务没有commit时,断电了,此时肯定数据是没有更新成功的,因为都还没有来得及写入事务日志,2.事务提交后,在写入事务日志的时候,发生断电,此时无论是参数的值是1还是2,都应该恢复不了数据了,3.每秒钟刷新磁盘时,发生断电,按照《高性能mysql》的字面意思,此时既然事务日志已经持久化了,那么重启后,数据是会自动恢复的。那么疑问来了,2和1的隐患到底在什么情况下会发生。

我在http://blog.csdn.net/qing_gee/article/details/42551179,这篇文章中有介绍。

2.innodb_buffer_pool_size=2G<br /> innodb_buffer_pool_instances=1

这两个参数,你必须得看看这个mysql:提升性能的最关键参数

3.binlog-format = MIXED

binlog_format=mixed:二进制日志的格式为mixed,该中模式是statement和row模式的结合体,注意查看我同事写的http://www.xx566.com/detail/177.html这篇文章,里面讲解了我们项目在二进制日志设置上遇到的问题和解决办法,如果遇到类似的问题后,会有所帮助。<br /> In MySQL 5.7, the default format is STATEMENT.<br /> You must have the SUPER privilege to set either the global or session binlog_format value.<br /> The rules governing when changes to this variable take effect and how long the effect lasts are the same<br /> as for other MySQL server system variables. See Section 13.7.4, &ldquo;SET Syntax&rdquo;, for more information.<br /> When MIXED is specified, statement-based replication is used, except for cases where only row-based<br /> replication is guaranteed to lead to proper results. For example, this happens when statements contain<br /> user-defined functions (UDF) or the UUID() function. An exception to this rule is that MIXED always<br /> uses statement-based replication for stored functions and triggers.

4.innodb_lock_wait_timeout = 20<br /> 你可以看看这个Transactional和mysql究竟有什么关系,你会明白的,我相信!

5.transaction-isolation = REPEATABLE-READ<br /> 高性能mysql札记:事务,这里面,我有大量的论证。

当然了,我之前也从各地摘录了一些关于参数介绍的,如果你觉得需要的话,我会给你地址的。mysql:配置参数优化建议

写到这,我觉得我的套路就要结束了,这些经验,我真想不说出来的!

最后啊,记得要重启mysql的不然,肯定是没有效果的。

<code class="shell hljs makefile"><code class="shell hljs ">service mysql restart</code></code>
Copy after login

<code class="shell hljs "><strong>结语</strong>:分享知识是快乐的,我只好这样安慰自己吧,哈哈,其实我心态是很宽的,所谓“人逢知己千杯少”,我主要是想结交朋友的,哈哈。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

See all articles