VPS主机上MySQL数据库的优化_MySQL
概述:在linux vps主机上配置mysql数据库,由于默认使用yum安装的数据库启动了InnoDB引擎,同时其/etc/my.cnf配置都我们的vps主机不是很适合,本文详细讲解如何配置适合自己vps主机的mysql配置文件
目的:优化vps主机上的mysql数据库,使之更合适vps主机这样的环境
query_cache_size = 268435456 query_cache_type=1 query_cache_limit=1048576
查看mysql-server静态参数值命令
mysql>show variables;
或者mysqladmin -uroot variables
mysql服务器的参数很多,如果需要了解某个参数的详细定义,可以使用mysqld –verbose –help | more
查看mysql-server动态状态信息命令
mysql>show status;
或者mysqladmin -uroot status
其中show status分为show [session|global]status;可以根据需要加上参数“session”或者”global”来显示session级(当前统计)的统计结果和global级(自数据库上次启动至今)的统计结果,如果不写,默认为”session”
1、查看和修改默认的存储引擎,关闭不需要的存储引擎
在我们使用vps主机过程中,很多用户只使用MyISAM一个引擎,关闭那么没有使用的InnoDB MEMORY等其他引擎
查看当前系统有哪些引擎
mysql>SHOW ENGINES \G 或者第二种方法 mysql>SHOW VARIABLES LIKE 'have%';
查看默认引擎
mysql> show variables like 'table_type'; +---------------+--------+ | Variable_name | Value | +---------------+--------+ | table_type | MyISAM | +---------------+--------+ 1 row in set (0.00 sec) 或者 mysql> show variables like 'storage_engine'; +----------------+--------+ | Variable_name | Value | +----------------+--------+ | storage_engine | MyISAM | +----------------+--------+ 1 row in set (0.00 sec)
mysql默认的存储引擎是MyISAM,修改默认的存储引擎可以修改/etc/my.cnf参数文件,在[mysqld]字段里面增加default-storage-engine=innodb,这里假设设置默认为innodb,根据自己的需要进行设置,关闭不需要的存储引擎,可以修改/etc/my.cnf参数文件,在[mysqld]字段里面增加–skip-innodb。
2、调节key_buffer_size大小
mysql> show variables like 'key_buffer_size'; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | key_buffer_size | 402653184 | +-----------------+-----------+ 1 row in set (0.00 sec) mysql> 从上面可以看到,key_buffer_size为:384M,可以修改/etc/my.cnf参数文件,在[mysqld]字段里面增加key_buffer_size = 384M,当然也可以使用mysql>set grobal key_buffer_size=512M修改。 此参数是用来<strong>设置索引块</strong>(Index Blocks)缓存的大小,它被所有的线程共享,只适合MyISAM存储引擎。
3、调节table_cache设置
这个参数表示数据库用户打开表的缓存数量,每个连接进来,都会至少打开一个表缓存,如此,table_cache与max_connections有关,
查看当前的table_cache值
mysql> show variables like 'table_cache'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | table_cache | 4096 | +---------------+-------+ 1 row in set (0.00 sec)
从上面可以看到,table_cache为:4096,可以修改/etc/my.cnf参数文件,在[mysqld]字段里面增加table_cachee = 4096
4、调节query_cache设置
Query Cache 在提高数据库性能方面具有非常重要的作用,其设定也非常简单,仅需要在配置文件写入两行: query_cache_type 和 query_cache _size,而且 MySQL 的 query cache 非常快!而且一旦命中,就直接发送给客户端,节约大量的 CPU 时间。
当然,非 SELECT 语句对缓冲是有影响的,它们可能使缓冲中的数据过期。一个 UPDATE 语句引起的部分表修改,将导致对该表所有的缓冲数据失效,这是 MySQL 为了平衡性能而没有采取的措施。因为,如果每次 UPDATE 需要检查修改的数据,然后撤出部分缓冲将导致代码的复杂度增加。
query_cache_type :0 代表不使用缓冲, 1 代表使用缓冲,2 代表根据需要使用。
设置 1 代表缓冲永远有效,如果不需要缓冲,就需要使用如下语句:
SELECT SQL_NO_CACHE * FROM my_table WHERE …
如果设置为 2 ,需要开启缓冲,可以用如下语句:
SELECT SQL_CACHE * FROM my_table WHERE …
用 SHOW STATUS 可以查看缓冲的情况:
mysql> show status like 'Qca%'; +-------------------------+----------+ | Variable_name | Value | +-------------------------+----------+ | Qcache_queries_in_cache | 8 | | Qcache_inserts | 545875 | | Qcache_hits | 83951 | | Qcache_lowmem_prunes | 0 | | Qcache_not_cached | 2343256 | | Qcache_free_memory | 33508248 | | Qcache_free_blocks | 1 | | Qcache_total_blocks | 18 | +-------------------------+----------+ 8 rows in set (0.00 sec)
如果需要计算命中率,需要知道服务器执行了多少 SELECT 语句:
mysql> show status like 'Com_sel%'; +---------------+---------+ | Variable_name | Value | +---------------+---------+ | Com_select | 2889628 | +---------------+---------+ 1 row in set (0.01 sec)
在本例中, MySQL 命中了 2,889,628 条查询中的 83,951 条,而且 INSERT 语句只有 545,875 条。因此,它们两者的和和280万的总查询相比有很大差距,因此,我们知道本例使用的缓冲类型是 2 。
而在类型是 1 的例子中, Qcache_hits 的数值会远远大于 Com_select 。 Qcache_not_cached是可以记录DML语句的数量的

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



According to news from this site on June 5, MSI participated in the 2024 Taipei International Computer Show and showcased a new flagship gaming computer called MEGVisionXAI. This game console is an extension of the existing Vision series and uses a very eye-catching surround glass design, with internal components clearly visible. The most attractive part is that the front of the host is equipped with an oversized touch screen. MSI staff said that it can synchronize MSI’s exclusive AI applications to further enhance various AI functions. The relevant pictures attached to this site are as follows: MSI has not yet explained more details. From the pictures shared, you can see that a local AI chatbot is running on the screen. Users can interact with it and ask it to complete AI tasks and locate locally stored documents. wait. Source of the above picture:

According to news from this site on July 23, Lenovo’s YOGA Portal high-performance desktop computer, which has been exposed for a long time, is now confirmed to be officially released at ChinaJoy in Shanghai on July 27. It is claimed to be a mini host designed for professional AI creation. It is a performance master and an expert in AI creation of 3D digital people. The AI virtual background is based on the on-site pictures provided by our friend @yuP in Shanghai. The volume of this small host is only 3.7L. It is made of anodized aluminum and is equipped with Intel Core. i7-14700 processor, equipped with 32GBDDR5 memory and 1TB solid state drive. YOGA Portal is both a host and an all-in-one AI creation machine. The high-performance host is combined with an algorithm-optimized camera to form an integrated solution. Just stand in front of the camera i.e.

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.
