Problem with mysql and HighCpu Usage!

Discussion in 'Site & Server Administration' started by vinnyband, Oct 30, 2008.

  1. #1
    Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (11)

    Pid Owner Priority Cpu % Mem % Command
    3506 mysql 0 152 1.8 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/server.uploadground.com.pid --skip-external-locking

    Mysql is taking all my cpu, also it is showing this message, why it is not connecting?
     
    vinnyband, Oct 30, 2008 IP
  2. BluAzn

    BluAzn Well-Known Member

    Messages:
    1,477
    Likes Received:
    127
    Best Answers:
    0
    Trophy Points:
    185
    #2
    I had this issue before, mySQL was killing my server too. I suggest that you should contact the host or the sysadmin who manages the server and take a look at the mySQL or in the my.cnf
     
    BluAzn, Oct 30, 2008 IP
  3. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Definitely install mytop.

    This is a clone of the Unix utility "top" that shows you what's going on inside MySQL.

    There could be any one of a hundred different problems but ones that I have found using mytop are "stuck" queries that have been running for hours and will probably never finish, contention issues with loads of SELECT queries stuck behind an UPDATE query, bots running strange searches as they crawl the search pages and problem queries that cause a backup of other queries until they are finished.

    When you are watching the queries as they happen you can run an EXPLAIN on any query by typing 'e' and then the ID of the query. This is great for finding queries that could be improved with an index.

    If you have a stuck query you can kill it by typing 'k' and the ID of the query or just restart MySQL on the server.

    You should also use the mysqldumpslow tool that comes with MySQL to analyse your slow logs.

    Use the -s option to control how it groups the slow queries. For instance, "mysqldumpslow -s at mysql-slow.log" will group all similar queries together and sort them by the average time it took to run them. This will give you the slowest single query that runs on your server. (I have one that runs each night that takes 20 minutes !) If you run "mysqldumpslow -s t mysql-slow.log" it will sort the similar queries by combined time which means that if you have a 5 second query that is run 1000 times per day it will appear before a single 20 minutes query in this list. Dropping that query down to 1 second will save you 4000 seconds of CPU time per day whereas eliminating the 20 minutes query will only save you 1200 seconds of CPU time per day.

    There are other options to mysqldumpslow that can help with the performance of your site but the time and average time options are the best for detecting wasted CPU cycles.

    Running OPTIMIZE TABLE on some tables can make a difference in CPU usage depending on the types of queries you normally run against them. This will be most useful if you have queries that end in ORDER BY and they are ordering by the primary key or at least one of the keys. It is also more useful on tables that have rows deleted from them from time to time.
     
    Ladadadada, Oct 30, 2008 IP