MariaDB社区
标题: mysql占用cpu越来越高,请高手指教! [打印本页]
作者: ljljy 时间: 2010-8-28 19:51
标题: mysql占用cpu越来越高,请高手指教!
运行环境: win2003+apache2.2+php5.2+mysql5.0
硬件配置:Intel 双核2.5G CPU,4G内存
问题:
如果不使用本机的mysql(使用外部数据库),网站工作正常,占用cpu一般不超过5,多数时间在1-2左右。使用本机数据库根据配置大小不同,一般十几分钟或半小时后,cpu的占用量会突然增加到30以上,其中httpd.exe达到10左右,mysqld-nt.exe也接近10,还有一个名字叫System的进程也会增高超过10,持续几分钟后cpu占用增高超过60,而且有继续增高的趋势,网站程序比较简单,数据库里只有一个表,只用到一些简单的查询语句,而且数据库连接都已经正确打开并且按要求关闭。
以下是cpu跳到30多的时候,mysql的show status命令的结果,为零的值已经忽略:
Aborted_clients 12
Aborted_connects 15
Bytes_received 145
Bytes_sent 24771
Cpmpression OFF
Created_tmp_files 5
Created_tmp_tables 5
Flush_commands 1
Handler_read_rnd_next 960
Handler_write 1086
Innodb_buffer_pool_pages_data 19
Innodb_buffer_pool_pages 493
Innodb_buffer_pool_pages_total 512
Innodb_buffer_pool_pages_ahead_rnd 1
Innodb_buffer_pool_pages_ahead_requests 77
Innodb_buffer_pool_pages_reads 12
Innodb_data_read 2494464
Innodb_data_reads 29
Innodb_data_writes 3
Innodb_data_written 1536
Innodb_log_writes 1
Innodb_log_fsyncs 3
Innodb_os_log_written 512
Innodb_pagesize 16384
Innodb_pages_read 19
Key_blocks_unused 345288
Last_query_cost 10.499000
Max_used_connections 9
Open_files 4
Open_tables 1
Qcache_free_blocks 1
Qcache_free_memory 32697672
Qcache_hits 6881806
Qcache_inserts 659
Qcache_queries_in_cache 659
Qcache_total_blocks 1320
Questions 6883761
Table_locks_immediate 674
Threads_cached 6
Threads_connected 3
Threads_created 9
Threads_running 1
Uptime 2516
mysql配置用的是自带的my-huge.ini,配置越低,出现异常越早。
花了两天时间了,实在发愁了,呵呵,请高手指点,非常感谢。
作者: kider 时间: 2010-8-30 10:11
跟你的配置有关系。
你用的什么表类型?
如果你用的是innodb类型的表多,那么my-huge.ini中默认是不打开对innodb引擎的配置。需要你自行打开或配置调整。
具体参考my-huge.ini中的注释说明或innodb手册。
作者: ljljy 时间: 2010-9-4 19:21
回复 kider 的帖子
我用的是MyISAM,设置default-storage-engine=MYISAM skip-innodb,结果还是一样。都是一些简单的读表操作。
作者: kider 时间: 2010-9-4 22:09
mysql>show gloabl variables;
看看你的参数配置
作者: ljljy 时间: 2010-9-5 20:30
本帖最后由 ljljy 于 2010-9-5 20:51 编辑
回复 kider 的帖子
谢谢朋友kider的回复,服务器找人重装了,由apache换成了iis6.0+mysql5.0,结果毛病还严重了,最长一个小时,然后cpu突然超过30,几分钟后是60,接着卡死。可能是没装命令行功能,用mysql -u -p根本进不去,我干脆把配置文件放贴在这儿:
# Example MySQL config file for very large systems.
#
# This is for a large system with memory of 1G-2G where the system runs mainly
# MySQL.
#
# You can copy this file to
# /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is C:\mysql\data) or
# ~/.my.cnf to set user-specific options.
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.
# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
default-storage-engine=MYISAM
skip-innodb
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-locking
key_buffer = 384M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1
# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
# MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
# where you replace <host>, <user>, <password> by quoted strings and
# <port> by the master's port number (3306 by default).
#
# Example:
#
# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
# MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
# start replication for the first time (even unsuccessfully, for example
# if you mistyped the password in master-password and the slave fails to
# connect), the slave will create a master.info file, and any later
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host = <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user = <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password = <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port = <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin
# Point the following paths to different dedicated disks
#tmpdir = /tmp/
#log-update = /path-to-dedicated-directory/hostname
# Uncomment the following if you are using BDB tables
#bdb_cache_size = 384M
#bdb_max_lock = 100000
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = C:\mysql\data/
#innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend
#innodb_log_group_home_dir = C:\mysql\data/
#innodb_log_arch_dir = C:\mysql\data/
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 384M
#innodb_additional_mem_pool_size = 20M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 100M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[isamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
作者: ljljy 时间: 2010-9-5 20:46
本帖最后由 ljljy 于 2010-9-5 20:55 编辑
回复 kider 的帖子
另外我把程序中和数据有关的代码也贴在这儿,您帮看看有什么毛病吗?
这是连接数据库,这段是连接数据库,单独放在一个文件中,每个用到的页面都用requirce_once包含这个文件:
$conn = mysql_connect($dbhost,$dbuser,$dbpwd) or die('数据库链接错误!');
mysql_query("SET NAMES UTF8");
$is_db_selected = mysql_select_db($dbname,$conn);
下面是读数据库的,有四段:
第一段
$sql = "select * from $table where cid=$cateid";
if($result=mysql_query($sql,$conn))
{
$cat_name=mysql_fetch_array($result);
}
第二段
$cat_nav = array();
$sql = "select * from $table where parent_cid=$pcid";
if($result=mysql_query($sql,$conn))
{
while($row=mysql_fetch_array($result))
{
$cat_nav[] = $row;
}
}
第三段
这是一个函数,主要是中间的循环,根据一个子分类的cid查找他的父类,再把当前cid换成父类的cid,直到cid为0.
function GetPosition($cid)
{
global $conn,$table;
$website = DB_WEBSITE;
$pos = '';
while($cid!=0)
{
$sql = "select parent_cid,name from $table where cid=$cid";
$query = mysql_query($sql,$conn);
if(mysql_num_rows($query)==1 )
{
$row = mysql_fetch_array($query);
$name = iconv("UTF-8", "GBK",$row['name']);
$pos = "<a href=\"category-$cid-1\">$name</a>>".$pos;
$cid = $row['parent_cid'];
}
}
$pos = "<a href=\"/\">$website</a>>".$pos;
return $pos;
}
使用数据库的语句只有两个页面,使用完毕后都用mysql_close($conn);关闭了连接,网站目前的访问量是日IP 1000左右,数据库正常的时候访问速度还是不慢的,常用的条件字段都建了索引。
欢迎光临 MariaDB社区 (http://123.56.88.72/) |
Powered by Discuz! X3.2 |