概述:在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语句的数量的

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


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

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

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
