我们在前面已经简单介绍了一些MYSQL数据库的基本操作,这一章我们将针对MYSQL数据库管理员详细介绍下MYSQL数据库的优化问题。
1 优化MySQL服务器
1.1 MYSQL服务器系统变量
我们在前面的章节中曾经讲到过MYSQL服务器的一些基本管理,这里我们再对MYSQL服务器的服务器变量和状态变量做个简单介绍。
查询MYSQL服务器系统变量:
C:\Program Files\MySQL\MySQL Server 5.0\bin> mysqld --verbose –help
C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqladmin -uroot -p variables > d:\init.txt Enter password: ******
+---------------------------------+----------------------------------------------------------------+ | Variable_name | Value | +---------------------------------+----------------------------------------------------------------+ | auto_increment_increment | 1 | | auto_increment_offset | 1 | | automatic_sp_privileges | ON | | back_log | 50 ………… | version_compile_machine | ia32 | | version_compile_os | Win32 | | wait_timeout | 28800 | +---------------------------------+----------------------------------------------------------------+
获得MYSQL实际使用的服务器系统变量:
mysql> show variables;
mysql> show variables like 'init_connect%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | init_connect | | +---------------+-------+ 1 row in set (0.00 sec)
mysqld服务器维护两种变量。全局变量影响服务器的全局操作。会话变量影响具体客户端连接相关操作。服务器启动时,将所有全局变量初始化为默认值。可以在选项文件或命令行中指定的选项来更改这些默认值。服务器启动后,通过连接服务器并执行SET GLOBAL var_name语句可以更改动态全局变量。要想更改全局变量,必须具有SUPER权限。
方法一:
mysql> SHOW VARIABLES LIKE 'query_cache_size'; +------------------+----------+ | Variable_name | Value | +------------------+----------+ | query_cache_size | 23068672 | +------------------+----------+ 1 row in set (0.01 sec) mysql> SET GLOBAL query_cache_size = 31457280; Query OK, 0 rows affected (0.00 sec) mysql> SHOW VARIABLES LIKE 'query_cache_size'; +------------------+----------+ | Variable_name | Value | +------------------+----------+ | query_cache_size | 31457280 | +------------------+----------+ 1 row in set (0.00 sec)
mysql> show variables like 'query_cache_size%'; +------------------+----------+ | Variable_name | Value | +------------------+----------+ | query_cache_size | 31457280 | +------------------+----------+ 1 row in set (0.00 sec) mysql> SET @@global.query_cache_size = 20971520; Query OK, 0 rows affected (0.09 sec) mysql> show variables like 'query_cache_size%'; +------------------+----------+ | Variable_name | Value | +------------------+----------+ | query_cache_size | 20971520 | +------------------+----------+ 1 row in set (0.00 sec) mysql> select @@query_cache_size; +--------------------+ | @@query_cache_size | +--------------------+ | 20971520 | +--------------------+ 1 row in set (0.06 sec)
mysql> SET sort_buffer_size = 10 * 1024 * 1024; Query OK, 0 rows affected (0.08 sec) mysql> show variables like 'sort_buffer%'; +------------------+----------+ | Variable_name | Value | +------------------+----------+ | sort_buffer_size | 10485760 | +------------------+----------+ 1 row in set (0.00 sec)
C:\ProgramFiles\MySQL\MySQL Server 5.0\bin>mysqld--key_buffer_size=16M
运行时,使用SET语句来设置系统变量。此时,不能使用后缀,但值可以采取下列表达式:
mysql> SET sort_buffer_size = 10 * 1024 * 1024;
更多内容请看PCdog.com--MySQL数据备份 MySQL性能优化 数据库技巧专题
