MySQL查询缓存与数据库管理
上一篇 /
下一篇 2010-01-25 21:51:07
/ 个人分类:MYSQL/Cache
MySQL查询缓存
注意:查询缓存绝不返回过期数据。
如果一个表发生的改变(insert,update,delect,truncate,alter或drop table|database),那么所有这张表使用的缓冲的查询将被得失效,并从缓存中移除。
如果一个查询包括下面的函数,它将不能被缓存。
user-defined functions connection_id found_rows get_lock release_lock
load_file master_pos_wait now sysdate current_timestamp curdate
current_date curtime current_time database encrypt last_insert_id
rand unix_timestamp user benchmark
查询缓存选项 select @@query_cache_type;
select sql_cache[选项on/off]
select sql_no_cache id,name from customer;
系统变量have_query_cache表示查询缓存是否可用。
show variables like ‘have_query_cache’;
系统查询query_cache_size表示查询缓存的大小。如果设置它为0,查询缓存将被禁止(缺省值为0)。
select @@global.query_cache_size;
若启用查询缓存,则系统变量query_cache_type表示查询缓存工作方式,他可以设置成如下值:
0(off,不缓存结果)
1(on,缓存所有的结果,除了select sql_no_cache… 查询)
2(demand,仅缓存select sql_cache… 查询)
用户可以禁止查询缓存;
set session query_cache_type=off;
若查询结果很大,缓存吗?
系统变量query_cache_limit控制可以缓存的查询结果的最大值,不缓存大于这个值的结果(缺省为1M)。
碎片整理
使用flush query cache命令,你可以整理查询缓存,以更好的利用它的内存。这个命令不会从缓存中移除任何查询。
清除cache
seset query cache命令从查询缓存中移除所有的查询结果。
flush tables也会清除查询缓存。
我们可以使用show status来监视查询缓存性能。
show status like ‘qcache%’;
变量 含义
Qcache_queries_in_cache 搜索缓存中结果集的查询数目
Qcache_inserts 被加入到缓存中的查询数目
Qcache_hits 缓存命令数目
Qcache_lowmem_prunes 因为缺少内存而被从缓存中删除的查询数目
Qcache_not_cached 没有被缓存的查询结果数目(不能被缓存的,或由于
Query_cache_type)
Qcache_free_memory 查询缓存的空闲内存总数
Qcache_free_blocks 查询缓存的空闲内存块的数目
Qcache_total_blocks 查询缓存中的块的总数目
--------------------------------------------
MySQL 数据库管理
MySQL数据库管理
MySQL启动/停止/测试
添加用户
增加一个用户test_user密码为123,让他可以在任何主机上登陆,并对所有数据库有查询、插入、修改、删除的权限。
grant select,insert,update,delete on *.* to ‘test_user’@ ‘%’ identified by
‘123’;
增加一个用户test_user2密码123,让他只可以在localhost上登陆,并可以对数据库test进行查询、插入、修改、删除的操作(localhost指本地主机,即MySQL数据库所在的那台主机),这样用户即使知道test_user2的密码,他也无法从Internet上直接访问数据库,只能通过MySQL主机上的web页来访问了。
grant select,insert,update,delete on test.* to ‘test_user2’@ ‘localhost’ identfied by ‘123’;
创建一个本地的完全的超级用户admin,口令 ‘123’
grant all privileges on *.* to admin@localhost identified by ‘123’ with grant option ;
删除用户
revoke all on *.* from ‘test_user’@localhost;
delete from user where user= ‘test_user’;
flush privileges;
删除匿名用户
mysql mysql
delete from user where host= ‘localhost’ and user= ‘’;
flush privileges;#刷新内存授权表
修改密码
mysqladmin –u用户名 –p旧密码password新密码
给root加个密码123,再修改密码为456
mysqladmin –uroot password 123
mysqladmin –uroot –p123 password 456
或
mysql –u root –p
set password for root =password(“456”);
或
update user set password =password(‘456’) where user= ‘root’;
flush privileges;
清除密码
mysqladmin –uroot –p456 password ‘’;
优化
optimize table 表1,表2…
对含有varchar,blob或text列的表(记录行长度可变)有用。
MySQL数据库备份与优化
导入
load data infile ‘/tmp/teacher.txt’into table teacher;
或 mysqlimport school /tmp/teacher.txt;
逻辑备份
将数据库school中的表全部备份到school_2004_9.sql文本文件
mysqldump –-opt school > school_2004_9.sql
仅备份数据库school中的一部分表teacher和student
mysqldump –-opt school teacher student >school_teacher_student_2004_9.sql
备份多个数据库
mysqldump –-databases school tset >school_test_2004_9.sql
恢复
将数据库school不慎破坏,从school_2004_9.sql备份文件中恢复数据
creat database school;
use school;
source school_2004_9.sql#恢复
恢复表teacher和student
use school;
soure school_teacher_student_2004_9.sql
恢复多个数据库
souce school_test_2004_9.sql
TAG:
我来说两句
显示全部
内容
昵称
验证
提交评论