LINUXQQ

七月 26, 2011

tuning-primer.sh

Filed under: linux — admin @ 12:21 下午

CU上看了个非常不错的MYSQL调优工具tuning-primer.sh  

下载地址:http://www.linuxqq.net/downloads/tuning-primer.sh  

运行时你可以选择在用户目录下生成一个保存mysql密码的.my.cnf文件方便下次调用
~/.my.cnf文件内容应该是这样的
cat ~/.my.cnf

  1. [client]
  2. user=test
  3. password=123qwe
  4. socket=/opt/mysql/mysql.sock

./tuning-primer.sh

  1. Using login values from ~/.my.cnf
  2. - INITIAL LOGIN ATTEMPT FAILED -
  3. Testing for stored webmin passwords:
  4.  None Found
  5. Could not auto detect login info!
  6. Found Sockets: /opt/mysql/mysql.sock
  7. Using: /opt/mysql/mysql.sock
  8. Would you like to provide a different socket?: [y/N] n
  9. Do you have your login handy ? [y/N] : y
  10. User: test
  11. Password: 123qwe
  12.  
  13. Would you like me to create a ~/.my.cnf file for you? [y/N] : y
  14.  
  15. ~/.my.cnf already exists!
  16.  
  17. Replace ? [y/N] : y
  18. - FINAL LOGIN ATTEMPT FAILED -
  19. Unable to log into socket: /opt/mysql/mysql.sock

因为我的mysql是编译安装在/opt/mysql/下,这里找不到执行文件,所以没有连接上.
编辑第219和220行,手动修改文件加上路径
vi tuning-primer.sh

  1. 215         if [ -f /etc/psa/.psa.shadow ] ; then
  2.  216                 mysql=”mysql -S $socket -u admin -p$(cat /etc/psa/.psa.shadow)”
  3.  217                 mysqladmin=”mysqladmin -S $socket -u admin -p$(cat /etc/psa/.psa.shadow)”
  4.  218         else
  5.  219                 mysql=”/opt/mysql/bin/mysql”
  6.  220                 mysqladmin=”/opt/mysql/bin/mysqladmin”
  7.  221                 # mysql=”mysql -S $socket”
  8.  222                 # mysqladmin=”mysqladmin -S $socket”
  9.  223         fi

顺带修复生成密码时的错误问题.
第313行”$pass\”后面需加个”n”

  1. 313                         printf “[client]\nuser=$user\npassword=$pass\nsocket=$socket” > ~/.my.cnf

提示:vi中显示行号方法.在命令模式下输入”:set number”

