在CentOS上优化LAMP(Linux, Apache, MySQL, PHP)环境中的数据库查询,可以遵循以下步骤:
编辑MySQL配置文件/etc/my.cnf或/etc/mysql/my.cnf,根据服务器的硬件资源进行调整。
[mysqld]
# 基本设置
innodb_buffer_pool_size = 70% of RAM
max_connections = 200
query_cache_size = 64M
query_cache_type = 1
tmp_table_size = 64M
max_heap_table_size = 64M
# 日志设置
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow-queries.log
long_query_time = 2
log_queries_not_using_indexes = 1
# 表和索引优化
table_open_cache = 2000
table_definition_cache = 1000
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_file_size = 128M
innodb_log_buffer_size = 64M
OPTIMIZE TABLE命令优化表。CREATE INDEX idx_column_name ON table_name (column_name);
SELECT column1, column2 FROM table_name WHERE condition = 'value' LIMIT 10;
通过以上步骤,可以显著提高CentOS LAMP环境中的数据库查询性能。记得在每次修改配置或代码后,重启相关服务以应用更改。