Mysql Issues always consuming high Memory

Hi Friends

I have an issue, mysql is always using max cpu utilization how to fix the same. below is my.cnf file content

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql

Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
long_query_time = 5

please do the needful ASAP
below is the details of top command

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
10336 mysql 20 0 4237m 52m 4716 S 112.7 0.3 6385:42 mysqld
28989 ngit 20 0 8552m 1.7g 10m S 1.7 10.7 1:15.89 java

Your hardware specifications (CPU, RAM, etc…? OS? mysql version?

Hii Please find below details

Quad Core Processor Dedicated Servers - 4 Cores ( Intel Xeon E3-1230 V3 )

Operating System: CentOS 6.5

Processor: Intel Xeon E3-1230 V3 Quad-core - 3.3GHz - 4 Core

RAM: 16GB DDR3 ECC

Database Software: MYSQL5

Does the MySQL working now for 48+ hours? if so, please execute this and post the output:

wget https://launchpad.net/mysql-tuning-primer/trunk/1.6-r1/+download/tuning-primer.sh
sh tuning-primer.sh

Hi,

Due to some issue I was forced to restart mysql service waited till now but issues are repeating so ran the script before 48 hours. please suggest me the solution in detail as I am new to mysql configurations.

    -- MYSQL PERFORMANCE TUNING PRIMER --
         - By: Matthew Montgomery -

MySQL Version 5.1.73 x86_64

Uptime = 1 days 23 hrs 36 min 13 sec
Avg. qps = 25
Total Questions = 4292240
Threads Connected = 41

Warning: Server has not been running for at least 48hrs.
It may not be safe to use these recommendations

To find out more information on how each of these
runtime variables effects performance visit:
http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html
Visit http://www.mysql.com/products/enterprise/advisors.html
for info about MySQL’s Enterprise Monitoring and Advisory Service

SLOW QUERIES
The slow query log is NOT enabled.
Current long_query_time = 10.000000 sec.
You have 18028 out of 4292261 that take longer than 10.000000 sec. to complete
Your long_query_time seems to be fine

BINARY UPDATE LOG
The binary update log is NOT enabled.
You will not be able to do point in time recovery
See http://dev.mysql.com/doc/refman/5.1/en/point-in-time-recovery.html

WORKER THREADS
Current thread_cache_size = 0
Current threads_cached = 0
Current threads_per_sec = 1
Historic threads_per_sec = 0
Your thread_cache_size is fine

MAX CONNECTIONS
Current max_connections = 151
Current threads_connected = 41
Historic max_used_connections = 77
The number of used connections is 50% of the configured maximum.
Your max_connections variable seems to be fine.

INNODB STATUS
Current InnoDB index space = 286 M
Current InnoDB data space = 426 M
Current InnoDB buffer pool free = 0 %
Current innodb_buffer_pool_size = 8 M
Depending on how much space your innodb indexes take up it may be safe
to increase this value to up to 2 / 3 of total system memory

MEMORY USAGE
Max Memory Ever Allocated : 240 M
Configured Max Per-thread Buffers : 434 M
Configured Max Global Buffers : 19 M
Configured Max Memory Limit : 453 M
Physical Memory : 15.53 G
Max memory limit seem to be within acceptable norms

KEY BUFFER
Current MyISAM index space = 3.39 G
Current key_buffer_size = 7 M
Key cache miss rate is 1 : 81
Key buffer free ratio = 81 %
Your key_buffer_size seems to be fine

QUERY CACHE
Query cache is enabled
Current query_cache_size = 1 M
Current query_cache_used = 793 K
Current query_cache_limit = 1 M
Current Query cache Memory fill ratio = 51.67 %
Current query_cache_min_res_unit = 4 K
MySQL won’t cache query results that are larger than query_cache_limit in size

SORT OPERATIONS
Current sort_buffer_size = 2 M
Current read_rnd_buffer_size = 256 K
Sort buffer seems to be fine

JOINS
Current join_buffer_size = 132.00 K
You have had 286 queries where a join could not use an index properly
You should enable “log-queries-not-using-indexes”
Then look for non indexed joins in the slow query log.
If you are unable to optimize your queries you may want to increase your
join_buffer_size to accommodate larger joins in one pass.

Note! This script will still suggest raising the join_buffer_size when
ANY joins not using indexes are found.

OPEN FILES LIMIT
Current open_files_limit = 1024 files
The open_files_limit should typically be set to at least 2x-3x
that of table_cache if you have heavy MyISAM usage.
Your open_files_limit value seems to be fine

TABLE CACHE
Current table_open_cache = 200 tables
Current table_definition_cache = 256 tables
You have a total of 1779 tables
You have 200 open tables.
Current table_cache hit rate is 0%
, while 100% of your table cache is in use
You should probably increase your table_cache
You should probably increase your table_definition_cache value.

TEMP TABLES
Current max_heap_table_size = 16 M
Current tmp_table_size = 16 M
Of 222530 temp tables, 13% were created on disk
Created disk tmp tables ratio seems fine

TABLE SCANS
Current read_buffer_size = 256 K
Current table scan ratio = 36982 : 1
You have a high ratio of sequential access requests to SELECTs
You may benefit from raising read_buffer_size and/or improving your use of indexes.

TABLE LOCKING
Current Lock Wait ratio = 1 : 8896
Your table locking seems to be fine

I see that your MySQL is optimized, could you please post the output of this command when you find a high CPU usage:

mysqladmin processlist

Taken when CPU is at 202%

±------±-----±-----------±--------------±--------±-----±---------------------±-----------------------------------------------------------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
±------±-----±-----------±--------------±--------±-----±---------------------±-----------------------------------------------------------------------------------------------------+
| 36717 | tomc | tomc:57719 | bat_live_db | Sleep | 6364 | | |
| 36718 | tomc | tomc:57720 | bat_live_db | Sleep | 6364 | | |
| 36719 | tomc | tomc:57721 | bat_live_db | Sleep | 6364 | | |
| 36720 | tomc | tomc:57722 | bat_live_db | Sleep | 0 | | |
| 36721 | tomc | tomc:57723 | bat_live_db | Sleep | 447 | | |
| 36722 | tomc | tomc:57744 | myv_live_db | Sleep | 2 | | |
| 36723 | tomc | tomc:57745 | myv_live_db | Sleep | 1 | | |
| 36724 | tomc | tomc:57746 | myv_live_db | Sleep | 7 | | |
| 36725 | tomc | tomc:57747 | myv_live_db | Sleep | 0 | | |
| 36726 | tomc | tomc:57748 | myv_live_db | Query | 0 | Sending data | select count(*) as hhinfocount from kpi_household_information where state=‘Andhra Pradesh’ and distr |
| 36727 | tomc | tomc:57774 | sfm_live_db | Sleep | 6354 | | |
| 36728 | tomc | tomc:57775 | sfm_live_db | Sleep | 6354 | | |
| 36729 | tomc | tomc:57776 | sfm_live_db | Sleep | 6354 | | |
| 36730 | tomc | tomc:57777 | sfm_live_db | Sleep | 6354 | | |
| 36731 | tomc | tomc:57778 | sfm_live_db | Sleep | 2125 | | |
| 36734 | tomc | tomc:57935 | sgba_live_db | Sleep | 6283 | | |
| 36735 | tomc | tomc:57936 | sgba_live_db | Sleep | 6283 | | |
| 36736 | tomc | tomc:57937 | sgba_live_db | Sleep | 6283 | | |
| 36737 | tomc | tomc:57938 | sgba_live_db | Sleep | 6283 | | |
| 36738 | tomc | tomc:57939 | sgba_live_db | Sleep | 0 | | |
| 36740 | tomc | tomc:58096 | sfga_local_db | Sleep | 6183 | | |
| 36741 | tomc | tomc:58097 | sfga_local_db | Sleep | 6183 | | |
| 36742 | tomc | tomc:58098 | sfga_local_db | Sleep | 6183 | | |
| 36743 | tomc | tomc:58099 | sfga_local_db | Sleep | 333 | | |
| 36744 | tomc | tomc:58100 | sfga_local_db | Sleep | 5019 | | |
| 36749 | tomc | tomc:58127 | myv_live_db | Sleep | 9 | | |
| 36751 | tomc | tomc:58315 | myv_live_db | Sleep | 8 | | |
| 36755 | tomc | tomc:58454 | myv_live_db | Query | 0 | Copying to tmp table | select CAST(sum(no_of_girlchild_marriages) AS SIGNED) as no_of_girlchild_marriages from kpi_househol |
| 36756 | tomc | tomc:58456 | myv_live_db | Sleep | 9 | | |
| 36761 | tomc | tomc:58671 | myv_live_db | Sleep | 14 | | |
| 36762 | tomc | tomc:58674 | myv_live_db | Query | 411 | Copying to tmp table | select CAST(sum(no_of_girlchild_marriages) AS SIGNED) as no_of_girlchild_marriages from kpi_househol |
| 36763 | tomc | tomc:58675 | myv_live_db | Sleep | 8 | | |
| 36764 | tomc | tomc:58676 | myv_live_db | Sleep | 10 | | |
| 36765 | tomc | tomc:58677 | myv_live_db | Sleep | 1 | | |
| 36766 | tomc | tomc:58678 | myv_live_db | Sleep | 2 | | |
| 36767 | tomc | tomc:58684 | myv_live_db | Sleep | 0 | | |
| 36768 | tomc | tomc:58685 | myv_live_db | Query | 0 | freeing items | insert into kpi_household_information(state,district,constituency,mandal,panchayat,habitation,d_code |
| 36769 | tomc | tomc:58687 | myv_live_db | Query | 449 | Copying to tmp table | select CAST(sum(no_of_girlchild_marriages) AS SIGNED) as no_of_girlchild_marriages from kpi_househol |
| 36770 | tomc | tomc:58688 | myv_live_db | Query | 175 | Copying to tmp table | select CAST(sum(no_of_girlchild_marriages) AS SIGNED) as no_of_girlchild_marriages from kpi_househol |
| 36771 | tomc | tomc:58689 | myv_live_db | Sleep | 1 | | |
| 37049 | tomc | tomc:60763 | adhr_live_db | Sleep | 4551 | | |
| 37050 | tomc | tomc:60764 | adhr_live_db | Sleep | 4551 | | |
| 37051 | tomc | tomc:60765 | adhr_live_db | Sleep | 4551 | | |
| 37052 | tomc | tomc:60766 | adhr_live_db | Sleep | 804 | | |
| 37053 | tomc | tomc:60767 | adhr_live_db | Sleep | 0 | | |
| 37607 | tomc | tomc:35182 | mrbdb | Sleep | 2964 | | |
| 37608 | tomc | tomc:35183 | mrbdb | Sleep | 2964 | | |
| 37609 | tomc | tomc:35184 | mrbdb | Sleep | 2964 | | |
| 37610 | tomc | tomc:35185 | mrbdb | Sleep | 2964 | | |
| 37611 | tomc | tomc:35186 | mrbdb | Sleep | 1949 | | |
| 38266 | tomc | tomc:39935 | nch_live_db | Sleep | 224 | | |
| 38267 | tomc | tomc:39936 | nch_live_db | Sleep | 225 | | |
| 38268 | tomc | tomc:39937 | nch_live_db | Sleep | 224 | | |
| 38269 | tomc | tomc:39938 | nch_live_db | Sleep | 209 | | |
| 38270 | tomc | tomc:39939 | nch_live_db | Sleep | 224 | | |
| 38271 | tomc | tomc:39957 | nch_live_db | Sleep | 211 | | |
| 38309 | root | localhost | | Query | 0 | | show processlist |
±------±-----±-----------±--------------±--------±-----±---------------------±-----------------------------------------------------------------------------------------------------+

I see that you have some queries which takes a lot of time to be copied to tmp table.

What’s the output of these commands:

df -h

mysql -e 'SHOW GLOBAL VARIABLES;' | grep ^tmpdir

smartctl -i $(df -h / | grep [0-9] | awk {' print $1 '} | sed 's/.$//') | grep -i 'model'

iostat

Kindly execute the last command when you have a high CPU usage.

Hi,

tuning-primer.sh this script created problem to my automated scripts daily we use to generate reports from mysql database after running tuning-primer.sh script it fixed empty password to mysql root account. without my presence it is not executing. Please let me know how to make passwordless login for mysql root

Thanks

tuning-primer.sh is checking and analyzing script, it doesn’t modify any of your files.

ok please let me know how to remove mysql password ie

if I type sudo mysql

It should just give us mysql> prompt, this is because I am trying to run some script for this even if the password is empty then the script is not working without pressing enter key please help.

my sample script code

echo “select username, email_id from users;” | mysql -u root userdatadb

for this it should run without asking password. Please help me

hey , i think the server is running many idle/long queries.
what i think is …
if u r pulling lot of data from one table making temp tables and then moving back to another tables …in that case CPU load is high.

u have options:-
1.restrict the no. of mysql queries
2. Set the CPU limit for mysql process
3. set the timeout time

Hi,

Please let me know how to solve this issue, when I run below line, it is asking me to press Enter key even if there is no password please do the needful as this is very urgent

echo “select username, email_id from users;” | mysql -u root userdatadb

Waiting for your valuable help please …

okay let me look into it…will repli in min

hi kiran,

I am looking into it

just check if u have file at /root

filename :- my.cnf(hidden)

or file @ /etc/mysql/my.cnf or /etc/my.cnf

i need file data …if thats okay for u

Hi,

Please find below content in my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql

Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
long_query_time = 5
max_connections = 350

Hi Any clues on my issue please …

hey,

check this out:-

create a hidden file in the /root directory

[root@server ~]# cat .my.cnf
[client]
user=root
password=***** (your root password)

restart the service and then try
I have tried the same on my system it works for me

[root@server ~]# mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.35-MariaDB MariaDB Server

Copyright © 2000, 2013, Oracle, Monty Program Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> Ctrl-C – exit!
Aborted

Hi Praduman,

Sorry for late reply actually what to do if I dont want to give password at all. See my aim is just to remove password and don’t want any password even enter should not be asked what to do in such case.

let us see this

echo “select * from sometablename;” | mysql -u root databasename

when I enter above command it should execute without password but every time it is asking to enter password if there is no password it is waiting till i press enter but earlier it is not the case when I run the command it gives output please help

Hello Modserv please don’t suggest tuning-primer.sh to anyone until you know how to remove password for mysql. I am suffering a lot without sleep for many nights because I need to manually type enter when we run automated script.

Still any help is appreciated…