保存后再次运行,在终端上按照问题重要程度分别用黄色/红色字符标记问题
./tuning-primer.sh

  1. – MYSQL PERFORMANCE TUNING PRIMER –
  2.              – By: Matthew Montgomery -
  3.  
  4. MySQL Version 5.1.26-rc x86_64
  5.  
  6. Uptime = 0 days 7 hrs 17 min 18 sec
  7. Avg. qps = 286
  8. Total Questions = 7527430
  9. Threads Connected = 6
  10.  
  11. Warning: Server has not been running for at least 48hrs.
  12. It may not be safe to use these recommendations
  13.  
  14. To find out more information on how each of these
  15. runtime variables effects performance visit:
  16. http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html
  17. Visit http://www.mysql.com/products/enterprise/advisors.html
  18. for info about MySQL’s Enterprise Monitoring and Advisory Service
  19.  
  20. SLOW QUERIES
  21. The slow query log is NOT enabled.
  22. Current long_query_time = 10.000000 sec.
  23. You have 29 out of 7527516 that take longer than 10.000000 sec. to complete
  24. Your long_query_time seems to be fine
  25.  
  26. BINARY UPDATE LOG
  27. The binary update log is NOT enabled.
  28. You will not be able to do point in time recovery
  29. See http://dev.mysql.com/doc/refman/5.1/en/point-in-time-recovery.html
  30.  
  31. WORKER THREADS
  32. Current thread_cache_size = 80
  33. Current threads_cached = 75
  34. Current threads_per_sec = 0
  35. Historic threads_per_sec = 0
  36. Your thread_cache_size is fine
  37.  
  38. MAX CONNECTIONS
  39. Current max_connections = 350
  40. Current threads_connected = 7
  41. Historic max_used_connections = 208
  42. The number of used connections is 59% of the configured maximum.
  43. Your max_connections variable seems to be fine.
  44.  
  45. No InnoDB Support Enabled!
  46.  
  47. MEMORY USAGE
  48. Max Memory Ever Allocated : 68.78 G
  49. Configured Max Per-thread Buffers : 112.17 G
  50. Configured Max Global Buffers : 2.12 G
  51. Configured Max Memory Limit : 114.29 G
  52. Physical Memory : 15.66 G
  53.  
  54. nMax memory limit exceeds 90% of physical memory
  55.  
  56. KEY BUFFER
  57. Current MyISAM index space = 3.00 G
  58. Current key_buffer_size = 2.00 G
  59. Key cache miss rate is 1 : 3262
  60. Key buffer free ratio = 62 %
  61. Your key_buffer_size seems to be fine
  62.  
  63. QUERY CACHE
  64. Query cache is enabled
  65. Current query_cache_size = 128 M
  66. Current query_cache_used = 61 M
  67. Current query_cache_limit = 2 M
  68. Current Query cache Memory fill ratio = 47.81 %
  69. Current query_cache_min_res_unit = 2 K
  70. Query Cache is 21 % fragmented
  71. Run “FLUSH QUERY CACHE” periodically to defragment the query cache memory
  72. If you have many small queries lower ‘query_cache_min_res_unit’ to reduce fragmentation.
  73. MySQL won’t cache query results that are larger than query_cache_limit in size
  74.  
  75. SORT OPERATIONS
  76. Current sort_buffer_size = 256 M
  77. Current read_rnd_buffer_size = 32 M
  78. Sort buffer seems to be fine
  79.  
  80. JOINS
  81. Current join_buffer_size = 32.00 M
  82. You have had 15 queries where a join could not use an index properly
  83. join_buffer_size >= 4 M
  84. This is not advised
  85. You should enable “log-queries-not-using-indexes”
  86. Then look for non indexed joins in the slow query log.
  87.  
  88. OPEN FILES LIMIT
  89. Current open_files_limit = 51200 files
  90. The open_files_limit should typically be set to at least 2x-3x
  91. that of table_cache if you have heavy MyISAM usage.
  92. Your open_files_limit value seems to be fine
  93.  
  94. TABLE CACHE
  95. Current table_open_cache = 3072 tables
  96. Current table_definition_cache = 256 tables
  97. You have a total of 658 tables
  98. You have 1814 open tables.
  99. The table_cache value seems to be fine
  100. You should probably increase your table_definition_cache value.
  101.  
  102. TEMP TABLES
  103. Current max_heap_table_size = 384 M
  104. Current tmp_table_size = 1.00 G
  105. Of 142450 temp tables, 18% were created on disk
  106. Effective in-memory tmp_table_size is limited to max_heap_table_size.
  107. Created disk tmp tables ratio seems fine
  108.  
  109. TABLE SCANS
  110. Current read_buffer_size = 8 M
  111. Current table scan ratio = 5065 : 1
  112. read_buffer_size seems to be fine
  113.  
  114. TABLE LOCKING
  115. Current Lock Wait ratio = 1 : 25
  116. You may benefit from selective use of InnoDB.
  117. If you have long running SELECT’s against MyISAM tables and perform
  118. frequent updates consider setting ‘low_priority_updates=1′
  119. If you have a high concurrency of inserts on Dynamic row-length tables
  120. consider setting ‘concurrent_insert=2′.

