Merge pull request #2990 from strugglingyouth/master

[Translated] tech/20150626 15 Useful MySQL or MariaDB Performance Tuning and Optimization Tips.md
This commit is contained in:
ictlyh 2015-06-29 13:04:55 +08:00
commit 4dfaeeccf5
2 changed files with 309 additions and 307 deletions

View File

@ -1,307 +0,0 @@
translation by strugglingyouth
15 Useful MySQL/MariaDB Performance Tuning and Optimization Tips
================================================================================
MySQL is a powerful open source Relational Database Management System or in short RDBMS. It was released back in 1995 (20 years old). It uses Structured Query Language which is probably the most popular choice for managing content within a database. The latest MySQL version is 5.6.25 and was released on 29 May 2015.
An interesting fact about MySQL is the fact that the name comes from Michael Wideniuss (MySQLs creator) daughter My. Even though there are plenty of interesting facts about MySQL, this article is meant to show you some useful practices to help you manage your MySQL server.
![MySQL Performance Tuning](http://www.tecmint.com/wp-content/uploads/2015/06/MySQL-Performance-Tuning1.png)
MySQL Performance Tuning
In April 2009 the MySQL project was bought by Oracle. As a result a MySQL community fork called MariaDB was created. The main reason for creating the fork was to keep the project free under the General Public License.
Today MySQL and MariaDB are one of the most (if not the most) frequently used RDBMS used for web applications such as WordPress, Joomla, Magento and others.
This article will show you some basic, yet useful tips how to optimize the fine tune the performance of MySQL/MariaDB. Please keep in mind that this article assumes that you already have MySQL or MariaDB installed. If you are still wondering how to install them on your system, you can follow our extensive guides here:
- [Installing LAMP on RHEL/CentOS 7][1]
- [Installing LAMP on Fedora 22][2]
- [Setting Up LAMP on Ubuntu 15.04][3]
- [Installing MariaDB on Debian 8][4]
- [Install MariaDB on Gentoo Linux][5]
- [Install MariaDB on Arch Linux][6]
**Important**: Before we start do not accept this suggestions blindly. Each MySQL setup is unique and requires additional thought, before making any changes.
Things you need to know:
- MySQL/MariaDB configuration file is located in `/etc/my.cnf`. Every time you modify this file you will need to restart the MySQL service so the new changes can take effect.
- For writing this article MySQL version 5.6 has been used as template.
### 1. Enable InnoDB file-per-table ###
First it is important to explain that InnoDB is a storage engine. MySQL and MariaDB use InnoDB as default storage engine. In the past MySQL used to keep database tables and indexes in a system tablespace. This approach was meant for servers which sole purpose is database processing and their storage disk is not used for any other purposes.
The InnoDB provides more flexible approach and each database information is kept in a `.ibd` data file. Each .ibd file represents a tablespace of its own. That way database operations such as “TRUNCATE” can be completed faster and you may also reclaim unused space when dropping or truncating a database table.
Another benefit of this configuration is the fact that you can keep some of the database tables in a separate storage device. This can greatly improve the I/O load on your disks.
The innodb_file_per_table is enabled by default in MySQL 5.6 and above. You can see that in /etc/my.cnf file. The directive looks like this:
innodb_file_per_table=1
### 2. Store MySQL Database Data on Separate Partition ###
**Note**: This setup only works with MySQL, but not with MariaDB.
Sometimes OS read/writes can slow down the performance of your MySQL server, especially if located on same hard drive. Instead, I would recommend using separate hard drive (preferably SSD) for the MySQL service.
To complete, this you will need to attach the new drive to your computer/server. For the purpose of this article, I will assume that the drive will be under /dev/sdb.
The next step is to prepare the new drive:
# fdisk /dev/sdb
Now press “n” to create new partition. Next press “p” to make the new partition primary. After that, set the partition number from 1-4. After that you will select the partition size. Press enter here. On the next step you will need to configure the size of the partition.
If you wish to use the entire disk press enter once more. Otherwise you can manually set the size of the new partition. When ready press “w” to write the changes. Now we will need to create a filesystem for our new partition. This can be easily done with:
# mkfs.ext4 /dev/sdb1
Now we will mount our new partition in a folder. I have named my folder “ssd” and created in the root directory:
# mkdir /ssd/
We are ready to mount the new partition we just made in the new folder:
# mount /dev/sdb1 /ssd/
You can perform the mount at startup by adding the following line in /etc/fstab file.
/dev/sdb1 /ssd ext3 defaults 0 0
Now you are ready to move MySQL to the new disk. First stop the MySQL service with:
# service mysqld stop
I would recommend you stopping Apache/nginx as well to prevent any attempts to write in the databases:
# service httpd stop
# service nginx stop
Now copy the entire MySQL directory in the new drive:
# cp /var/lib/mysql /ssd/ -Rp
This may take a while depending on the site of your MySQL databases. Once this process is complete rename the MySQL directory:
# mv /var/lib/mysql /var/lib/mysql-backup
Next we will create a symlink.
# ln -s /ssd/mysql /var/lib/mysql
Now you are ready to start your MySQL and web service:
# service mysqld start
# service httpd start
# service nginx start
At this point your MySQL databases will be accessed from the new drive.
### 3. Optimizing InnoDB buffer pool Usage ###
The InnoDB engine has a buffer pool used for caching data and indexes in memory. This of course will help your MySQL/MariaDB queries be executed significantly faster. Choosing the proper size here requires some very important decisions and good knowledge on your systems memory consumption.
Here is what you need to consider:
- How much memory you need for other processes. This includes your system processes, page tables, socket buffers.
- Is your server dedicated for MySQL or you will be running other memory hungry services.
On a dedicated box, you would probably want to give about 60-70% of the memory to the innodb_buffer_pool_size. If you plan on running more services on a single box, you should re-consider the amount of memory you dedicate for your innodb_buffer_pool_size.
The value that you should edit in my.cnf is:
innodb_buffer_pool_size
### 4. Avoid Swappiness in MySQL ###
Swapping is process that occurs when system moves part of memory to a special disk space called “swap”. The event usually appears when your system runs out of physical memory and instead of freeing up some RAM, the system pushed the information into disk. As you might have guess the disk is much slower than your RAM.
By default the option is enabled:
# sysctl vm.swappiness
vm.swappiness = 60
To disable swappiness, run the following command:
# sysctl -w vm.swappiness=0
### 5. Set MySQL Max Connections ###
The max_connections directive tells your server how many concurrent connections are permitted. The MySQL/MariaDB server allows the value given in max_connections + 1 for user with SUPER privileges. The connection is opened only for the time MySQL query is executed after that it is closed and new connection can take its place.
Keep in mind that too many connections can cause high RAM usage and lock up your MySQL server. Usually small websites will require between 100-200 connections while larger may require 500-800 or even more. The value you apply here strongly depends on your particular MySQL/MariaDB usage.
You can dynamically change the value of `max_connections`, without having to restart the MySQL service by running:
# mysql -u root -p
mysql> set global max_connections := 300;
### 6. Configure MySQL thread_cache_size ###
The `thread_cache_size` directive sets the amount of threads that your server should cache. As the client disconnects, his threads are put in the cache if they are less than the thread_cache_size. Further requests are completed by using the threads stored in the cache.
To improve your performance you can set the thread_cache_size to a relatively high number. To find the thread cache hit rate, you can use the following technique:
mysql> show status like 'Threads_created';
mysql> show status like 'Connections';
Now use the following formula to calculate the thread cache hit rate percentage:
100 - ((Threads_created / Connections) * 100)
If you get a low number, it means that most of the new mysql connections are starting new thread instead of loading from cache. You will surely want to increase the thread_cache_size in such cases.
The good thing here is that the thread_cache_size can be dynamically changed without having to restart the MySQL service. You can achieve this by running:
mysql> set global thread_cache_size = 16;
### 7. Disable MySQL Reverse DNS Lookups ###
By default MySQL/MariaDB perform a DNS lookup of the users IP address/Hostname from which the connection is coming. For each client connection, the IP address is checked by resolving it to a host name. After that the host name is resolved back to an IP to verify that both match.
This unfortunately may cause delays in case of badly configured DNS or problems with DNS server. This is why you can disable the reverse DNS lookup by adding the following in your configuration file:
[mysqld]
# Skip reverse DNS lookup of clients
skip-name-resolve
You will have to restart the MySQL service after applying these changes.
### 8. Configure MySQL query_cache_size ###
If you have many repetitive queries and your data does not change often use query cache. People often do not understand the concept behind the `query_cache_size` and set this value to gigabytes, which can actually cause degradation in the performance.
The reason behind that is the fact that threads need to lock the cache during updates. Usually value of 200-300 MB should be more than enough. If your website is relatively small, you can try giving the value of 64M and increase in time.
You will have to add the following settings in the MySQL configuration file:
query_cache_type = 1
query_cache_limit = 256K
query_cache_min_res_unit = 2k
query_cache_size = 80M
### 9. Configure tmp_table_size and max_heap_table_size ###
Both directives should have the same size and will help you prevent disk writes. The `tmp_table_size` is the maximum amount of size of internal in-memory tables. In case the limit in question is exceeded the table will be converted to on-disk MyISAM table.
This will affect the database performance. Administrators usually recommend giving 64M for both values for every GB of RAM on the server.
[mysqld]
tmp_table_size= 64M
max_heap_table_size= 64M
### 10. Enable MySQL Slow query Logs ###
Logging slow queries can help you determine issues with your database and help you debug them. This can be easily enabled by adding the following values in your MySQL configuration file:
slow-query-log = 1
slow-query-log-file = /var/lib/mysql/mysql-slow.log
long_query_time = 1
The first directive enables the logging of slow queries, while the second one tells MySQL where to store the actual log file. Use `long_query_time` to define the amount of time that is considered long for MySQL query to be completed.
### 11. Check for MySQL idle Connections ###
Idle connections consume resources and should be interrupted or refreshed when possible. Such connections are in “sleep” state and usually stay that way for long period of time. To look for idled connections you can run the following command:
# mysqladmin processlist -u root -p | grep “Sleep”
This will show you list of processes that are in sleep state. The event appears when the code is using persistent connection to the database. When using PHP this event can appear when using mysql_pconnect which opens the connection, after that executes queries, removes the authentication and leaves the connection open. This will cause any per-thread buffers to be kept in memory until the thread dies.
The first thing you would do here is to check the code and fix it. If you dont have access to the code that is being ran, you can change the `wait_timeout` directive. The default value is 28800 seconds, while you can safely decrease it to something like 60:
wait_timeout=60
### 12. Choosing Right MySQL Filesystem ###
Choosing the right filesystem is vital for your databases. Most important things you need to consider here are data integrity, performance and ease of administration.
As per MariaDBs recommendations, the best file systems are XFS, Ext4 and Btrfs. All of them are enterprise journaling filesystems that can be used with very large files and large storage volumes.
Below you can find some useful information about the three filesystems:
注:表格
<table cellspacing="0" border="0">
<colgroup width="179"></colgroup>
<colgroup width="85" span="3"></colgroup>
<tbody>
<tr>
<td align="center" height="18" style="border: 1px solid #000000;"><b><span style="color: black; font-family: Arial;">Filesystems</span></b></td>
<td align="center" style="border: 1px solid #000000;"><b><span style="color: black; font-family: Arial;">XFS</span></b></td>
<td align="center" style="border: 1px solid #000000;"><b><span style="color: black; font-family: Arial;">Ext4</span></b></td>
<td align="center" style="border: 1px solid #000000;"><b><span style="color: black; font-family: Arial;">Btrfs</span></b></td>
</tr>
<tr class="alt">
<td align="center" height="18" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">Maximum filesystem size</span></td>
<td align="center" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">8EB</span></td>
<td align="center" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">1EB</span></td>
<td align="center" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">16EB</span></td>
</tr>
<tr>
<td align="center" height="18" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">Maximum file size</span></td>
<td align="center" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">8EB</span></td>
<td align="center" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">16TB</span></td>
<td align="center" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">16EB</span></td>
</tr>
</tbody>
</table>
The pros and cons of the Linux filesystems have been extensively covered in our article:
- [Linux Filesystem Explained][7]
### 13. Set MySQL max_allowed_packet ###
MySQL splits data into packets. Usually a single packet is considered a row that is sent to a client. The `max_allowed_packet` directive defines the maximum size of packet that can be sent.
Setting this value too low can cause a query to stall and you will receive an error in your MySQL error log. It is recommended to set the value to the size of your largest packet.
### 14. Check MySQL Performance Tuning ###
Measuring your MySQL/MariaDB performance is something that you should do on regular basis. This will help you see if something in the resource usage changes or needs to be improved.
There are plenty of tools available for benchmarking, but I would like to suggest you one that is simple and easy to use. The tool is called mysqltuner.
To download and run it, use the following set of commands:
# wget https://github.com/major/MySQLTuner-perl/tarball/master
# tar xf master
# cd major-MySQLTuner-perl-993bc18/
# ./mysqltuner.pl
You will receive a detailed report about your MySQL usage and recommendation tips. Here is a sample output of default MariaDB installation:
![MySQL Performance Tuning](http://www.tecmint.com/wp-content/uploads/2015/06/MySQL-Performance-Tuning.png)
### 15. Optimize and Repair MySQL Databases ###
Sometimes MySQL/MariaDB database tables get crashed quite easily, especially when unexpected server shut down, sudden file system corruption or during copy operation, when database is still accessed. Surprisingly, there is a free open source tool called mysqlcheck, which automatically check, repair and optimize databases of all tables in Linux.
# mysqlcheck -u root -p --auto-repair --check --optimize --all-databases
# mysqlcheck -u root -p --auto-repair --check --optimize databasename
Thats it! I hope you have found the above article useful and help you tune up your MySQL server. As always if you have any further questions or comments, please submit them in the comment section below.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/mysql-mariadb-performance-tuning-and-optimization/
作者:[Marin Todorov][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/marintodorov89/
[1]:http://www.tecmint.com/install-lamp-in-centos-7/
[2]:http://www.tecmint.com/install-lamp-linux-apache-mysql-php-on-fedora-22/
[3]:http://www.tecmint.com/install-lamp-on-ubuntu-15-04/
[4]:http://www.tecmint.com/install-mariadb-in-debian/
[5]:http://www.tecmint.com/install-lemp-in-gentoo-linux/
[6]:http://www.tecmint.com/install-lamp-in-arch-linux/
[7]:http://www.tecmint.com/linux-file-system-explained/

View File

@ -0,0 +1,309 @@
15 个有用的 MySQL/MariaDB 性能调整和优化技巧
================================================================================
MySQL是一个强大的开源关系型数据库管理系统或简称RDBMS。它被发布在1995年20年前。它采用的结构化查询语言在数据库管理中可能是最好的选择。最新的MySQL版本是5.6.252015年5月29日发行的。
尽管有许多关于MySQL有趣的传闻其中一个有趣的事实是这个名字来自Michael Widenius(MySQL的创始人)的女儿。本文旨在向您展示一些有用的做法以帮助您管理您的MySQL服务器。
![MySQL Performance Tuning](http://www.tecmint.com/wp-content/uploads/2015/06/MySQL-Performance-Tuning1.png)
MySQL性能优化
2009年4月MySQL被Oracle收购。其结果导致一个叫MariaDB的社区成立。创建的主要原因是为了保持MySQL项目的自由。
今天MySQL和MariaDB对于Web应用程序像WordPress, Joomla, Magento和其他的是使用最广泛的但不是使用最多的RDBMS。
这篇文章将告诉你一些基本的但有用如何优化MySQL / MariaDB性能的技巧。说明本文假定您已经安装了MySQL或MariaDB。如果你仍然不知道如何在系统上安装它们你可以按照以下说明去安装
- [Installing LAMP on RHEL/CentOS 7][1]
- [Installing LAMP on Fedora 22][2]
- [Setting Up LAMP on Ubuntu 15.04][3]
- [Installing MariaDB on Debian 8][4]
- [Install MariaDB on Gentoo Linux][5]
- [Install MariaDB on Arch Linux][6]
**说明**: 在开始之前不要盲目的接受这些建议。每个MySQL的设置是不同的在进行任何更改之前需要慎重考虑。
你需要明白这些:
- MySQL/MariaDB配置文件位于 `/etc/my.cnf`. 每次需改此文件后你需要重新启动MySQL服务以使新更改生效。
- 这篇文章使用MySQL 5.6版本 。
### 1. 启动 InnoDB 的`file-per-table` ###
首先它是非常重要的对于解释InnoDB是一个存储引擎。MySQL和MariaDB使用的默认存储引擎是InnoDB。过去MySQL使用系统表空间来保存数据库中的表和索引。这意味着服务器唯一的目的就是数据库处理它的存储盘不用于其他的目的。
InnoDB提供了更灵活的方式它把每个数据库的信息保存在一个`.ibd` 数据文件中。像“TRUNCATE”这样的操作可以更快地完成当删除或截断一个数据库表时你也可以回收未使用的空间。
这种结构的另一个好处是可以保留一些数据库表在一个单独的存储设备。这可以大大提高你磁盘的I/O负载。
MySQL 5.6及以上的版本默认启用`innodb_file_per_table`。你可以在/etc/my.cnf文件中看到。该指令看起来是这样的
innodb_file_per_table=1
### 2. 将MySQL数据库中的数据放在独立分区上 ###
**注意**: 此设置只能工作在MySQL上, 而不能在MariaDB上.
有时OS的读/写操作会减缓你MySQL服务器的性能尤其是如果位于同一块磁盘上。因此我建议你使用单独的磁盘SSD最好用于MySQL服务。
要完成这步,你需要将新的磁盘连接到您的计算机/服务器上。对于这篇文章,我会认为磁盘将被识别为/dev/sdb。
下一步将是准备新的分区:
# fdisk /dev/sdb
现在按“N”来创建新的分区。接着按“P”以使其创建为主分区。在此之后设置分区号为1-4之间。之后你可以选择分区大小。这里按enter。在接下来的步骤中你需要配置分区的大小。
如果你希望使用全部的磁盘按一次enter。否则你需要手动设置新分区的大小。准备就绪后按“w”保存。现在我们需要为我们的新分区创建一个文件系统。这可以很轻松的完成
# mkfs.ext4 /dev/sdb1
现在挂载新分区到文件夹下。在根目录下创建一个名为“ssd”的文件夹:
# mkdir /ssd/
挂载新分区到刚才创建的文件夹下:
# mount /dev/sdb1 /ssd/
你可以添加如下行在/etc/fstab文件中设置开机自动挂载
/dev/sdb1 /ssd ext3 defaults 0 0
现在我们移动MySQL到新磁盘中。首先停止服务
# service mysqld stop
我建议你停止Apache/nginx以及以防止任何操作试图在数据库中写入
# service httpd stop
# service nginx stop
现在我们复制完整的MySQL目录到新分区中:
# cp /var/lib/mysql /ssd/ -Rp
这可能需要一段时间具体取决于你的MySQL数据库的大小。一旦这个过程完成后重命名MySQL的目录
# mv /var/lib/mysql /var/lib/mysql-backup
然后创建一个链接:
# ln -s /ssd/mysql /var/lib/mysql
现在启动你的MySQL和web服务:
# service mysqld start
# service httpd start
# service nginx start
以后你的数据库将使用新的磁盘访问。
### 3. 优化InnoDB的缓冲区 ###
InnoDB引擎在内存中有一个缓冲数据和索引的缓冲区。这将有助于你在MySQL/MariaDB中的查询更快的执行。选择合适的内存大小对系统的查询来说是非常重要的并且使你对系统的内存消耗也会有一个更好的认识。
下面是你需要考虑的:
- 其他的进程需要消耗多少内存,包括你的系统进程,表的数量,套接字缓冲区大小。
- 你的服务器是否专用于MySQL还是你也运行其他非常消耗内存的服务。
在一个专用的机器上你可能会使用60-70的内存分配给`innodb_buffer_pool_size`。如果你打算在一个机器上运行多个服务,你应该重新考虑`innodb_buffer_pool_size`的内存大小。
你需要设置my.cnf中的此项:
innodb_buffer_pool_size
### 4. 在MySQL中避免使用Swappiness ###
使用交换空间需要一个进程当系统移动一部分内存到一个空闲的分区中就叫做“swap”。通常当你的系统用完物理内存后就会出现这种情况而不是释放一些RAM然后将信息写进磁盘中。你可能已经猜测到磁盘比你的内存要慢得多。
默认情况下该选项已经启用:
# sysctl vm.swappiness
vm.swappiness = 60
使用以下命令关闭swappiness:
# sysctl -w vm.swappiness=0
### 5. 设置MySQL的最大连接数 ###
`max_connections`变量告诉你的服务器当前允许多少并发连接。MySQL/ MariaDB服务器允许的`max_connections` + 1为超级用户给定的值。当连接建立后执行MySQL查询会有时间的限制 - 之后,它被关闭,新连接可以取代其位置。
请记住太多的连接会导致RAM的使用量过高并且会锁定你的MySQL服务器。一般小网站需要100-200的连接数而较大可能需要500-800甚至更多。这里的值很大程度上取决于你的MySQL/MariaDB的使用情况。
你可以动态的改变`max_connections`的值而无需重启MySQL服务器:
# mysql -u root -p
mysql> set global max_connections := 300;
### 6. 配置MySQL的`thread_cache_size`变量 ###
`thread_cache_size`变量用来设置你服务器缓存的线程数量。当客户端断开连接时,如果当前线程数小于`thread_cache_size`,他的线程将被放入缓存中。下一个请求将使用缓存池中的线程来完成。
要提高服务器的性能,你可以设置`thread_cache_size`的值相对高一些。你可以通过以下方法来查看线程池的使用情况::
mysql> show status like 'Threads_created';
mysql> show status like 'Connections';
你可以用以下公式来计算线程池的使用率:
100 - ((Threads_created / Connections) * 100)
如果你得到一个较低的数字这意味着大多数mysql连接请求使用新的线程而不是从缓存加载。在这种情况下你需要增加`thread_cache_size`。
但有一个好处是,`thread_cache_size`可以动态的改变而无需重启MySQL服务。你可以通过以下方式来实现
mysql> set global thread_cache_size = 16;
### 7.禁用MySQL的DNS反向查询 ###
当新的连接出现时默认情况下MySQL/MariaDB会使用DNS来解析用户的IP地址/主机名每个新的连接它的IP都会被解析为主机名。然后主机名又被反解析为IP来验证这两个是否一致。
当DNS服务器出现问题或者配置有问题时解析会变得非常慢这就是为什么要关闭DNS的反向解析你可以在你的配置文件中添加以下选项去设定
[mysqld]
# Skip reverse DNS lookup of clients
skip-name-resolve
更改后需要重新启动你的MySQL服务器.
### 8. 配置MySQL的`query_cache_size`变量 ###
如果你有很多重复的查询或者不经常改变的数据 请使用缓存查询。 人们常常不理解`query_cache_size`的实际含义而将此值设置为几十兆,这实际上会降低服务器的性能。
背后的原因是线程需要在更新过程中锁定缓存。通常设置为200-300 MB应该足够了。如果你的网站比较小的你可以尝试给64M并在以后及时去增加。
添加以下选项到你的MySQL配置文件中:
query_cache_type = 1
query_cache_limit = 256K
query_cache_min_res_unit = 2k
query_cache_size = 80M
### 9. 配置`tmp_table_size`变量和`max_heap_table_size`变量 ###
这两个变量的大小相同都将帮助你避免将数据直接写入到磁盘中去。`tmp_table_size` 是内置内存表的最大空间如果超出限值表的大小将被转换到磁盘上的MyISAM表。
这将影响数据库的性能。管理员通常建议在服务器上设置这两个值为RAM的每GB为64M。
[mysqld]
tmp_table_size= 64M
max_heap_table_size= 64M
### 10. 开启MySQL慢速查询日志 ###
慢查询日志可以帮助你定位数据库的问题并帮助您调试。在你的MySQL配置文件中添加以下选项来启用
slow-query-log = 1
slow-query-log-file = /var/lib/mysql/mysql-slow.log
long_query_time = 1
第一个变量开启慢查询日志第二个告诉MySQL实际的日志文件存储在哪。使用`long_query_time`来定义MySQL查询完成时长。
### 11.检查MySQL的空闲连接 ###
空闲连接会消耗资源应中断或者尽可能被刷新。这样的连接都在“sleep”状态并且会保持一段时间。通过以下命令可以查看空闲的连接
# mysqladmin processlist -u root -p | grep “Sleep”
这会显示处于睡眠状态的进程列表。当代码使用到数据库持久连接时会出现以下情况。使用PHP调用mysql_pconnect可以打开这个连接即执行查询删除认证最后关闭打开的连接。这会导致每个线程的缓冲区被保存在缓存中直到该线程死亡。
首先你要做的就是检查代码并修复它。如果你不能访问正在运行的代码,你可以修改`wait_timeout`变量。默认值是28800秒而你可以将其降低到60
wait_timeout=60
### 12. 为MySQL选择正确的文件系统 ###
选择正确的文件系统对数据库至关重要。在这里你需要考虑的最重要的事情是 - 数据的完整性,性能和易管理性。
按照MariaDB的建议最好的文件系统是XFSext4或者BTRFS。所有这些都可以作为企业用的日志文件系统它们可以使用非常大的文件和大容量存储。
关于这三个文件系统你可以在下面看到一些有用的信息:
注:表格
<table cellspacing="0" border="0">
<colgroup width="179"></colgroup>
<colgroup width="85" span="3"></colgroup>
<tbody>
<tr>
<td align="center" height="18" style="border: 1px solid #000000;"><b><span style="color: black; font-family: Arial;">Filesystems</span></b></td>
<td align="center" style="border: 1px solid #000000;"><b><span style="color: black; font-family: Arial;">XFS</span></b></td>
<td align="center" style="border: 1px solid #000000;"><b><span style="color: black; font-family: Arial;">Ext4</span></b></td>
<td align="center" style="border: 1px solid #000000;"><b><span style="color: black; font-family: Arial;">Btrfs</span></b></td>
</tr>
<tr class="alt">
<td align="center" height="18" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">Maximum filesystem size</span></td>
<td align="center" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">8EB</span></td>
<td align="center" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">1EB</span></td>
<td align="center" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">16EB</span></td>
</tr>
<tr>
<td align="center" height="18" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">Maximum file size</span></td>
<td align="center" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">8EB</span></td>
<td align="center" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">16TB</span></td>
<td align="center" style="border: 1px solid #000000;"><span style="color: black; font-family: Arial;">16EB</span></td>
</tr>
</tbody>
</table>
这篇文章将详细介绍Linux文件系统的利与弊:
- [Linux Filesystem Explained][7]
### 13. 设置MySQL的`max_allowed_packet` ###
MySQL的数据被拆分成包发送。通常单个报文被认为是一次发送到客户端。`max_allowed_packet`变量可以定义被发送包的大小。
此值设置得过低可能会导致查询速度变得非常慢然后你会看到一个错误在MySQL的错误日志中。它建议你将数据包的大小设置成最大。
### 14. 测试MySQL的性能 ###
你应该定期检测MySQL/MariaDB的性能。这将帮助你查看资源的使用情况或需要调整某些变量的值。
有大量的测试工具可用但我推荐你一个简单易用的。该工具被称为mysqltuner。
使用下面的命令下载并运行它:
# wget https://github.com/major/MySQLTuner-perl/tarball/master
# tar xf master
# cd major-MySQLTuner-perl-993bc18/
# ./mysqltuner.pl
你将收到有关MySQL使用和推荐提示的详细报告。下面是在MariaDB上安装后的默认输出
![MySQL Performance Tuning](http://www.tecmint.com/wp-content/uploads/2015/06/MySQL-Performance-Tuning.png)
### 15. 优化和修复MySQL数据库 ###
有时候MySQL/MariaDB数据库中的表很容易崩溃尤其是当服务器意外关机时数据库仍然被访问中或者在执行复制操作文件系统会突然崩溃。然而有一个免费的开源工具被称为'mysqlcheck'的它会自动检查修复和优化Linux中数据库的所有表。
# mysqlcheck -u root -p --auto-repair --check --optimize --all-databases
# mysqlcheck -u root -p --auto-repair --check --optimize databasename
就是这样我希望你已经发现了上述文章有用的地方并帮助你优化你的MySQL服务器。一如往常如果你有任何问题或意见请在下面的评论部分提交。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/mysql-mariadb-performance-tuning-and-optimization/
作者:[Marin Todorov][a]
译者:[strugglingyouth](https://github.com/strugglingyouth)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/marintodorov89/
[1]:http://www.tecmint.com/install-lamp-in-centos-7/
[2]:http://www.tecmint.com/install-lamp-linux-apache-mysql-php-on-fedora-22/
[3]:http://www.tecmint.com/install-lamp-on-ubuntu-15-04/
[4]:http://www.tecmint.com/install-mariadb-in-debian/
[5]:http://www.tecmint.com/install-lemp-in-gentoo-linux/
[6]:http://www.tecmint.com/install-lamp-in-arch-linux/
[7]:http://www.tecmint.com/linux-file-system-explained/