我的my.cnf

  1. [mysqld]
  2. port = 3306
  3. socket = /opt/mysql/mysql.sock
  4. skip-locking
  5. skip-name-resolve
  6. back_log=100
  7. key_buffer_size = 2048M
  8. query_cache_size = 128M
  9. query_cache_limit = 2M #default=1M
  10. query_cache_min_res_unit = 2k #default=4K
  11.  
  12. max_allowed_packet = 16M
  13. table_cache = 3072
  14.  
  15. tmp_table_size = 1024M #default=16M
  16. max_heap_table_size = 384M #default=16M
  17.  
  18. read_buffer_size = 8M
  19. read_rnd_buffer_size = 32M
  20.  
  21. sort_buffer_size = 256M
  22. join_buffer_size = 32M
  23. myisam_sort_buffer_size = 256M
  24. thread_cache_size = 80 #default=0
  25. thread_stack = 192K #default=192K
  26.  
  27. # Try number of CPU’s*2 for thread_concurrency
  28. thread_concurrency = 16 #default=10
  29. connect_timeout = 30
  30. #interactive timeout = 600
  31. max_connection = 350
  32. max_connect_errors = 30
  33. wait_timeout = 30

七月 20, 2011

django admin 设置

Filed under: python — admin @ 2:18 下午

# Include these import statements…
from django.contrib import admin
from django.conf.urls.defaults import *
admin.autodiscover()

# And include this URLpattern…
urlpatterns = patterns(”,
    # …
    (r’^admin/’, include(admin.site.urls)),
    # …
)

顺便理清下自己最近学习PYTHON的思路

 先是URLS  在是VIEWS 然后是模版template

如果新做项目

python manage.py startapp books
接下来编辑books/model.py 里面的文件
添加需要做的数据库
python manage.py validate  这个命令验证模型的有效性
 
python manage.py sqlall books   这个命令查看转换出来的SQL语句
 
python manage.py syncdb      这个命令将SQL 添加到库里
 

模版系统

修改setting.py 将里面TEMPLATE_DIRS = (
           '/var/mysite/html',
)
修改views.py
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import Template, Context
def ua_display_bad(request):
        ua=request.META['HTTP_USER_AGENT']
        return HttpResponse("Your browser is %s" % ua)

def ua_display_good1(request):
        try:
            ua=request.META['HTTP_USER_AGENT']
        except KeyError:
            ua='unknown'
        return HttpResponse("Your browser is %s" % ua)

def ua_display_good2(request):
        ua=request.META.get('HTTP_USER_AGENT','unknown')
        return HttpResponse("Ypur brow is %s" % ua)

def search_form(request):
    return render_to_response('search_form.html')

 

 

URLS里面的设置

 

# Include these import statements...
from django.contrib import admin
from django.conf.urls.defaults import *
from views import *
admin.autodiscover()

# And include this URLpattern...
urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
    (r'^good1/',ua_display_good1),
    (r'^good2/',ua_display_good2),
    (r'^display/',ua_display_bad),
    (r'^search-form/$',search_form),
)

 

没办法啊 我新手刚学PYTHON。所以记录下来 免的老忘掉 理清思路

七月 19, 2011

/usr/bin/ld: cannot find -lmysqlclient_r

Filed under: python — admin @ 1:08 下午

我的环境 centos 5.2,mysql 5.0.89 版(源码安装的),配置为:

./configure –prefix=/usr/local/mysql –with-charset=utf8 –with-extra-charsets=all

python 是自带的 2.4.3,按照 官方 web.py 0.3 写例子。安装 MySQL-python 想从源码安装,但后来报错

gcc -pthread -shared build/temp.linux-x86_64-2.4/_mysql.o -L/usr/local/mysql/lib/mysql -lmysqlclient_r -lz -lpthread -lcrypt -lnsl -lm -lpthread -o build/lib.linux-x86_64-2.4/_mysql.so
/usr/bin/ld: cannot find -lmysqlclient_r
collect2: ld returned 1 exit status
error: command ‘gcc’ failed with exit status 1

说没有 mysqlclient_r 后 google 到答案:把 mysql-python/site.cfg 的 threadsafe = True 改为 False。就可以安装,的确如此。再看看 mysql 源码里的 ./configure –help 就知道有个 –enable-thread-safe-client 选项,如果安装的时候指定就可以了(就还是用 threadsfe = True)。

安装时又说没有 setuptools 模块,这个可以直接运动 mysql-python/ez_setup.py 即可。 然后 python setup.py install 就可以了。

按照 web.py 0.3 的例子:

import web  
render = web.template.render(‘templates/’)  
db = web.database(dbn=’mysql’, user=’test’, pw=’test’, db=’test’)  
urls = (  
        ‘/’, ‘index’ 
)  
 
class index:  
        def GET(self):  
                todos = db.select(‘todo’)  
                return render.index(todos)  
 
app = web.application(urls, globals())  
 
if __name__ == “__main__”: app.run() 

import web
render = web.template.render(‘templates/’)
db = web.database(dbn=’mysql’, user=’test’, pw=’test’, db=’test’)
urls = (
        ‘/’, ‘index’
)

class index:
        def GET(self):
                todos = db.select(‘todo’)
                return render.index(todos)

app = web.application(urls, globals())

if __name__ == “__main__”: app.run()
然后运行它,打开浏览器访问,N多错误信息。后台错误:

  File “/usr/lib/python2.4/site-packages/web/db.py”, line 641, in select
    return self.query(qout, processed=True)
  File “/usr/lib/python2.4/site-packages/web/db.py”, line 602, in query
    db_cursor = self._db_cursor()
  File “/usr/lib/python2.4/site-packages/web/db.py”, line 533, in _db_cursor
    return self.ctx.db.cursor()
  File “/usr/lib/python2.4/site-packages/web/db.py”, line 474, in _getctx
    self._load_context(self._ctx)
  File “/usr/lib/python2.4/site-packages/web/db.py”, line 485, in _load_context
    ctx.db = self._connect(self.keywords)
  File “/usr/lib/python2.4/site-packages/web/db.py”, line 513, in _connect
    return self.db_module.connect(**keywords)
  File “build/bdist.linux-x86_64/egg/MySQLdb/__init__.py”, line 81, in Connect
  File “build/bdist.linux-x86_64/egg/MySQLdb/connections.py”, line 188, in __init__
OperationalError: (2002, “Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)”)用 /var/lib/mysql/mysql.sock 肯定链接不上,因为 mysql.sock 在 /tmp/mysql.sock 里。如果你还没安装 mysql,安装 mysql 时可以用 –with-unix-socket-path=/var/lib/mysql/mysql.sock 指定,这就不会报错的。

网上文章已经记录过此问题,没给出解决方案:MySQLdb connect 问题

然后看了 django 连接 mysql 是可以设置 mysql.sock 的。恩那应该 web.py 也可以的。

想找 connections.py 源码又没发现在哪里,原来在 mysql-python/MySQLdb 目录下,看了 __init__,里面有个 unix_socket 变量,恩有救了,于是在 web.py 的例子里加入 unix_socket 参数值。就可以了连接 mysql 了:

db = web.database(dbn=’mysql’, user=’test’, pw=’test’, db=’test’, unix_socket=’/tmp/mysql.sock’) 

db = web.database(dbn=’mysql’, user=’test’, pw=’test’, db=’test’, unix_socket=’/tmp/mysql.sock’)
浏览器终于显示 Learn web.py 了。

比较诡异的是,有时即使安装完成,在python中执行
import MySQLdb
仍然可能出错:
libmysqlclient.so.12: cannot open shared object file: No such file or directory

遇到这种问题,需要在/usr/lib/下为libmysqlclient.so.15设置一个符号链接(这里假设mysql安装在/usr/local/mysql下)
ln -s /usr/lib/mysql/lib/libmysqlclient.so /usr/lib/libmysqlclient.so.15
再次执行
import MySQLdb

Powered by LINUXQQ   ICP 10203065